ApiService.php
11 months ago
ElementService.php
11 months ago
EmailService.php
11 months ago
FormService.php
11 months ago
LevelOrderService.php
11 months ago
LevelService.php
11 months ago
MembershipService.php
11 months ago
RedirectService.php
11 months ago
SanitizationService.php
11 months ago
StatisticsService.php
11 months ago
UserService.php
11 months ago
LevelService.php
96 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Service; |
| 4 | |
| 5 | use FapiMember\Container\Container; |
| 6 | use FapiMember\Model\MemberSection; |
| 7 | use FapiMember\Repository\LevelRepository; |
| 8 | use FapiMember\Repository\PageRepository; |
| 9 | |
| 10 | class LevelService |
| 11 | { |
| 12 | private LevelRepository $levelRepository; |
| 13 | private LevelOrderService $levelOrderService; |
| 14 | private PageRepository $pageRepository; |
| 15 | |
| 16 | public function __construct() |
| 17 | { |
| 18 | $this->levelRepository = Container::get(LevelRepository::class); |
| 19 | $this->levelOrderService = Container::get(LevelOrderService::class); |
| 20 | $this->pageRepository = Container::get(PageRepository::class); |
| 21 | } |
| 22 | |
| 23 | /** @return array<MemberSection>|null */ |
| 24 | public function getAllSectionsInOrder(): array|null |
| 25 | { |
| 26 | $order = $this->levelOrderService->getOrder(); |
| 27 | $sections = $this->levelRepository->getAllSections(); |
| 28 | $orderedSections = []; |
| 29 | |
| 30 | foreach ($sections as $section) { |
| 31 | $orderedLevels = []; |
| 32 | $levelOrder = $order[$section->getId()]['levels']; |
| 33 | |
| 34 | foreach ($section->getLevels() as $level) { |
| 35 | $orderedLevels[$levelOrder[$level->getId()]] = $level; |
| 36 | } |
| 37 | |
| 38 | if(count($section->getLevels()) !== count($orderedLevels)) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | ksort($orderedLevels); |
| 43 | $section->setLevels($orderedLevels); |
| 44 | |
| 45 | $orderedSections[$order[$section->getId()]['index']] = $section; |
| 46 | } |
| 47 | |
| 48 | if(count($sections) !== count($orderedSections)) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | ksort($orderedSections); |
| 53 | |
| 54 | return $orderedSections; |
| 55 | } |
| 56 | |
| 57 | public function create(string $name, int|null $parentId = null): int|null |
| 58 | { |
| 59 | $levelId = $this->levelRepository->create($name, $parentId); |
| 60 | |
| 61 | if ($levelId === null) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | $level = $this->levelRepository->getLevelById($levelId); |
| 66 | $this->levelRepository->createDefaultLevelEmails($level); |
| 67 | |
| 68 | return $levelId; |
| 69 | } |
| 70 | |
| 71 | public function updateName(int $id, string $name): void |
| 72 | { |
| 73 | $this->levelRepository->update($id, ['name' => $name]); |
| 74 | } |
| 75 | |
| 76 | public function getLoginUrl(int|null $levelId = null): string |
| 77 | { |
| 78 | if ($levelId !== null) { |
| 79 | $loginPageId = $this->pageRepository->getLoginPageId($levelId); |
| 80 | |
| 81 | if ($loginPageId !== null) { |
| 82 | return get_permalink($loginPageId); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $commonLoginPageId = $this->pageRepository->getCommonLoginPageId(); |
| 87 | |
| 88 | if ($commonLoginPageId !== null) { |
| 89 | return get_permalink($commonLoginPageId); |
| 90 | } |
| 91 | |
| 92 | return wp_login_url(); |
| 93 | } |
| 94 | |
| 95 | } |
| 96 |