src/Entity/Article.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Traits\PictureTrait;
  4. use App\Traits\SortTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
  8. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  9. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  10. use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
  11. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  12. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  13. /**
  14.  * Article
  15.  *
  16.  * @ORM\Table(name="article_article")
  17.  * @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
  18.  * @ORM\AssociationOverrides({
  19. *      @ORM\AssociationOverride(name="pictures",
  20. *          joinTable=@ORM\JoinTable(
  21. *              name="article_article_picture"
  22. *          ),
  23. *          joinColumns=@ORM\JoinColumn(
  24. *              name="article_id", referencedColumnName="id"
  25. *          )
  26. *      )
  27. * })
  28.  */
  29. class Article implements TimestampableInterfaceTranslatableInterfaceSoftDeletableInterface
  30. {
  31.     use TimestampableTrait,
  32.         TranslatableTrait,
  33.         SoftDeletableTrait,
  34.         SortTrait,
  35.         PictureTrait;
  36.     /**
  37.      * @var int
  38.      *
  39.      * @ORM\Column(name="id", type="integer")
  40.      * @ORM\Id
  41.      * @ORM\GeneratedValue(strategy="AUTO")
  42.      */
  43.     private $id;
  44.     /**
  45.      * @var \DateTime
  46.      *
  47.      * @ORM\Column(name="start_date", type="datetime", nullable=true)
  48.      */
  49.     private $startDate;
  50.     /**
  51.      * @var \DateTime
  52.      *
  53.      * @ORM\Column(name="end_date", type="datetime", nullable=true)
  54.      */
  55.     private $endDate;
  56.     /**
  57.     *@ORM\ManyToMany(targetEntity="App\Entity\CategoryArticle", inversedBy="articles", cascade={"persist"})
  58.     **/
  59.     private $categories;
  60.     public function __construct()
  61.     {
  62.         $this->categories = new ArrayCollection();
  63.     }
  64.     /**
  65.      * Get id
  66.      *
  67.      * @return int
  68.      */
  69.     public function getId()
  70.     {
  71.         return $this->id;
  72.     }
  73.     /**
  74.      * Set startDate
  75.      *
  76.      * @param \DateTime $startDate
  77.      *
  78.      * @return Article
  79.      */
  80.     public function setStartDate($startDate)
  81.     {
  82.         $this->startDate $startDate;
  83.         return $this;
  84.     }
  85.     /**
  86.      * Get startDate
  87.      *
  88.      * @return \DateTime
  89.      */
  90.     public function getStartDate()
  91.     {
  92.         return $this->startDate;
  93.     }
  94.     /**
  95.      * Set endDate
  96.      *
  97.      * @param \DateTime $endDate
  98.      *
  99.      * @return Article
  100.      */
  101.     public function setEndDate($endDate)
  102.     {
  103.         $this->endDate $endDate;
  104.         return $this;
  105.     }
  106.     /**
  107.      * Get endDate
  108.      *
  109.      * @return \DateTime
  110.      */
  111.     public function getEndDate()
  112.     {
  113.         return $this->endDate;
  114.     }
  115.     public function duplicateFrom(Article $article){
  116.         $this->setStartDate($article->getStartDate());
  117.         $this->setEndDate($article->getEndDate());
  118.         foreach ($article->getPictures() as $picture){
  119.             $newPicture = new Picture();
  120.             $newPicture->duplicateFrom($picture);
  121.             $this->addPicture($newPicture);
  122.         }
  123.         foreach ($article->getTranslations() as $translation){
  124.             $newArticleTrans = new ArticleTranslation();
  125.             $newArticleTrans->duplicateFrom($translation);
  126.             $this->addTranslation($newArticleTrans);
  127.         }
  128.     }
  129.     /**
  130.      * @return mixed
  131.      */
  132.     public function getCategories()
  133.     {
  134.         return $this->categories;
  135.     }
  136.     /**
  137.      * @param mixed $category
  138.      */
  139.     public function setCategories($categories): void
  140.     {
  141.         $this->categories $categories;
  142.     }
  143.     public function isActive(){
  144.         if($this->getDeletedAt()){
  145.             return false;
  146.         }
  147.         $now = new \DateTime();
  148.         if($this->getStartDate() && $this->getStartDate()>$now){
  149.             return false;
  150.         }
  151.         if($this->getEndDate() && $this->getEndDate()<$now){
  152.             return false;
  153.         }
  154.         return true;
  155.     }
  156. }