PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / libraries / adapter / mvc / model.php

model.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at libraries/adapter/mvc/model.php

396 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.mvc
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 JLoader::import('adapter.application.object');
15
16 /**
17 * The model class used by the MVC framework.
18 * A model can be used by a controller or a view to handle
19 * a certain entity of the plugin.
20 *
21 * The model can be invoked when the value contained
22 * in $_REQUEST['task'] is equals to 'ComponentModel' + $_REQUEST['task'].
23 *
24 * e.g. $_REQUEST['task'] = 'groups.save' -> ComponentModelGroups
25 *
26 * @since 10.0
27 */
28 abstract class JModel extends JObject
29 {
30 /**
31 * A list of model instances.
32 *
33 * @var array
34 */
35 protected static $instances = array();
36
37 /**
38 * A list of included paths.
39 *
40 * @var array
41 * @since 10.1.24
42 */
43 protected static $paths = array();
44
45 /**
46 * The model name.
47 * e.g. ComponentModel[NAME]
48 *
49 * @var string
50 */
51 protected $_name = null;
52
53 /**
54 * The component name.
55 * e.g. [COMPONENT]ModelName
56 *
57 * @var string
58 */
59 protected $_component = null;
60
61 /**
62 * The application client.
63 *
64 * @var string
65 */
66 protected $_client = null;
67
68 /**
69 * The database table name.
70 *
71 * @var array
72 * @since 10.1.35
73 */
74 protected $_config;
75
76 /**
77 * Tries to load the specified model, only creating it if
78 * it doesn't exist yet.
79 *
80 * @param string $type The model type to instantiate
81 * @param string $prefix Prefix for the model class name.
82 * @param array $config Configuration array for model.
83 *
84 * @return mixed The model instance if found, otherwise null.
85 *
86 * @since 10.1.35 Switch the ordering of $type and $prefix argument.
87 * @since 10.1.35 Added $config argument.
88 * @since 10.1.35 Removed $client, $table and $pk arguments.
89 */
90 public static function getInstance($type, $prefix = '', $config = array())
91 {
92 /**
93 * For backward compatibility, we should check whether the 3rd argument is passed,
94 * meaning that we need to force a client section in which to load the model.
95 *
96 * @since 10.1.35
97 */
98 if (is_string($config))
99 {
100 $config = ['client' => $config];
101 }
102 else
103 {
104 $config = (array) $config;
105 }
106
107 if (empty($config['client']))
108 {
109 $config['client'] = JFactory::getApplication()->isAdmin() ? 'admin' : 'site';
110 }
111
112 /**
113 * The current version of JModel is not using the same arguments as
114 * specified by Joomla, since the model name is always passed as
115 * first argument.
116 *
117 * For this reason, we need to swap the prefix and the name when
118 * passed using Joomla standards. This can be done when at least one
119 * of the following conditions is verified:
120 *
121 * - the name contains 'model' word
122 * - the name is actually the folder name and the prefix isn't
123 *
124 * @since 10.1.24
125 */
126 if (preg_match("/model/i", $type)
127 || (is_dir(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $type) && !is_dir(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . strtolower($prefix))))
128 {
129 // swap name with prefix
130 $tmp = $type;
131 $type = $prefix;
132 $prefix = $tmp;
133 }
134
135 // remove 'model' from prefix (if any) and make it lowercase
136 $prefix = strtolower(preg_replace("/model/i", '', $prefix));
137
138 $sign = serialize(array($prefix, $type, $config['client']));
139
140 if (!isset(static::$instances[$sign]))
141 {
142 static::$instances[$sign] = false;
143
144 // create classname
145 $classname = ucfirst($prefix) . 'Model' . ucfirst($type);
146
147 /**
148 * Merge default include path (the fallback) with specified directories.
149 * Then iterate the list to find the first available model.
150 *
151 * @since 10.1.24
152 */
153 $paths = array_merge(
154 // default include path
155 array(
156 /**
157 * Search also inside the libraries folder of the current plugin.
158 * Prioritize this folder to avoid conflicts with deprecated files.
159 *
160 * @since 10.1.35
161 */
162 implode(DIRECTORY_SEPARATOR, array(WP_PLUGIN_DIR, $prefix, 'libraries', 'mvc', $config['client'], 'models')),
163 implode(DIRECTORY_SEPARATOR, array(WP_PLUGIN_DIR, $prefix, $config['client'], 'models')),
164 ),
165 // specified include paths
166 self::addIncludePath()
167 );
168
169 // iterate paths until we find an existing file (or till the list is empty)
170 for ($i = 0, $path = null; $i < count($paths) && !$path; $i++)
171 {
172 // create path
173 $path = $paths[$i] . DIRECTORY_SEPARATOR . strtolower($type) . '.php';
174
175 // make sure the file exists
176 if (!is_file($path))
177 {
178 // unset path to keep iterating
179 $path = null;
180 }
181 }
182
183 // make sure we have a path
184 if ($path)
185 {
186 // include model
187 require_once $path;
188
189 // make sure the class exists
190 if (class_exists($classname))
191 {
192 // cache model instance
193 static::$instances[$sign] = new $classname($config);
194 }
195 }
196 }
197
198 return static::$instances[$sign];
199 }
200
201 /**
202 * Add a directory where the class should search for models.
203 * You may either pass a string or an array of directories.
204 *
205 * @param mixed $path A path or array of paths to search.
206 *
207 * @return array An array with directory elements.
208 *
209 * @since 10.1.24
210 */
211 public static function addIncludePath($path = null)
212 {
213 if (!empty($path))
214 {
215 JLoader::import('adapter.filesystem.path');
216
217 foreach ((array) $path as $includePath)
218 {
219 $includePath = JPath::clean($includePath);
220
221 // check if the path is a dir
222 if (!is_dir($includePath))
223 {
224 // extract name between component and models
225 if (preg_match("/components[\/\\\\]com_([a-z0-9_]+)[\/\\\\]models/", $includePath, $match))
226 {
227 // access adapters of current plugin
228 $option = JFactory::getApplication()->input->get('option');
229 // strip initial com_ from option name
230 $option = preg_replace("/^com_/", '', $option);
231
232 // rewrite include path
233 $includePath = JPath::clean(WP_PLUGIN_DIR . '/' . $option . '/libraries/adapter/mvc/models/' . end($match));
234 }
235 }
236
237 // make sure the folder is not already in the list
238 if (!in_array($includePath, static::$paths))
239 {
240 // push directory as first
241 array_unshift(static::$paths, $includePath);
242 }
243 }
244 }
245
246 return static::$paths;
247 }
248
249 /**
250 * Class constructor.
251 *
252 * @param array $config An array of configuration options.
253 *
254 * @since 10.1.35 Added $config argument.
255 * @since 10.1.35 Removed $table and $pk arguments.
256 */
257 public function __construct($config = array())
258 {
259 $option = $this->getComponentName();
260
261 if (!array_key_exists('table_path', $config) && $option)
262 {
263 $config['table_path'] = [
264 JPath::clean(WP_PLUGIN_DIR . '/' . $option . '/libraries/mvc/' . $config['client'] . '/tables'),
265 JPath::clean(WP_PLUGIN_DIR . '/' . $option . '/' . $config['client'] . '/tables'),
266 ];
267 }
268
269 // set the default view search path
270 if (!empty($config['table_path']))
271 {
272 $this->addTablePath($config['table_path']);
273 }
274
275 $this->_config = $config;
276 }
277
278 /**
279 * Adds to the stack of model table paths in LIFO order.
280 *
281 * @param mixed $path The directory as a string or directories as an array to add.
282 *
283 * @return void
284 *
285 * @since 10.1.35
286 */
287 public function addTablePath($path)
288 {
289 JTable::addIncludePath($path);
290 }
291
292 /**
293 * Method to get a table object.
294 *
295 * @param string $name The table name.
296 * @param string $prefix The class prefix.
297 * @param array $options Configuration array for table.
298 *
299 * @return JTable A table object.
300 *
301 * @since 10.1.35
302 */
303 public function getTable($name = '', $prefix = 'JTable', $options = array())
304 {
305 if (!$name)
306 {
307 $name = $this->getName();
308 }
309
310 return JTable::getInstance($name, $prefix, $options);
311 }
312
313 /**
314 * Returns the model name.
315 *
316 * @return string Model name.
317 */
318 public function getModelName()
319 {
320 if (is_null($this->_name))
321 {
322 $class = get_class($this);
323
324 if (preg_match("/Model(.*?)$/", $class, $match))
325 {
326 $this->_name = strtolower($match[1]);
327 }
328 else
329 {
330 $this->_name = $class;
331 }
332 }
333
334 return $this->_name;
335 }
336
337 /**
338 * Returns the model name.
339 * Proxy for getModelName() method.
340 *
341 * @return string Model name.
342 *
343 * @since 1.0.1.35
344 */
345 public function getName()
346 {
347 return $this->getModelName();
348 }
349
350 /**
351 * Returns the component name.
352 *
353 * @return string Component name.
354 */
355 public function getComponentName()
356 {
357 if (is_null($this->_component))
358 {
359 $class = get_class($this);
360
361 if (preg_match("/(.*?)Model/", $class, $match))
362 {
363 $this->_component = strtolower($match[1]);
364 }
365 else
366 {
367 $this->_component = $class;
368 }
369 }
370
371 return $this->_component;
372 }
373
374 /**
375 * Returns the application client folder (admin or site).
376 *
377 * @return string The client.
378 */
379 public function getClientFolder()
380 {
381 if (is_null($this->_client))
382 {
383 $this->_client = JFactory::getApplication()->isAdmin() ? 'admin' : 'site';
384 }
385
386 return $this->_client;
387 }
388 }
389
390 /**
391 * Alias for JModel, which is still used by the components.
392 *
393 * @since 10.1.24
394 */
395 class_alias('JModel', 'JModelLegacy');
396