<?php
namespace Cms\FileBundle\Entity;
use Cms\ContainerBundle\Entity\Container;
use Cms\CoreBundle\Model\Interfaces\Nestable\NestableInterface;
use Cms\CoreBundle\Model\Interfaces\Nestable\NestableTrait;
use Cms\FileBundle\Entity\Nodes\File;
use Cms\FileBundle\Entity\Nodes\Files\DocumentFile;
use Cms\FileBundle\Entity\Nodes\Files\GenericFile;
use Cms\FileBundle\Entity\Nodes\Files\ImageFile;
use Cms\FileBundle\Entity\Nodes\Folder;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Represents a "filesystem" object, like a folder or file.
* Nodes are stored as trees, just as a real filesystem would.
*
* Class Node
* @package Cms\FileBundle\Entity
*
* @ORM\Entity(repositoryClass = "Cms\FileBundle\Doctrine\NodeRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name = "_discr", type = "string")
* @ORM\DiscriminatorMap({
* "folder" = "Cms\FileBundle\Entity\Nodes\Folder",
* "file" = "Cms\FileBundle\Entity\Nodes\File",
* "generic" = "Cms\FileBundle\Entity\Nodes\Files\GenericFile",
* "image" = "Cms\FileBundle\Entity\Nodes\Files\ImageFile",
* "document" = "Cms\FileBundle\Entity\Nodes\Files\DocumentFile",
* })
* @ORM\Table(
* name = "cms__file__node",
* indexes = {
* @ORM\Index(
* name = "idx__name",
* columns = {
* "name"
* }
* )
* }
* )
* @Gedmo\Tree(type = "nested")
*/
abstract class Node extends TenantedEntity implements NestableInterface
{
const DISCRS = [
Folder::DISCR => Folder::class,
File::DISCR => File::class,
GenericFile::DISCR => GenericFile::class,
ImageFile::DISCR => ImageFile::class,
DocumentFile::DISCR => DocumentFile::class,
];
const DISCR = null;
const TYPE = null;
/**
* Basic tree-related fields.
*/
use NestableTrait;
/**
* The Node that acts as the parent for this one.
* If no parent is given, then this Node is a "root" level node.
*
* @var Node
*
* @ORM\ManyToOne(targetEntity = "Node", inversedBy = "children")
* @ORM\JoinColumn(name = "parent", referencedColumnName = "id", onDelete = "CASCADE")
* @Gedmo\TreeParent
*/
protected $parent;
/**
* The direct descendant Nodes of this one.
*
* @var ArrayCollection|Node[]
*
* @ORM\OneToMany(targetEntity = "Node", mappedBy = "parent")
*/
protected $children;
/**
* The Container that this item is tied to.
* ALL Nodes MUST be tied to some Container in the system.
* This needs to be set on every Node.
* However, descendant Nodes MUST have the same Container as their "root".
*
* @var Container|null
*
* @ORM\ManyToOne(targetEntity = "Cms\ContainerBundle\Entity\Container")
* @ORM\JoinColumn(name = "container", referencedColumnName = "id", onDelete = "SET NULL")
*/
protected $container;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = false
* )
*/
protected $name;
/**
* {@inheritdoc}
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* @return string
*/
public function getType()
{
if (static::TYPE === null) {
throw new \Exception();
}
return static::TYPE;
}
/**
* {@inheritdoc}
* @return Node
*/
public function getParent()
{
return $this->parent;
}
/**
* {@inheritdoc}
* @return ArrayCollection|Node[]
*/
public function getChildren()
{
return $this->children;
}
/**
* @return Container|null
*/
public function getContainer()
{
return $this->container;
}
/**
* {@inheritdoc}
* @param Node $value
*/
public function setParent(NestableInterface $value = null)
{
$this->parent = $value;
return $this;
}
/**
* @param Container|null $value
* @return $this
*/
public function setContainer(?Container $value)
{
$this->container = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function setName($value)
{
$this->name = $value;
return $this;
}
/**
* @return string
*/
public function ui()
{
return $this->getName();
}
}