classes
2 days ago
export
2 days ago
forms
2 days ago
samples
2 days ago
column.php
2 days ago
exportable.php
2 days ago
factory.php
2 days ago
index.html
2 days ago
object.php
2 days ago
factory.php
352 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 | * Factory class to access the handler object to import items. |
| 16 | * |
| 17 | * @since 1.6 |
| 18 | */ |
| 19 | final class ImportFactory |
| 20 | { |
| 21 | /** |
| 22 | * A list containing all the classes already instantiated. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | private static $instances = array(); |
| 27 | |
| 28 | /** |
| 29 | * A list containing all the exportable classes already instantiated. |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | private static $exportable = array(); |
| 34 | |
| 35 | /** |
| 36 | * Gets the import handler object, only creating it |
| 37 | * if it doesn't exist yet. |
| 38 | * |
| 39 | * @param string $type The entity type to import. |
| 40 | * |
| 41 | * @return mixed ImportObject on success, otherwise null. |
| 42 | */ |
| 43 | public static function getObject($type) |
| 44 | { |
| 45 | if (!isset(static::$instances[$type])) |
| 46 | { |
| 47 | static::$instances[$type] = null; |
| 48 | |
| 49 | // check whether the import file is supported |
| 50 | if (static::isSupported($type)) |
| 51 | { |
| 52 | VAPLoader::import('libraries.import.object'); |
| 53 | |
| 54 | /** |
| 55 | * Trigger event to let the plugins include external importers. |
| 56 | * The plugins should include the resources needed, otherwise |
| 57 | * it wouldn't be possible to instantiate the returned classes. |
| 58 | * |
| 59 | * @param string $type The name of the import object to include. |
| 60 | * |
| 61 | * @return string The classname of the object. |
| 62 | * |
| 63 | * @since 1.7 |
| 64 | */ |
| 65 | $classname = VAPFactory::getEventDispatcher()->triggerOnce('onFetchImportObjectClassname', array($type)); |
| 66 | |
| 67 | if (!$classname) |
| 68 | { |
| 69 | // in case no plugins returned a valid classname, |
| 70 | // attempt to load one of the existing ones |
| 71 | $classname = 'ImportObject'; |
| 72 | |
| 73 | if (VAPLoader::import('libraries.import.classes.' . $type)) |
| 74 | { |
| 75 | // object found, use it as is |
| 76 | $classname .= ucwords($type); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // load import configuration |
| 81 | $xml = static::loadXml($type); |
| 82 | |
| 83 | // create instance |
| 84 | static::$instances[$type] = new $classname($xml, $type); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return static::$instances[$type]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if the specified entity type is supported. |
| 93 | * |
| 94 | * @param string $type The entity type to import. |
| 95 | * |
| 96 | * @return mixed The file path if supported, otherwise false. |
| 97 | */ |
| 98 | public static function isSupported($type) |
| 99 | { |
| 100 | // check whether the file exists |
| 101 | foreach (static::getIncludePaths() as $path) |
| 102 | { |
| 103 | $file = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $type . '.xml'; |
| 104 | |
| 105 | if (is_file($file)) |
| 106 | { |
| 107 | return $file; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Loads the XML containing the instructions for the import. |
| 116 | * |
| 117 | * @param string $type The entity type to import. |
| 118 | * |
| 119 | * @return SimpleXMLElement |
| 120 | */ |
| 121 | protected static function loadXml($type) |
| 122 | { |
| 123 | return simplexml_load_file(static::isSupported($type)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns a list of paths in which to search for import objects. |
| 128 | * |
| 129 | * @return array |
| 130 | * |
| 131 | * @since 1.7 |
| 132 | */ |
| 133 | protected static function getIncludePaths() |
| 134 | { |
| 135 | static $paths = null; |
| 136 | |
| 137 | // load paths only once |
| 138 | if (is_null($paths)) |
| 139 | { |
| 140 | $paths = array(); |
| 141 | // add native folder |
| 142 | $paths[] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'forms'; |
| 143 | |
| 144 | /** |
| 145 | * Trigger event to let the plugins register external import objects. |
| 146 | * |
| 147 | * @return array A list of include paths. |
| 148 | * |
| 149 | * @since 1.7 |
| 150 | */ |
| 151 | $results = VAPFactory::getEventDispatcher()->trigger('onFetchImportIncludePaths'); |
| 152 | |
| 153 | foreach ($results as $tmp) |
| 154 | { |
| 155 | // merge default files with specified ones |
| 156 | $paths = array_merge($paths, (array) $tmp); |
| 157 | } |
| 158 | |
| 159 | // exclude duplicate |
| 160 | $paths = array_unique($paths); |
| 161 | } |
| 162 | |
| 163 | return $paths; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Gets the export handler object, only creating it |
| 168 | * if it doesn't exist yet. |
| 169 | * |
| 170 | * @param string $type The export handler. |
| 171 | * @param array $args An array of options. |
| 172 | * |
| 173 | * @return mixed Exportable on success, otherwise null. |
| 174 | */ |
| 175 | public static function getExportable($type, array $args = array()) |
| 176 | { |
| 177 | if (!isset(static::$exportable[$type])) |
| 178 | { |
| 179 | static::$exportable[$type] = null; |
| 180 | |
| 181 | VAPLoader::import('libraries.import.exportable'); |
| 182 | |
| 183 | /** |
| 184 | * Trigger event to let the plugins include external drivers. |
| 185 | * The plugins should include the resources needed, otherwise |
| 186 | * it wouldn't be possible to instantiate the returned classes. |
| 187 | * |
| 188 | * @param string $driver The name of the driver to include. |
| 189 | * |
| 190 | * @return string The classname of the driver. |
| 191 | * |
| 192 | * @since 1.7 |
| 193 | */ |
| 194 | $classname = VAPFactory::getEventDispatcher()->triggerOnce('onFetchExportDriverClassname', array($type)); |
| 195 | |
| 196 | if (!$classname) |
| 197 | { |
| 198 | // in case the no plugins returned a valid classname, |
| 199 | // attempt to load one of the existing ones |
| 200 | VAPLoader::import('libraries.import.export.' . $type); |
| 201 | |
| 202 | $classname = str_replace('_', ' ', $type); |
| 203 | $classname = str_replace(' ', '', ucwords($classname)); |
| 204 | $classname = 'Exportable' . $classname; |
| 205 | } |
| 206 | |
| 207 | if (class_exists($classname)) |
| 208 | { |
| 209 | static::$exportable[$type] = new $classname($args); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return static::$exportable[$type]; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the list of all the supported exportable handlers. |
| 218 | * |
| 219 | * @param string $query The query to use. |
| 220 | * The character '*' obtains all the files. |
| 221 | * |
| 222 | * @return array The exportable list. |
| 223 | */ |
| 224 | public static function getExportList($query = '*') |
| 225 | { |
| 226 | if (!$query) |
| 227 | { |
| 228 | $query = '*'; |
| 229 | } |
| 230 | |
| 231 | $paths = array(); |
| 232 | |
| 233 | // include default internal path |
| 234 | $paths[] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'export'; |
| 235 | |
| 236 | /** |
| 237 | * Trigger event to let the plugins register external drivers |
| 238 | * as supported exporters. |
| 239 | * |
| 240 | * @return array A list of include paths. |
| 241 | * |
| 242 | * @since 1.7 |
| 243 | */ |
| 244 | $results = VAPFactory::getEventDispatcher()->trigger('onFetchExportIncludePaths'); |
| 245 | |
| 246 | foreach ($results as $tmp) |
| 247 | { |
| 248 | // merge default files with specified ones |
| 249 | $paths = array_merge($paths, (array) $tmp); |
| 250 | } |
| 251 | |
| 252 | // exclude duplicate |
| 253 | $paths = array_unique($paths); |
| 254 | |
| 255 | $pool = array(); |
| 256 | |
| 257 | // iterate all defined paths |
| 258 | foreach ($paths as $path) |
| 259 | { |
| 260 | // extract files contained within this folder |
| 261 | $files = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $query . '.php'); |
| 262 | |
| 263 | foreach ($files as $file) |
| 264 | { |
| 265 | // fetch driver type |
| 266 | $type = basename($file); |
| 267 | $type = substr($type, 0, strrpos($type, '.')); |
| 268 | |
| 269 | // create driver instance |
| 270 | $exportable = static::getExportable($type); |
| 271 | |
| 272 | if ($exportable) |
| 273 | { |
| 274 | $pool[$type] = $exportable; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Sort by driver name. |
| 281 | * |
| 282 | * @since 1.7 |
| 283 | */ |
| 284 | uasort($pool, function($a, $b) |
| 285 | { |
| 286 | return strcmp($a->getName(), $b->getName()); |
| 287 | }); |
| 288 | |
| 289 | return $pool; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns the JForm object used to display the additional |
| 294 | * parameters of the given export type. |
| 295 | * |
| 296 | * @param string $type The export type. |
| 297 | * @param array $args The data to bind. |
| 298 | * |
| 299 | * @return mixed The form object on success, otherwise false. |
| 300 | */ |
| 301 | public static function getExportableForm($type, array $args = array()) |
| 302 | { |
| 303 | /** |
| 304 | * Trigger event to let the plugins include returns the correct |
| 305 | * file in which the configuration of the driver is located. |
| 306 | * |
| 307 | * @param string $driver The name of the driver. |
| 308 | * |
| 309 | * @return string The path of the XML file. |
| 310 | * |
| 311 | * @since 1.7 |
| 312 | */ |
| 313 | $path = VAPFactory::getEventDispatcher()->triggerOnce('onFetchExportDriverFormXML', array($type)); |
| 314 | |
| 315 | if (!$path || !is_file($path)) |
| 316 | { |
| 317 | // no plugins returned a custom XML file, attempt |
| 318 | // to load it from the default folder |
| 319 | $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'export' . DIRECTORY_SEPARATOR . $type . '.xml'; |
| 320 | } |
| 321 | |
| 322 | // check if the XML fieldset exists |
| 323 | if (!is_file($path)) |
| 324 | { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Auto-wrap the form fields within import_args[] array without |
| 330 | * having to specify it from the XML file. |
| 331 | * |
| 332 | * @since 1.7 |
| 333 | */ |
| 334 | $options = array( |
| 335 | 'control' => 'import_args', |
| 336 | ); |
| 337 | |
| 338 | // try to load the form |
| 339 | $form = JForm::getInstance('exportable' . $type, $path, $options); |
| 340 | |
| 341 | if (!$form) |
| 342 | { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | // inject the form values |
| 347 | $form->bind($args); |
| 348 | |
| 349 | return $form; |
| 350 | } |
| 351 | } |
| 352 |