1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Pathway
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Class to maintain a pathway.
14 *
15 * The user's navigated path within the application.
16 *
17 * @since 1.5
18 */
19 class JPathway
20 {
21 /**
22 * @var array Array to hold the pathway item objects
23 * @since 1.5
24 * @deprecated 4.0 Will convert to $pathway
25 */
26 protected $_pathway = array();
27
28 /**
29 * @var integer Integer number of items in the pathway
30 * @since 1.5
31 * @deprecated 4.0 Will convert to $count
32 */
33 protected $_count = 0;
34
35 /**
36 * JPathway instances container.
37 *
38 * @var JPathway[]
39 * @since 1.7
40 */
41 protected static $instances = array();
42
43 /**
44 * Class constructor
45 *
46 * @param array $options The class options.
47 *
48 * @since 1.5
49 */
50 public function __construct($options = array())
51 {
52 }
53
54 /**
55 * Returns a JPathway object
56 *
57 * @param string $client The name of the client
58 * @param array $options An associative array of options
59 *
60 * @return JPathway A JPathway object.
61 *
62 * @since 1.5
63 * @throws RuntimeException
64 */
65 public static function getInstance($client, $options = array())
66 {
67 if (empty(self::$instances[$client]))
68 {
69 // Create a JPathway object
70 $classname = 'JPathway' . ucfirst($client);
71
72 if (!class_exists($classname))
73 {
74 // @deprecated 4.0 Everything in this block is deprecated but the warning is only logged after the file_exists
75 // Load the pathway object
76 $info = JApplicationHelper::getClientInfo($client, true);
77
78 if (is_object($info))
79 {
80 $path = $info->path . '/includes/pathway.php';
81
82 JLoader::register($classname, $path);
83
84 if (class_exists($classname))
85 {
86 JLog::add('Non-autoloadable JPathway subclasses are deprecated, support will be removed in 4.0.', JLog::WARNING, 'deprecated');
87 }
88 }
89 }
90
91 if (class_exists($classname))
92 {
93 self::$instances[$client] = new $classname($options);
94 }
95 else
96 {
97 throw new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_PATHWAY_LOAD', $client), 500);
98 }
99 }
100
101 return self::$instances[$client];
102 }
103
104 /**
105 * Return the JPathway items array
106 *
107 * @return array Array of pathway items
108 *
109 * @since 1.5
110 */
111 public function getPathway()
112 {
113 $pw = $this->_pathway;
114
115 // Use array_values to reset the array keys numerically
116 return array_values($pw);
117 }
118
119 /**
120 * Set the JPathway items array.
121 *
122 * @param array $pathway An array of pathway objects.
123 *
124 * @return array The previous pathway data.
125 *
126 * @since 1.5
127 */
128 public function setPathway($pathway)
129 {
130 $oldPathway = $this->_pathway;
131
132 // Set the new pathway.
133 $this->_pathway = array_values((array) $pathway);
134
135 return array_values($oldPathway);
136 }
137
138 /**
139 * Create and return an array of the pathway names.
140 *
141 * @return array Array of names of pathway items
142 *
143 * @since 1.5
144 */
145 public function getPathwayNames()
146 {
147 $names = array();
148
149 // Build the names array using just the names of each pathway item
150 foreach ($this->_pathway as $item)
151 {
152 $names[] = $item->name;
153 }
154
155 // Use array_values to reset the array keys numerically
156 return array_values($names);
157 }
158
159 /**
160 * Create and add an item to the pathway.
161 *
162 * @param string $name The name of the item.
163 * @param string $link The link to the item.
164 *
165 * @return boolean True on success
166 *
167 * @since 1.5
168 */
169 public function addItem($name, $link = '')
170 {
171 $ret = false;
172
173 if ($this->_pathway[] = $this->makeItem($name, $link))
174 {
175 $ret = true;
176 $this->_count++;
177 }
178
179 return $ret;
180 }
181
182 /**
183 * Set item name.
184 *
185 * @param integer $id The id of the item on which to set the name.
186 * @param string $name The name to set.
187 *
188 * @return boolean True on success
189 *
190 * @since 1.5
191 */
192 public function setItemName($id, $name)
193 {
194 $ret = false;
195
196 if (isset($this->_pathway[$id]))
197 {
198 $this->_pathway[$id]->name = $name;
199 $ret = true;
200 }
201
202 return $ret;
203 }
204
205 /**
206 * Create and return a new pathway object.
207 *
208 * @param string $name Name of the item
209 * @param string $link Link to the item
210 *
211 * @return JPathway Pathway item object
212 *
213 * @since 1.5
214 * @deprecated 4.0 Use makeItem() instead
215 * @codeCoverageIgnore
216 */
217 protected function _makeItem($name, $link)
218 {
219 return $this->makeItem($name, $link);
220 }
221
222 /**
223 * Create and return a new pathway object.
224 *
225 * @param string $name Name of the item
226 * @param string $link Link to the item
227 *
228 * @return JPathway Pathway item object
229 *
230 * @since 3.1
231 */
232 protected function makeItem($name, $link)
233 {
234 $item = new stdClass;
235 $item->name = html_entity_decode($name, ENT_COMPAT, 'UTF-8');
236 $item->link = $link;
237
238 return $item;
239 }
240 }
241