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