src/Entity/Category.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Criteria;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  9.  */
  10. class Category
  11. {
  12.     const TYPE_POST_CATEGORY 1;
  13.     const TYPE_CLASSIFIED_GROUP 2;
  14.     const TYPE_CUSTOMER_LEVEL 3;
  15.     
  16.     const STATUS_ACTIVE 1;
  17.     const STATUS_INACTIVE 0;
  18.     
  19.     const SITE_RCS 1;
  20.     const SITE_AAR 2;
  21.     const SITE_MCS 3;
  22.     const SITE_CCS 4;
  23.     
  24.     /**
  25.      * @ORM\Id()
  26.      * @ORM\GeneratedValue()
  27.      * @ORM\Column(type="bigint")
  28.      */
  29.     private $id;
  30.     
  31.     /**
  32.      * @ORM\Version @ORM\Column(type="integer")
  33.      */
  34.     private $version;
  35.     
  36.     /**
  37.      * @ORM\ManyToMany(targetEntity="App\Entity\Site", inversedBy="categories")
  38.      */
  39.     private $site;
  40.     
  41.     /**
  42.      * @ORM\Column(type="string", length=255)
  43.      */
  44.     private $title;
  45.     /**
  46.      * @ORM\Column(type="text")
  47.      */
  48.     private $description;
  49.     /**
  50.      * @ORM\Column(type="string", length=255)
  51.      */
  52.     private $slug;
  53.     /**
  54.      * @ORM\Column(type="integer")
  55.      */
  56.     private $type;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
  59.      * @ORM\JoinColumn(nullable=true)
  60.      */
  61.     private $prnt;
  62.     
  63.     /**
  64.      * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="prnt")
  65.      */
  66.     private $children;
  67.     /**
  68.      * @ORM\Column(type="datetime")
  69.      */
  70.     private $modified_at;
  71.     /**
  72.      * @ORM\Column(type="datetime")
  73.      */
  74.     private $created_at;
  75.     /**
  76.      * @ORM\Column(type="integer")
  77.      * ... not needed
  78.      */
  79.     private $sort;
  80.     /**
  81.      * @ORM\Column(type="integer")
  82.      */
  83.     private $status;
  84.     /**
  85.      * @ O R M\OneToMany(targetEntity="App\Entity\Customer", mappedBy="customer_type")
  86.      * /
  87.     private $customers;
  88.     */
  89.     /**
  90.      * @ORM\OneToMany(targetEntity="App\Entity\Content", mappedBy="category")
  91.      */
  92.     private $content;
  93.     
  94.     /**
  95.      * @ORM\ManyToMany(targetEntity="App\Entity\Content", mappedBy="secondary_categories", cascade={"detach"})
  96.      */
  97.     private $secondary_content;
  98.     
  99.     /**
  100.      * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="category")
  101.      */
  102.     private $products;
  103.     
  104.     /**
  105.      * @ORM\ManyToMany(targetEntity="App\Entity\Product", mappedBy="secondary_categories", cascade={"persist"})
  106.      */
  107.     private $secondary_products;
  108.     /**
  109.      * @ORM\Column(type="string", length=255)
  110.      */
  111.     private $taxonomy;
  112.     /**
  113.      * @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="categories")
  114.      */
  115.     private $customers;
  116.     /**
  117.      * @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
  118.      * @ORM\JoinColumn(nullable=true)
  119.      */
  120.     private $media;
  121.     
  122.     public function __construct ()
  123.     {
  124.         $this->sort 0;
  125.         $this->status 1;
  126.         $this->type 0;
  127.         $this->description "";
  128.         $this->site = new ArrayCollection();
  129.         
  130.         $this->modified_at = new \DateTime("now");
  131.         $this->created_at = new \DateTime("now");
  132.         $this->customer_level = new ArrayCollection();
  133.         $this->customers = new ArrayCollection();
  134.         $this->content = new ArrayCollection();
  135.         $this->secondary_content = new ArrayCollection();
  136.         $this->products = new ArrayCollection();
  137.         $this->secondary_products = new ArrayCollection();
  138.         
  139.         $this->children = new ArrayCollection();
  140.     }
  141.     public function getId()
  142.     {
  143.         return $this->id;
  144.     }
  145.     
  146.     public function getVersion() {
  147.         return $this->version;
  148.     }
  149.     
  150.     /*
  151.     public function getSite(): ?int
  152.     {
  153.         return $this->site;
  154.     }
  155.     
  156.     public function setSite(int $site): self
  157.     {
  158.         $this->site = $site;
  159.         return $this;
  160.     }
  161.     */
  162.     
  163.     /**
  164.      * @return Collection|Site[]
  165.      */
  166.     public function getSite(): Collection
  167.     {
  168.         return $this->site;
  169.     }
  170.     
  171.     public function getSites(): Collection
  172.     {
  173.         return $this->site;
  174.     }
  175.     public function addSite(Site $site): self
  176.     {
  177.         if (!$this->site->contains($site)) {
  178.             $this->site[] = $site;
  179.             $site->addCategory($this);
  180.         }
  181.         return $this;
  182.     }
  183.     
  184.     public function resetSite(): self
  185.     {
  186.         $this->site = new ArrayCollection();
  187.         return $this;
  188.     }
  189.     
  190.     public function removeSite(Site $site): self
  191.     {
  192.         if ($this->site->contains($site)) {
  193.             $this->site->removeElement($site);
  194.             $site->removeCategory($this);
  195.         }
  196.         return $this;
  197.     }
  198.     
  199.     
  200.     public function getSiteTextShort()
  201.     {
  202.         $site_texts = [];
  203.         foreach($this->getSite() as $s) {
  204.             $site_texts[] = $s->getSiteTextShort();
  205.         }
  206.         return implode(", "$site_texts);
  207.     }
  208.     
  209.     public function getSiteIds()
  210.     {
  211.         $site_ids = [];
  212.         foreach($this->getSite() as $s) {
  213.             $site_ids[] = $s->getId();
  214.         }
  215.         return $site_ids;
  216.     }
  217.     
  218.     
  219.     public function getTitle(): ?string
  220.     {
  221.         return $this->title;
  222.     }
  223.     public function setTitle(string $title null): self
  224.     {
  225.         $this->title $title $title "";
  226.         return $this;
  227.     }
  228.     public function getDescription(): ?string
  229.     {
  230.         return $this->description;
  231.     }
  232.     public function setDescription(string $description null): self
  233.     {
  234.         $this->description $description $description "";
  235.         return $this;
  236.     }
  237.     public function getSlug(): ?string
  238.     {
  239.         return $this->slug;
  240.     }
  241.     public function setSlug(string $slug null): self
  242.     {
  243.         $this->slug $slug $slug "";
  244.         return $this;
  245.     }
  246.     public function getType(): ?int
  247.     {
  248.         return $this->type;
  249.     }
  250.     public function setType(int $type null): self
  251.     {
  252.         $this->type $type $type 0;
  253.         return $this;
  254.     }
  255.     public function getPrnt(): ?self
  256.     {
  257.         return $this->prnt;
  258.     }
  259.     public function setPrnt(?self $prnt): self
  260.     {
  261.         $this->prnt $prnt;
  262.         return $this;
  263.     }
  264.     public function getModifiedAt(): ?\DateTimeInterface
  265.     {
  266.         return $this->modified_at;
  267.     }
  268.     public function setModifiedAt(\DateTimeInterface $modified_at): self
  269.     {
  270.         $this->modified_at $modified_at;
  271.         return $this;
  272.     }
  273.     public function getCreatedAt(): ?\DateTimeInterface
  274.     {
  275.         return $this->created_at;
  276.     }
  277.     public function setCreatedAt(\DateTimeInterface $created_at): self
  278.     {
  279.         $this->created_at $created_at;
  280.         return $this;
  281.     }
  282.     public function getSort(): ?int
  283.     {
  284.         return $this->sort;
  285.     }
  286.     public function setSort(int $sort): self
  287.     {
  288.         $this->sort $sort;
  289.         return $this;
  290.     }
  291.     public function getStatus(): ?int
  292.     {
  293.         return $this->status;
  294.     }
  295.     public function setStatus(int $status): self
  296.     {
  297.         $this->status $status;
  298.         return $this;
  299.     }
  300.     /**
  301.      * @return Collection|Customer[]
  302.      * /
  303.     public function getCustomers(): Collection
  304.     {
  305.         return $this->customers;
  306.     }
  307.     public function addCustomer(Customer $customer): self
  308.     {
  309.         if (!$this->customers->contains($customer)) {
  310.             $this->customers[] = $customer;
  311.             $customer->setCustomerType($this);
  312.         }
  313.         return $this;
  314.     }
  315.     public function removeCustomer(Customer $customer): self
  316.     {
  317.         if ($this->customers->contains($customer)) {
  318.             $this->customers->removeElement($customer);
  319.             // set the owning side to null (unless already changed)
  320.             if ($customer->getCustomerType() === $this) {
  321.                 $customer->setCustomerType(null);
  322.             }
  323.         }
  324.         return $this;
  325.     }
  326.     */
  327.     /**
  328.      * @return Collection|Content[]
  329.      */
  330.     public function getContent(): Collection
  331.     {
  332.         return $this->content;
  333.     }
  334.     public function addContent(Content $content): self
  335.     {
  336.         if (!$this->content->contains($content)) {
  337.             $this->content[] = $content;
  338.             $content->setCategory($this);
  339.         }
  340.         return $this;
  341.     }
  342.     public function removeContent(Content $content): self
  343.     {
  344.         if ($this->content->contains($content)) {
  345.             $this->content->removeElement($content);
  346.             // set the owning side to null (unless already changed)
  347.             if ($content->getCategory() === $this) {
  348.                 $content->setCategory(null);
  349.             }
  350.         }
  351.         return $this;
  352.     }
  353.     
  354.     public function getSecondaryContent (): Collection
  355.     {
  356.         return $this->secondary_content;
  357.     }
  358.     
  359.     public function addSecondaryContent (Content $content)
  360.     {
  361.         if (!$this->secondary_content->contains($content)) {
  362.             $this->secondary_content[] = $content;
  363.             $content->addSecondaryCategory($this);
  364.         }
  365.         return $this;
  366.     }
  367.     
  368.     public function removeSecondaryContent (Content $content): self
  369.     {
  370.         if ($this->secondary_content->contains($content)) {
  371.             $this->secondary_content->removeElement($content);
  372.             $content->removeSecondaryCategory($this);
  373.         }
  374.         return $this;
  375.     }
  376.     
  377.     public function getPinnedEbook($site "") {
  378.         
  379.         $criteria Criteria::create()
  380.             ->where(Criteria::expr()->eq("status""1"))
  381.             ->orderBy(["id" => Criteria::ASC])
  382.         ;
  383.         
  384.         $content $this->getSecondaryContent()->matching($criteria);
  385.         //$content = $this->getSecondaryContent();
  386.         
  387.         if(!$content) {
  388.             return null;
  389.         }
  390.         if($site) {
  391.             $tempContent $content;
  392.             $content = [];
  393.             foreach($tempContent as $c) {
  394.                 foreach($c->getSites() as $s) {
  395.                     if($s->getId() == $site) {
  396.                         $content[] = $c;
  397.                     }
  398.                 }
  399.             }
  400.             if(!$content) {
  401.                 return null;
  402.             }
  403.         }
  404.         $pinnedContent = [];
  405.         foreach($content as $c) {
  406.             if($c->getEbookPinned()) {
  407.                 $pinnedContent[] = $c;
  408.             }
  409.         }
  410.         
  411.         if(!empty($pinnedContent)) {
  412.             return $pinnedContent[array_rand($pinnedContent)];
  413.         }
  414.         
  415.         //return $content->first();
  416.         return $content[0];
  417.         
  418.         
  419.     }
  420.     
  421.     /**
  422.      * @return Collection|Product[]
  423.      */
  424.     public function getProducts(): Collection
  425.     {
  426.         return $this->products;
  427.     }
  428.     public function addProduct(Product $product): self
  429.     {
  430.         if (!$this->products->contains($product)) {
  431.             $this->products[] = $product;
  432.             $product->setCategory($this);
  433.         }
  434.         return $this;
  435.     }
  436.     public function removeProduct(Product $product): self
  437.     {
  438.         if ($this->products->contains($product)) {
  439.             $this->products->removeElement($product);
  440.             // set the owning side to null (unless already changed)
  441.             if ($product->getCategory() === $this) {
  442.                 $product->setCategory(null);
  443.             }
  444.         }
  445.         return $this;
  446.     }
  447.     
  448.     public function getSecondaryProducts (): Collection
  449.     {
  450.         return $this->secondary_products;
  451.     }
  452.     
  453.     public function addSecondaryProduct (Product $product)
  454.     {
  455.         if (!$this->secondary_products->contains($product)) {
  456.             $this->secondary_products[] = $product;
  457.             $product->addSecondaryCategory($this);
  458.         }
  459.         return $this;
  460.     }
  461.     
  462.     public function removeSecondaryProduct (Product $product): self
  463.     {
  464.         if ($this->secondary_products->contains($product)) {
  465.             $this->secondary_products->removeElement($product);
  466.             $product->removeSecondaryCategory($this);
  467.         }
  468.         return $this;
  469.     }
  470.     public function getTaxonomy(): ?string
  471.     {
  472.         return $this->taxonomy;
  473.     }
  474.     public function setTaxonomy(string $taxonomy null): self
  475.     {
  476.         $this->taxonomy $taxonomy $taxonomy "";
  477.         return $this;
  478.     }
  479.     /**
  480.      * @return Collection|Customer[]
  481.      */
  482.     public function getCustomers(): Collection
  483.     {
  484.         return $this->customers;
  485.     }
  486.     public function addCustomer(Customer $customer): self
  487.     {
  488.         if (!$this->customers->contains($customer)) {
  489.             $this->customers[] = $customer;
  490.             $customer->addCategory($this);
  491.         }
  492.         return $this;
  493.     }
  494.     public function removeCustomer(Customer $customer): self
  495.     {
  496.         if ($this->customers->contains($customer)) {
  497.             $this->customers->removeElement($customer);
  498.             $customer->removeCategory($this);
  499.         }
  500.         return $this;
  501.     }
  502.     
  503.     public function getChildren ()
  504.     {
  505.         return $this->children;
  506.     }
  507.     
  508.     public function addChild (Category $child) {
  509.         if (!$this->children->contains($child)) {
  510.             $this->children[] = $child;
  511.             $child->setPrnt($this);
  512.         }
  513.         
  514.         return $this;
  515.     }
  516.     
  517.     public function removeChild (Category $children): self
  518.     {
  519.         if ($this->children->contains($children)) {
  520.             $this->children->removeElement($children);
  521.             // set the owning side to null (unless already changed)
  522.             if ($children->getPrnt() === $this) {
  523.                 $children->setPrnt(null);
  524.             }
  525.         }
  526.         
  527.         return $this;
  528.     }
  529.     
  530.     public function getURL ()
  531.     {
  532.         // grab URL based on category taxonomy
  533.         
  534.         if ($this->taxonomy && strtolower($this->taxonomy) != "category") {
  535.             return "/taxonomy/{$this->taxonomy}/{$this->slug}";
  536.         }
  537.         
  538.         return "/category/{$this->slug}";
  539.     }
  540.     
  541.     public function getSiteMapURL()
  542.     {
  543.         if ($this->taxonomy && strtolower($this->taxonomy) == "ebook-category") {
  544.             return "/category/ebooks/{$this->slug}";
  545.         }
  546.         return "/category/{$this->slug}";
  547.     }
  548.     public function getMedia (): ?Media
  549.     {
  550.         return $this->media;
  551.     }
  552.     
  553.     public function setMedia (Media $media null): self
  554.     {
  555.         $this->media $media;
  556.         
  557.         return $this;
  558.     }
  559.     public function __toString ()
  560.     {
  561.         return $this->title $this->title "";
  562.     }
  563.     
  564.     
  565. }