src/Entity/Content/Item.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Content;
  3. use Symfony\Component\Validator\Constraints AS Constraints;
  4. use Symfony\Component\Validator\Constraints AS Assert;
  5. use Doctrine\ORM\Mapping AS ORM;
  6. use Ramsey\Uuid\Uuid;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Cocur\Slugify\Slugify;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\Content\ItemRepository")
  11.  * @ORM\Table(name="content_item")
  12.  * @ORM\HasLifecycleCallbacks()
  13.  */
  14. class Item
  15. {
  16.     /**
  17.     * @ORM\Id
  18.     * @ORM\GeneratedValue(strategy="AUTO")
  19.     * @ORM\Column(type="integer")
  20.     */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="uuid")
  24.      * @Assert\Uuid
  25.      */
  26.      protected $uuid;
  27.     /**
  28.      * @var \DateTime
  29.      *
  30.      * @ORM\Column(type="datetime", nullable=false)
  31.      */
  32.     private $created;
  33.     /**
  34.      * @ORM\Column(type="integer", length=5, nullable=false)
  35.      */
  36.     private $version;
  37.     /**
  38.      * @ORM\Column(type="string", length=200, nullable=false)
  39.      */
  40.     private $name;
  41.     /**
  42.      * @ORM\Column(type="array", nullable=false)
  43.      */
  44.     private $content;
  45.     /**
  46.      * @ORM\Column(type="string", length=200, nullable=false)
  47.      */
  48.     private $slug;
  49.     /**
  50.      * @ORM\Column(type="string", length=200, nullable=false)
  51.      */
  52.     private $path;
  53.     /**
  54.      * @ORM\Column(type="boolean", nullable=false)
  55.      */
  56.     private $enableGroupRestrictions;
  57.     /**
  58.      * @ORM\Column(type="boolean", nullable=false)
  59.      */
  60.     private $enableRoleRestrictions;
  61.     /**
  62.      * @ORM\Column(type="string", length=60, nullable=false)
  63.      */
  64.     private $status;
  65.     /**
  66.      * @ORM\Column(type="string", length=200, nullable=true)
  67.      */
  68.     private $primaryImage;
  69.     /**
  70.      * @ORM\Column(type="string", length=255, nullable=true)
  71.      */
  72.     private $seoPageTitle;
  73.     /**
  74.      * @ORM\Column(type="string", length=500, nullable=true)
  75.      */
  76.     private $seoDescription;
  77.     /**
  78.      * @ORM\Column(type="string", length=255, nullable=true)
  79.      */
  80.     private $seoKeywords;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      */
  84.     private $ogTitle;
  85.     /**
  86.      * @ORM\Column(type="text", nullable=true)
  87.      */
  88.     private $ogDescription;
  89.     /**
  90.      * @ORM\Column(type="string", length=200, nullable=true)
  91.      */
  92.     private $ogImage;
  93.     /**
  94.      * @ORM\Column(type="string", length=60, nullable=true)
  95.      */
  96.     private $ogType;
  97.     /**
  98.      * @ORM\Column(type="string", length=255, nullable=true)
  99.      */
  100.     private $ogUrl;
  101.     /**
  102.      * @ORM\Column(type="string", length=60, nullable=true)
  103.      */
  104.     private $twitterCard;
  105.     /**
  106.      * @ORM\Column(type="string", length=100, nullable=true)
  107.      */
  108.     private $twitterSite;
  109.     /**
  110.      * @ORM\ManyToOne(targetEntity="Type", inversedBy="items")
  111.      * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
  112.      **/
  113.     private $type;
  114.     /**
  115.      * @ORM\OneToMany(targetEntity="Item", mappedBy="parent", cascade={"all"})
  116.      **/
  117.     private $children;
  118.     /**
  119.      * @ORM\ManyToOne(targetEntity="Item", inversedBy="children")
  120.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  121.      **/
  122.     private $parent;
  123.     /**
  124.      * @ORM\OneToMany(targetEntity="Block", mappedBy="item", cascade={"all"})
  125.      **/
  126.     private $blocks;
  127.     /**
  128.      * @ORM\ManyToMany(targetEntity="App\Entity\Users\Group")
  129.      * @ORM\JoinTable(name="content_item_grouprestriction")
  130.      * @ORM\JoinColumn(name="item_id", referencedColumnName="id", unique=false)
  131.      **/
  132.     private $groupRestrictions;
  133.     /**
  134.      * @ORM\ManyToMany(targetEntity="App\Entity\Users\Role")
  135.      * @ORM\JoinTable(name="content_item_rolerestriction")
  136.      * @ORM\JoinColumn(name="item_id", referencedColumnName="id", unique=false)
  137.      **/
  138.     private $roleRestrictions;
  139.     /**
  140.      * @ORM\OneToMany(targetEntity="Revision", mappedBy="item", cascade={"all"})
  141.      **/
  142.     private $revisions;
  143.     /**
  144.      * @ORM\ManyToOne(targetEntity="\App\Entity\Settings\Language")
  145.      **/
  146.     private $language;
  147.     public function __construct($data = array())
  148.     {
  149.         // UUID
  150.         if(!$this->getUuid())
  151.         {
  152.             $uuid Uuid::uuid1();
  153.             $this->setUuid($uuid->toString());
  154.         }
  155.         // Set some defaults
  156.         $this->setCreated(new \DateTime());
  157.         $this->children = new ArrayCollection();
  158.         $this->blocks = new ArrayCollection();
  159.         $this->revisions = new ArrayCollection();
  160.         $this->setVersion(1);
  161.         $this->content = array();
  162.         $this->slug "";
  163.         $this->path "";
  164.         $this->enableGroupRestrictions false;
  165.         $this->enableRoleRestrictions false;
  166.         $this->groupRestrictions = new ArrayCollection();
  167.         $this->roleRestrictions = new ArrayCollection();
  168.         $this->status "published";
  169.         // For display purposes
  170.         $this->displayDepth 0;
  171.         // Setup with defaults
  172.         foreach($data AS $k => $v)
  173.         {
  174.             $setterName "set" ucfirst($k);
  175.             if(method_exists($this$setterName))
  176.                 $this->$setterName($v);
  177.         }
  178.     }
  179.     public function getSeoCompletedCount()
  180.     {
  181.         $count 0;
  182.         if($this->getSeoPageTitle())
  183.             $count ++;
  184.         if($this->getSeoDescription())
  185.             $count ++;
  186.         if($this->getSeoKeywords())
  187.             $count ++;
  188.         return $count;
  189.     }
  190.     public function __toString()
  191.     {
  192.         return $this->getName();
  193.     }
  194.     public function getIndentedName()
  195.     {
  196.         return trim(str_pad(""$this->getDisplayDepth() * 2'-'STR_PAD_LEFT) . " " $this->getName());
  197.     }
  198.     public function setDisplayDepth($displayDepth)
  199.     {
  200.         $this->displayDepth $displayDepth;
  201.     }
  202.     public function getDisplayDepth()
  203.     {
  204.         return $this->displayDepth;
  205.     }
  206.     public function getFriendlyPath()
  207.     {
  208.         return '/' $this->getPath();
  209.     }
  210.     public function getId()
  211.     {
  212.         return $this->id;
  213.     }
  214.     public function setUuid($uuid)
  215.     {
  216.         $this->uuid $uuid;
  217.         return $this;
  218.     }
  219.     public function getUuid()
  220.     {
  221.         return $this->uuid;
  222.     }
  223.     public function setCreated(\DateTime $created)
  224.     {
  225.         $this->created $created;
  226.     }
  227.     public function getCreated()
  228.     {
  229.         return $this->created;
  230.     }
  231.     public function setVersion(int $version)
  232.     {
  233.         $this->version $version;
  234.     }
  235.     public function getVersion()
  236.     {
  237.         return $this->version;
  238.     }
  239.     public function getName()
  240.     {
  241.         return $this->name;
  242.     }
  243.     public function setName(string $name)
  244.     {
  245.         $this->name $name;
  246.     }
  247.     public function getPrimaryImage()
  248.     {
  249.         return $this->primaryImage;
  250.     }
  251.     public function setPrimaryImage(string $primaryImage null)
  252.     {
  253.         $this->primaryImage $primaryImage;
  254.     }
  255.     public function getPrimaryImageWebPath()
  256.     {
  257.         if(!strlen($this->getPrimaryImage()))
  258.             return null;
  259.         return '/files/' $this->getPrimaryImage();
  260.     }
  261.     public function getStatus()
  262.     {
  263.         return $this->status;
  264.     }
  265.     public function setStatus(string $status)
  266.     {
  267.         // Make sure it's valid
  268.         if(!in_array($status, array("draft""published")))
  269.             throw new \Exception("Invalid content item status: " $status);
  270.         $this->status $status;
  271.     }
  272.     public function getEnableGroupRestrictions()
  273.     {
  274.         return $this->enableGroupRestrictions;
  275.     }
  276.     public function setEnableGroupRestrictions(bool $enableGroupRestrictions)
  277.     {
  278.         $this->enableGroupRestrictions $enableGroupRestrictions;
  279.     }
  280.     public function getEnableRoleRestrictions()
  281.     {
  282.         return $this->enableRoleRestrictions;
  283.     }
  284.     public function setEnableRoleRestrictions(bool $enableRoleRestrictions)
  285.     {
  286.         $this->enableRoleRestrictions $enableRoleRestrictions;
  287.     }
  288.     public function getSlug()
  289.     {
  290.         return $this->slug;
  291.     }
  292.     public function setSlug(string $slug)
  293.     {
  294.         $this->slug $slug;
  295.     }
  296.     public function getPath()
  297.     {
  298.         return $this->path;
  299.     }
  300.     public function setPath(string $path)
  301.     {
  302.         $this->path $path;
  303.     }
  304.     public function setContent(array $content = array())
  305.     {
  306.         $this->content $content;
  307.     }
  308.     public function getContent()
  309.     {
  310.         return $this->content;
  311.     }
  312.     public function addChild(\App\Entity\Content\Item $item)
  313.     {
  314.         if(in_array($item$this->children->toArray()))
  315.             return true;
  316.         $this->children[] = $item;
  317.         $item->setParent($this);
  318.         return $this;
  319.     }
  320.     public function removeChild(\App\Entity\Content\Item $item)
  321.     {
  322.         $this->children->removeElement($item);
  323.         $item->setParent(null);
  324.     }
  325.     public function getChildren()
  326.     {
  327.         return $this->children;
  328.     }
  329.     public function setParent(\App\Entity\Content\Item $parent null)
  330.     {
  331.         $this->parent $parent;
  332.         if($parent)
  333.             $parent->addChild($this);
  334.     }
  335.     public function getParent()
  336.     {
  337.         return $this->parent;
  338.     }
  339.     public function addBlock(\App\Entity\Content\Block $block)
  340.     {
  341.         if(in_array($block$this->blocks->toArray()))
  342.             return true;
  343.         $this->blocks[] = $block;
  344.         $block->setItem($this);
  345.         return $this;
  346.     }
  347.     public function removeBlock(\App\Entity\Content\Block $block)
  348.     {
  349.         $this->blocks->removeElement($block);
  350.         $block->setItem(null);
  351.     }
  352.     public function getBlocks()
  353.     {
  354.         return $this->blocks;
  355.     }
  356.     public function setType(\App\Entity\Content\Type $type null)
  357.     {
  358.         $this->type $type;
  359.         if($type)
  360.             $type->addItem($this);
  361.     }
  362.     public function getType()
  363.     {
  364.         return $this->type;
  365.     }
  366.     public function getGroupRestrictions()
  367.     {
  368.         return $this->groupRestrictions;
  369.     }
  370.     public function addGroupRestriction(\App\Entity\Users\Group $group)
  371.     {
  372.         if($this->groupRestrictions->contains($group))
  373.             return true;
  374.         // Add it
  375.         $this->groupRestrictions->add($group);
  376.     }
  377.     public function removeGroupRestriction(\App\Entity\Users\Group $group)
  378.     {
  379.         $this->groupRestrictions->removeElement($group);
  380.     }
  381.     public function getRoleRestrictions()
  382.     {
  383.         return $this->roleRestrictions;
  384.     }
  385.     public function addRoleRestriction(\App\Entity\Users\Role $role)
  386.     {
  387.         if($this->roleRestrictions->contains($role))
  388.             return true;
  389.         // Add it
  390.         $this->roleRestrictions->add($role);
  391.     }
  392.     public function removeRoleRestriction(\App\Entity\Users\Role $role)
  393.     {
  394.         $this->roleRestrictions->removeElement($role);
  395.     }
  396.     public function addRevision(\App\Entity\Content\Revision $revision)
  397.     {
  398.         if(in_array($revision$this->revisions->toArray()))
  399.             return true;
  400.         $this->revisions[] = $revision;
  401.         $revision->setItem($this);
  402.         return $this;
  403.     }
  404.     public function removeRevision(\App\Entity\Content\Revision $revision)
  405.     {
  406.         $this->revisions->removeElement($revision);
  407.         $revision->setItem(null);
  408.     }
  409.     public function getRevisions()
  410.     {
  411.         return $this->revisions;
  412.     }
  413.     public function setLanguage(\App\Entity\Settings\Language $language null)
  414.     {
  415.         $this->language $language;
  416.     }
  417.     public function getLanguage()
  418.     {
  419.         return $this->language;
  420.     }
  421.     public function setSeoPageTitle(string $seoPageTitle null)
  422.     {
  423.         $this->seoPageTitle $seoPageTitle;
  424.     }
  425.     public function getSeoPageTitle()
  426.     {
  427.         return $this->seoPageTitle;
  428.     }
  429.     public function setSeoDescription(string $seoDescription null)
  430.     {
  431.         $this->seoDescription $seoDescription;
  432.     }
  433.     public function getSeoDescription()
  434.     {
  435.         return $this->seoDescription;
  436.     }
  437.     public function setSeoKeywords(string $seoKeywords null)
  438.     {
  439.         $this->seoKeywords $seoKeywords;
  440.     }
  441.     public function getSeoKeywords()
  442.     {
  443.         return $this->seoKeywords;
  444.     }
  445.     public function setOgTitle(string $ogTitle null)
  446.     {
  447.         $this->ogTitle $ogTitle;
  448.     }
  449.     public function getOgTitle()
  450.     {
  451.         return $this->ogTitle;
  452.     }
  453.     public function setOgDescription(string $ogDescription null)
  454.     {
  455.         $this->ogDescription $ogDescription;
  456.     }
  457.     public function getOgDescription()
  458.     {
  459.         return $this->ogDescription;
  460.     }
  461.     public function setOgImage(string $ogImage null)
  462.     {
  463.         $this->ogImage $ogImage;
  464.     }
  465.     public function getOgImage()
  466.     {
  467.         return $this->ogImage;
  468.     }
  469.     public function setOgType(string $ogType null)
  470.     {
  471.         $this->ogType $ogType;
  472.     }
  473.     public function getOgType()
  474.     {
  475.         return $this->ogType;
  476.     }
  477.     public function setOgUrl(string $ogUrl null)
  478.     {
  479.         $this->ogUrl $ogUrl;
  480.     }
  481.     public function getOgUrl()
  482.     {
  483.         return $this->ogUrl;
  484.     }
  485.     public function setTwitterCard(string $twitterCard null)
  486.     {
  487.         $this->twitterCard $twitterCard;
  488.     }
  489.     public function getTwitterCard()
  490.     {
  491.         return $this->twitterCard;
  492.     }
  493.     public function setTwitterSite(string $twitterSite null)
  494.     {
  495.         $this->twitterSite $twitterSite;
  496.     }
  497.     public function getTwitterSite()
  498.     {
  499.         return $this->twitterSite;
  500.     }
  501. }