pathway.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikWP - Libraries |
| 4 | * @subpackage adapter.application |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 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 to maintain a pathway. |
| 16 | * |
| 17 | * The user's navigated path within the application. |
| 18 | * |
| 19 | * @since 10.1.19 |
| 20 | */ |
| 21 | class JPathway |
| 22 | { |
| 23 | /** |
| 24 | * JPathway instances container. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected static $instances = array(); |
| 29 | |
| 30 | /** |
| 31 | * Array to hold the pathway item objects. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected $pathway = array(); |
| 36 | |
| 37 | /** |
| 38 | * Class constructor. |
| 39 | * |
| 40 | * @param array $options The class options. |
| 41 | */ |
| 42 | public function __construct($options = array()) |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns a Pathway object. |
| 49 | * |
| 50 | * @param string $client The name of the client. |
| 51 | * @param array $options An associative array of options. |
| 52 | * |
| 53 | * @return self A Pathway object. |
| 54 | * |
| 55 | * @throws RuntimeException |
| 56 | */ |
| 57 | public static function getInstance($client, $options = array()) |
| 58 | { |
| 59 | $client = strtolower($client); |
| 60 | |
| 61 | if (empty(self::$instances[$client])) |
| 62 | { |
| 63 | // try to load file |
| 64 | if (!JLoader::import('adapter.pathway.classes.' . $client)) |
| 65 | { |
| 66 | throw new Exception(sprintf('Pathway [%s] not found', $client), 404); |
| 67 | } |
| 68 | |
| 69 | // create a Pathway object |
| 70 | $classname = 'JPathway' . ucfirst($client); |
| 71 | |
| 72 | // make sure the class exists and it is valid |
| 73 | if (!class_exists($classname) || !is_subclass_of($classname, 'JPathway')) |
| 74 | { |
| 75 | throw new Exception(sprintf('Invalid pathway [%s] class', $classname), 500); |
| 76 | } |
| 77 | |
| 78 | // instantiate the class and cache it |
| 79 | self::$instances[$client] = new $classname($options); |
| 80 | } |
| 81 | |
| 82 | return self::$instances[$client]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the Pathway items array. |
| 87 | * |
| 88 | * @return array Array of pathway items. |
| 89 | */ |
| 90 | public function getPathway() |
| 91 | { |
| 92 | $pw = $this->pathway; |
| 93 | |
| 94 | // use array_values to reset the array keys numerically |
| 95 | return array_values($pw); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets the Pathway items array. |
| 100 | * |
| 101 | * @param array $pathway An array of pathway objects. |
| 102 | * |
| 103 | * @return array The previous pathway data. |
| 104 | */ |
| 105 | public function setPathway($pathway) |
| 106 | { |
| 107 | $oldPathway = $this->pathway; |
| 108 | |
| 109 | // set the new pathway |
| 110 | $this->pathway = array_values((array) $pathway); |
| 111 | |
| 112 | return array_values($oldPathway); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Creates and return an array of the pathway names. |
| 117 | * |
| 118 | * @return array Array of names of pathway items. |
| 119 | */ |
| 120 | public function getPathwayNames() |
| 121 | { |
| 122 | $names = array(); |
| 123 | |
| 124 | // build the names array using just the names of each pathway item |
| 125 | foreach ($this->pathway as $item) |
| 126 | { |
| 127 | $names[] = $item->name; |
| 128 | } |
| 129 | |
| 130 | // use array_values to reset the array keys numerically |
| 131 | return array_values($names); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Creates and adds an item to the pathway. |
| 136 | * |
| 137 | * @param string $name The name of the item. |
| 138 | * @param string $link The link to the item. |
| 139 | * |
| 140 | * @return boolean True on success. |
| 141 | */ |
| 142 | public function addItem($name, $link = '') |
| 143 | { |
| 144 | $item = $this->makeItem($name, $link); |
| 145 | |
| 146 | if ($item) |
| 147 | { |
| 148 | $this->pathway[] = $item; |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Sets the item name. |
| 158 | * |
| 159 | * @param integer $id The id of the item on which to set the name. |
| 160 | * @param string $name The name to set. |
| 161 | * |
| 162 | * @return boolean True on success |
| 163 | */ |
| 164 | public function setItemName($id, $name) |
| 165 | { |
| 166 | if (isset($this->pathway[$id])) |
| 167 | { |
| 168 | $this->pathway[$id]->name = $name; |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Creates and returns a new pathway object. |
| 178 | * |
| 179 | * @param string $name Name of the item. |
| 180 | * @param string $link Link to the item. |
| 181 | * |
| 182 | * @return object Pathway item object. |
| 183 | */ |
| 184 | protected function makeItem($name, $link) |
| 185 | { |
| 186 | $item = new stdClass; |
| 187 | $item->name = html_entity_decode($name, ENT_COMPAT, 'UTF-8'); |
| 188 | $item->link = $link; |
| 189 | |
| 190 | return $item; |
| 191 | } |
| 192 | } |
| 193 |