Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Menu Tree and Node classes implemented for menu module menu item hier…
…archy.
- Loading branch information
1 parent
13048b6
commit 912d428
Showing
7 changed files
with
890 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
<?php | ||
/** | ||
* Joomla! Content Management System | ||
* | ||
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved. | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | ||
namespace Joomla\CMS\Menu; | ||
|
||
defined('JPATH_PLATFORM') or die; | ||
|
||
use Joomla\Registry\Registry; | ||
|
||
/** | ||
* A Node for MenuTree | ||
* | ||
* @see Tree | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
class Node | ||
{ | ||
/** | ||
* Node Id | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $id = null; | ||
|
||
/** | ||
* CSS Class for node | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $class = null; | ||
|
||
/** | ||
* Whether this node is active | ||
* | ||
* @var bool | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $active = false; | ||
|
||
/** | ||
* Additional custom node params | ||
* | ||
* @var Registry | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $params; | ||
|
||
/** | ||
* Parent node object | ||
* | ||
* @var Node | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $parent = null; | ||
|
||
/** | ||
* Array of Children node objects | ||
* | ||
* @var Node[] | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $children = array(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->params = new Registry; | ||
} | ||
|
||
/** | ||
* Add child to this node | ||
* | ||
* If the child already has a parent, the link is unset | ||
* | ||
* @param Node $child The child to be added | ||
* | ||
* @return Node The new added child | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function addChild(Node $child) | ||
{ | ||
$hash = spl_object_hash($child); | ||
|
||
if (isset($child->parent)) | ||
{ | ||
$child->parent->removeChild($child); | ||
} | ||
|
||
$child->parent = $this; | ||
$this->children[$hash] = $child; | ||
|
||
return $child; | ||
} | ||
|
||
/** | ||
* Remove a child from this node | ||
* | ||
* If the child exists it is unset | ||
* | ||
* @param Node $child The child to be added | ||
* | ||
* @return void | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function removeChild(Node $child) | ||
{ | ||
$hash = spl_object_hash($child); | ||
|
||
if (isset($this->children[$hash])) | ||
{ | ||
$child->parent = null; | ||
|
||
unset($this->children[$hash]); | ||
} | ||
} | ||
|
||
/** | ||
* Test if this node has a parent | ||
* | ||
* @return bool True if there is a parent | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function hasParent() | ||
{ | ||
return isset($this->parent); | ||
} | ||
|
||
/** | ||
* Get the parent of this node | ||
* | ||
* @return Node The Node object's parent or null for no parent | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getParent() | ||
{ | ||
return $this->parent; | ||
} | ||
|
||
/** | ||
* Test if this node has children | ||
* | ||
* @return bool | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function hasChildren() | ||
{ | ||
return count($this->children) > 0; | ||
} | ||
|
||
/** | ||
* Get the children of this node | ||
* | ||
* @return Node[] The children | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getChildren() | ||
{ | ||
return $this->children; | ||
} | ||
|
||
/** | ||
* Find the current node depth in the tree hierarchy | ||
* | ||
* @return int The node level in the hierarchy, where ROOT == 0, First level menu item == 1, an so on. | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getLevel() | ||
{ | ||
return $this->hasParent() ? $this->getParent()->getLevel() + 1 : 0; | ||
} | ||
|
||
/** | ||
* Check whether the object instance node is the root node | ||
* | ||
* @return bool | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function isRoot() | ||
{ | ||
return !$this->hasParent(); | ||
} | ||
|
||
/** | ||
* Set the active state on or off | ||
* | ||
* @param bool $active The new active state | ||
* | ||
* @return void | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function setActive($active) | ||
{ | ||
$this->active = (bool) $active; | ||
} | ||
|
||
/** | ||
* set the params array | ||
* | ||
* @param Registry $params The params attributes | ||
* | ||
* @return void | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function setParams(Registry $params) | ||
{ | ||
$this->params = $params; | ||
} | ||
|
||
/** | ||
* Get the param value from the node params | ||
* | ||
* @param string $key The param name | ||
* | ||
* @return mixed | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function getParam($key) | ||
{ | ||
return isset($this->params[$key]) ? $this->params[$key] : null; | ||
} | ||
|
||
/** | ||
* Get an attribute value | ||
* | ||
* @param string $name The attribute name | ||
* | ||
* @return mixed | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function get($name) | ||
{ | ||
switch ($name) | ||
{ | ||
case 'id': | ||
case 'class': | ||
case 'active': | ||
case 'params': | ||
return $this->$name; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
/** | ||
* Joomla! Content Management System | ||
* | ||
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved. | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | ||
namespace Joomla\CMS\Menu\Node; | ||
|
||
defined('JPATH_PLATFORM') or die; | ||
|
||
use Joomla\CMS\Menu\Node; | ||
|
||
/** | ||
* A Component type of node for MenuTree | ||
* | ||
* @see Node | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
class Component extends Node | ||
{ | ||
/** | ||
* Node Title | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $title = null; | ||
|
||
/** | ||
* The component name for this node link | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $element = null; | ||
|
||
/** | ||
* Node Link | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $link = null; | ||
|
||
/** | ||
* Link Target | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $target = null; | ||
|
||
/** | ||
* Link title icon | ||
* | ||
* @var string | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
protected $icon = null; | ||
|
||
/** | ||
* Constructor for the class. | ||
* | ||
* @param string $title The title of the node | ||
* @param string $element The component name | ||
* @param string $link The node link | ||
* @param string $target The link target | ||
* @param string $class The CSS class for the node | ||
* @param string $id The node id | ||
* @param string $icon The title icon for the node | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function __construct($title, $element, $link, $target = null, $class = null, $id = null, $icon = null) | ||
{ | ||
$this->title = $title; | ||
$this->element = $element; | ||
$this->link = $link ? \JFilterOutput::ampReplace($link) : 'index.php?option=' . $element; | ||
$this->target = $target; | ||
$this->class = $class; | ||
$this->id = $id; | ||
$this->icon = $icon; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Get an attribute value | ||
* | ||
* @param string $name The attribute name | ||
* | ||
* @return mixed | ||
* | ||
* @since __DEPLOY_VERSION__ | ||
*/ | ||
public function get($name) | ||
{ | ||
switch ($name) | ||
{ | ||
case 'title': | ||
case 'element': | ||
case 'link': | ||
case 'target': | ||
case 'icon': | ||
return $this->$name; | ||
} | ||
|
||
return parent::get($name); | ||
} | ||
} |
Oops, something went wrong.