src/CmsBundle/Entity/Template.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\Entity;
  3. use App\CmsBundle\Enum\LogActionEnum;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\CmsBundle\Repository\TemplateRepository")
  9.  */
  10. class Template implements LogInterface
  11. {
  12.     private const MAX_COLUMN_WIDTH 12;
  13.     private const SECTION_PRIME 'prime';
  14.     private const SECTION_MAIN 'main';
  15.     use IdTraitTitleTraitParametersTraitContentTraitLogTrait;
  16.     /**
  17.      * @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="templates", cascade={"persist"})
  18.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  19.      */
  20.     private $route;
  21.     /**
  22.      * @var ArrayCollection
  23.      * @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Page", mappedBy="template", cascade={"persist","remove"})
  24.      * @ORM\JoinColumn(nullable=false)
  25.      */
  26.     protected $pages;
  27.     /**
  28.      * @var ArrayCollection
  29.      * @ORM\OneToMany(targetEntity="App\CmsBundle\Entity\Widget", mappedBy="template", cascade={"persist","remove"})
  30.      * @ORM\JoinColumn(nullable=false)
  31.      */
  32.     protected $widgets;
  33.     /**
  34.      * @ORM\Column(name="is_default", type="boolean", options={"default":"0"})
  35.      */
  36.     private $default;
  37.     /**
  38.      * @ORM\Column(type="text", nullable=true)
  39.      */
  40.     private string $columns;
  41.     /**
  42.      * @ORM\Column(type="string", options={"default":"casinosanalyzer"})
  43.      */
  44.     private string $path;
  45.     private bool $acceptForAllRoutePages;
  46.     public function __construct()
  47.     {
  48.         $this->pages = new ArrayCollection();
  49.         $this->widgets = new ArrayCollection();
  50.         $this->parameters json_encode([]);
  51.         $this->default false;
  52.     }
  53.     public function getParameters(): array
  54.     {
  55.         return array_merge(
  56.             json_decode($this->parameters ?? '',true) ?? [],
  57.             [
  58.                 'id' => (string)$this->id,
  59.                 'columns' => $this->getColumns()
  60.             ]
  61.         );
  62.     }
  63.     public function __toString(): string
  64.     {
  65.         return $this->getPath() ?? '';
  66.     }
  67.     public function getFullTitle(): string
  68.     {
  69.         return $this->getRoute()->getTitle() . ' - '$this->title;
  70.     }
  71.     public function setDefault(bool $value true): self
  72.     {
  73.         $this->default $value;
  74.         return $this;
  75.     }
  76.     public function getDefault(): bool
  77.     {
  78.         return $this->default;
  79.     }
  80.     public function getRoute(): ?Route
  81.     {
  82.         return $this->route;
  83.     }
  84.     public function setRoute(?Route $route): self
  85.     {
  86.         $this->route $route;
  87.         if ($route$route->addTemplate($this);
  88.         return $this;
  89.     }
  90.     /**
  91.      * @return Collection
  92.      */
  93.     public function getPages(): Collection
  94.     {
  95.         return ($this->pages)
  96.             ? $this->pages
  97.             : new ArrayCollection();
  98.     }
  99.     /**
  100.      * @param Page $page
  101.      * @return $this
  102.      */
  103.     public function addPage(Page $page): self
  104.     {
  105.         if (!$this->pages->contains($page)) {
  106.             $this->pages[] = $page;
  107.             $page->setTemplate($this);
  108.             $this->addCollectionLog(LogActionEnum::ADD'page'$page);
  109.         }
  110.         return $this;
  111.     }
  112.     /**
  113.      * @param Page $page
  114.      * @return $this
  115.      */
  116.     public function removePage(Page $page): self
  117.     {
  118.         if ($this->pages->contains($page)) {
  119.             $this->pages->removeElement($page);
  120.             $page->setRoute(null);
  121.             $this->addCollectionLog(LogActionEnum::REMOVE'page'$page);
  122.         }
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return Collection
  127.      */
  128.     public function getWidgets(): Collection
  129.     {
  130.         return ($this->widgets)
  131.             ? $this->widgets
  132.             : new ArrayCollection();
  133.     }
  134.     /**
  135.      * @param Widget $widget
  136.      * @return $this
  137.      */
  138.     public function addWidget(Widget $widget): self
  139.     {
  140.         if (!$this->widgets->contains($widget)) {
  141.             $this->widgets[] = $widget;
  142.             $widget->setTemplate($this);
  143.             $this->addCollectionLog(LogActionEnum::ADD'widget'$widget);
  144.         }
  145.         return $this;
  146.     }
  147.     /**
  148.      * @param Widget $widget
  149.      * @return $this
  150.      */
  151.     public function removeWidget(Widget $widget): self
  152.     {
  153.         if ($this->widgets->contains($widget)) {
  154.             $this->widgets->removeElement($widget);
  155.             $widget->setTemplate(null);
  156.             $this->addCollectionLog(LogActionEnum::REMOVE'widget'$widget);
  157.         }
  158.         return $this;
  159.     }
  160.     /**
  161.      * @param int $width
  162.      * @return $this
  163.      */
  164.     public function setMainSection1(int $width): self
  165.     {
  166.         return $this->setSectionWidth(self::SECTION_MAIN1$width);
  167.     }
  168.     /**
  169.      * @return int
  170.      */
  171.     public function getMainSection1(): int
  172.     {
  173.         return $this->getSectionWidth(self::SECTION_MAIN1);
  174.     }
  175.     /**
  176.      * @param int $width
  177.      * @return $this
  178.      */
  179.     public function setMainSection2(int $width): self
  180.     {
  181.         return $this->setSectionWidth(self::SECTION_MAIN2$width);
  182.     }
  183.     /**
  184.      * @return int
  185.      */
  186.     public function getMainSection2(): int
  187.     {
  188.         return $this->getSectionWidth(self::SECTION_MAIN2);
  189.     }
  190.     /**
  191.      * @param int $width
  192.      * @return $this
  193.      */
  194.     public function setMainSection3(int $width): self
  195.     {
  196.         return $this->setSectionWidth(self::SECTION_MAIN3$width);
  197.     }
  198.     /**
  199.      * @return int
  200.      */
  201.     public function getMainSection3(): int
  202.     {
  203.         return $this->getSectionWidth(self::SECTION_MAIN3);
  204.     }
  205.     /**
  206.      * @param int $width
  207.      * @return $this
  208.      */
  209.     public function setMainSection4(int $width): self
  210.     {
  211.         return $this->setSectionWidth(self::SECTION_MAIN4$width);
  212.     }
  213.     /**
  214.      * @return int
  215.      */
  216.     public function getMainSection4(): int
  217.     {
  218.         return $this->getSectionWidth(self::SECTION_MAIN4);
  219.     }
  220.     /**
  221.      * @param int $width
  222.      * @return $this
  223.      */
  224.     public function setMainSection5(int $width): self
  225.     {
  226.         return $this->setSectionWidth(self::SECTION_MAIN5$width);
  227.     }
  228.     /**
  229.      * @return int
  230.      */
  231.     public function getMainSection5(): int
  232.     {
  233.         return $this->getSectionWidth(self::SECTION_MAIN5);
  234.     }
  235.     /**
  236.      * @param int $width
  237.      * @return $this
  238.      */
  239.     public function setMainSection6(int $width): self
  240.     {
  241.         return $this->setSectionWidth(self::SECTION_MAIN6$width);
  242.     }
  243.     /**
  244.      * @return int
  245.      */
  246.     public function getMainSection6(): int
  247.     {
  248.         return $this->getSectionWidth(self::SECTION_MAIN6);
  249.     }
  250.     /**
  251.      * @param int $width
  252.      * @return $this
  253.      */
  254.     public function setMainSection7(int $width): self
  255.     {
  256.         return $this->setSectionWidth(self::SECTION_MAIN7$width);
  257.     }
  258.     /**
  259.      * @return int
  260.      */
  261.     public function getMainSection7(): int
  262.     {
  263.         return $this->getSectionWidth(self::SECTION_MAIN7);
  264.     }
  265.     /**
  266.      * @param int $width
  267.      * @return $this
  268.      */
  269.     public function setMainSection8(int $width): self
  270.     {
  271.         return $this->setSectionWidth(self::SECTION_MAIN8$width);
  272.     }
  273.     /**
  274.      * @return int
  275.      */
  276.     public function getMainSection8(): int
  277.     {
  278.         return $this->getSectionWidth(self::SECTION_MAIN8);
  279.     }
  280.     /**
  281.      * @param int $width
  282.      * @return $this
  283.      */
  284.     public function setMainSection9(int $width): self
  285.     {
  286.         return $this->setSectionWidth(self::SECTION_MAIN9$width);
  287.     }
  288.     /**
  289.      * @return int
  290.      */
  291.     public function getMainSection9(): int
  292.     {
  293.         return $this->getSectionWidth(self::SECTION_MAIN9);
  294.     }
  295.     /**
  296.      * @param int $width
  297.      * @return $this
  298.      */
  299.     public function setMainSection10(int $width): self
  300.     {
  301.         return $this->setSectionWidth(self::SECTION_MAIN10$width);
  302.     }
  303.     /**
  304.      * @return int
  305.      */
  306.     public function getMainSection10(): int
  307.     {
  308.         return $this->getSectionWidth(self::SECTION_MAIN10);
  309.     }
  310.     /**
  311.      * @param int $width
  312.      * @return $this
  313.      */
  314.     public function setPrimeSection1(int $width): self
  315.     {
  316.         return $this->setSectionWidth(self::SECTION_PRIME1$width);
  317.     }
  318.     /**
  319.      * @return int
  320.      */
  321.     public function getPrimeSection1(): int
  322.     {
  323.         return $this->getSectionWidth(self::SECTION_PRIME1);
  324.     }
  325.     /**
  326.      * @param int $width
  327.      * @return $this
  328.      */
  329.     public function setPrimeSection2(int $width): self
  330.     {
  331.         return $this->setSectionWidth(self::SECTION_PRIME2$width);
  332.     }
  333.     /**
  334.      * @return int
  335.      */
  336.     public function getPrimeSection2(): int
  337.     {
  338.         return $this->getSectionWidth(self::SECTION_PRIME2);
  339.     }
  340.     /**
  341.      * @param int $width
  342.      * @return $this
  343.      */
  344.     public function setPrimeSection3(int $width): self
  345.     {
  346.         return $this->setSectionWidth(self::SECTION_PRIME3$width);
  347.     }
  348.     /**
  349.      * @return int
  350.      */
  351.     public function getPrimeSection3(): int
  352.     {
  353.         return $this->getSectionWidth(self::SECTION_PRIME3);
  354.     }
  355.     /**
  356.      * @param int $width
  357.      * @return $this
  358.      */
  359.     public function setPrimeSection4(int $width): self
  360.     {
  361.         return $this->setSectionWidth(self::SECTION_PRIME4$width);
  362.     }
  363.     /**
  364.      * @return int
  365.      */
  366.     public function getPrimeSection4(): int
  367.     {
  368.         return $this->getSectionWidth(self::SECTION_PRIME4);
  369.     }
  370.     /**
  371.      * @param int $width
  372.      * @return $this
  373.      */
  374.     public function setPrimeSection5(int $width): self
  375.     {
  376.         return $this->setSectionWidth(self::SECTION_PRIME5$width);
  377.     }
  378.     /**
  379.      * @return int
  380.      */
  381.     public function getPrimeSection5(): int
  382.     {
  383.         return $this->getSectionWidth(self::SECTION_PRIME5);
  384.     }
  385.     /**
  386.      * @param int $width
  387.      * @return $this
  388.      */
  389.     public function setPrimeSection6(int $width): self
  390.     {
  391.         return $this->setSectionWidth(self::SECTION_PRIME6$width);
  392.     }
  393.     /**
  394.      * @return int
  395.      */
  396.     public function getPrimeSection6(): int
  397.     {
  398.         return $this->getSectionWidth(self::SECTION_PRIME6);
  399.     }
  400.     /**
  401.      * @param string $path
  402.      * @return $this
  403.      */
  404.     public function setPath(string $path): self
  405.     {
  406.         $this->path $path;
  407.         return $this;
  408.     }
  409.     /**
  410.      * @return string
  411.      */
  412.     public function getPath(): string
  413.     {
  414.         return $this->path;
  415.     }
  416.     /**
  417.      * @param int $number
  418.      * @param string $type
  419.      * @return int
  420.      */
  421.     public function getSectionWidth(string $typeint $number): int
  422.     {
  423.         return (array_key_exists($type$this->getColumns())
  424.             && array_key_exists($number$this->getColumns()[$type]))
  425.             ? $this->getColumns()[$type][$number]
  426.             : self::MAX_COLUMN_WIDTH;
  427.     }
  428.     /**
  429.      * @param string $type
  430.      * @param int $number
  431.      * @param int $value
  432.      * @return $this
  433.      */
  434.     public function setSectionWidth(string $typeint $numberint $value): self
  435.     {
  436.         if ($value && $value <= self::MAX_COLUMN_WIDTH) {
  437.             $this->setColumns(
  438.                 array_replace_recursive(
  439.                     $this->getColumns(),
  440.                     [$type => [$number => $value]]
  441.                 )
  442.             );
  443.         }
  444.         return $this;
  445.     }
  446.     public function getColumns(): array
  447.     {
  448.         return json_decode($this->columns ?? '',true) ?? [];
  449.     }
  450.     /**
  451.      * @param array $columns
  452.      * @return void
  453.      */
  454.     public function setColumns(array $columns): void
  455.     {
  456.         $this->columns json_encode($columns);
  457.     }
  458.     /**
  459.      * @return bool
  460.      */
  461.     public function getAcceptForAllRoutePages(): bool
  462.     {
  463.         return $this->acceptForAllRoutePages;
  464.     }
  465.     /**
  466.      * @param $acceptForAllRoutePages
  467.      * @return void
  468.      */
  469.     public function setAcceptForAllRoutePages($acceptForAllRoutePages): void
  470.     {
  471.         $this->acceptForAllRoutePages $acceptForAllRoutePages;
  472.     }
  473.     /**
  474.      * @return int
  475.      */
  476.     public function getWidgetIntent(): int
  477.     {
  478.         foreach ($this->getWidgets() as $widget) {
  479.             if($widget->getParameter('intentFit')){
  480.                 return 1;
  481.             }
  482.         }
  483.         return 0;
  484.     }
  485. }