<?php
namespace App\CmsBundle\Entity;
use App\CmsBundle\Enum\WidgetPosition;
use App\CmsBundle\Helper\WidgetHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Growthbook\Growthbook;
use Doctrine\Common\Collections\Collection;
/**
* Widget
*
* @ORM\Table(
* name="widget",
* indexes={
* @ORM\Index(name="widget_position_index", columns={"position"}),
* @ORM\Index(name="widget_published_index", columns={"published"}),
* @ORM\Index(name="widget_template_index", columns={"template_id"}),
* @ORM\Index(name="widget_route_index", columns={"route_id"})
* }
* )
* @ORM\Entity(repositoryClass="App\CmsBundle\Repository\WidgetRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Widget
{
use IdTrait, TitleTrait, AlternativeTitleTrait, ParametersTrait, PublishedTrait, TimeStampedTrait;
/**
* @ORM\Column(name="summary_title", type="text", length=255, nullable=true)
*/
private $summaryTitle;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer", nullable=false)
*/
private $position;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $handler;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $displayTitle;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $generateSlugs;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Template", inversedBy="widgets", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $template;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="widgets", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $route;
/**
* @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Preset", inversedBy="widgets")
* @ORM\JoinColumn(name="preset_id", referencedColumnName="id")
*/
private ?Preset $preset = null;
/**
* @ORM\Column(type="integer", options={"default":"12"})
*/
private int $width;
/**
* @ORM\Column(type="integer", nullable=true, options={"default":"1"})
*/
private ?int $section;
/**
* @ORM\ManyToMany(targetEntity=WidgetCategory::class)
* @ORM\JoinTable(name="widget_widget_category",
* joinColumns={@ORM\JoinColumn(name="widget_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="widget_category_id", referencedColumnName="id")}
* )
*/
private Collection $categories;
/**
* @ORM\OneToOne(
* targetEntity="App\CmsBundle\Entity\WidgetSummator",
* mappedBy="widget",
* cascade={"persist", "remove"})
*/
private ?WidgetSummator $widgetSummator = null;
private ?string $htmlContent;
private $content;
private $staticContent;
private ?string $style = null;
private ?string $script = null;
private ?array $autoReplacedVariables = null;
public function __construct()
{
$this->categories = new ArrayCollection();
$this->setPosition(WidgetPosition::BLOCK_1);
$this->parameters = json_encode([]);
$this->setWidth(12);
}
public function __toString(): string
{
return $this->getTitle() ?? '';
}
/**
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return int|null
*/
public function getPosition(): ?int
{
return $this->position;
}
/**
* @return string
*/
public function getPositionStr(): string
{
return WidgetPosition::getKey($this->position);
}
/**
* @param int|null $position
* @return $this
*/
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
/**
* @return string|null
*/
public function getHandler(): ?string
{
return $this->handler;
}
/**
* @param string|null $handler
* @return $this
*/
public function setHandler(?string $handler): self
{
$this->handler = $handler;
return $this;
}
/**
* @return bool
*/
public function getGenerateSlugs(): bool
{
return (is_null($this->generateSlugs))
? false
: (bool)$this->generateSlugs;
}
/**
* @param bool $generateSlugs
* @return $this
*/
public function setGenerateSlugs(bool $generateSlugs): self
{
$this->generateSlugs = $generateSlugs;
return $this;
}
/**
* @return bool
*/
public function getDisplayTitle(): bool
{
return is_null($this->displayTitle) || (bool)$this->displayTitle;
}
/**
* @param bool $displayTitle
* @return $this
*/
public function setDisplayTitle(bool $displayTitle): self
{
$this->displayTitle = $displayTitle;
return $this;
}
/**
* @return Route|null
*/
public function getRoute(): ?Route
{
return $this->route;
}
/**
* @param Route|null $route
* @return $this
*/
public function setRoute(?Route $route): self
{
$this->route = $route;
if ($route) $route->addWidget($this);
return $this;
}
/**
* @return Template|null
*/
public function getTemplate(): ?Template
{
return $this->template;
}
/**
* @param Template|null $template
* @return $this
*/
public function setTemplate(?Template $template): self
{
$this->template = $template;
if ($template) {
$template->addWidget($this);
$this->setRoute($template->getRoute());
}
return $this;
}
/**
* @return string|null
*/
public function getHtmlContent(): ?string
{
return $this->htmlContent;
}
/**
* @param string|null $htmlContent
* @return $this
*/
public function setHtmlContent(?string $htmlContent = null): self
{
$this->htmlContent = $htmlContent;
return $this;
}
/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @param $content
* @return $this
*/
public function setContent($content): self
{
$this->content = $content;
return $this;
}
/**
* @return mixed
*/
public function getStaticContent()
{
return $this->staticContent;
}
/**
* @param $staticContent
* @return $this
*/
public function setStaticContent($staticContent): self
{
$this->staticContent = $staticContent;
return $this;
}
/**
* @return string|null
*/
public function getStyle(): ?string
{
return $this->style;
}
/**
* @param string|null $style
* @return $this
*/
public function setStyle(?string $style): self
{
$this->style = $style;
return $this;
}
/**
* @return string|null
*/
public function getScript(): ?string
{
return $this->script;
}
/**
* @param string|null $script
* @return $this
*/
public function setScript(?string $script): self
{
$this->script = $script;
return $this;
}
/**
* @param int $width
* @return $this
*/
public function setWidth(int $width): self
{
$this->width = $width;
return $this;
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @param int|null $section
* @return $this
*/
public function setSection(?int $section): self
{
$this->section = $section;
return $this;
}
/**
* @return int|null
*/
public function getSection(): ?int
{
return $this->section;
}
/**
* @return ?Preset
*/
public function getPreset(): ?Preset
{
return $this->preset;
}
/**
* @param Preset|null $preset
* @return $this
*/
public function setPreset(?Preset $preset): self
{
$this->preset = $preset;
return $this;
}
/**
* @param string $name
* @param Growthbook|null $growthbook
* @param array|null $commonVariables
* @return false|mixed|null
*/
public function getParameter(string $name, ?Growthbook $growthbook = null, ?array $commonVariables = null)
{
if ($growthbook) {
$value = $growthbook->getValue( 'w'.$this->getId().'.'.$name, null);
if (!is_null($value)) return $value;
}
if(!array_key_exists($name, $this->getParameters())){
return null;
}
$parameterValue = $this->getParameters()[$name] ?? null;
if (is_string($parameterValue) && isset($commonVariables)) {
if (WidgetHelper::isVariable($parameterValue)) {
$normalized = WidgetHelper::normalizeVariable($parameterValue);
if (array_key_exists($normalized, $commonVariables)) {
return $commonVariables[$normalized];
}
}
}
return $this->getParameters()[$name] === 'false'
? false
: $this->getParameters()[$name];
}
public function getWidgetSummator(): ?WidgetSummator
{
return $this->widgetSummator;
}
public function setWidgetSummator(?WidgetSummator $widgetSummator): self
{
$this->widgetSummator = $widgetSummator;
return $this;
}
/**
* @return array|null
*/
public function getAutoreplacedVariables(): ?array
{
return $this->autoReplacedVariables;
}
/**
* @param array|null $autoreplacedVariables
* @return $this
*/
public function setAutoreplacedVariables(?array $autoreplacedVariables): self
{
$this->autoReplacedVariables = $autoreplacedVariables;
return $this;
}
/**
* @return Collection
*/
public function getCategories(): Collection
{
return $this->categories;
}
/**
* @param WidgetCategory $category
* @return $this
*/
public function addCategory(WidgetCategory $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
}
return $this;
}
/**
* @param WidgetCategory $category
* @return $this
*/
public function removeCategory(WidgetCategory $category): self
{
$this->categories->removeElement($category);
return $this;
}
}