src/Entity/Product.php line 12

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. /**
  7.  * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
  8.  */
  9. class Product implements \Serializable 
  10. {
  11.     const PRODUCT_TYPE_DIRECTORY 1;
  12.     const PRODUCT_TYPE_CLASSIFIED 2;
  13.     const PRODUCT_TYPE_MEMBERSHIP 3;
  14.     const PRODUCT_TYPE_SHOP 4;
  15.     
  16.     const RENEW_YEARLY "annual";
  17.     const RENEW_MONTHLY "month";
  18.     const RENEW_BIMONTHLY "bimonthly";
  19.     const RENEW_TRIMONTHLY "trimonthly";
  20.     const RENEW_NOT_SET "none";
  21.     
  22.     /**
  23.      * @ORM\Id()
  24.      * @ORM\GeneratedValue()
  25.      * @ORM\Column(type="bigint")
  26.      */
  27.     private $id;
  28.     
  29.     /**
  30.      * @ORM\Version @ORM\Column(type="integer")
  31.      */
  32.     private $version;
  33.     /**
  34.      * @ORM\Column(type="string", length=255)
  35.      */
  36.     private $title;
  37.     /**
  38.      * @ORM\Column(type="text")
  39.      */
  40.     private $description;
  41.     
  42.     /**
  43.      * @ORM\Column(type="string", length=255)
  44.      */
  45.     private $slug;
  46.     
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
  49.      * @ORM\JoinColumn(nullable=true)
  50.      */
  51.     private $media;
  52.     
  53.     /**
  54.      * @ORM\ManyToOne(targetEntity="App\Entity\Media", cascade={"persist"})
  55.      * @ORM\JoinColumn(nullable=true)
  56.      */
  57.     private $dummy_media//same as in the content admin
  58.     /**
  59.      * @ O R M\Column(type="decimal", precision=10, scale=2)
  60.      * @ORM\Column(type="integer")
  61.      * 
  62.      * Store as pennies
  63.      */
  64.     private $price;
  65.     /**
  66.      * @ORM\Column(type="string", length=32)
  67.      * 
  68.      * YEARLY, MONTHLY, ETC ...
  69.      * 
  70.      */
  71.     private $payment_rate;
  72.     /**
  73.      * @ORM\Column(type="integer")
  74.      */
  75.     private $type;
  76.     /**
  77.      * @ORM\Column(type="integer")
  78.      */
  79.     private $status;
  80.     /**
  81.      * @ORM\Column(type="datetime")
  82.      */
  83.     private $published;
  84.     /**
  85.      * @ORM\Column(type="datetime")
  86.      */
  87.     private $modified_at;
  88.     /**
  89.      * @ORM\Column(type="datetime")
  90.      */
  91.     private $created_at;
  92.     /**
  93.      * @ORM\OneToMany(targetEntity="App\Entity\PurchaseItem", mappedBy="product")
  94.      */
  95.     private $purchase_items;
  96.     /**
  97.      * @ORM\ManyToMany(targetEntity="App\Entity\Coupon", mappedBy="products")
  98.      */
  99.     private $coupons;
  100.     
  101.     /**
  102.      * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products")
  103.      */
  104.     private $category;
  105.     
  106.     /**
  107.      * The secondary categories should contain ALL the categories assigned to the product object.
  108.      * @ORM\ManyToMany(targetEntity="App\Entity\Category", inversedBy="secondary_products")
  109.      */
  110.     private $secondary_categories;
  111.     
  112.     /**
  113.      * @ORM\ManyToMany(targetEntity="App\Entity\Customer", mappedBy="products", cascade={"persist"})
  114.      */
  115.     private $customers;
  116.     
  117.     /**
  118.      * @ORM\OneToMany(targetEntity="App\Entity\ProductOption", mappedBy="product", orphanRemoval=true, cascade={"persist", "remove"}, fetch="EAGER")
  119.      */
  120.     private $productOptions;
  121.     public function __construct()
  122.     {
  123.         $this->price 0;
  124.         $this->payment_rate "";
  125.         $this->description "";
  126.         $this->purchase_items = new ArrayCollection();
  127.         $this->coupons = new ArrayCollection();
  128.         
  129.         $this->published = new \DateTime();
  130.         $this->modified_at = new \DateTime();
  131.         $this->created_at = new \DateTime ();
  132.         
  133.         $this->customers = new ArrayCollection();
  134.         $this->secondary_categories = new ArrayCollection();
  135.         
  136.         $this->productOptions = new ArrayCollection();
  137.     }
  138.     public function getId()
  139.     {
  140.         return $this->id;
  141.     }
  142.     
  143.     public function getVersion() {
  144.         return $this->version;
  145.     }
  146.     
  147.     public function getTitle(): ?string
  148.     {
  149.         return $this->title;
  150.     }
  151.     public function setTitle(string $title null): self
  152.     {
  153.         $this->title $title $title "";
  154.         return $this;
  155.     }
  156.     public function getDescription(): ?string
  157.     {
  158.         return $this->description;
  159.     }
  160.     public function setDescription(string $description null): self
  161.     {
  162.         $this->description $description $description "";
  163.         return $this;
  164.     }
  165.     
  166.     public function getIntroText($words 50): ?string
  167.     {
  168.         $description strip_tags($this->description);
  169.         $description trim($description);
  170.         
  171.         $description explode(" "$description);
  172.         
  173.         if ($words count($description)) {
  174.             $description array_slice($description0$words);
  175.             $description[] = "...";
  176.         }
  177.         
  178.         $description implode(" "$description);
  179.         $description htmlspecialchars_decode($descriptionENT_QUOTES);
  180.         $description html_entity_decode($description);
  181.         
  182.         return $description;
  183.     }
  184.     
  185.     public function getSlug(): ?string
  186.     {
  187.         return $this->slug;
  188.     }
  189.     public function setSlug(string $slug null): self
  190.     {
  191.         $this->slug $slug $slug "";
  192.         return $this;
  193.     }
  194.     
  195.     public function getFeaturedImageURL ()
  196.     {
  197.         if($this->media) {
  198.             return $this->media->getURL();
  199.         }
  200.         return "";
  201.     }
  202.     
  203.     public function getFeaturedImageTag ($class ""$style "")
  204.     {
  205.         if($this->media) {
  206.             return $this->media->getImageTag($class$style);
  207.         }
  208.         return "";
  209.     }
  210.     
  211.     public function getMedia (): ?Media
  212.     {
  213.         return $this->media;
  214.     }
  215.     
  216.     public function setMedia (Media $media null): self
  217.     {
  218.         $this->media $media;
  219.         
  220.         return $this;
  221.     }
  222.     
  223.     public function getDummyMedia (): ?Media
  224.     {
  225.         return $this->dummy_media;
  226.     }
  227.     
  228.     public function setDummyMedia (Media $dummy_media null): self
  229.     {
  230.         $this->dummy_media $dummy_media;
  231.         
  232.         return $this;
  233.     }
  234.     public function getPrice()
  235.     {
  236.         return $this->price;
  237.     }
  238.     
  239.     public function getPriceUsd ()
  240.     {
  241.         return "$" number_format(($this->price 100), 2);
  242.     }
  243.     public function setPrice($price null): self
  244.     {
  245.         /*
  246.         if (strpos($price, ".") == false) {
  247.             $price = preg_replace("/[^0-9]/", "", $price);
  248.             $price .= "00"; // add pennies
  249.         } else {
  250.             $price = preg_replace("/[^0-9]/", "", $price);
  251.         }
  252.         */
  253.         $price = (int) $price;
  254.         $this->price $price $price 0;
  255.         
  256.         return $this;
  257.     }
  258.     
  259.     public function getPaymentRate(): ?string
  260.     {
  261.         return $this->payment_rate;
  262.     }
  263.     public function setPaymentRate(string $payment_rate): self
  264.     {
  265.         $this->payment_rate $payment_rate;
  266.         return $this;
  267.     }
  268.     
  269.     public function getPaymentRateNice(): ?string
  270.     {
  271.         switch($this->payment_rate) {
  272.             case SELF::RENEW_YEARLY:
  273.                 return "Annually";
  274.             case SELF::RENEW_MONTHLY:
  275.                 return "Monthly";
  276.             case SELF::RENEW_BIMONTHLY:
  277.                 return "Bimonthly";
  278.             case SELF::RENEW_TRIMONTHLY:
  279.                 return "Trimonthly";
  280.             default:
  281.                 return "One Time";
  282.         }
  283.     }
  284.     public function getType(): ?int
  285.     {
  286.         return $this->type;
  287.     }
  288.     public function setType(int $type): self
  289.     {
  290.         $this->type $type;
  291.         return $this;
  292.     }
  293.     
  294.     public function getTypeString(): ?string
  295.     {
  296.         
  297.         switch ($this->type) {
  298.             case self::PRODUCT_TYPE_DIRECTORY:
  299.                 return "Directory Listing";
  300.             
  301.             case self::PRODUCT_TYPE_CLASSIFIED:
  302.                 return "Classified Listing";        
  303.                 
  304.             case self::PRODUCT_TYPE_MEMBERSHIP:
  305.                 return "R-Club Membership";    
  306.                 
  307.             case self::PRODUCT_TYPE_SHOP:
  308.                 return "RCS Shop Item";
  309.         }
  310.         
  311.         return "Product";
  312.     }
  313.     public function getStatus(): ?int
  314.     {
  315.         return $this->status;
  316.     }
  317.     public function setStatus(int $status): self
  318.     {
  319.         $this->status $status;
  320.         return $this;
  321.     }
  322.     public function getPublished(): ?\DateTimeInterface
  323.     {
  324.         return $this->published;
  325.     }
  326.     public function setPublished(\DateTimeInterface $published): self
  327.     {
  328.         $this->published $published;
  329.         return $this;
  330.     }
  331.     public function getModifiedAt(): ?\DateTimeInterface
  332.     {
  333.         return $this->modified_at;
  334.     }
  335.     public function setModifiedAt(\DateTimeInterface $modified_at): self
  336.     {
  337.         $this->modified_at $modified_at;
  338.         return $this;
  339.     }
  340.     public function getCreatedAt(): ?\DateTimeInterface
  341.     {
  342.         return $this->created_at;
  343.     }
  344.     public function setCreatedAt(\DateTimeInterface $created_at): self
  345.     {
  346.         $this->created_at $created_at;
  347.         return $this;
  348.     }
  349.     /**
  350.      * @return Collection|Order[]
  351.      */
  352.     public function getpurchase_items(): Collection
  353.     {
  354.         return $this->purchase_items;
  355.     }
  356.     public function addPurchase(Purchase $order): self
  357.     {
  358.         if (!$this->purchase_items->contains($order)) {
  359.             $this->purchase_items[] = $order;
  360.             $order->setProduct($this);
  361.         }
  362.         return $this;
  363.     }
  364.     public function removePurchase(Purchase $order): self
  365.     {
  366.         if ($this->purchase_items->contains($order)) {
  367.             $this->purchase_items->removeElement($order);
  368.             // set the owning side to null (unless already changed)
  369.             if ($order->getProduct() === $this) {
  370.                 $order->setProduct(null);
  371.             }
  372.         }
  373.         return $this;
  374.     }
  375.     
  376.     public function getFullPrice ()
  377.     {
  378.         return "$" number_format(($this->price 100), 2);
  379.     }
  380.     
  381.     public function __toString ()
  382.     {
  383.         return $this->title $this->title "";
  384.     }
  385.     /**
  386.      * @return Collection|Coupon[]
  387.      */
  388.     public function getCoupons(): Collection
  389.     {
  390.         return $this->coupons;
  391.     }
  392.     public function addCoupon(Coupon $coupon): self
  393.     {
  394.         if (!$this->coupons->contains($coupon)) {
  395.             $this->coupons[] = $coupon;
  396.             $coupon->addProduct($this);
  397.         }
  398.         return $this;
  399.     }
  400.     public function removeCoupon(Coupon $coupon): self
  401.     {
  402.         if ($this->coupons->contains($coupon)) {
  403.             $this->coupons->removeElement($coupon);
  404.             $coupon->removeProduct($this);
  405.         }
  406.         return $this;
  407.     }
  408.     
  409.     public function getCategory(): ?Category
  410.     {
  411.         return $this->category;
  412.     }
  413.     public function setCategory(?Category $category): self
  414.     {
  415.         //remove the previous primary category from the list of all categories
  416.         //(is there a better way to do this?
  417.         if($this->category) {
  418.             $this->removeSecondaryCategory($this->category);
  419.         }
  420.         
  421.         // also make sure category is set in the secondary categories too.
  422.         if($category) {
  423.             
  424.             $this->addSecondaryCategory($category);
  425.         }
  426.         
  427.         $this->category $category;
  428.         return $this;
  429.     }
  430.     
  431.     /**
  432.      * @return Collection|Category[]
  433.      */
  434.     public function getSecondaryCategories(): Collection
  435.     {
  436.         return $this->secondary_categories;
  437.     }
  438.     public function addSecondaryCategory(Category $secondaryCategory): self
  439.     {
  440.         if (!$this->secondary_categories->contains($secondaryCategory)) {
  441.             $this->secondary_categories[] = $secondaryCategory;
  442.             $secondaryCategory->addSecondaryProduct($this);
  443.         }
  444.         
  445.         return $this;
  446.     }
  447.     
  448.     public function resetSecondaryCategories(): self
  449.     {
  450.         $this->secondary_categories = new ArrayCollection();
  451.         
  452.         // retain the primary category if it exists
  453.         if ($this->category) {
  454.             $this->addSecondaryCategory($this->category);
  455.         }
  456.         return $this;
  457.     }
  458.     
  459.     public function removeSecondaryCategory(Category $secondaryCategory): self
  460.     {
  461.         if ($this->secondary_categories->contains($secondaryCategory)) {
  462.             $this->secondary_categories->removeElement($secondaryCategory);
  463.             $secondaryCategory->removeSecondaryProduct($this);
  464.         }
  465.         return $this;
  466.     }
  467.     
  468.     /**
  469.      * @return Collection|Customer[]
  470.      */
  471.     public function getCustomers(): Collection
  472.     {
  473.         return $this->customers;
  474.     }
  475.     
  476.     //don't think Sonata is using this
  477.     public function addCustomers($customers): self
  478.     {
  479.         if(is_array($customers)) {
  480.             foreach($customers as $customer) {
  481.                 $this->addCustomer($customer);
  482.             }
  483.         }
  484.         else {
  485.             $this->customers->clear();
  486.             $this->addCustomer($customers);
  487.         }
  488.         
  489.         
  490.         return $this;
  491.     }
  492.     public function addCustomer(Customer $customer): self
  493.     {
  494.         if (!$this->customers->contains($customer)) {
  495.             $this->customers[] = $customer;
  496.             $customer->addProduct($this);
  497.         }
  498.         return $this;
  499.     }
  500.     public function removeCustomer(Customer $customer): self
  501.     {
  502.         if ($this->customers->contains($customer)) {
  503.             $this->customers->removeElement($customer);
  504.             $customer->removeProduct($this);
  505.         }
  506.         return $this;
  507.     }
  508.     
  509.     public function resetCustomers(): self
  510.     {
  511.         foreach($this->customers as $customer) {
  512.             $this->removeCustomer($customer);
  513.         }
  514.         return $this;
  515.     }
  516.     
  517.     /**
  518.      * @return Collection|ProductOption[]
  519.      */
  520.     public function getProductOptions(): Collection
  521.     {
  522.         return $this->productOptions;
  523.     }
  524.     public function addProductOption(ProductOption $productOption): self
  525.     {
  526.         if (!$this->productOptions->contains($productOption)) {
  527.             $this->productOptions[] = $productOption;
  528.             $productOption->setProduct($this);
  529.         }
  530.         return $this;
  531.     }
  532.     public function removeProductOption(ProductOption $productOption): self
  533.     {
  534.         if ($this->productOptions->contains($productOption)) {
  535.             $this->productOptions->removeElement($productOption);
  536.             // set the owning side to null (unless already changed)
  537.             if ($productOption->getProduct() === $this) {
  538.                 $productOption->setProduct(null);
  539.             }
  540.         }
  541.         
  542.         return $this;
  543.     }
  544.     
  545.     public function getURL ()
  546.     {
  547.         return "/product/{$this->slug}";
  548.     }
  549.     
  550.     public function serialize ()
  551.     {
  552.         return serialize(array(
  553.             $this->id,
  554.             $this->title,
  555.             $this->description,
  556.             $this->price,
  557.             $this->payment_rate,
  558.             $this->type,
  559.             $this->status,
  560.         ));
  561.     }
  562.     
  563.     public function unserialize ($serialized)
  564.     {
  565.         list (
  566.             $this->id,
  567.             $this->title,
  568.             $this->description,
  569.             $this->price,
  570.             $this->payment_rate,
  571.             $this->type,
  572.             $this->status
  573.         ) = unserialize($serialized, array ("allowed_classes" => false));
  574.     }
  575.     
  576.     public function toJSON ()
  577.     {
  578.         return json_encode([
  579.             "id" => $this->id,
  580.             "title" => $this->title,
  581.             "description" => $this->description,
  582.             "price" => $this->price,
  583.             "payment_rate" => $this->payment_rate,
  584.             "type" => $this->type,
  585.             "status" => $this->status,
  586.         ]);
  587.     }
  588. }