src/Cms/ContainerBundle/Entity/Container.php line 81

Open in your IDE?
  1. <?php
  2. namespace Cms\ContainerBundle\Entity;
  3. use Cms\ContainerBundle\Entity\Containers\GenericContainer;
  4. use Cms\ContainerBundle\Entity\Containers\IntranetContainer;
  5. use Cms\ContainerBundle\Entity\Containers\PersonalContainer;
  6. use Cms\ContainerBundle\Entity\Containers\StorageContainer;
  7. use Cms\ContainerBundle\Entity\Containers\ThemeSettingsTrait;
  8. use Cms\ContainerBundle\Service\ContainerWriteableInterface;
  9. use Cms\CoreBundle\Entity\HtmlOverrides;
  10. use Cms\CoreBundle\Model\EntityRestoreInterface;
  11. use Cms\CoreBundle\Model\EntityRestoreTrait;
  12. use Cms\CoreBundle\Model\HtmlOverridableInterface;
  13. use Cms\CoreBundle\Model\Interfaces\Nestable\NestableInterface;
  14. use Cms\CoreBundle\Model\Interfaces\Nestable\NestableTrait;
  15. use Cms\CoreBundle\Model\Interfaces\Loggable\LoggableInterface;
  16. use Cms\DomainBundle\Entity\Domain;
  17. use Cms\ThemeBundle\Model\ThemeOverridableInterface;
  18. use Doctrine\Common\Collections\ArrayCollection;
  19. use Doctrine\ORM\Mapping as ORM;
  20. use Gedmo\Mapping\Annotation as Gedmo;
  21. use Cms\TenantBundle\Entity\TenantedEntity;
  22. use Symfony\Component\Serializer\Annotation\Groups;
  23. /**
  24.  * Represents an object that holds module data.
  25.  * Containers belong to a site, and are built in a tree hierarchy.
  26.  * They work similar to something like folders in a filesystem.
  27.  * TODO: unique index on slug, level, and site
  28.  *
  29.  * Class Container
  30.  * @package Cms\ContainerBundle\Entity
  31.  *
  32.  * @method Container getParent();
  33.  * @method ArrayCollection|Container[] getChildren();
  34.  *
  35.  * @ORM\HasLifecycleCallbacks
  36.  * @ORM\Entity(
  37.  *     repositoryClass = "Cms\ContainerBundle\Doctrine\ContainerRepository"
  38.  * )
  39.  * @ORM\InheritanceType("SINGLE_TABLE")
  40.  * @ORM\DiscriminatorColumn(
  41.  *     name = "_discr",
  42.  *     type = "string"
  43.  * )
  44.  * @ORM\DiscriminatorMap({
  45.  *     GenericContainer::DISCR = "Cms\ContainerBundle\Entity\Containers\GenericContainer",
  46.  *     PersonalContainer::DISCR = "Cms\ContainerBundle\Entity\Containers\PersonalContainer",
  47.  *     IntranetContainer::DISCR = "Cms\ContainerBundle\Entity\Containers\IntranetContainer",
  48.  *     StorageContainer::DISCR = "Cms\ContainerBundle\Entity\Containers\StorageContainer"
  49.  * })
  50.  * @ORM\Table(
  51.  *      name = "cms__container__container",
  52.  *      uniqueConstraints = {
  53.  *          @ORM\UniqueConstraint(
  54.  *              name = "uidx__unique__container",
  55.  *              columns = {
  56.  *                  "tenant",
  57.  *                  "account",
  58.  *                  "parent",
  59.  *                  "name"
  60.  *              }
  61.  *          ),
  62.  *          @ORM\UniqueConstraint(
  63.  *              name = "uidx__unique__slug",
  64.  *              columns = {
  65.  *                  "tenant",
  66.  *                  "account",
  67.  *                  "parent",
  68.  *                  "slug"
  69.  *              }
  70.  *          ),
  71.  *      }
  72.  * )
  73.  * @Gedmo\Tree(
  74.  *     type = "nested"
  75.  * )
  76.  */
  77. abstract class Container extends TenantedEntity implements
  78.     NestableInterface,
  79.     ThemeOverridableInterface,
  80.     HtmlOverridableInterface,
  81.     ContainerWriteableInterface,
  82.     EntityRestoreInterface,
  83.     LoggableInterface
  84. {
  85.     use NestableTrait;
  86.     use ThemeSettingsTrait;
  87.     use EntityRestoreTrait;
  88.     use ContainerRestoreTrait;
  89.     const DISCR null;
  90.     const ROUTING_SLUG null;
  91.     /**
  92.      * The container that acts as the parent for this one.
  93.      * If null, this means that the container is at the root.
  94.      *
  95.      * @var Container
  96.      *
  97.      * @Gedmo\TreeParent
  98.      * @ORM\ManyToOne(
  99.      *     targetEntity = "Cms\ContainerBundle\Entity\Container",
  100.      *     inversedBy = "children"
  101.      * )
  102.      * @ORM\JoinColumn(
  103.      *     name = "parent",
  104.      *     referencedColumnName = "id",
  105.      *     onDelete = "CASCADE"
  106.      * )
  107.      */
  108.     protected $parent null;
  109.     /**
  110.      * The immediate children of the container (i.e. those whose parent is this one).
  111.      *
  112.      * @var ArrayCollection|Container[]
  113.      *
  114.      * @ORM\OneToMany(
  115.      *     targetEntity = "Cms\ContainerBundle\Entity\Container",
  116.      *     mappedBy = "parent"
  117.      * )
  118.      * @ORM\OrderBy({
  119.      *     "name" = "ASC"
  120.      * })
  121.      */
  122.     protected $children;
  123.     /**
  124.      * Display name for the container.
  125.      *
  126.      * @var string
  127.      *
  128.      * @ORM\Column(
  129.      *     type = "string",
  130.      *     nullable = false
  131.      * )
  132.      *
  133.      * @Groups({"department", "department_minimal"})
  134.      */
  135.     protected $name;
  136.     /**
  137.      * @var string
  138.      *
  139.      * @ORM\Column(
  140.      *     type = "string",
  141.      *     nullable = false,
  142.      *     unique = false
  143.      * )
  144.      */
  145.     protected $slug;
  146.     /**
  147.      * @var Domain
  148.      *
  149.      * @ORM\OneToOne(
  150.      *     targetEntity = "Cms\DomainBundle\Entity\Domain"
  151.      * )
  152.      * @ORM\JoinColumn(
  153.      *     name = "domain",
  154.      *     referencedColumnName = "id",
  155.      *     onDelete = "SET NULL"
  156.      * )
  157.      */
  158.     protected $domain;
  159.     /**
  160.      * @var ModuleFlags
  161.      * @ORM\Embedded(
  162.      *     class = "ModuleFlags",
  163.      *     columnPrefix = "moduleFlags_"
  164.      * )
  165.      */
  166.     protected $moduleFlags;
  167.     /**
  168.      * @var HtmlOverrides
  169.      *
  170.      * @ORM\Embedded(
  171.      *     class = "Cms\CoreBundle\Entity\HtmlOverrides",
  172.      *     columnPrefix = "htmlOverrides_"
  173.      * )
  174.      */
  175.     protected $htmlOverrides;
  176.     /**
  177.      * @var bool
  178.      * @ORM\Column(type = "boolean", nullable = true, options = {"default" = true})
  179.      */
  180.     protected $hidden true;
  181.     /**
  182.      * @var int
  183.      *
  184.      * @ORM\Column(
  185.      *     type = "integer",
  186.      *     nullable = true
  187.      * )
  188.      */
  189.     protected $color;
  190.     /**
  191.      * @var string|null
  192.      *
  193.      * @ORM\Column(
  194.      *     type = "string",
  195.      *     nullable = true
  196.      * )
  197.      */
  198.     protected $code null;
  199.     /**
  200.      * @var bool
  201.      *
  202.      * @ORM\Column(
  203.      *     type = "boolean",
  204.      *     nullable = false,
  205.      *     options = {
  206.      *         "default" = false,
  207.      *     },
  208.      * )
  209.      */
  210.     protected bool $schoolNowMigrated false;
  211.     /**
  212.      * @return bool
  213.      */
  214.     public function isSchoolNow(): bool
  215.     {
  216.         return $this->getTenant()->isSchoolNow() || $this->isSchoolNowMigrated();
  217.     }
  218.     /**
  219.      * @return bool
  220.      */
  221.     public function isSchoolNowMigrated(): bool
  222.     {
  223.         return $this->schoolNowMigrated;
  224.     }
  225.     /**
  226.      * @param bool $schoolNowMigrated
  227.      * @return $this
  228.      */
  229.     public function setSchoolNowMigrated(bool $schoolNowMigrated): self
  230.     {
  231.         $this->schoolNowMigrated $schoolNowMigrated;
  232.         return $this;
  233.     }
  234.     /**
  235.      * @return bool
  236.      */
  237.     public function isRoot()
  238.     {
  239.         return empty($this->getParent());
  240.     }
  241.     /**
  242.      * @return int
  243.      */
  244.     public function getColor()
  245.     {
  246.         return $this->color;
  247.     }
  248.     /**
  249.      * @param int $value
  250.      * @return $this
  251.      */
  252.     public function setColor($value)
  253.     {
  254.         $this->color $value;
  255.         return $this;
  256.     }
  257.     /**
  258.      * @return boolean
  259.      */
  260.     public function isHidden()
  261.     {
  262.         return $this->hidden;
  263.     }
  264.     /**
  265.      * @param boolean $hidden
  266.      * @return $this
  267.      */
  268.     public function setHidden($hidden)
  269.     {
  270.         $this->hidden $hidden;
  271.         return $this;
  272.     }
  273.     /**
  274.      *
  275.      */
  276.     public function __construct()
  277.     {
  278.         $this->children = new ArrayCollection();
  279.         $this->moduleFlags = new ModuleFlags();
  280.         $this->htmlOverrides = new HtmlOverrides();
  281.     }
  282.     /**
  283.      * @return string
  284.      */
  285.     public function getType()
  286.     {
  287.         return static::DISCR;
  288.     }
  289.     /**
  290.      * {@inheritdoc}
  291.      */
  292.     public function getInnerLayoutOverride()
  293.     {
  294.         return $this->getDefaultInnerLayout();
  295.     }
  296.     /**
  297.      * {@inheritdoc}
  298.      */
  299.     public function getOuterLayoutOverride()
  300.     {
  301.         return $this->getDefaultOuterLayout();
  302.     }
  303.     /**
  304.      * {@inheritdoc}
  305.      */
  306.     public function getThemeOverride()
  307.     {
  308.         return $this->getDefaultTheme();
  309.     }
  310.     /**
  311.      * TODO: this is a hack for entity fields needing tree of these things...
  312.      *
  313.      * @return string
  314.      */
  315.     public function __toString()
  316.     {
  317.         $padding '';
  318.         for ($i 0$i $this->getLevel(); $i++) {
  319.             $padding .= ' > ';
  320.         }
  321.         return $padding $this->getName();
  322.     }
  323.     /**
  324.      * @return string
  325.      */
  326.     public function getName()
  327.     {
  328.         return $this->name;
  329.     }
  330.     /**
  331.      * @return string
  332.      */
  333.     public function getSlug()
  334.     {
  335.         return $this->slug;
  336.     }
  337.     /**
  338.      * @return Domain
  339.      */
  340.     public function getDomain()
  341.     {
  342.         return $this->domain;
  343.     }
  344.     /**
  345.      * @param Domain $value
  346.      * @return $this
  347.      */
  348.     public function setDomain(Domain $value null)
  349.     {
  350.         $this->domain $value;
  351.         return $this;
  352.     }
  353.     /**
  354.      * @param string $value
  355.      * @return $this
  356.      */
  357.     public function setName($value)
  358.     {
  359.         $this->name $value;
  360.         return $this;
  361.     }
  362.     /**
  363.      * @param string $value
  364.      * @return $this
  365.      */
  366.     public function setSlug($value)
  367.     {
  368.         $this->slug $value;
  369.         return $this;
  370.     }
  371.     /**
  372.      * @return ModuleFlags
  373.      */
  374.     public function getModuleFlags()
  375.     {
  376.         return $this->moduleFlags->associateDepartment($this);
  377.     }
  378.     /**
  379.      * @return HtmlOverrides
  380.      */
  381.     public function getHtmlOverrides()
  382.     {
  383.         return $this->htmlOverrides;
  384.     }
  385.     /**
  386.      * {@inheritdoc}
  387.      */
  388.     public function getBodyClass()
  389.     {
  390.         return $this->getHtmlOverrides()->getBodyClass();
  391.     }
  392.     /**
  393.      * {@inheritdoc}
  394.      */
  395.     public function getHeadScripts()
  396.     {
  397.         return $this->getHtmlOverrides()->getHeadScripts();
  398.     }
  399.     /**
  400.      * {@inheritdoc}
  401.      */
  402.     public function getTopScripts()
  403.     {
  404.         return $this->getHtmlOverrides()->getTopScripts();
  405.     }
  406.     /**
  407.      * {@inheritdoc}
  408.      */
  409.     public function getBottomScripts()
  410.     {
  411.         return $this->getHtmlOverrides()->getBottomScripts();
  412.     }
  413.     /**
  414.      * Gets all of the ancestors (parents, grandparents, etc) of this container.
  415.      * Returns them with the "youngest" ancestor first.
  416.      * This should only be used when ancestors are already pre-loaded in the ORM.
  417.      * If not, it generates a lot of queries as each ancestor is found individually through ORM lazy loading.
  418.      *
  419.      * @return array|Container[]
  420.      */
  421.     public function getAncestors()
  422.     {
  423.         $ancestors = [];
  424.         $current $this->getParent();
  425.         while ( ! empty($current)) {
  426.             $ancestors[] = $current;
  427.             $current $current->getParent();
  428.         }
  429.         return array_reverse($ancestors);
  430.     }
  431.     /**
  432.      * {@inheritdoc}
  433.      */
  434.     public function getLoggableDetails()
  435.     {
  436.         return array(
  437.             'id' => $this->getId(),
  438.             'name' => $this->getName(),
  439.         );
  440.     }
  441.     /**
  442.      * @return string|null
  443.      */
  444.     public function getCode(): ?string
  445.     {
  446.         return $this->code;
  447.     }
  448.     /**
  449.      * @param string|null $code
  450.      * @return $this
  451.      */
  452.     public function setCode(?string $code null): self
  453.     {
  454.         if (empty($code)) {
  455.             $code null;
  456.         }
  457.         $this->code $code;
  458.         return $this;
  459.     }
  460. }