<?php
namespace App\Entity;
use App\Traits\PictureTrait;
use App\Traits\SortTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
/**
* Article
*
* @ORM\Table(name="article_article")
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(name="pictures",
* joinTable=@ORM\JoinTable(
* name="article_article_picture"
* ),
* joinColumns=@ORM\JoinColumn(
* name="article_id", referencedColumnName="id"
* )
* )
* })
*/
class Article implements TimestampableInterface, TranslatableInterface, SoftDeletableInterface
{
use TimestampableTrait,
TranslatableTrait,
SoftDeletableTrait,
SortTrait,
PictureTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="start_date", type="datetime", nullable=true)
*/
private $startDate;
/**
* @var \DateTime
*
* @ORM\Column(name="end_date", type="datetime", nullable=true)
*/
private $endDate;
/**
*@ORM\ManyToMany(targetEntity="App\Entity\CategoryArticle", inversedBy="articles", cascade={"persist"})
**/
private $categories;
public function __construct()
{
$this->categories = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set startDate
*
* @param \DateTime $startDate
*
* @return Article
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* Get startDate
*
* @return \DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* Set endDate
*
* @param \DateTime $endDate
*
* @return Article
*/
public function setEndDate($endDate)
{
$this->endDate = $endDate;
return $this;
}
/**
* Get endDate
*
* @return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
public function duplicateFrom(Article $article){
$this->setStartDate($article->getStartDate());
$this->setEndDate($article->getEndDate());
foreach ($article->getPictures() as $picture){
$newPicture = new Picture();
$newPicture->duplicateFrom($picture);
$this->addPicture($newPicture);
}
foreach ($article->getTranslations() as $translation){
$newArticleTrans = new ArticleTranslation();
$newArticleTrans->duplicateFrom($translation);
$this->addTranslation($newArticleTrans);
}
}
/**
* @return mixed
*/
public function getCategories()
{
return $this->categories;
}
/**
* @param mixed $category
*/
public function setCategories($categories): void
{
$this->categories = $categories;
}
public function isActive(){
if($this->getDeletedAt()){
return false;
}
$now = new \DateTime();
if($this->getStartDate() && $this->getStartDate()>$now){
return false;
}
if($this->getEndDate() && $this->getEndDate()<$now){
return false;
}
return true;
}
}