<?php
namespace App\CmsBundle\Entity;
use App\CmsBundle\Enum\LogActionEnum;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\CmsBundle\Repository\TemplateRepository")
*/
class Template implements LogInterface
{
private const MAX_COLUMN_WIDTH = 12;
private const SECTION_PRIME = 'prime';
private const SECTION_MAIN = 'main';
use IdTrait, TitleTrait, ParametersTrait, ContentTrait, LogTrait;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="templates", cascade={"persist"})
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $route;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Page", mappedBy="template", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=false)
*/
protected $pages;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Widget", mappedBy="template", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=false)
*/
protected $widgets;
/**
* @ORM\Column(name="is_default", type="boolean", options={"default":"0"})
*/
private $default;
/**
* @ORM\Column(type="text", nullable=true)
*/
private string $columns;
/**
* @ORM\Column(type="string", options={"default":"casinosanalyzer"})
*/
private string $path;
private bool $acceptForAllRoutePages;
public function __construct()
{
$this->pages = new ArrayCollection();
$this->widgets = new ArrayCollection();
$this->parameters = json_encode([]);
$this->default = false;
}
public function getParameters(): array
{
return array_merge(
json_decode($this->parameters ?? '',true) ?? [],
[
'id' => (string)$this->id,
'columns' => $this->getColumns()
]
);
}
public function __toString(): string
{
return $this->getPath() ?? '';
}
public function getFullTitle(): string
{
return $this->getRoute()->getTitle() . ' - '. $this->title;
}
public function setDefault(bool $value = true): self
{
$this->default = $value;
return $this;
}
public function getDefault(): bool
{
return $this->default;
}
public function getRoute(): ?Route
{
return $this->route;
}
public function setRoute(?Route $route): self
{
$this->route = $route;
if ($route) $route->addTemplate($this);
return $this;
}
/**
* @return Collection
*/
public function getPages(): Collection
{
return ($this->pages)
? $this->pages
: new ArrayCollection();
}
/**
* @param Page $page
* @return $this
*/
public function addPage(Page $page): self
{
if (!$this->pages->contains($page)) {
$this->pages[] = $page;
$page->setTemplate($this);
$this->addCollectionLog(LogActionEnum::ADD, 'page', $page);
}
return $this;
}
/**
* @param Page $page
* @return $this
*/
public function removePage(Page $page): self
{
if ($this->pages->contains($page)) {
$this->pages->removeElement($page);
$page->setRoute(null);
$this->addCollectionLog(LogActionEnum::REMOVE, 'page', $page);
}
return $this;
}
/**
* @return Collection
*/
public function getWidgets(): Collection
{
return ($this->widgets)
? $this->widgets
: new ArrayCollection();
}
/**
* @param Widget $widget
* @return $this
*/
public function addWidget(Widget $widget): self
{
if (!$this->widgets->contains($widget)) {
$this->widgets[] = $widget;
$widget->setTemplate($this);
$this->addCollectionLog(LogActionEnum::ADD, 'widget', $widget);
}
return $this;
}
/**
* @param Widget $widget
* @return $this
*/
public function removeWidget(Widget $widget): self
{
if ($this->widgets->contains($widget)) {
$this->widgets->removeElement($widget);
$widget->setTemplate(null);
$this->addCollectionLog(LogActionEnum::REMOVE, 'widget', $widget);
}
return $this;
}
/**
* @param int $width
* @return $this
*/
public function setMainSection1(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 1, $width);
}
/**
* @return int
*/
public function getMainSection1(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 1);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection2(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 2, $width);
}
/**
* @return int
*/
public function getMainSection2(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 2);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection3(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 3, $width);
}
/**
* @return int
*/
public function getMainSection3(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 3);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection4(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 4, $width);
}
/**
* @return int
*/
public function getMainSection4(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 4);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection5(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 5, $width);
}
/**
* @return int
*/
public function getMainSection5(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 5);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection6(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 6, $width);
}
/**
* @return int
*/
public function getMainSection6(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 6);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection7(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 7, $width);
}
/**
* @return int
*/
public function getMainSection7(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 7);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection8(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 8, $width);
}
/**
* @return int
*/
public function getMainSection8(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 8);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection9(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 9, $width);
}
/**
* @return int
*/
public function getMainSection9(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 9);
}
/**
* @param int $width
* @return $this
*/
public function setMainSection10(int $width): self
{
return $this->setSectionWidth(self::SECTION_MAIN, 10, $width);
}
/**
* @return int
*/
public function getMainSection10(): int
{
return $this->getSectionWidth(self::SECTION_MAIN, 10);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection1(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 1, $width);
}
/**
* @return int
*/
public function getPrimeSection1(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 1);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection2(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 2, $width);
}
/**
* @return int
*/
public function getPrimeSection2(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 2);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection3(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 3, $width);
}
/**
* @return int
*/
public function getPrimeSection3(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 3);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection4(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 4, $width);
}
/**
* @return int
*/
public function getPrimeSection4(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 4);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection5(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 5, $width);
}
/**
* @return int
*/
public function getPrimeSection5(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 5);
}
/**
* @param int $width
* @return $this
*/
public function setPrimeSection6(int $width): self
{
return $this->setSectionWidth(self::SECTION_PRIME, 6, $width);
}
/**
* @return int
*/
public function getPrimeSection6(): int
{
return $this->getSectionWidth(self::SECTION_PRIME, 6);
}
/**
* @param string $path
* @return $this
*/
public function setPath(string $path): self
{
$this->path = $path;
return $this;
}
/**
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* @param int $number
* @param string $type
* @return int
*/
public function getSectionWidth(string $type, int $number): int
{
return (array_key_exists($type, $this->getColumns())
&& array_key_exists($number, $this->getColumns()[$type]))
? $this->getColumns()[$type][$number]
: self::MAX_COLUMN_WIDTH;
}
/**
* @param string $type
* @param int $number
* @param int $value
* @return $this
*/
public function setSectionWidth(string $type, int $number, int $value): self
{
if ($value > 0 && $value <= self::MAX_COLUMN_WIDTH) {
$this->setColumns(
array_replace_recursive(
$this->getColumns(),
[$type => [$number => $value]]
)
);
}
return $this;
}
public function getColumns(): array
{
return json_decode($this->columns ?? '',true) ?? [];
}
/**
* @param array $columns
* @return void
*/
public function setColumns(array $columns): void
{
$this->columns = json_encode($columns);
}
/**
* @return bool
*/
public function getAcceptForAllRoutePages(): bool
{
return $this->acceptForAllRoutePages;
}
/**
* @param $acceptForAllRoutePages
* @return void
*/
public function setAcceptForAllRoutePages($acceptForAllRoutePages): void
{
$this->acceptForAllRoutePages = $acceptForAllRoutePages;
}
/**
* @return int
*/
public function getWidgetIntent(): int
{
foreach ($this->getWidgets() as $widget) {
if($widget->getParameter('intentFit')){
return 1;
}
}
return 0;
}
}