src/CmsBundle/Entity/Widget.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\Entity;
  3. use App\CmsBundle\Enum\WidgetPosition;
  4. use App\CmsBundle\Helper\WidgetHelper;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Growthbook\Growthbook;
  8. use Doctrine\Common\Collections\Collection;
  9. /**
  10.  * Widget
  11.  *
  12.  * @ORM\Table(
  13.  *     name="widget",
  14.  *     indexes={
  15.  *         @ORM\Index(name="widget_position_index", columns={"position"}),
  16.  *         @ORM\Index(name="widget_published_index", columns={"published"}),
  17.  *         @ORM\Index(name="widget_template_index", columns={"template_id"}),
  18.  *         @ORM\Index(name="widget_route_index", columns={"route_id"})
  19.  *     }
  20.  * )
  21.  * @ORM\Entity(repositoryClass="App\CmsBundle\Repository\WidgetRepository")
  22.  * @ORM\HasLifecycleCallbacks()
  23.  */
  24. class Widget
  25. {
  26.     use IdTraitTitleTraitAlternativeTitleTraitParametersTraitPublishedTraitTimeStampedTrait;
  27.     /**
  28.      * @ORM\Column(name="summary_title", type="text", length=255, nullable=true)
  29.      */
  30.     private $summaryTitle;
  31.     /**
  32.      * @var integer
  33.      *
  34.      * @ORM\Column(name="position", type="integer", nullable=false)
  35.      */
  36.     private $position;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $handler;
  41.     /**
  42.      * @ORM\Column(type="boolean", nullable=true)
  43.      */
  44.     private $displayTitle;
  45.     /**
  46.      * @ORM\Column(type="boolean", nullable=true)
  47.      */
  48.     private $generateSlugs;
  49.     /**
  50.      * @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Template", inversedBy="widgets", cascade={"persist"})
  51.      * @ORM\JoinColumn(nullable=true)
  52.      */
  53.     private $template;
  54.     /**
  55.      * @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Route", inversedBy="widgets", cascade={"persist"})
  56.      * @ORM\JoinColumn(nullable=true)
  57.      */
  58.     private $route;
  59.     /**
  60.      * @ORM\ManyToOne(targetEntity="App\CmsBundle\Entity\Preset", inversedBy="widgets")
  61.      * @ORM\JoinColumn(name="preset_id", referencedColumnName="id")
  62.      */
  63.     private ?Preset $preset null;
  64.     /**
  65.      * @ORM\Column(type="integer", options={"default":"12"})
  66.      */
  67.     private int $width;
  68.     /**
  69.      * @ORM\Column(type="integer", nullable=true,  options={"default":"1"})
  70.      */
  71.     private ?int $section;
  72.     /**
  73.      * @ORM\ManyToMany(targetEntity=WidgetCategory::class)
  74.      * @ORM\JoinTable(name="widget_widget_category",
  75.      *      joinColumns={@ORM\JoinColumn(name="widget_id", referencedColumnName="id")},
  76.      *      inverseJoinColumns={@ORM\JoinColumn(name="widget_category_id", referencedColumnName="id")}
  77.      * )
  78.      */
  79.     private Collection $categories;
  80.     /**
  81.      * @ORM\OneToOne(
  82.      *     targetEntity="App\CmsBundle\Entity\WidgetSummator",
  83.      *     mappedBy="widget",
  84.      *     cascade={"persist", "remove"})
  85.      */
  86.     private ?WidgetSummator $widgetSummator null;
  87.     private ?string $htmlContent;
  88.     private $content;
  89.     private $staticContent;
  90.     private ?string $style null;
  91.     private ?string $script null;
  92.     private ?array $autoReplacedVariables null;
  93.     public function __construct()
  94.     {
  95.         $this->categories = new ArrayCollection();
  96.         $this->setPosition(WidgetPosition::BLOCK_1);
  97.         $this->parameters json_encode([]);
  98.         $this->setWidth(12);
  99.     }
  100.     public function __toString(): string
  101.     {
  102.         return $this->getTitle() ?? '';
  103.     }
  104.     /**
  105.      * @return int
  106.      */
  107.     public function getId(): ?int
  108.     {
  109.         return $this->id;
  110.     }
  111.     /**
  112.      * @param int $id
  113.      */
  114.     public function setId(int $id): void
  115.     {
  116.         $this->id $id;
  117.     }
  118.     /**
  119.      * @return int|null
  120.      */
  121.     public function getPosition(): ?int
  122.     {
  123.         return $this->position;
  124.     }
  125.     /**
  126.      * @return string
  127.      */
  128.     public function getPositionStr(): string
  129.     {
  130.         return WidgetPosition::getKey($this->position);
  131.     }
  132.     /**
  133.      * @param int|null $position
  134.      * @return $this
  135.      */
  136.     public function setPosition(?int $position): self
  137.     {
  138.         $this->position $position;
  139.         return $this;
  140.     }
  141.     /**
  142.      * @return string|null
  143.      */
  144.     public function getHandler(): ?string
  145.     {
  146.         return $this->handler;
  147.     }
  148.     /**
  149.      * @param string|null $handler
  150.      * @return $this
  151.      */
  152.     public function setHandler(?string $handler): self
  153.     {
  154.         $this->handler $handler;
  155.         return $this;
  156.     }
  157.     /**
  158.      * @return bool
  159.      */
  160.     public function getGenerateSlugs(): bool
  161.     {
  162.         return (is_null($this->generateSlugs))
  163.             ? false
  164.             : (bool)$this->generateSlugs;
  165.     }
  166.     /**
  167.      * @param bool $generateSlugs
  168.      * @return $this
  169.      */
  170.     public function setGenerateSlugs(bool $generateSlugs): self
  171.     {
  172.         $this->generateSlugs $generateSlugs;
  173.         return $this;
  174.     }
  175.     /**
  176.      * @return bool
  177.      */
  178.     public function getDisplayTitle(): bool
  179.     {
  180.         return is_null($this->displayTitle) || (bool)$this->displayTitle;
  181.     }
  182.     /**
  183.      * @param bool $displayTitle
  184.      * @return $this
  185.      */
  186.     public function setDisplayTitle(bool $displayTitle): self
  187.     {
  188.         $this->displayTitle $displayTitle;
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return Route|null
  193.      */
  194.     public function getRoute(): ?Route
  195.     {
  196.         return $this->route;
  197.     }
  198.     /**
  199.      * @param Route|null $route
  200.      * @return $this
  201.      */
  202.     public function setRoute(?Route $route): self
  203.     {
  204.         $this->route $route;
  205.         if ($route$route->addWidget($this);
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return Template|null
  210.      */
  211.     public function getTemplate(): ?Template
  212.     {
  213.         return $this->template;
  214.     }
  215.     /**
  216.      * @param Template|null $template
  217.      * @return $this
  218.      */
  219.     public function setTemplate(?Template $template): self
  220.     {
  221.         $this->template $template;
  222.         if ($template) {
  223.             $template->addWidget($this);
  224.             $this->setRoute($template->getRoute());
  225.         }
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return string|null
  230.      */
  231.     public function getHtmlContent(): ?string
  232.     {
  233.         return $this->htmlContent;
  234.     }
  235.     /**
  236.      * @param string|null $htmlContent
  237.      * @return $this
  238.      */
  239.     public function setHtmlContent(?string $htmlContent null): self
  240.     {
  241.         $this->htmlContent $htmlContent;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @return mixed
  246.      */
  247.     public function getContent()
  248.     {
  249.         return $this->content;
  250.     }
  251.     /**
  252.      * @param $content
  253.      * @return $this
  254.      */
  255.     public function setContent($content): self
  256.     {
  257.         $this->content $content;
  258.         return $this;
  259.     }
  260.     /**
  261.      * @return mixed
  262.      */
  263.     public function getStaticContent()
  264.     {
  265.         return $this->staticContent;
  266.     }
  267.     /**
  268.      * @param $staticContent
  269.      * @return $this
  270.      */
  271.     public function setStaticContent($staticContent): self
  272.     {
  273.         $this->staticContent $staticContent;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return string|null
  278.      */
  279.     public function getStyle(): ?string
  280.     {
  281.         return $this->style;
  282.     }
  283.     /**
  284.      * @param string|null $style
  285.      * @return $this
  286.      */
  287.     public function setStyle(?string $style): self
  288.     {
  289.         $this->style $style;
  290.         return $this;
  291.     }
  292.     /**
  293.      * @return string|null
  294.      */
  295.     public function getScript(): ?string
  296.     {
  297.         return $this->script;
  298.     }
  299.     /**
  300.      * @param string|null $script
  301.      * @return $this
  302.      */
  303.     public function setScript(?string $script): self
  304.     {
  305.         $this->script $script;
  306.         return $this;
  307.     }
  308.     /**
  309.      * @param int $width
  310.      * @return $this
  311.      */
  312.     public function setWidth(int $width): self
  313.     {
  314.         $this->width $width;
  315.         return $this;
  316.     }
  317.     /**
  318.      * @return int
  319.      */
  320.     public function getWidth(): int
  321.     {
  322.         return $this->width;
  323.     }
  324.     /**
  325.      * @param int|null $section
  326.      * @return $this
  327.      */
  328.     public function setSection(?int $section): self
  329.     {
  330.         $this->section $section;
  331.         return $this;
  332.     }
  333.     /**
  334.      * @return int|null
  335.      */
  336.     public function getSection(): ?int
  337.     {
  338.         return $this->section;
  339.     }
  340.     /**
  341.      * @return ?Preset
  342.      */
  343.     public function getPreset(): ?Preset
  344.     {
  345.         return $this->preset;
  346.     }
  347.     /**
  348.      * @param Preset|null $preset
  349.      * @return $this
  350.      */
  351.     public function setPreset(?Preset $preset): self
  352.     {
  353.         $this->preset $preset;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @param string $name
  358.      * @param Growthbook|null $growthbook
  359.      * @param array|null $commonVariables
  360.      * @return false|mixed|null
  361.      */
  362.     public function getParameter(string $name, ?Growthbook $growthbook null, ?array $commonVariables null)
  363.     {
  364.         if ($growthbook) {
  365.             $value $growthbook->getValue'w'.$this->getId().'.'.$namenull);
  366.             if (!is_null($value)) return $value;
  367.         }
  368.         if(!array_key_exists($name$this->getParameters())){
  369.             return null;
  370.         }
  371.         $parameterValue $this->getParameters()[$name] ?? null;
  372.         if (is_string($parameterValue) && isset($commonVariables)) {
  373.             if (WidgetHelper::isVariable($parameterValue)) {
  374.                 $normalized WidgetHelper::normalizeVariable($parameterValue);
  375.                 if (array_key_exists($normalized$commonVariables)) {
  376.                     return $commonVariables[$normalized];
  377.                 }
  378.             }
  379.         }
  380.         return $this->getParameters()[$name] === 'false'
  381.             false
  382.             $this->getParameters()[$name];
  383.     }
  384.     public function getWidgetSummator(): ?WidgetSummator
  385.     {
  386.         return $this->widgetSummator;
  387.     }
  388.     public function setWidgetSummator(?WidgetSummator $widgetSummator): self
  389.     {
  390.         $this->widgetSummator $widgetSummator;
  391.         return $this;
  392.     }
  393.     /**
  394.      * @return array|null
  395.      */
  396.     public function getAutoreplacedVariables(): ?array
  397.     {
  398.         return $this->autoReplacedVariables;
  399.     }
  400.     /**
  401.      * @param array|null $autoreplacedVariables
  402.      * @return $this
  403.      */
  404.     public function setAutoreplacedVariables(?array $autoreplacedVariables): self
  405.     {
  406.         $this->autoReplacedVariables $autoreplacedVariables;
  407.         return $this;
  408.     }
  409.     /**
  410.      * @return Collection
  411.      */
  412.     public function getCategories(): Collection
  413.     {
  414.         return $this->categories;
  415.     }
  416.     /**
  417.      * @param WidgetCategory $category
  418.      * @return $this
  419.      */
  420.     public function addCategory(WidgetCategory $category): self
  421.     {
  422.         if (!$this->categories->contains($category)) {
  423.             $this->categories[] = $category;
  424.         }
  425.         return $this;
  426.     }
  427.     /**
  428.      * @param WidgetCategory $category
  429.      * @return $this
  430.      */
  431.     public function removeCategory(WidgetCategory $category): self
  432.     {
  433.         $this->categories->removeElement($category);
  434.         return $this;
  435.     }
  436. }