<?php
namespace AdminBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* News
*/
#[ORM\Table(name: 'news')]
#[ORM\Entity(repositoryClass: \AdminBundle\Repository\NewsRepository::class)]
#[ORM\HasLifecycleCallbacks]
class News
{
const TYPE_CLIENT = 0;
const TYPE_DRIVER = 1;
const FILTER_ALL_CLIENTS = 0;
const FILTER_ALL_DRIVERS = 1;
const FILTER_ACTIVE_DRIVERS_7_DAYS = 2;
const FILTER_ACTIVE_CLIENTS_30_DAYS = 3;
const FILTER_CLIENTS_1_BOOKING = 4;
const FILTER_INACTIVE_DRIVERS = 5;
const TYPES = [
self::TYPE_CLIENT => 'Client',
self::TYPE_DRIVER => 'Driver',
];
const FILTER_TYPES = [
self::FILTER_ALL_CLIENTS => 'All Clients',
self::FILTER_ALL_DRIVERS => 'All Drivers',
self::FILTER_ACTIVE_DRIVERS_7_DAYS => 'Drivers active last 7 days',
self::FILTER_ACTIVE_CLIENTS_30_DAYS => 'Clients active last 30 days',
self::FILTER_CLIENTS_1_BOOKING => 'Clients with 1 booking',
self::FILTER_INACTIVE_DRIVERS => 'Inactive drivers',
];
const FILE_FOLDER = 'upload/news/';
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 255)]
protected $title;
/**
* @var string
*/
#[ORM\Column(name: 'description', type: 'text', nullable: true)]
protected $description;
/**
* @var int
*/
#[ORM\Column(name: 'type', type: 'integer', nullable: false)]
protected $type;
/**
* @var int
*/
#[ORM\Column(name: 'filter_type', type: 'integer', nullable: false)]
protected $filterType;
/**
* @var string
*/
#[ORM\Column(name: 'picture', type: 'string', length: 255, nullable: true)]
private $picture;
/**
* @var UploadedFile
*/
#[Assert\File(mimeTypes: ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'])]
private $pictureFile;
/**
* @var DateTime
*/
#[ORM\Column(name: 'is_active', type: 'boolean', options: ['default' => false])]
protected $isActive;
/**
* @var DateTime
*/
#[ORM\Column(name: 'updated_at', type: 'datetime')]
protected $updatedAt;
public function __construct()
{
$this->isActive = false;
$this->updatedAt = new DateTime();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return News
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*
* @return News
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*
* @return News
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @return int
*/
public function getFilterType()
{
return $this->filterType;
}
/**
* @param int $filterType
*
* @return News
*/
public function setFilterType($filterType)
{
$this->filterType = $filterType;
return $this;
}
/**
* @param UploadedFile $pictureFile
*/
public function setPictureFile(UploadedFile $pictureFile = null)
{
$this->pictureFile = $pictureFile;
return $this;
}
/**
* @return UploadedFile
*/
public function getPictureFile()
{
return $this->pictureFile;
}
public function getUploadFilePath()
{
if (!is_dir(self::FILE_FOLDER . $this->id)) {
mkdir(self::FILE_FOLDER . $this->id, 0777, true);
}
return self::FILE_FOLDER . $this->id . '/';
}
public function getPictureFilePath()
{
if (!$this->picture) {
return;
}
return $this->getUploadFilePath() . $this->picture;
}
public function uploadFile()
{
$pictureFile = $this->getPictureFile();
if (!$pictureFile) {
return;
}
$this->removePictureFile();
$nDate = new DateTime();
$filename = 'NEWS_FILE_' . $nDate->format('Y-m-d-His');
$this->picture = $filename . '.' . $pictureFile->guessExtension();
$pictureFile->move(
$this->getUploadFilePath(),
$this->picture
);
$this->setPictureFile(null);
}
#[ORM\PreUpdate]
#[ORM\PrePersist]
public function lifecycleFileUpload()
{
$this->uploadFile();
}
#[ORM\PostRemove]
public function removeUpload()
{
$this->removePictureFile();
}
#[ORM\PostPersist]
public function onPostPersist()
{
rename(
self::FILE_FOLDER . $this->picture,
$this->getUploadFilePath() . $this->picture
);
}
public function removePictureFile()
{
if ($pictureFile = $this->getPictureFilePath()) {
@unlink($pictureFile);
}
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param DateTime $updatedAt
*
* @return MobileAds
*/
public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return boolean
*/
public function isActive()
{
return $this->isActive;
}
/**
* @param boolean $isActive
*
* @return News
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
}