2019-03-20 23:16:07 +01:00
|
|
|
<?php
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* part-db version 0.1
|
|
|
|
* Copyright (C) 2005 Christoph Lechner
|
|
|
|
* http://www.cl-projects.de/
|
|
|
|
*
|
|
|
|
* part-db version 0.2+
|
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
|
|
|
* http://code.google.com/p/part-db/
|
|
|
|
*
|
|
|
|
* Part-DB Version 0.4+
|
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer
|
|
|
|
* https://github.com/jbtronics
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
|
|
|
* Part-DB Version 0.4+ "nextgen"
|
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer
|
2019-03-20 23:16:07 +01:00
|
|
|
* https://github.com/jbtronics.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\Base;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement;
|
2019-04-11 22:41:21 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2019-02-23 22:41:13 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
use Doctrine\ORM\PersistentCollection;
|
2019-03-28 19:24:34 +01:00
|
|
|
use App\Validator\Constraints\NoneOfItsChildren;
|
2019-04-06 18:45:26 +02:00
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
2019-04-07 19:30:42 +02:00
|
|
|
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
|
|
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* All elements with the fields "id", "name" and "parent_id" (at least).
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* This class is for managing all database objects with a structural design.
|
|
|
|
* All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)!
|
|
|
|
* The root node has always the ID '0'.
|
|
|
|
* It's allowed to have instances of root elements, but if you try to change
|
|
|
|
* an attribute of a root element, you will get an exception!
|
|
|
|
*
|
2019-03-25 12:44:44 +01:00
|
|
|
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository")
|
2019-04-06 18:45:26 +02:00
|
|
|
*
|
2019-08-20 12:34:43 +02:00
|
|
|
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener", "App\EntityListeners\TreeCacheInvalidationListener"})
|
|
|
|
*
|
2019-04-06 18:45:26 +02:00
|
|
|
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name")
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
abstract class StructuralDBElement extends AttachmentContainingDBElement
|
|
|
|
{
|
2019-03-20 23:24:20 +01:00
|
|
|
public const ID_ROOT_ELEMENT = 0;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
//This is a not standard character, so build a const, so a dev can easily use it
|
2019-03-20 23:24:20 +01:00
|
|
|
public const PATH_DELIMITER_ARROW = ' → ';
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
// We can not define the mapping here or we will get an exception. Unfortunatly we have to do the mapping in the
|
|
|
|
// subclasses
|
|
|
|
/**
|
|
|
|
* @var StructuralDBElement[]
|
2019-04-07 19:30:42 +02:00
|
|
|
* @Groups({"include_children"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
protected $children;
|
|
|
|
/**
|
|
|
|
* @var StructuralDBElement
|
2019-03-28 19:24:34 +01:00
|
|
|
* @NoneOfItsChildren()
|
2019-04-07 19:30:42 +02:00
|
|
|
* @Groups({"include_parents"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
protected $parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string The comment info for this element
|
2019-08-06 13:18:29 +02:00
|
|
|
* @ORM\Column(type="text")
|
2019-04-07 19:30:42 +02:00
|
|
|
* @Groups({"simple", "extended", "full"})
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-08-06 13:18:29 +02:00
|
|
|
protected $comment = "";
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
2019-02-24 12:59:41 +01:00
|
|
|
* @ORM\Column(type="integer", nullable=true)
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
|
|
|
protected $parent_id;
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
|
|
|
* @var bool If this property is set, this element can not be selected for part properties.
|
|
|
|
* Useful if this element should be used only for grouping, sorting.
|
|
|
|
* @ORM\Column(type="boolean")
|
|
|
|
*/
|
|
|
|
protected $not_selectable = false;
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
protected $level = 0;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
|
|
|
/** @var string[] all names of all parent elements as a array of strings,
|
2019-04-07 19:30:42 +02:00
|
|
|
* the last array element is the name of the element itself
|
|
|
|
*
|
|
|
|
*/
|
2019-03-20 22:53:06 +01:00
|
|
|
private $full_path_strings;
|
2019-02-23 22:41:13 +01:00
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->children = new ArrayCollection();
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* StructuralDBElement constructor.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Check if this element is a child of another element (recursive).
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param StructuralDBElement $another_element the object to compare
|
2019-03-28 19:24:34 +01:00
|
|
|
* IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)!
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
|
|
|
* @return bool True, if this element is child of $another_element.
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if there was an error
|
|
|
|
*/
|
|
|
|
public function isChildOf(StructuralDBElement $another_element)
|
|
|
|
{
|
|
|
|
$class_name = \get_class($this);
|
|
|
|
|
|
|
|
//Check if both elements compared, are from the same type:
|
|
|
|
if ($class_name != \get_class($another_element)) {
|
2019-03-28 19:24:34 +01:00
|
|
|
throw new \InvalidArgumentException('isChildOf() only works for objects of the same type!');
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:24:34 +01:00
|
|
|
if (null == $this->getParent()) { // this is the root node
|
2019-02-23 22:41:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-20 22:53:06 +01:00
|
|
|
|
2019-03-20 23:16:07 +01:00
|
|
|
//If this' parents element, is $another_element, then we are finished
|
|
|
|
return ($this->parent->getID() == $another_element->getID())
|
|
|
|
|| $this->parent->isChildOf($another_element); //Otherwise, check recursivley
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Getters
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-03-28 19:24:34 +01:00
|
|
|
* Get the parent-ID
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-28 19:24:34 +01:00
|
|
|
* @return integer * the ID of the parent element
|
2019-03-13 13:23:12 +01:00
|
|
|
* * NULL means, the parent is the root node
|
|
|
|
* * the parent ID of the root node is -1
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-28 19:24:34 +01:00
|
|
|
protected function getParentID(): int
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
return $this->parent_id ?? self::ID_ROOT_ELEMENT; //Null means root element
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
/**
|
|
|
|
* Get the parent of this element.
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-18 19:05:41 +01:00
|
|
|
* @return StructuralDBElement|null The parent element. Null if this element, does not have a parent.
|
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getParent(): ?self
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/**
|
|
|
|
* Get the comment of the element.
|
2019-03-28 19:24:34 +01:00
|
|
|
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return string the comment
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-28 19:24:34 +01:00
|
|
|
public function getComment(): ?string
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-03-28 19:24:34 +01:00
|
|
|
return $this->comment;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the level.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-04-07 19:30:42 +02:00
|
|
|
* The level of the root node is -1.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return int the level of this element (zero means a most top element
|
|
|
|
* [a subelement of the root node])
|
2019-04-07 19:30:42 +02:00
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getLevel(): int
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-04-07 19:30:42 +02:00
|
|
|
/**
|
|
|
|
* Only check for nodes that have a parent. In the other cases zero is correct.
|
|
|
|
*/
|
|
|
|
if (0 === $this->level && $this->parent !== null) {
|
2019-02-23 22:41:13 +01:00
|
|
|
$element = $this->parent;
|
2019-04-07 19:30:42 +02:00
|
|
|
while ($element !== null) {
|
2019-02-23 22:41:13 +01:00
|
|
|
/** @var StructuralDBElement $element */
|
|
|
|
$element = $element->parent;
|
2019-03-20 23:16:07 +01:00
|
|
|
++$this->level;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get the full path.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param string $delimeter the delimeter of the returned string
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return string the full path (incl. the name of this element), delimeted by $delimeter
|
2019-04-07 19:30:42 +02:00
|
|
|
*
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW): string
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-03-20 23:16:07 +01:00
|
|
|
if (!\is_array($this->full_path_strings)) {
|
2019-02-23 22:41:13 +01:00
|
|
|
$this->full_path_strings = array();
|
|
|
|
$this->full_path_strings[] = $this->getName();
|
2019-02-24 18:05:06 +01:00
|
|
|
$element = $this;
|
|
|
|
|
2019-03-28 19:24:34 +01:00
|
|
|
$overflow = 20; //We only allow 20 levels depth
|
|
|
|
|
|
|
|
while (null != $element->parent && $overflow >= 0) {
|
2019-02-24 18:05:06 +01:00
|
|
|
$element = $element->parent;
|
2019-02-23 22:41:13 +01:00
|
|
|
$this->full_path_strings[] = $element->getName();
|
2019-03-28 19:24:34 +01:00
|
|
|
//Decrement to prevent mem overflow.
|
|
|
|
$overflow--;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
2019-02-24 18:05:06 +01:00
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
$this->full_path_strings = array_reverse($this->full_path_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode($delimeter, $this->full_path_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Get all subelements of this element.
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param bool $recursive if true, the search is recursive
|
2019-02-23 22:41:13 +01:00
|
|
|
*
|
2019-03-20 23:16:07 +01:00
|
|
|
* @return static[] all subelements as an array of objects (sorted by their full path)
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-04-11 22:41:21 +02:00
|
|
|
public function getSubelements(): iterable
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-03-24 15:25:40 +01:00
|
|
|
return $this->children;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
public function getChildren(): iterable
|
2019-04-07 19:30:42 +02:00
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:47:25 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isNotSelectable(): bool
|
|
|
|
{
|
|
|
|
return $this->not_selectable;
|
|
|
|
}
|
|
|
|
|
2019-02-23 22:41:13 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Setters
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2019-03-28 19:24:34 +01:00
|
|
|
* Sets the new parent object
|
|
|
|
* @param self $new_parent The new parent object
|
|
|
|
* @return StructuralDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-28 19:24:34 +01:00
|
|
|
public function setParent(?self $new_parent) : self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
2019-03-28 19:24:34 +01:00
|
|
|
/*
|
|
|
|
if ($new_parent->isChildOf($this)) {
|
|
|
|
throw new \InvalidArgumentException('You can not use one of the element childs as parent!');
|
|
|
|
} */
|
|
|
|
|
|
|
|
$this->parent = $new_parent;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-20 23:16:07 +01:00
|
|
|
* Set the comment.
|
|
|
|
* @param string $new_comment the new comment
|
2019-03-28 19:24:34 +01:00
|
|
|
* @return StructuralDBElement
|
2019-02-23 22:41:13 +01:00
|
|
|
*/
|
2019-03-28 19:24:34 +01:00
|
|
|
public function setComment(?string $new_comment): self
|
2019-02-23 22:41:13 +01:00
|
|
|
{
|
|
|
|
$this->comment = $new_comment;
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-02-24 12:54:11 +01:00
|
|
|
return $this;
|
2019-02-23 22:41:13 +01:00
|
|
|
}
|
2019-04-11 22:41:21 +02:00
|
|
|
|
|
|
|
public function setChildren(array $element) : self
|
|
|
|
{
|
|
|
|
$this->children = $element;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-12 21:47:25 +02:00
|
|
|
/**
|
|
|
|
* @param bool $not_selectable
|
|
|
|
* @return StructuralDBElement
|
|
|
|
*/
|
|
|
|
public function setNotSelectable(bool $not_selectable): StructuralDBElement
|
|
|
|
{
|
|
|
|
$this->not_selectable = $not_selectable;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-04-11 22:41:21 +02:00
|
|
|
public function clearChildren() : self
|
|
|
|
{
|
|
|
|
$this->children = new ArrayCollection();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|