src/Entity/CommonCategory.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Annotation\EsUploadable;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use App\Entity\Traits\TimestampableEntity;
  7. use Doctrine\Common\Collections\Collection;
  8. use ApiPlatform\Core\Annotation\ApiProperty;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use App\Repository\CommonCategoryRepository;
  11. use App\Entity\Interfaces\OrderMappedInterface;
  12. use App\Entity\Interfaces\ClientMappedInterface;
  13. use App\Entity\Interfaces\UploadMappedInterface;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  18. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  19. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  20. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  21. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  22. /**
  23.  * @ORM\Entity(repositoryClass=CommonCategoryRepository::class)
  24.  */
  25. /**
  26.  * @ApiResource(
  27.  *      attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
  28.  *      normalizationContext={"groups"={"CommonCategory:Read"}, "skip_null_values"=false},
  29.  *      denormalizationContext={"groups"={"CommonCategory:Write"}},
  30.  *      collectionOperations={
  31.  *          "get"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
  32.  *          "get_for_public_page"={
  33.  *              "route_name"="api_pub_common_categories_tags_get_for_public_page_collection",
  34.  *              "method"="GET",
  35.  *              "security"="is_granted('PUBLIC_ACCESS')",
  36.  *              "normalization_context"={"groups"={"CommonCategory:PRead"}, "skip_null_values"=false}
  37.  *          },
  38.  *          "post"
  39.  *      },
  40.  *      itemOperations={
  41.  *          "get"={"security"="is_granted('IS_CLIENT_OPR', object)"},
  42.  *          "put"={"security"="is_granted('IS_CLIENT_OPR', object)"},
  43.  *          "patch"={"security"="is_granted('IS_CLIENT_OPR', object)"},
  44.  *          "patch_change_ord"={
  45.  *              "path"="/common_categories/{id}/change-ord",
  46.  *              "method"="PATCH",
  47.  *              "denormalization_context"={"groups"={"CommonCategory:ChangeOrd"}},
  48.  *              "security"="is_granted('IS_CLIENT_OPR', object)"
  49.  *          },
  50.  *          "delete"={"security"="is_granted('IS_CLIENT_OPR', object)"}
  51.  *     }
  52.  * )
  53.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "client.id": "exact", "type": "exact", "translations.name": "partial", "parent.id": "exact"})
  54.  * @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id", "parent.id"})
  55.  * @ApiFilter(ExistsFilter::class, properties={"parent"})
  56.  * @ORM\Entity(repositoryClass=CommonCategoryRepository::class)
  57.  * @ORM\Table(
  58.  *      indexes={
  59.  *          @ORM\Index(name="common_category_type_idx", columns={"type"})
  60.  *      }
  61.  * )
  62.  */
  63. class CommonCategory extends AbstractTranslatable implements ClientMappedInterfaceOrderMappedInterfaceUploadMappedInterface
  64. {
  65.     /**
  66.      * Hook timestampable behavior
  67.      * updates createdAt, updatedAt fields
  68.      */
  69.     use TimestampableEntity;
  70.     public const CGR = [
  71.         "CommonCategory:Read""CommonCategory:PRead",
  72.         "Container:ReadSecure""Container:Storage""Container:PRead""Container:Overview",
  73.         "Conference:Read""Conference:PRead",
  74.         "Session:Read""Session:GetItemDetail""Session:PRead""Session:Agenda""Session:Read-Watched""Session:VideoGallery",
  75.         "ElCourse:Read""ElCourse:PRead",
  76.         "ElLession:Detail",
  77.         "ElTask:Detail",
  78.         "Newsfeed:Read",
  79.         "VideoGallery:Read""VideoGallery:PRead""VideoGallery:Read-Public",
  80.         "User:Read""User:Write""User:Me""User:Attendee-List""User:Attendee-List-O""User:Change-Profile""User:Get-Limited""User:PRead""User:Exp",
  81.         "DocFile:Read",
  82.         "InfoPage:Read",
  83.         "Company:Read""Company:PRead",
  84.     ];
  85.     public const CG = [
  86.         ...self::CGR,
  87.         "CommonCategory:Write",
  88.         "User:Write"
  89.     ];
  90.     public const ECG = [
  91.         ...self::CG,
  92.         "Conference:EL""Session:EL""VideoGallery:EL""ElCourse:EL""Newsfeed:EL""User:EL"
  93.     ];
  94.     /**
  95.      * @ORM\OneToMany(targetEntity="CommonCategoryTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
  96.      *
  97.      * @Groups({"Conference:EL", "Session:EL", "VideoGallery:EL", "ElCourse:EL", "Newsfeed:EL", "User:EL", "CommonCategory:Write", "CommonCategoryTranslationGroup", "CommonCategory:PRead"})
  98.      * @Assert\Valid()
  99.      */
  100.     protected $translations;
  101.     /**
  102.      * @Groups(self::CG)
  103.      */
  104.     private $name;
  105.     /**
  106.      * @ORM\Id
  107.      * @ORM\GeneratedValue
  108.      * @ORM\Column(type="integer")
  109.      * @Groups(self::CGR)
  110.      */
  111.     private $id;
  112.     /**
  113.      * @ORM\ManyToOne(targetEntity=Client::class)
  114.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  115.      * @Groups({"CommonCategory:Write"})
  116.      */
  117.     private $client;
  118.     /**
  119.      * @ORM\Column(type="string", length=16, nullable=true)
  120.      * @Groups(self::ECG)
  121.      */
  122.     private $color;
  123.     /**
  124.      * @ORM\Column(type="string", length=16, nullable=true)
  125.      * @Groups(self::ECG)
  126.      */
  127.     private $textColor;
  128.     /**
  129.      * @ORM\Column(type="integer", nullable=true)
  130.      * @Groups(self::CG)
  131.      */
  132.     private $ord;
  133.     /**
  134.      * @Groups({"CommonCategory:ChangeOrd"})
  135.      */
  136.     private int $newOrd;
  137.     private $ordChangeDirection;
  138.     /**
  139.      * @ORM\Column(type="string", length=32)
  140.      * @Groups(self::CG)
  141.      */
  142.     private $type;
  143.     /**
  144.      * @ORM\Column(type="string", length=255, nullable=true)
  145.      * @Groups(self::ECG)
  146.      *
  147.      * @Assert\Length(max=255, maxMessage="validation.commonCategory:imageName.max")
  148.      * @EsUploadable()
  149.      */
  150.     private $imageName;
  151.     /**
  152.      * @ORM\ManyToOne(targetEntity=CommonCategory::class, inversedBy="childrens")
  153.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  154.      * @Groups(self::CG)
  155.      * @ApiProperty(readableLink=false, writableLink=false)
  156.      */
  157.     private $parent;
  158.     /**
  159.      * @ORM\OneToMany(targetEntity=CommonCategory::class, mappedBy="parent")
  160.      * @Groups(self::CGR)
  161.      */
  162.     private $childrens;
  163.     /**
  164.      * @ORM\OneToMany(targetEntity=SpecialTitle::class, mappedBy="newsfeedCategory")
  165.      */
  166.     private $specialTitles;
  167.     /**
  168.      * @ORM\ManyToMany(targetEntity=User::class, mappedBy="newsfeedSubscriptions")
  169.      */
  170.     private $newsfeedSubscribedUsers;
  171.     public function __construct()
  172.     {
  173.         parent::__construct();
  174.         $this->childrens = new ArrayCollection();
  175.         $this->specialTitles = new ArrayCollection();
  176.         $this->newsfeedSubscribedUsers = new ArrayCollection();
  177.     }
  178.     public const CATEGORYTYPE_CONTAINER "CONTAINER";
  179.     public const CATEGORYTYPE_CONFERENCE "CONFERENCE";
  180.     public const CATEGORYTYPE_SESSION "SESSION";
  181.     public const CATEGORYTYPE_VIDEOGALLERY "VIDEOGALLERY";
  182.     public const CATEGORYTYPE_COURSE "COURSE";
  183.     public const CATEGORYTYPE_NEWSFEED "NEWSFEED";
  184.     public const CATEGORYTYPE_DOCFILE "DOCFILE";
  185.     public const CATEGORYTYPE_INFOPAGE "INFOPAGE";
  186.     public const CATEGORYTYPE_USER "USER";
  187.     public const CATEGORYTYPE_COMPANY "COMPANY";
  188.     protected function createTranslation(): TranslationInterface
  189.     {
  190.         return new CommonCategoryTranslation();
  191.     }
  192.     public function addTranslation(TranslationInterface $translation): void
  193.     {
  194.         $this->setUpdatedAt(new \DateTime());
  195.         parent::addTranslation($translation);
  196.     }
  197.     public function getName(): ?string
  198.     {
  199.         return $this->getTranslation()->getName();
  200.     }
  201.     public function setName(string $name): self
  202.     {
  203.         $this->getTranslation()->setName($name);
  204.         return $this;
  205.     }
  206.     public function getId(): ?int
  207.     {
  208.         return $this->id;
  209.     }
  210.     public function getClient(): ?Client
  211.     {
  212.         return $this->client;
  213.     }
  214.     public function setClient(?Client $client): self
  215.     {
  216.         $this->client $client;
  217.         return $this;
  218.     }
  219.     public function getColor(): ?string
  220.     {
  221.         return $this->color;
  222.     }
  223.     public function setColor(?string $color): self
  224.     {
  225.         $this->color $color;
  226.         return $this;
  227.     }
  228.     public function getTextColor(): ?string
  229.     {
  230.         return $this->textColor;
  231.     }
  232.     public function setTextColor(?string $textColor): self
  233.     {
  234.         $this->textColor $textColor;
  235.         return $this;
  236.     }
  237.     public function getOrd(): ?int
  238.     {
  239.         return $this->ord;
  240.     }
  241.     public function setOrd(?int $ord): self
  242.     {
  243.         $this->ord $ord;
  244.         return $this;
  245.     }
  246.     public function getOrdParents(): array
  247.     {
  248.         return [
  249.             'client' => $this->getClient(),
  250.             'type' => $this->getType()
  251.         ];
  252.     }
  253.     public function getOrdChangeDirection()
  254.     {
  255.         return $this->ordChangeDirection;
  256.     }
  257.     public function getNewOrd(): int
  258.     {
  259.         return $this->newOrd ?? 0;
  260.     }
  261.     public function setNewOrd(int $newOrd): self
  262.     {
  263.         $oldOrd $this->ord;
  264.         $this->newOrd $newOrd;
  265.         $this->ord $newOrd;
  266.         $this->ordChangeDirection = ($this->newOrd $oldOrd) ? 'desc' 'asc';
  267.         return $this;
  268.     }
  269.     public function getType(): ?string
  270.     {
  271.         return $this->type;
  272.     }
  273.     public function setType(string $type): self
  274.     {
  275.         $this->type $type;
  276.         return $this;
  277.     }
  278.     public function getImageName(): ?string
  279.     {
  280.         return $this->imageName;
  281.     }
  282.     public function setImageName(?string $imageName): self
  283.     {
  284.         $this->imageName $imageName;
  285.         return $this;
  286.     }
  287.     public function getAllTranslation()
  288.     {
  289.         $translations = [];
  290.         foreach ($this->getTranslations() as $translation) {
  291.             $translations[] = [
  292.                 $translation->getLocale() => [
  293.                     'name' => $translation->getName()
  294.                 ]
  295.             ];
  296.         }
  297.         return $translations;
  298.     }
  299.     public function getMeta()
  300.     {
  301.         $meta = [
  302.             'translations' => $this->getAllTranslation(),
  303.             'color' => $this->getColor(),
  304.             'textColor' => $this->getTextColor()
  305.         ];
  306.         return $meta;
  307.     }
  308.     public function getQueueInfo(): array
  309.     {
  310.         return [
  311.             'type' => 'CommonCategory',
  312.             'id' => $this->getId(),
  313.             'categoryType' => $this->getType()
  314.         ];
  315.     }
  316.     public function getParent(): ?self
  317.     {
  318.         return $this->parent;
  319.     }
  320.     public function setParent(?self $parent): self
  321.     {
  322.         $this->parent $parent;
  323.         return $this;
  324.     }
  325.     /**
  326.      * @return Collection<int, self>
  327.      */
  328.     public function getChildrens(): Collection
  329.     {
  330.         return $this->childrens;
  331.     }
  332.     public function addChildren(self $children): self
  333.     {
  334.         if (!$this->childrens->contains($children)) {
  335.             $this->childrens[] = $children;
  336.             $children->setParent($this);
  337.         }
  338.         return $this;
  339.     }
  340.     public function removeChildren(self $children): self
  341.     {
  342.         if ($this->childrens->removeElement($children)) {
  343.             // set the owning side to null (unless already changed)
  344.             if ($children->getParent() === $this) {
  345.                 $children->setParent(null);
  346.             }
  347.         }
  348.         return $this;
  349.     }
  350.     /**
  351.      * @return Collection<int, SpecialTitle>
  352.      */
  353.     public function getSpecialTitles(): Collection
  354.     {
  355.         return $this->specialTitles;
  356.     }
  357.     public function addSpecialTitle(SpecialTitle $specialTitle): self
  358.     {
  359.         if (!$this->specialTitles->contains($specialTitle)) {
  360.             $this->specialTitles[] = $specialTitle;
  361.             $specialTitle->setNewsfeedCategory($this);
  362.         }
  363.         return $this;
  364.     }
  365.     public function removeSpecialTitle(SpecialTitle $specialTitle): self
  366.     {
  367.         if ($this->specialTitles->removeElement($specialTitle)) {
  368.             // set the owning side to null (unless already changed)
  369.             if ($specialTitle->getNewsfeedCategory() === $this) {
  370.                 $specialTitle->setNewsfeedCategory(null);
  371.             }
  372.         }
  373.         return $this;
  374.     }
  375.     /**
  376.      * @return Collection<int, User>
  377.      */
  378.     public function getNewsfeedSubscribedUsers(): Collection
  379.     {
  380.         return $this->newsfeedSubscribedUsers;
  381.     }
  382.     public function addNewsfeedSubscribedUser(User $newsfeedSubscribedUser): self
  383.     {
  384.         if (!$this->newsfeedSubscribedUsers->contains($newsfeedSubscribedUser)) {
  385.             $this->newsfeedSubscribedUsers[] = $newsfeedSubscribedUser;
  386.             $newsfeedSubscribedUser->addNewsfeedSubscription($this);
  387.         }
  388.         return $this;
  389.     }
  390.     public function removeNewsfeedSubscribedUser(User $newsfeedSubscribedUser): self
  391.     {
  392.         if ($this->newsfeedSubscribedUsers->removeElement($newsfeedSubscribedUser)) {
  393.             $newsfeedSubscribedUser->removeNewsfeedSubscription($this);
  394.         }
  395.         return $this;
  396.     }
  397. }