driver.php
353 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 | * Driver class used to handle a specific orders export function. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | abstract class VAPOrderExportDriver |
| 20 | { |
| 21 | /** |
| 22 | * Group identifier. |
| 23 | * The property is private so that it cannot be |
| 24 | * modified at runtime by the children classes. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private $group; |
| 29 | |
| 30 | /** |
| 31 | * A registry of options. |
| 32 | * |
| 33 | * @var JObject |
| 34 | */ |
| 35 | protected $options; |
| 36 | |
| 37 | /** |
| 38 | * Class constructor. |
| 39 | * |
| 40 | * @param string $group The section to which the orders belong. |
| 41 | * @param mixed $options Either an array or an object of options to be passed |
| 42 | * to the order instance. |
| 43 | */ |
| 44 | public function __construct($group, $options = array()) |
| 45 | { |
| 46 | $this->group = $group; |
| 47 | $this->options = new JObject($options); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the driver name/identifier. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public function getName() |
| 56 | { |
| 57 | // get current class name |
| 58 | $class = get_class($this); |
| 59 | |
| 60 | // extract driver name from class |
| 61 | if (preg_match("/^VAPOrderExportDriver([a-z0-9_]+)$/i", $class, $match)) |
| 62 | { |
| 63 | // return driver name (lowercase) |
| 64 | return strtolower(end($match)); |
| 65 | } |
| 66 | |
| 67 | // the driver doesn't follow the standard notation, return full class name |
| 68 | return $class; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the driver title. |
| 73 | * By default, the title is a translatable string built |
| 74 | * in the following format: VAP_ORDER_EXPORT_DRIVER_[NAME]. |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function getTitle() |
| 79 | { |
| 80 | // get driver name (UPPERCASE) |
| 81 | $driver = strtoupper($this->getName()); |
| 82 | |
| 83 | // build language key |
| 84 | $key = 'VAP_ORDER_EXPORT_DRIVER_' . $driver; |
| 85 | |
| 86 | // try to translate the title |
| 87 | $title = JText::translate($key); |
| 88 | |
| 89 | // check if the description is equals to language key |
| 90 | if ($title === $key) |
| 91 | { |
| 92 | // missing translation, return driver name |
| 93 | return $driver; |
| 94 | } |
| 95 | |
| 96 | // return translated title instead |
| 97 | return $title; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the driver description. |
| 102 | * By default, the description is a translatable string built |
| 103 | * in the following format: VAP_ORDER_EXPORT_DRIVER_[NAME]_DESC. |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | public function getDescription() |
| 108 | { |
| 109 | // build language key |
| 110 | $key = 'VAP_ORDER_EXPORT_DRIVER_' . strtoupper($this->getName()) . '_DESC'; |
| 111 | |
| 112 | // try to translate the description |
| 113 | $desc = JText::translate($key); |
| 114 | |
| 115 | // check if the description is equals to language key |
| 116 | if ($desc === $key) |
| 117 | { |
| 118 | // missing translation, return empty description |
| 119 | return ''; |
| 120 | } |
| 121 | |
| 122 | // return translated description instead |
| 123 | return $desc; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Checks whether the specified group matches the one set |
| 128 | * in the driver properties. |
| 129 | * |
| 130 | * @param string $group The group to check. |
| 131 | * |
| 132 | * @return boolean True if matching, false otherwise. |
| 133 | */ |
| 134 | public function isGroup($group) |
| 135 | { |
| 136 | return !strcasecmp($this->group, $group); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Checks whether the specified group is supported by the |
| 141 | * export driver. Children classes can override this method |
| 142 | * to drop the support for a specific group. |
| 143 | * |
| 144 | * @param string $group The group to check. |
| 145 | * |
| 146 | * @return boolean True if supported, false otherwise. |
| 147 | */ |
| 148 | public function isSupported($group) |
| 149 | { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Updates the options of the registry. |
| 155 | * |
| 156 | * @param mixed $options Either an array or an object of options to be passed |
| 157 | * to the order instance. |
| 158 | * |
| 159 | * @return self This object to support chaining. |
| 160 | */ |
| 161 | public function setOptions($options = array()) |
| 162 | { |
| 163 | $this->options->setProperties($options); |
| 164 | |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Updates or insert a value within the configuration. |
| 170 | * |
| 171 | * @param string $key The option key. |
| 172 | * @param mixed $val The option value. |
| 173 | * |
| 174 | * @return self This object to support chaining. |
| 175 | */ |
| 176 | public function setOption($key, $val) |
| 177 | { |
| 178 | $this->options->set($key, $val); |
| 179 | |
| 180 | return $this; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the configuration options |
| 185 | * |
| 186 | * @return array |
| 187 | */ |
| 188 | public function getOptions() |
| 189 | { |
| 190 | // get all properties |
| 191 | return $this->options->getProperties(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns a configuration option. |
| 196 | * |
| 197 | * @param string $key The option key. |
| 198 | * @param mixed $def The default value. |
| 199 | * |
| 200 | * @return mixed The option value if exists, the default value otherwise. |
| 201 | */ |
| 202 | public function getOption($key, $def = null) |
| 203 | { |
| 204 | return $this->options->get($key, $def); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Override this method to return a list of |
| 209 | * arguments required for the driver. |
| 210 | * |
| 211 | * @return array |
| 212 | */ |
| 213 | public function getForm() |
| 214 | { |
| 215 | // construct the default driver form |
| 216 | $form = $this->buildForm(); |
| 217 | |
| 218 | // extend the form with hooks |
| 219 | $dispatcher = VAPFactory::getEventDispatcher(); |
| 220 | |
| 221 | /** |
| 222 | * Trigger event to allow the plugins to manipulate the parameters |
| 223 | * form used by this export driver. |
| 224 | * |
| 225 | * @param array &$form A configuration array. |
| 226 | * @param mixed $handler The export driver instance. |
| 227 | * |
| 228 | * @return void |
| 229 | * |
| 230 | * @since 1.7 |
| 231 | */ |
| 232 | $dispatcher->trigger('onBuildParametersForm' . strtoupper($this->getName()), array(&$form, $this)); |
| 233 | |
| 234 | return $form; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Override this method to construct a list of |
| 239 | * arguments required for the driver. |
| 240 | * |
| 241 | * @return array |
| 242 | */ |
| 243 | protected function builForm() |
| 244 | { |
| 245 | return array(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns the value of the parameters used the |
| 250 | * last time this driver was invoked. |
| 251 | * |
| 252 | * @return array |
| 253 | */ |
| 254 | public function getParams() |
| 255 | { |
| 256 | // get JSON drivers params |
| 257 | $params = VAPFactory::getConfig()->getArray('exportresparams'); |
| 258 | |
| 259 | // get driver identifier |
| 260 | $driver = $this->getName(); |
| 261 | |
| 262 | // check if the driver was ever used |
| 263 | if (!isset($params[$driver])) |
| 264 | { |
| 265 | // driver never used |
| 266 | return array(); |
| 267 | } |
| 268 | |
| 269 | // first of all, take driver params only |
| 270 | $params = $params[$driver]; |
| 271 | |
| 272 | // check if the driver was used for the specified group |
| 273 | if (isset($params[$this->group])) |
| 274 | { |
| 275 | // return configuration |
| 276 | return $params[$this->group]; |
| 277 | } |
| 278 | |
| 279 | // otherwise use first available configuration |
| 280 | return (array) reset($params); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Saves the last used parameters of the driver |
| 285 | * within the configuration. |
| 286 | * |
| 287 | * @return void |
| 288 | */ |
| 289 | public function saveParams() |
| 290 | { |
| 291 | $config = VAPFactory::getConfig(); |
| 292 | |
| 293 | // get JSON drivers params |
| 294 | $params = $config->getArray('exportresparams'); |
| 295 | |
| 296 | if (!is_array($params)) |
| 297 | { |
| 298 | // create from scratch |
| 299 | $params = array(); |
| 300 | } |
| 301 | |
| 302 | // get driver identifier |
| 303 | $driver = $this->getName(); |
| 304 | |
| 305 | // check if the driver was ever used |
| 306 | if (!isset($params[$driver])) |
| 307 | { |
| 308 | // create array for this driver |
| 309 | $params[$driver] = array(); |
| 310 | } |
| 311 | |
| 312 | // create/update array for current group |
| 313 | $params[$driver][$this->group] = array(); |
| 314 | |
| 315 | // iterate the driver form to save only the internal parameters |
| 316 | foreach ($this->getForm() as $k => $field) |
| 317 | { |
| 318 | // set driver param |
| 319 | $params[$driver][$this->group][$k] = $this->getOption($k); |
| 320 | } |
| 321 | |
| 322 | // update configuration |
| 323 | $config->set('exportresparams', $params); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Megic method to return the driver name |
| 328 | * when the object is casted to string. |
| 329 | * |
| 330 | * @return string |
| 331 | */ |
| 332 | public function __toString() |
| 333 | { |
| 334 | return $this->getName(); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Exports the orders in the given format. |
| 339 | * |
| 340 | * @return string The resulting export string. |
| 341 | */ |
| 342 | abstract public function export(); |
| 343 | |
| 344 | /** |
| 345 | * Downloads the orders in a file compatible with the given format. |
| 346 | * |
| 347 | * @param string $filename The name of the file that will be downloaded. |
| 348 | * |
| 349 | * @return void |
| 350 | */ |
| 351 | abstract public function download($filename = null); |
| 352 | } |
| 353 |