getID() == null) { // this is the root node
return false;
} else {
//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
}
}
/******************************************************************************
*
* Getters
*
******************************************************************************/
/**
* @brief Get the parent-ID
*
* @retval integer @li the ID of the parent element
* @li NULL means, the parent is the root node
* @li the parent ID of the root node is -1
*/
public function getParentID() : int
{
return $this->parent_id ?? self::ID_ROOT_ELEMENT; //Null means root element
}
/**
* Get the comment of the element.
*
* @param boolean $parse_bbcode Should BBCode converted to HTML, before returning
* @return string the comment
*/
public function getComment(bool $parse_bbcode = true) : string
{
$val = htmlspecialchars($this->comment ?? '');
if ($parse_bbcode) {
//$bbcode = new BBCodeParser();
//$val = $bbcode->parse($val);
}
return $val;
}
/**
* Get the level
*
* @note The level of the root node is -1.
*
* @return integer the level of this element (zero means a most top element
* [a subelement of the root node])
*
*/
public function getLevel() : int
{
if ($this->level === 0) {
$element = $this->parent;
$parent_id = $element->getParentID();
while ($parent_id > 0) {
/** @var StructuralDBElement $element */
$element = $element->parent;
$parent_id = $element->getParentID();
$this->level++;
}
}
return $this->level;
}
/**
* Get the full path
*
* @param string $delimeter the delimeter of the returned string
*
* @return string the full path (incl. the name of this element), delimeted by $delimeter
*
* @throws Exception if there was an error
*/
public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW) : string
{
if (! \is_array($this->full_path_strings)) {
$this->full_path_strings = array();
$this->full_path_strings[] = $this->getName();
$element = $this;
while ($element->parent != null) {
$element = $element->parent;
$this->full_path_strings[] = $element->getName();
}
$this->full_path_strings = array_reverse($this->full_path_strings);
}
return implode($delimeter, $this->full_path_strings);
}
/**
* Get all subelements of this element
*
* @param boolean $recursive if true, the search is recursive
*
* @return static[] all subelements as an array of objects (sorted by their full path)
*/
public function getSubelements(bool $recursive) : PersistentCollection
{
if ($this->children == null) {
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
if (! $recursive) {
return $this->children;
} else {
$all_elements = array();
foreach ($this->children as $subelement) {
$all_elements[] = $subelement;
$all_elements = array_merge($all_elements, $subelement->getSubelements(true));
}
return $all_elements;
}
}
/******************************************************************************
*
* Setters
*
******************************************************************************/
/**
* Change the parent ID of this element
*
* @param integer|null $new_parent_id @li the ID of the new parent element
* @li NULL if the parent should be the root node
*/
public function setParentID($new_parent_id) : self
{
$this->parent_id = $new_parent_id;
return $this;
}
/**
* Set the comment
*
* @param string $new_comment the new comment
* @throws Exception if there was an error
*/
public function setComment(string $new_comment) : self
{
$this->comment = $new_comment;
return $this;
}
/********************************************************************************
*
* Tree / Table Builders
*
*********************************************************************************/
/**
* Build a HTML tree with all subcategories of this element
*
* This method prints a ';
} else {
$root_level = $this->getLevel() + 1;
}
// get all subelements
$subelements = $this->getSubelements($recursive);
foreach ($subelements as $element) {
$level = $element->getLevel() - $root_level;
$selected = ($element->getID() == $selected_id) ? 'selected' : '';
$html[] = '';
}
return implode("\n", $html);
}
public function buildBootstrapTree(
$page,
$parameter,
$recursive = false,
$show_root = false,
$use_db_root_name = true,
$root_name = '$$'
): array
{
if ($root_name == '$$') {
$root_name = _('Oberste Ebene');
}
$subelements = $this->getSubelements(false);
$nodes = array();
foreach ($subelements as $element) {
$nodes[] = $element->buildBootstrapTree($page, $parameter);
}
// if we are on root level?
if ($this->getParentID() == -1) {
if ($show_root) {
$tree = array(
array('text' => $use_db_root_name ? htmlspecialchars($this->getName()) : $root_name ,
'href' => $page . '?' . $parameter . '=' . $this->getID(),
'nodes' => $nodes)
);
} else { //Dont show root node
$tree = $nodes;
}
} elseif (!empty($nodes)) {
$tree = array('text' => htmlspecialchars($this->getName()),
'href' => $page . '?' . $parameter . '=' . $this->getID(),
'nodes' => $nodes
);
} else {
$tree = array('text' => htmlspecialchars($this->getName()),
'href' => $page . '?' . $parameter . '=' . $this->getID()
);
}
return $tree;
}
/**
* Creates a template loop for a Breadcrumb bar, representing the structural DB element.
* @param $page string The base page, to which the breadcrumb links should be directing to.
* @param $parameter string The parameter, which selects the ID of the StructuralDBElement.
* @param bool $show_root Show the root as its own breadcrumb.
* @param string $root_name The label which should be used for the root breadcrumb.
* @return array An Loop containing multiple arrays, which contains href and caption for the breadcrumb.
*/
public function buildBreadcrumbLoop(string $page, string $parameter, bool $show_root = false, $root_name = '$$', bool $element_is_link = false) : array
{
$breadcrumb = array();
if ($root_name == '$$') {
$root_name = _('Oberste Ebene');
}
if ($show_root) {
$breadcrumb[] = array('label' => $root_name,
'disabled' => true);
}
if (!$this->current_user->canDo(static::getPermissionName(), StructuralPermission::READ)) {
return array('label' => '???',
'disabled' => true);
}
$tmp = array();
if ($element_is_link) {
$tmp[] = array('label' => $this->getName(), 'href' => $page . '?' . $parameter . '=' .$this->getID(), 'selected' => true);
} else {
$tmp[] = array('label' => $this->getName(), 'selected' => true);
}
$parent_id = $this->getParentID();
while ($parent_id > 0) {
/** @var StructuralDBElement $element */
$element = static::getInstance($this->database, $this->current_user, $this->log, $parent_id);
$parent_id = $element->getParentID();
$tmp[] = array('label' => $element->getName(), 'href' => $page . '?' . $parameter . '=' . $element->getID());
}
$tmp = array_reverse($tmp);
$breadcrumb = array_merge($breadcrumb, $tmp);
return $breadcrumb;
}
}