| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace AbterPhp\Website\Service\Execute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use AbterPhp\Admin\Service\Execute\RepoServiceAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use AbterPhp\Framework\Constant\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use AbterPhp\Framework\Domain\Entities\IStringerEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use AbterPhp\Website\Constant\Authorization; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use AbterPhp\Website\Domain\Entities\ContentList as Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use AbterPhp\Website\Domain\Entities\ContentListItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use AbterPhp\Website\Orm\ContentListRepo as GridRepo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use AbterPhp\Website\Validation\Factory\ContentList as ValidatorFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Casbin\Enforcer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cocur\Slugify\Slugify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Opulence\Events\Dispatchers\IEventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Opulence\Http\Requests\UploadedFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Opulence\Orm\IUnitOfWork; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Opulence\Sessions\ISession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class ContentList extends RepoServiceAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** @var Slugify */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $slugify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var GridRepo */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $repo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var Enforcer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $enforcer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var ISession */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * ContentList constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param GridRepo         $repo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param ValidatorFactory $validatorFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param IUnitOfWork      $unitOfWork | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param IEventDispatcher $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param Slugify          $slugify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         GridRepo $repo, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ValidatorFactory $validatorFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         IUnitOfWork $unitOfWork, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         IEventDispatcher $eventDispatcher, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Slugify $slugify, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ISession $session, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         Enforcer $enforcer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->slugify  = $slugify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->session  = $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->enforcer = $enforcer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $entityId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return Entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function createEntity(string $entityId): IStringerEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return new Entity($entityId, '', '', '', false, false, false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @SuppressWarnings(PHPMD.UnusedFormalParameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param IStringerEntity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array           $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param UploadedFile[]  $fileData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return Entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         assert($entity instanceof Entity, new \InvalidArgumentException()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $postData = $this->protectPostData($entity, $postData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $typeId = (string)$postData['type_id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $name   = (string)$postData['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $identifier = empty($postData['identifier']) ? $name : (string)$postData['identifier']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $identifier = $this->slugify->slugify($identifier); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $protected = empty($postData['protected']) ? false : (bool)$postData['protected']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $withImage = empty($postData['with_image']) ? false : (bool)$postData['with_image']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $withLinks = empty($postData['with_links']) ? false : (bool)$postData['with_links']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $withHtml  = empty($postData['with_html']) ? false : (bool)$postData['with_html']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->setTypeId($typeId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->setIdentifier($identifier) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->setName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->setProtected($protected) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->setWithImage($withImage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->setWithLinks($withLinks) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->setWithHtml($withHtml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if ($entity->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $entity->setItems($this->createItems($postData, $entity->getId())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param Entity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param array  $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @throws \Casbin\Exceptions\CasbinException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     protected function protectPostData(Entity $entity, array $postData): array | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         if (!$entity->isProtected()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             return $postData; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         $username        = $this->session->get(Session::USERNAME); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         $advancedAllowed = $this->enforcer->enforce( | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             $username, | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |             Authorization::RESOURCE_LISTS, | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |             Authorization::ROLE_ADVANCED_WRITE | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         if ($advancedAllowed) { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             return $postData; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         $postData['type_id']    = $entity->getTypeId(); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         $postData['identifier'] = $entity->getIdentifier(); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         $postData['protected']  = $entity->isProtected(); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         $postData['with_image'] = $entity->isWithImage(); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         $postData['with_links'] = $entity->isWithLinks(); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         $postData['with_html']  = $entity->isWithHtml(); | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |         return $postData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param array  $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string $listId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @return ContentListItem[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     protected function createItems(array $postData, string $listId): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $items = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $i = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         while (isset($postData["new$i"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             $d = $postData["new$i"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             if (!empty($d['is_deleted'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $items[] = new ContentListItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 $listId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $d['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $d['name_href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 $d['body'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 $d['body_href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 $d['img_src'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $d['img_alt'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 $d['img_href'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $i++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $i = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         while (isset($postData["existing$i"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $d = $postData["existing$i"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $deletedAt = empty($d['is_deleted']) ? null : new DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $items[] = new ContentListItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 $d['id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 $listId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                 $d['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                 $d['name_href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                 $d['body'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 $d['body_href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 $d['img_src'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 $d['img_alt'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |                 $d['img_href'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                 $deletedAt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |             $i++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         return $items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 205 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  |