initial commit
This commit is contained in:
commit
e869a1cab4
107 changed files with 9029 additions and 0 deletions
82
app/library/Navigation/Container.php
Normal file
82
app/library/Navigation/Container.php
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace Navigation;
|
||||
|
||||
class Container
|
||||
{
|
||||
/**
|
||||
* Children
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_children = array();
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getChildren()
|
||||
{
|
||||
return $this->_children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasChildren()
|
||||
{
|
||||
return empty($this->getChildren()) === false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $child
|
||||
* @return Node
|
||||
*/
|
||||
public function addChild($child)
|
||||
{
|
||||
if (is_array($child)) {
|
||||
|
||||
$node = new Node();
|
||||
|
||||
foreach($child as $k => $v) {
|
||||
|
||||
if ($k == 'children') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$node->{'set' . ucfirst($k)}($v);
|
||||
}
|
||||
|
||||
if (isset($child['children'])) {
|
||||
|
||||
foreach($child['children'] as $c_data) {
|
||||
$node->addChild($c_data);
|
||||
}
|
||||
}
|
||||
|
||||
$child = $node;
|
||||
}
|
||||
|
||||
if (!($child instanceof Node)) {
|
||||
throw new Exception('Must be of type node.');
|
||||
}
|
||||
|
||||
$this->_children[] = $child;
|
||||
|
||||
$child->setParent($this);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $children
|
||||
* @return Node
|
||||
*/
|
||||
public function addChildren($children)
|
||||
{
|
||||
foreach($children as $child) {
|
||||
$this->addChild($child);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
7
app/library/Navigation/Exception.php
Normal file
7
app/library/Navigation/Exception.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Navigation;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
}
|
||||
13
app/library/Navigation/Navigation.php
Normal file
13
app/library/Navigation/Navigation.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Navigation;
|
||||
|
||||
class Navigation extends Container
|
||||
{
|
||||
public function __construct($config)
|
||||
{
|
||||
foreach($config as $node) {
|
||||
$this->addChild($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
256
app/library/Navigation/Node.php
Normal file
256
app/library/Navigation/Node.php
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
<?php
|
||||
|
||||
namespace Navigation;
|
||||
|
||||
use \Phalcon\Di;
|
||||
|
||||
class Node extends Container
|
||||
{
|
||||
/**
|
||||
* Caption
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_caption = null;
|
||||
|
||||
/**
|
||||
* Controller name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_controller = null;
|
||||
|
||||
/**
|
||||
* Controller action.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_action = null;
|
||||
|
||||
/**
|
||||
* ACL Resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_resource = null;
|
||||
|
||||
/**
|
||||
* Route name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_route = null;
|
||||
|
||||
/**
|
||||
* Active flag.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_active = null;
|
||||
|
||||
/**
|
||||
* Visible flag
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_visible = true;
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
protected $_parent = null;
|
||||
|
||||
/**
|
||||
* @param $parent
|
||||
* @return Node
|
||||
*/
|
||||
public function setParent($parent)
|
||||
{
|
||||
$this->_parent = $parent;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return $this->_parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCaption()
|
||||
{
|
||||
return $this->_caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $caption
|
||||
* @return Node
|
||||
*/
|
||||
public function setCaption($caption)
|
||||
{
|
||||
$this->_caption = (string) $caption;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getController()
|
||||
{
|
||||
return $this->_controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $controller
|
||||
* @return Node
|
||||
*/
|
||||
public function setController($controller)
|
||||
{
|
||||
$this->_controller = (string) $controller;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAction()
|
||||
{
|
||||
return $this->_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @return Node
|
||||
*/
|
||||
public function setAction($action)
|
||||
{
|
||||
$this->_action = (string) $action;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getResource()
|
||||
{
|
||||
return $this->_resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @return Node
|
||||
*/
|
||||
public function setResource($resource)
|
||||
{
|
||||
$this->_resource = (string) $resource;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRoute()
|
||||
{
|
||||
return $this->_route;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @return Node
|
||||
*/
|
||||
public function setRoute($route)
|
||||
{
|
||||
$this->_route = (string) $route;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the href for this node.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHref()
|
||||
{
|
||||
/** @var \Phalcon\Mvc\Url */
|
||||
$url = Di::getDefault()->get('url');
|
||||
|
||||
// Assemble route if set.
|
||||
if (strlen($this->getRoute()) > 0) {
|
||||
|
||||
$href = array(
|
||||
'for' => $this->getRoute(),
|
||||
'controller' => $this->getController(),
|
||||
'action' => $this->getAction()
|
||||
);
|
||||
}
|
||||
// Otherwise, use default route.
|
||||
else {
|
||||
$href = $this->getController();
|
||||
if (is_string($this->getAction())) {
|
||||
$href .= '/' . $this->getAction();
|
||||
}
|
||||
}
|
||||
|
||||
return $url->get($href);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return Node
|
||||
*/
|
||||
public function setActive($value)
|
||||
{
|
||||
$this->_active = (bool) $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
// If active flag is not set explicitly.
|
||||
// Test this node against the current request.
|
||||
if ($this->_active === null) {
|
||||
|
||||
// But only if the node is visible.
|
||||
if ($this->isVisible() === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// first. Check children.
|
||||
foreach($this->getChildren() as $child) {
|
||||
|
||||
if ($child->isActive() == true) {
|
||||
$this->setActive(true);
|
||||
return $this->_active;
|
||||
}
|
||||
}
|
||||
|
||||
$dispatcher = Di::getDefault()->get('dispatcher');
|
||||
|
||||
$controller = strtolower($dispatcher->getControllerName());
|
||||
$action = strtolower($dispatcher->getActionName());
|
||||
|
||||
$active = $controller == $this->_controller && $action == $this->_action;
|
||||
$this->setActive($active);
|
||||
}
|
||||
|
||||
return $this->_active;
|
||||
}
|
||||
|
||||
public function setVisible($value)
|
||||
{
|
||||
$this->_visible = (bool) $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isVisible()
|
||||
{
|
||||
return $this->_visible;
|
||||
}
|
||||
}
|
||||
Reference in a new issue