<?php
namespace App\Entity\Web;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Behaviors\TranslationTrait;
use App\Entity\Behaviors\SluggableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping\Index;
/**
* @ORM\Table(name="web_news_translation",uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
* indexes={@Index(name="locale_slug", columns={"locale","slug"})})
* @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
* @ORM\EntityListeners({"App\Listener\EntityListener"})
* @Vich\Uploadable()
* @ORM\HasLifecycleCallbacks()
*/
class NewsTranslation implements TranslationInterface, SluggableInterface
{
use TranslationTrait;
use SluggableTrait;
/**
* @ORM\Column(type="string", length=2)
*/
protected $locale;
/**
* @ORM\Column(type="string", length=255)
*/
private string $title = '';
/**
* @ORM\Column(type="string", length=190)
*/
protected $slug;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @Vich\UploadableField(mapping="news", fileNameProperty="image")
* @Assert\Image(mimeTypes={"image/png", "image/jpeg", "image/jpg", "image/gif"})
* @var File|null
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\Column(type="datetime")
*/
private $createts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
private $createUser;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $editUser;
public function __construct($locale = null) {
if($locale !== null){
$this->setLocale($locale);
}
}
public function getId(): ?int
{
return $this->id;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
/**
* Get ImageFile
*
* @return null|File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
$this->editts = new \DateTime();
}
}
/**
* @return mixed
*/
public function getCreatets()
{
return $this->createts;
}
/**
* @param mixed $createts
*/
public function setCreatets($createts): void
{
$this->createts = $createts;
}
/**
* @return mixed
*/
public function getCreateUser()
{
return $this->createUser;
}
/**
* @param mixed $createUser
*/
public function setCreateUser($createUser): void
{
$this->createUser = $createUser;
}
/**
* @return mixed
*/
public function getEditts()
{
return $this->editts;
}
/**
* @param mixed $editts
*/
public function setEditts($editts): void
{
$this->editts = $editts;
}
/**
* @return mixed
*/
public function getEditUser()
{
return $this->editUser;
}
/**
* @param mixed $editUser
*/
public function setEditUser($editUser): void
{
$this->editUser = $editUser;
}
public function getSluggableFields(): array
{
return ['title'];
}
/**
* @return mixed
*/
public function getLocale(): string
{
return $this->locale;
}
/**
* @param mixed $locale
*/
public function setLocale($locale): void
{
$this->locale = $locale;
}
/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle(string $title): void
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getSlug(): string
{
return (string) $this->slug;
}
/**
* @param mixed $slug
*/
public function setSlug($slug): void
{
$this->slug = $slug;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description): void
{
$this->description = $description;
}
public function generateSlug(): void { }
/**
* @ORM\PreUpdate()
* @ORM\PrePersist()
*/
public function updateSlug( LifecycleEventArgs $eventArgs )
{
$translatable = $this->getTranslatable();
$oldSlug = $translatable->getSlug();
if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
$fields = $this->getSluggableFields();
$values = [];
foreach ( $fields as $field ) {
if ( property_exists( $this, $field ) ) {
$val = $this->{$field};
} else {
$methodName = 'get' . ucfirst( $field );
if ( method_exists( $this, $methodName ) ) {
$val = $this->{$methodName}();
} else {
$val = null;
}
}
$values[] = $val;
}
$this->slug = $this->generateSlugValue( $values );
if ( $oldSlug !== $this->slug ) {
$repo = $eventArgs->getEntityManager()->getRepository( get_class( $this ) );
$repo->updateSlug( $this );
}
}
}
}