<?php
namespace App\Entity\Content;
use Symfony\Component\Validator\Constraints AS Constraints;
use Symfony\Component\Validator\Constraints AS Assert;
use Doctrine\ORM\Mapping AS ORM;
use Ramsey\Uuid\Uuid;
use Doctrine\Common\Collections\ArrayCollection;
use Cocur\Slugify\Slugify;
/**
* @ORM\Entity(repositoryClass="App\Repository\Content\ItemRepository")
* @ORM\Table(name="content_item")
* @ORM\HasLifecycleCallbacks()
*/
class Item
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="uuid")
* @Assert\Uuid
*/
protected $uuid;
/**
* @var \DateTime
*
* @ORM\Column(type="datetime", nullable=false)
*/
private $created;
/**
* @ORM\Column(type="integer", length=5, nullable=false)
*/
private $version;
/**
* @ORM\Column(type="string", length=200, nullable=false)
*/
private $name;
/**
* @ORM\Column(type="array", nullable=false)
*/
private $content;
/**
* @ORM\Column(type="string", length=200, nullable=false)
*/
private $slug;
/**
* @ORM\Column(type="string", length=200, nullable=false)
*/
private $path;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $enableGroupRestrictions;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $enableRoleRestrictions;
/**
* @ORM\Column(type="string", length=60, nullable=false)
*/
private $status;
/**
* @ORM\Column(type="string", length=200, nullable=true)
*/
private $primaryImage;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $seoPageTitle;
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
private $seoDescription;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $seoKeywords;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ogTitle;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $ogDescription;
/**
* @ORM\Column(type="string", length=200, nullable=true)
*/
private $ogImage;
/**
* @ORM\Column(type="string", length=60, nullable=true)
*/
private $ogType;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $ogUrl;
/**
* @ORM\Column(type="string", length=60, nullable=true)
*/
private $twitterCard;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $twitterSite;
/**
* @ORM\ManyToOne(targetEntity="Type", inversedBy="items")
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
**/
private $type;
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="parent", cascade={"all"})
**/
private $children;
/**
* @ORM\ManyToOne(targetEntity="Item", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
**/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Block", mappedBy="item", cascade={"all"})
**/
private $blocks;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Users\Group")
* @ORM\JoinTable(name="content_item_grouprestriction")
* @ORM\JoinColumn(name="item_id", referencedColumnName="id", unique=false)
**/
private $groupRestrictions;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Users\Role")
* @ORM\JoinTable(name="content_item_rolerestriction")
* @ORM\JoinColumn(name="item_id", referencedColumnName="id", unique=false)
**/
private $roleRestrictions;
/**
* @ORM\OneToMany(targetEntity="Revision", mappedBy="item", cascade={"all"})
**/
private $revisions;
/**
* @ORM\ManyToOne(targetEntity="\App\Entity\Settings\Language")
**/
private $language;
public function __construct($data = array())
{
// UUID
if(!$this->getUuid())
{
$uuid = Uuid::uuid1();
$this->setUuid($uuid->toString());
}
// Set some defaults
$this->setCreated(new \DateTime());
$this->children = new ArrayCollection();
$this->blocks = new ArrayCollection();
$this->revisions = new ArrayCollection();
$this->setVersion(1);
$this->content = array();
$this->slug = "";
$this->path = "";
$this->enableGroupRestrictions = false;
$this->enableRoleRestrictions = false;
$this->groupRestrictions = new ArrayCollection();
$this->roleRestrictions = new ArrayCollection();
$this->status = "published";
// For display purposes
$this->displayDepth = 0;
// Setup with defaults
foreach($data AS $k => $v)
{
$setterName = "set" . ucfirst($k);
if(method_exists($this, $setterName))
$this->$setterName($v);
}
}
public function getSeoCompletedCount()
{
$count = 0;
if($this->getSeoPageTitle())
$count ++;
if($this->getSeoDescription())
$count ++;
if($this->getSeoKeywords())
$count ++;
return $count;
}
public function __toString()
{
return $this->getName();
}
public function getIndentedName()
{
return trim(str_pad("", $this->getDisplayDepth() * 2, '-', STR_PAD_LEFT) . " " . $this->getName());
}
public function setDisplayDepth($displayDepth)
{
$this->displayDepth = $displayDepth;
}
public function getDisplayDepth()
{
return $this->displayDepth;
}
public function getFriendlyPath()
{
return '/' . $this->getPath();
}
public function getId()
{
return $this->id;
}
public function setUuid($uuid)
{
$this->uuid = $uuid;
return $this;
}
public function getUuid()
{
return $this->uuid;
}
public function setCreated(\DateTime $created)
{
$this->created = $created;
}
public function getCreated()
{
return $this->created;
}
public function setVersion(int $version)
{
$this->version = $version;
}
public function getVersion()
{
return $this->version;
}
public function getName()
{
return $this->name;
}
public function setName(string $name)
{
$this->name = $name;
}
public function getPrimaryImage()
{
return $this->primaryImage;
}
public function setPrimaryImage(string $primaryImage = null)
{
$this->primaryImage = $primaryImage;
}
public function getPrimaryImageWebPath()
{
if(!strlen($this->getPrimaryImage()))
return null;
return '/files/' . $this->getPrimaryImage();
}
public function getStatus()
{
return $this->status;
}
public function setStatus(string $status)
{
// Make sure it's valid
if(!in_array($status, array("draft", "published")))
throw new \Exception("Invalid content item status: " . $status);
$this->status = $status;
}
public function getEnableGroupRestrictions()
{
return $this->enableGroupRestrictions;
}
public function setEnableGroupRestrictions(bool $enableGroupRestrictions)
{
$this->enableGroupRestrictions = $enableGroupRestrictions;
}
public function getEnableRoleRestrictions()
{
return $this->enableRoleRestrictions;
}
public function setEnableRoleRestrictions(bool $enableRoleRestrictions)
{
$this->enableRoleRestrictions = $enableRoleRestrictions;
}
public function getSlug()
{
return $this->slug;
}
public function setSlug(string $slug)
{
$this->slug = $slug;
}
public function getPath()
{
return $this->path;
}
public function setPath(string $path)
{
$this->path = $path;
}
public function setContent(array $content = array())
{
$this->content = $content;
}
public function getContent()
{
return $this->content;
}
public function addChild(\App\Entity\Content\Item $item)
{
if(in_array($item, $this->children->toArray()))
return true;
$this->children[] = $item;
$item->setParent($this);
return $this;
}
public function removeChild(\App\Entity\Content\Item $item)
{
$this->children->removeElement($item);
$item->setParent(null);
}
public function getChildren()
{
return $this->children;
}
public function setParent(\App\Entity\Content\Item $parent = null)
{
$this->parent = $parent;
if($parent)
$parent->addChild($this);
}
public function getParent()
{
return $this->parent;
}
public function addBlock(\App\Entity\Content\Block $block)
{
if(in_array($block, $this->blocks->toArray()))
return true;
$this->blocks[] = $block;
$block->setItem($this);
return $this;
}
public function removeBlock(\App\Entity\Content\Block $block)
{
$this->blocks->removeElement($block);
$block->setItem(null);
}
public function getBlocks()
{
return $this->blocks;
}
public function setType(\App\Entity\Content\Type $type = null)
{
$this->type = $type;
if($type)
$type->addItem($this);
}
public function getType()
{
return $this->type;
}
public function getGroupRestrictions()
{
return $this->groupRestrictions;
}
public function addGroupRestriction(\App\Entity\Users\Group $group)
{
if($this->groupRestrictions->contains($group))
return true;
// Add it
$this->groupRestrictions->add($group);
}
public function removeGroupRestriction(\App\Entity\Users\Group $group)
{
$this->groupRestrictions->removeElement($group);
}
public function getRoleRestrictions()
{
return $this->roleRestrictions;
}
public function addRoleRestriction(\App\Entity\Users\Role $role)
{
if($this->roleRestrictions->contains($role))
return true;
// Add it
$this->roleRestrictions->add($role);
}
public function removeRoleRestriction(\App\Entity\Users\Role $role)
{
$this->roleRestrictions->removeElement($role);
}
public function addRevision(\App\Entity\Content\Revision $revision)
{
if(in_array($revision, $this->revisions->toArray()))
return true;
$this->revisions[] = $revision;
$revision->setItem($this);
return $this;
}
public function removeRevision(\App\Entity\Content\Revision $revision)
{
$this->revisions->removeElement($revision);
$revision->setItem(null);
}
public function getRevisions()
{
return $this->revisions;
}
public function setLanguage(\App\Entity\Settings\Language $language = null)
{
$this->language = $language;
}
public function getLanguage()
{
return $this->language;
}
public function setSeoPageTitle(string $seoPageTitle = null)
{
$this->seoPageTitle = $seoPageTitle;
}
public function getSeoPageTitle()
{
return $this->seoPageTitle;
}
public function setSeoDescription(string $seoDescription = null)
{
$this->seoDescription = $seoDescription;
}
public function getSeoDescription()
{
return $this->seoDescription;
}
public function setSeoKeywords(string $seoKeywords = null)
{
$this->seoKeywords = $seoKeywords;
}
public function getSeoKeywords()
{
return $this->seoKeywords;
}
public function setOgTitle(string $ogTitle = null)
{
$this->ogTitle = $ogTitle;
}
public function getOgTitle()
{
return $this->ogTitle;
}
public function setOgDescription(string $ogDescription = null)
{
$this->ogDescription = $ogDescription;
}
public function getOgDescription()
{
return $this->ogDescription;
}
public function setOgImage(string $ogImage = null)
{
$this->ogImage = $ogImage;
}
public function getOgImage()
{
return $this->ogImage;
}
public function setOgType(string $ogType = null)
{
$this->ogType = $ogType;
}
public function getOgType()
{
return $this->ogType;
}
public function setOgUrl(string $ogUrl = null)
{
$this->ogUrl = $ogUrl;
}
public function getOgUrl()
{
return $this->ogUrl;
}
public function setTwitterCard(string $twitterCard = null)
{
$this->twitterCard = $twitterCard;
}
public function getTwitterCard()
{
return $this->twitterCard;
}
public function setTwitterSite(string $twitterSite = null)
{
$this->twitterSite = $twitterSite;
}
public function getTwitterSite()
{
return $this->twitterSite;
}
}