1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
/*
* Copyright 2005-2016 OCSInventory-NG/OCSInventory-ocsreports contributors.
* See the Contributors file for more details about them.
*
* This file is part of OCSInventory-NG/OCSInventory-ocsreports.
*
* OCSInventory-NG/OCSInventory-ocsreports 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.
*
* OCSInventory-NG/OCSInventory-ocsreports 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 OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/**
* Menu class
*
* The class generate the menu
*
*/
class Menu {
private $_children;
private $_priority;
/**
* Constructor
*
* @param array $_children An array of MenuElem
* @param number $_priority The priority of this element to sort
*/
public function __construct(array $_children = array(), $_priority = 0) {
$this->_children = $_children;
$this->_priority = $_priority;
}
/**
* Sort the Menu
*
* @return number
*/
public function sortMenu() {
foreach ($this->getChildren() as $menu) {
if ($menu->hasChildren()) {
$menu->sortMenu();
}
}
uasort($this->_children, function($a, $b) {
if ($a->getPriority() == $b->getPriority()) {
return 0;
}
return ($a->getPriority() < $b->getPriority()) ? -1 : 1;
});
}
/**
* Get the MenuElem children
*
* @return Ambigous <array, MenuElem>
*/
public function getChildren() {
return $this->_children;
}
/**
* Set the MenuElem children
*
* @param MenuElem $_children Children for this MenuElem
*
* @return MenuElem
*/
public function setChildren(array $_children) {
$this->_children = $_children;
return $this;
}
/**
* Get the MenuElem by an index
*
* @param string $index The index of the MenuElem
*
* @return array An array of the childrens
*/
public function getElem($index) {
return $this->_children[$index];
}
/**
* Check if this MenuElem has childrens
*
* @return boolean
*/
public function hasChildren() {
return !empty($this->_children);
}
/**
* Get the priority of this MenuElem
*
* @return number Priority
*/
public function getPriority() {
return $this->_priority;
}
/**
* Set the priority of this MenuElem
*
* @param number $_priority The priority of this MenuElem
*
* @return MenuElem
*/
public function setPriority($_priority) {
$this->_priority = $_priority;
return $this;
}
/**
* Find MenuElem by its index
*
* @param string $elem_index The index we searching for
*
* @return <string, MenuElem> The MenuElem if function find it
*/
public function findElemByIndex($elem_index) {
foreach ($this->getChildren() as $index => $menu) {
if ($index == $elem_index) {
return $menu;
} else {
$res = $menu->findElemByIndex($elem_index);
if ($res) {
return $res;
}
}
}
}
/**
* Delete a MenuElem
*
* @param string $elem_index The index of MenuElem to delete
*
* @return Menu
*/
public function delElem($elem_index) {
unset($this->_children[$elem_index]);
return $this;
}
/**
* Replace the MenuElem by this pass in parameter if it exists
*
* @param string $elem_index The index of MenuElem to replace
* @param MenuElem $menuElem The new MenuElem
*
* @return Menu
*/
public function replaceElem($elem_index, MenuElem $menuElem) {
if (isset($this->_children[$elem_index])) {
$this->_children[$elem_index] = $menuElem;
}
return $this;
}
/**
* Add a MenuElem
*
* @param string $index Index name for the MenuElem we want to add
* @param MenuElem $menuElem MenuEleme to add
*
* @return MenuElem Return the current MenuElem
*/
public function addElem($index, MenuElem $menuElem) {
$this->_children[$index] = $menuElem;
return $this;
}
}
|