466 lines
8.9 KiB
PHP
466 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace KupShop\ContentBundle\Entity;
|
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Doctrine\ORM\PersistentCollection;
|
|
use KupShop\ContentBundle\Util\BlockCollection;
|
|
use KupShop\I18nBundle\Entity\Interfaces\ITranslatableEntity;
|
|
use KupShop\I18nBundle\Translations\BlocksTranslation;
|
|
use KupShop\I18nBundle\Translations\ITranslation;
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
|
|
/**
|
|
* Html Pages Blocks.
|
|
*
|
|
* @ORM\Table(name="blocks", indexes={
|
|
*
|
|
* @ORM\Index(name="fk_id_root", columns={"id_root"}),
|
|
* @ORM\Index(name="position", columns={"position"}),
|
|
* @ORM\Index(name="fk_id_parent", columns={"id_parent"})
|
|
* })
|
|
*
|
|
* @ORM\Entity
|
|
*/
|
|
class Block implements ITranslatableEntity
|
|
{
|
|
public function getTranslation(): ITranslation
|
|
{
|
|
return ServiceContainer::getService(BlocksTranslation::class);
|
|
}
|
|
|
|
/**
|
|
* @var int
|
|
*
|
|
* @ORM\Column(name="id", type="integer", options={"unsigned"=true})
|
|
*
|
|
* @ORM\Id
|
|
*
|
|
* @ORM\GeneratedValue(strategy="IDENTITY")
|
|
*/
|
|
private $id;
|
|
|
|
/**
|
|
* @var int
|
|
*
|
|
* @ORM\Column(name="position", type="integer", nullable=false, options={"unsigned"=true, "default"="0"})
|
|
*/
|
|
private $position = '0';
|
|
|
|
/**
|
|
* @var string
|
|
*
|
|
* @ORM\Column(name="identifier", type="string", length=255, options={"default"=""})
|
|
*/
|
|
private $identifier = '';
|
|
|
|
/**
|
|
* @var string
|
|
*
|
|
* @ORM\Column(name="name", type="string", length=50, options={"default"=""})
|
|
*/
|
|
private $name = '';
|
|
|
|
/**
|
|
* @var string
|
|
*
|
|
* @ORM\Column(name="content", type="text", nullable=true)
|
|
*/
|
|
private $content;
|
|
|
|
/**
|
|
* @var string
|
|
*
|
|
* @ORM\Column(name="json_content", type="text", nullable=true)
|
|
*/
|
|
private $json_content;
|
|
|
|
/**
|
|
* One root Block has Many nested Blocks.
|
|
*
|
|
* @ORM\OneToMany(targetEntity="Block", mappedBy="root")
|
|
*
|
|
* @ORM\OrderBy(value={"position":"ASC"})
|
|
*/
|
|
private $blocks;
|
|
|
|
/** @var BlockCollection */
|
|
private $blocksHierarchy;
|
|
|
|
/**
|
|
* Many Blocks have One root Block.
|
|
*
|
|
* @ORM\ManyToOne(targetEntity="Block", inversedBy="blocks")
|
|
*
|
|
* @ORM\JoinColumn(name="id_root", referencedColumnName="id")
|
|
*/
|
|
private $root;
|
|
|
|
/**
|
|
* One parent Block has Many direct child Blocks.
|
|
*
|
|
* @ORM\OneToMany(targetEntity="Block", mappedBy="parent")
|
|
*/
|
|
private $children;
|
|
|
|
/**
|
|
* Many Blocks have One parent Block.
|
|
*
|
|
* @ORM\ManyToOne(targetEntity="Block", inversedBy="children")
|
|
*
|
|
* @ORM\JoinColumn(name="id_parent", referencedColumnName="id")
|
|
*/
|
|
private $parent;
|
|
|
|
/**
|
|
* One Block has Many Photos.
|
|
*
|
|
* @ORM\ManyToMany(targetEntity="KupShop\ContentBundle\Entity\Photo")
|
|
*
|
|
* @ORM\JoinTable(name="photos_blocks_relation",
|
|
* joinColumns={@ORM\JoinColumn(name="id_block", referencedColumnName="id")},
|
|
* inverseJoinColumns={@ORM\JoinColumn(name="id_photo", referencedColumnName="id")}
|
|
* )
|
|
* ORM\OrderBy({"position" = "ASC"})
|
|
*/
|
|
private $photos;
|
|
|
|
private $isWrapped = false;
|
|
|
|
private $unwrapped_content;
|
|
|
|
/**
|
|
* Get id.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getId()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function setIsWrapped($isWrapped)
|
|
{
|
|
$this->isWrapped = $isWrapped;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getIsWrapped()
|
|
{
|
|
return $this->isWrapped;
|
|
}
|
|
|
|
public function setUnwrapped_content($unwrapped_content)
|
|
{
|
|
$this->unwrapped_content = $unwrapped_content;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getUnwrapped_content()
|
|
{
|
|
return $this->unwrapped_content;
|
|
}
|
|
|
|
/**
|
|
* Set position.
|
|
*
|
|
* @param int $position
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setPosition($position)
|
|
{
|
|
$this->position = $position;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get position.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getPosition()
|
|
{
|
|
return $this->position;
|
|
}
|
|
|
|
/**
|
|
* Set identifier.
|
|
*
|
|
* @param string $identifier
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setIdentifier($identifier)
|
|
{
|
|
$this->identifier = $identifier;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get identifier.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getIdentifier()
|
|
{
|
|
return $this->identifier;
|
|
}
|
|
|
|
/**
|
|
* Set name.
|
|
*
|
|
* @param string $name
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get name.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* Set content.
|
|
*
|
|
* @param string $content
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setContent($content)
|
|
{
|
|
$this->content = $content;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get content.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getContent()
|
|
{
|
|
return $this->content;
|
|
}
|
|
|
|
/**
|
|
* Set content.
|
|
*
|
|
* @param string $json_content
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setJsonContent($json_content)
|
|
{
|
|
$this->json_content = $json_content;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get content.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getJsonContent()
|
|
{
|
|
return $this->json_content;
|
|
}
|
|
|
|
/**
|
|
* Set parent.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setParent(?Block $parent = null)
|
|
{
|
|
$this->parent = $parent;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get parent.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function getParent()
|
|
{
|
|
return $this->parent;
|
|
}
|
|
|
|
/**
|
|
* @return Collection
|
|
*/
|
|
public function getChildren()
|
|
{
|
|
return $this->children;
|
|
}
|
|
|
|
public function addChild(Block $child)
|
|
{
|
|
$this->children[] = $child;
|
|
$child->setParent($this);
|
|
}
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->blocks = new \Doctrine\Common\Collections\ArrayCollection();
|
|
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
|
|
$this->photos = new \Doctrine\Common\Collections\ArrayCollection();
|
|
}
|
|
|
|
/**
|
|
* Add photo.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function addPhoto(Block $photo)
|
|
{
|
|
$this->photos[] = $photo;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Remove photo.
|
|
*/
|
|
public function removePhoto(Block $photo)
|
|
{
|
|
$this->photos->removeElement($photo);
|
|
}
|
|
|
|
/**
|
|
* Get photos.
|
|
*
|
|
* @return Photo[]|Collection
|
|
*/
|
|
public function getPhotos(): Collection
|
|
{
|
|
return $this->photos;
|
|
}
|
|
|
|
/**
|
|
* Set root.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function setRoot(?Block $root = null)
|
|
{
|
|
$this->root = $root;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get root.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function getRoot()
|
|
{
|
|
return $this->root;
|
|
}
|
|
|
|
/**
|
|
* Get blocks.
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function getBlocks()
|
|
{
|
|
return $this->blocks;
|
|
}
|
|
|
|
/** Get blocks hierarchy
|
|
* returns only root blocks with cached children.
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function getBlocksHierarchy()
|
|
{
|
|
if (!$this->blocksHierarchy) {
|
|
$this->blocksHierarchy = $this::buildBlocksHierarchy($this->getBlocks());
|
|
}
|
|
|
|
return $this->blocksHierarchy;
|
|
}
|
|
|
|
public static function buildBlocksHierarchy(Collection $blocks): BlockCollection
|
|
{
|
|
/** @var Block[] $block_ids */
|
|
$block_ids = [];
|
|
$blocksHierarchy = new BlockCollection();
|
|
|
|
/** @var Block $block */
|
|
foreach ($blocks as $index => $block) {
|
|
if ($block->children instanceof PersistentCollection) {
|
|
$block->children->setInitialized(true);
|
|
}
|
|
|
|
$block_ids[$block->getId()] = $block;
|
|
|
|
if ($block->getParent() !== $block->getRoot()) {
|
|
$block_ids[$block->getParent()->getId()]->addChild($block);
|
|
} else {
|
|
$blocksHierarchy[] = $block;
|
|
}
|
|
}
|
|
|
|
return $blocksHierarchy;
|
|
}
|
|
|
|
/**
|
|
* Add block.
|
|
*
|
|
* @return Block
|
|
*/
|
|
public function addBlock(Block $block)
|
|
{
|
|
$this->blocks[] = $block;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Remove block.
|
|
*/
|
|
public function removeBlock(Block $block)
|
|
{
|
|
$this->blocks->removeElement($block);
|
|
}
|
|
|
|
/**
|
|
* Remove child.
|
|
*/
|
|
public function removeChild(Block $child)
|
|
{
|
|
$this->children->removeElement($child);
|
|
}
|
|
|
|
public function setId(int $id): void
|
|
{
|
|
$this->id = $id;
|
|
}
|
|
}
|