horizontal
3 days ago
leftboard
3 days ago
abstractlist.php
3 days ago
custom.php
3 days ago
factory.php
3 days ago
index.html
3 days ago
item.php
3 days ago
separator.php
3 days ago
shape.php
3 days ago
separator.php
293 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Class used to represent the shape of a separator menu. |
| 16 | * |
| 17 | * @since 1.5 |
| 18 | * |
| 19 | * @see MenuAbstractList |
| 20 | * @see MenuItemShape |
| 21 | */ |
| 22 | abstract class SeparatorItemShape extends MenuAbstractList |
| 23 | { |
| 24 | /** |
| 25 | * The title of the separator. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | private $title; |
| 30 | |
| 31 | /** |
| 32 | * The url of the separator. This value can be ignored. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $href; |
| 37 | |
| 38 | /** |
| 39 | * A custom value to use during the building. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | private $custom; |
| 44 | |
| 45 | /** |
| 46 | * If the separator is selected. |
| 47 | * |
| 48 | * @var boolean |
| 49 | */ |
| 50 | private $selected; |
| 51 | |
| 52 | /** |
| 53 | * The construct sets all the required attributes of this class. |
| 54 | * |
| 55 | * @param string $title The title of the separator. |
| 56 | * @param string $href Default empty. |
| 57 | * @param boolean $selected Default false. |
| 58 | * |
| 59 | * @uses setTitle() |
| 60 | * @uses setHref() |
| 61 | * @uses setSelected() |
| 62 | */ |
| 63 | public function __construct($title, $href = '', $selected = false) |
| 64 | { |
| 65 | $this->setTitle($title) |
| 66 | ->setHref($href) |
| 67 | ->setSelected($selected); |
| 68 | |
| 69 | parent::__construct(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the title of the separator. |
| 74 | * |
| 75 | * @param string $title The title of the separator. |
| 76 | * |
| 77 | * @return self This object to support chaining. |
| 78 | */ |
| 79 | public function setTitle($title) |
| 80 | { |
| 81 | $this->title = $title; |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the title of the separator. |
| 88 | * |
| 89 | * @return string The title of the separator. |
| 90 | */ |
| 91 | public function getTitle() |
| 92 | { |
| 93 | return $this->title; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the url of the separator. |
| 98 | * |
| 99 | * @param string $href The url of the separator. |
| 100 | * |
| 101 | * @return self This object to support chaining. |
| 102 | */ |
| 103 | public function setHref($href) |
| 104 | { |
| 105 | $this->href = $href; |
| 106 | |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the url of the separator. |
| 112 | * |
| 113 | * @return string The url of the separator. |
| 114 | */ |
| 115 | public function getHref() |
| 116 | { |
| 117 | return $this->href; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets the custom value of the separator |
| 122 | * |
| 123 | * @param string $custom The custom value of the separator. |
| 124 | * |
| 125 | * @return self This object to support chaining. |
| 126 | */ |
| 127 | public function setCustom($custom) |
| 128 | { |
| 129 | $this->custom = $custom; |
| 130 | |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the custom value of the separator. |
| 136 | * |
| 137 | * @return string The custom value of the separator. |
| 138 | */ |
| 139 | public function getCustom() |
| 140 | { |
| 141 | return $this->custom; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Sets if the separator is selected or not. |
| 146 | * |
| 147 | * @param boolean $selected The selection of the separator. |
| 148 | * |
| 149 | * @return self This object to support chaining. |
| 150 | */ |
| 151 | public function setSelected($selected) |
| 152 | { |
| 153 | $this->selected = $selected; |
| 154 | |
| 155 | return $this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns true if the separator is selected, otherwise false. |
| 160 | * |
| 161 | * @return boolean If the separator is selected. |
| 162 | */ |
| 163 | public function isSelected() |
| 164 | { |
| 165 | return $this->selected; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns true if the separator is collapsed, otherwise false. |
| 170 | * A separator is collapsed when it contains at least a selected child. |
| 171 | * |
| 172 | * @return boolean If the separator is collapsed. |
| 173 | * |
| 174 | * @uses MenuItemShape::isSelected() |
| 175 | */ |
| 176 | public function isCollapsed() |
| 177 | { |
| 178 | foreach ($this->menu as $c) |
| 179 | { |
| 180 | if ($c instanceof SeparatorItemShape && $c->isCollapsed()) |
| 181 | { |
| 182 | return true; |
| 183 | } |
| 184 | else if ($c instanceof MenuItemShape && $c->isSelected()) |
| 185 | { |
| 186 | return true; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Adds a child into the separator. |
| 195 | * |
| 196 | * @param MenuItemShape $child The child to add. |
| 197 | * |
| 198 | * @return self This object to support chaining. |
| 199 | */ |
| 200 | public function addChild(MenuItemShape $child) |
| 201 | { |
| 202 | return parent::push($child); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets a child at the specified position. Returns true on success, otherwise false. |
| 207 | * |
| 208 | * @param int $i The position of the child to replace or add. |
| 209 | * @param MenuItemShape $child The child to add. |
| 210 | * |
| 211 | * @return boolean True on success. |
| 212 | */ |
| 213 | public function setChild($i, MenuItemShape $child) |
| 214 | { |
| 215 | return parent::set($i, $child); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Deletes the requested item. |
| 220 | * It is possible to delete an item by index, reference or through |
| 221 | * an associative array that determines the query arguments. |
| 222 | * |
| 223 | * @param mixed $item The item index, reference or a search query. |
| 224 | * |
| 225 | * @return boolean True if removed, false otherwise. |
| 226 | */ |
| 227 | public function unsetChild($i) |
| 228 | { |
| 229 | return parent::delete($i); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Empties the children list. |
| 234 | * |
| 235 | * @return self This object to support chaining. |
| 236 | */ |
| 237 | public function clearChildren() |
| 238 | { |
| 239 | return parent::setMenu(array()); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns the list of the children of the separator. |
| 244 | * |
| 245 | * @return array The MenuItemShape array list of the children. |
| 246 | */ |
| 247 | public function children() |
| 248 | { |
| 249 | return parent::getMenu(); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Builds and returns the html structure of the separator and its children. |
| 254 | * @usedby MenuShape::build |
| 255 | * |
| 256 | * @return string The html structure. |
| 257 | * |
| 258 | * @uses buildHtml() |
| 259 | */ |
| 260 | public function build() |
| 261 | { |
| 262 | $html = ""; |
| 263 | |
| 264 | // get the HTML structure from each child of the separator |
| 265 | foreach ($this->menu as $c) |
| 266 | { |
| 267 | if ($c instanceof SeparatorItemShape) |
| 268 | { |
| 269 | // get the HTML if the child is a separator |
| 270 | $html .= $c->build(); |
| 271 | } |
| 272 | else if ($c instanceof MenuItemShape) |
| 273 | { |
| 274 | // get the HTML if the child is a menu item |
| 275 | $html .= $c->buildHtml(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // build the structure of the separator, which will contain the evaluated $html |
| 280 | return $this->buildHtml($html); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Builds and returns the html structure of the separator that wraps the children. |
| 285 | * This method must be implemented to define a specific graphic of the separator. |
| 286 | * |
| 287 | * @param string $html The full structure of the children of the separator. |
| 288 | * |
| 289 | * @return string The html of the separator. |
| 290 | */ |
| 291 | abstract protected function buildHtml($html); |
| 292 | } |
| 293 |