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.     /**
  94.      * @ORM\Column(name="align_with_upper_widget", type="boolean", nullable=true)
  95.      */
  96.     private ?bool $alignWithUpperWidget null;
  97.     public function __construct()
  98.     {
  99.         $this->categories = new ArrayCollection();
  100.         $this->setPosition(WidgetPosition::BLOCK_1);
  101.         $this->parameters json_encode([]);
  102.         $this->setWidth(12);
  103.     }
  104.     public function __toString(): string
  105.     {
  106.         return $this->getTitle() ?? '';
  107.     }
  108.     /**
  109.      * @return int
  110.      */
  111.     public function getId(): ?int
  112.     {
  113.         return $this->id;
  114.     }
  115.     /**
  116.      * @param int $id
  117.      */
  118.     public function setId(int $id): void
  119.     {
  120.         $this->id $id;
  121.     }
  122.     /**
  123.      * @return int|null
  124.      */
  125.     public function getPosition(): ?int
  126.     {
  127.         return $this->position;
  128.     }
  129.     /**
  130.      * @return string
  131.      */
  132.     public function getPositionStr(): string
  133.     {
  134.         return WidgetPosition::getKey($this->position);
  135.     }
  136.     /**
  137.      * @param int|null $position
  138.      * @return $this
  139.      */
  140.     public function setPosition(?int $position): self
  141.     {
  142.         $this->position $position;
  143.         return $this;
  144.     }
  145.     /**
  146.      * @return string|null
  147.      */
  148.     public function getHandler(): ?string
  149.     {
  150.         return $this->handler;
  151.     }
  152.     /**
  153.      * @param string|null $handler
  154.      * @return $this
  155.      */
  156.     public function setHandler(?string $handler): self
  157.     {
  158.         $this->handler $handler;
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return bool
  163.      */
  164.     public function getGenerateSlugs(): bool
  165.     {
  166.         return (is_null($this->generateSlugs))
  167.             ? false
  168.             : (bool)$this->generateSlugs;
  169.     }
  170.     /**
  171.      * @param bool $generateSlugs
  172.      * @return $this
  173.      */
  174.     public function setGenerateSlugs(bool $generateSlugs): self
  175.     {
  176.         $this->generateSlugs $generateSlugs;
  177.         return $this;
  178.     }
  179.     /**
  180.      * @return bool
  181.      */
  182.     public function getDisplayTitle(): bool
  183.     {
  184.         return is_null($this->displayTitle) || (bool)$this->displayTitle;
  185.     }
  186.     /**
  187.      * @param bool $displayTitle
  188.      * @return $this
  189.      */
  190.     public function setDisplayTitle(bool $displayTitle): self
  191.     {
  192.         $this->displayTitle $displayTitle;
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return Route|null
  197.      */
  198.     public function getRoute(): ?Route
  199.     {
  200.         return $this->route;
  201.     }
  202.     /**
  203.      * @param Route|null $route
  204.      * @return $this
  205.      */
  206.     public function setRoute(?Route $route): self
  207.     {
  208.         $this->route $route;
  209.         if ($route$route->addWidget($this);
  210.         return $this;
  211.     }
  212.     /**
  213.      * @return Template|null
  214.      */
  215.     public function getTemplate(): ?Template
  216.     {
  217.         return $this->template;
  218.     }
  219.     /**
  220.      * @param Template|null $template
  221.      * @return $this
  222.      */
  223.     public function setTemplate(?Template $template): self
  224.     {
  225.         $this->template $template;
  226.         if ($template) {
  227.             $template->addWidget($this);
  228.             $this->setRoute($template->getRoute());
  229.         }
  230.         return $this;
  231.     }
  232.     /**
  233.      * @return string|null
  234.      */
  235.     public function getHtmlContent(): ?string
  236.     {
  237.         return $this->htmlContent;
  238.     }
  239.     /**
  240.      * @param string|null $htmlContent
  241.      * @return $this
  242.      */
  243.     public function setHtmlContent(?string $htmlContent null): self
  244.     {
  245.         $this->htmlContent $htmlContent;
  246.         return $this;
  247.     }
  248.     /**
  249.      * @return mixed
  250.      */
  251.     public function getContent()
  252.     {
  253.         return $this->content;
  254.     }
  255.     /**
  256.      * @param $content
  257.      * @return $this
  258.      */
  259.     public function setContent($content): self
  260.     {
  261.         $this->content $content;
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return mixed
  266.      */
  267.     public function getStaticContent()
  268.     {
  269.         return $this->staticContent;
  270.     }
  271.     /**
  272.      * @param $staticContent
  273.      * @return $this
  274.      */
  275.     public function setStaticContent($staticContent): self
  276.     {
  277.         $this->staticContent $staticContent;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @return string|null
  282.      */
  283.     public function getStyle(): ?string
  284.     {
  285.         return $this->style;
  286.     }
  287.     /**
  288.      * @param string|null $style
  289.      * @return $this
  290.      */
  291.     public function setStyle(?string $style): self
  292.     {
  293.         $this->style $style;
  294.         return $this;
  295.     }
  296.     /**
  297.      * @return string|null
  298.      */
  299.     public function getScript(): ?string
  300.     {
  301.         return $this->script;
  302.     }
  303.     /**
  304.      * @param string|null $script
  305.      * @return $this
  306.      */
  307.     public function setScript(?string $script): self
  308.     {
  309.         $this->script $script;
  310.         return $this;
  311.     }
  312.     /**
  313.      * @param int $width
  314.      * @return $this
  315.      */
  316.     public function setWidth(int $width): self
  317.     {
  318.         $this->width $width;
  319.         return $this;
  320.     }
  321.     /**
  322.      * @return int
  323.      */
  324.     public function getWidth(): int
  325.     {
  326.         return $this->width;
  327.     }
  328.     /**
  329.      * @param int|null $section
  330.      * @return $this
  331.      */
  332.     public function setSection(?int $section): self
  333.     {
  334.         $this->section $section;
  335.         return $this;
  336.     }
  337.     /**
  338.      * @return int|null
  339.      */
  340.     public function getSection(): ?int
  341.     {
  342.         return $this->section;
  343.     }
  344.     /**
  345.      * @return ?Preset
  346.      */
  347.     public function getPreset(): ?Preset
  348.     {
  349.         return $this->preset;
  350.     }
  351.     /**
  352.      * @param Preset|null $preset
  353.      * @return $this
  354.      */
  355.     public function setPreset(?Preset $preset): self
  356.     {
  357.         $this->preset $preset;
  358.         return $this;
  359.     }
  360.     /**
  361.      * @param string $name
  362.      * @param Growthbook|null $growthbook
  363.      * @param array|null $commonVariables
  364.      * @return false|mixed|null
  365.      */
  366.     public function getParameter(string $name, ?Growthbook $growthbook null, ?array $commonVariables null)
  367.     {
  368.         if ($growthbook) {
  369.             $value $growthbook->getValue'w'.$this->getId().'.'.$namenull);
  370.             if (!is_null($value)) return $value;
  371.         }
  372.         if(!array_key_exists($name$this->getParameters())){
  373.             return null;
  374.         }
  375.         $parameterValue $this->getParameters()[$name] ?? null;
  376.         if (is_string($parameterValue) && isset($commonVariables)) {
  377.             if (WidgetHelper::isVariable($parameterValue)) {
  378.                 $normalized WidgetHelper::normalizeVariable($parameterValue);
  379.                 if (array_key_exists($normalized$commonVariables)) {
  380.                     return $commonVariables[$normalized];
  381.                 }
  382.             }
  383.         }
  384.         return $this->getParameters()[$name] === 'false'
  385.             false
  386.             $this->getParameters()[$name];
  387.     }
  388.     public function getWidgetSummator(): ?WidgetSummator
  389.     {
  390.         return $this->widgetSummator;
  391.     }
  392.     public function setWidgetSummator(?WidgetSummator $widgetSummator): self
  393.     {
  394.         $this->widgetSummator $widgetSummator;
  395.         return $this;
  396.     }
  397.     /**
  398.      * @return array|null
  399.      */
  400.     public function getAutoreplacedVariables(): ?array
  401.     {
  402.         return $this->autoReplacedVariables;
  403.     }
  404.     /**
  405.      * @param array|null $autoreplacedVariables
  406.      * @return $this
  407.      */
  408.     public function setAutoreplacedVariables(?array $autoreplacedVariables): self
  409.     {
  410.         $this->autoReplacedVariables $autoreplacedVariables;
  411.         return $this;
  412.     }
  413.     /**
  414.      * Align current widget visually with the widget above
  415.      */
  416.     public function getAlignWithUpperWidget(): bool
  417.     {
  418.         return (bool)($this->alignWithUpperWidget ?? false);
  419.     }
  420.     public function setAlignWithUpperWidget(?bool $alignWithUpperWidget): self
  421.     {
  422.         $this->alignWithUpperWidget $alignWithUpperWidget;
  423.         return $this;
  424.     }
  425.     /**
  426.      * @return Collection
  427.      */
  428.     public function getCategories(): Collection
  429.     {
  430.         return $this->categories;
  431.     }
  432.     /**
  433.      * @param WidgetCategory $category
  434.      * @return $this
  435.      */
  436.     public function addCategory(WidgetCategory $category): self
  437.     {
  438.         if (!$this->categories->contains($category)) {
  439.             $this->categories[] = $category;
  440.         }
  441.         return $this;
  442.     }
  443.     /**
  444.      * @param WidgetCategory $category
  445.      * @return $this
  446.      */
  447.     public function removeCategory(WidgetCategory $category): self
  448.     {
  449.         $this->categories->removeElement($category);
  450.         return $this;
  451.     }
  452. }