PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / order / export / factory.php
vikappointments / site / helpers / libraries / order / export Last commit date
drivers 3 days ago driver.php 3 days ago factory.php 3 days ago index.html 3 days ago
factory.php
253 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 VAPLoader::import('libraries.order.export.driver');
15
16 /**
17 * Factory class used to export the orders by using the specifications
18 * of the requested driver.
19 *
20 * @since 1.7
21 */
22 abstract class VAPOrderExportFactory
23 {
24 /**
25 * Returns a list of supported drivers.
26 *
27 * @param string $group The section to which the orders belong.
28 * @param boolean $object True to return the instances instead of
29 * the title of the drivers.
30 *
31 * @return array An associative array of drivers.
32 *
33 * @uses getInstance()
34 */
35 public static function getSupportedDrivers($group, $object = false)
36 {
37 $drivers = array();
38
39 // get default drivers
40 $files = glob(VAPLIB . DIRECTORY_SEPARATOR . 'order' . DIRECTORY_SEPARATOR . 'export' . DIRECTORY_SEPARATOR . 'drivers' . DIRECTORY_SEPARATOR . '*.php');
41
42 // iterate files
43 foreach ($files as $i => $file)
44 {
45 // keep the filename only
46 $files[$i] = basename($file);
47 }
48
49 /**
50 * Trigger event to let the plugins register external drivers
51 * as supported exporters.
52 *
53 * @return array A list of supported drivers.
54 *
55 * @since 1.7
56 */
57 $list = VAPFactory::getEventDispatcher()->trigger('onFetchSupportedExportDrivers');
58
59 foreach ($list as $chunk)
60 {
61 // merge default files with specified ones
62 $files = array_merge($files, (array) $chunk);
63 }
64
65 // iterate files
66 foreach ($files as $file)
67 {
68 try
69 {
70 // try to instantiate the driver
71 $driver = static::getInstance($file, $group);
72
73 // make sure the driver supports the group
74 if ($driver->isSupported($group))
75 {
76 if ($object === true)
77 {
78 // register instance
79 $drivers[$driver->getName()] = $driver;
80 }
81 else
82 {
83 // register driver title only
84 $drivers[$driver->getName()] = $driver->getTitle();
85 }
86 }
87 }
88 catch (Exception $e)
89 {
90 // driver not supported
91 }
92 }
93
94 // sort drivers alphabetically
95 uasort($drivers, function($a, $b)
96 {
97 if ($a instanceof VAPOrderExportDriver)
98 {
99 // use title in case of driver instance
100 $a = $a->getTitle();
101 }
102
103 if ($b instanceof VAPOrderExportDriver)
104 {
105 // use title in case of driver instance
106 $b = $b->getTitle();
107 }
108
109 // compare as normal strings
110 return strcasecmp($a, $b);
111 });
112
113 return $drivers;
114 }
115
116 /**
117 * Returns the export driver instance, ready for the usage.
118 * Searches for any arguments set in the request required
119 * by the export driver.
120 *
121 * @param string $driver The export driver name.
122 * @param string $group The section to which the orders belong.
123 * @param mixed $options Either an array or an object of options to be passed
124 * to the order instance.
125 *
126 * @return VAPOrderExportDriver
127 *
128 * @throws Exception
129 *
130 * @uses getInstance()
131 */
132 public static function getDriver($driver, $group, $options = array())
133 {
134 // get driver first
135 $driver = static::getInstance($driver, $group, $options);
136
137 $input = JFactory::getApplication()->input;
138
139 // iterate form arguments
140 foreach ($driver->getForm() as $k => $field)
141 {
142 // use registry for ease of use
143 $field = new JRegistry($field);
144
145 // validate field filter
146 if ($field->get('multiple') == 1)
147 {
148 // retrieve an array
149 $filter = 'array';
150 }
151 else
152 {
153 // retrieve a string otherwise
154 $filter = 'string';
155 }
156
157 // get value from request (use the "export_" prefix)
158 $value = $input->get('export_' . $k, null, $filter);
159
160 // cast to integer in case of checkbox,
161 // so that we can use "0" instead of NULL
162 if ($field->get('type') == 'checkbox')
163 {
164 $value = (int) $value;
165 }
166
167 // make sure we have a value in the request
168 if ($value !== null)
169 {
170 // Push value within the options.
171 // Any value previously set will be overwritten.
172 $driver->setOption($k, $value);
173 }
174
175 // check if the field was mandatory
176 if ($field->get('required') == 1)
177 {
178 // get value from driver configuration
179 $value = $driver->getOption($k, null);
180
181 // make sure the value exists
182 if ($value === null || $value === array())
183 {
184 // missing field, throw exception
185 throw new Exception(sprintf('Export driver missing required [%s] field', $k), 400);
186 }
187 }
188 }
189
190 return $driver;
191 }
192
193 /**
194 * Returns the export driver instance.
195 *
196 * @param string $driver The export driver name.
197 * @param string $group The section to which the orders belong.
198 * @param mixed $options Either an array or an object of options to be passed
199 * to the order instance.
200 *
201 * @return VAPOrderExportDriver
202 *
203 * @throws Exception
204 */
205 public static function getInstance($driver, $group, $options = array())
206 {
207 // remove file extension if provided
208 $driver = preg_replace("/\.php$/", '', $driver);
209
210 /**
211 * Trigger event to let the plugins include external drivers.
212 * The plugins MUST include the resources needed, otherwise
213 * it wouldn't be possible to instantiate the returned classes.
214 *
215 * @param string $driver The name of the driver to include.
216 *
217 * @return string The classname of the driver.
218 *
219 * @since 1.7
220 */
221 $classname = VAPFactory::getEventDispatcher()->triggerOnce('onFetchExportDriverClassname', array($driver));
222
223 if (!$classname)
224 {
225 // load handler class from default folder
226 if (!VAPLoader::import('libraries.order.export.drivers.' . $driver))
227 {
228 throw new Exception(sprintf('Order export driver [%s] not found', $driver), 404);
229 }
230
231 // create class name
232 $classname = 'VAPOrderExportDriver' . ucfirst($driver);
233 }
234
235 // make sure the class handler exists
236 if (!class_exists($classname))
237 {
238 throw new Exception(sprintf('Order export class [%s] does not exist', $classname), 404);
239 }
240
241 // instantiate handler
242 $driver = new $classname($group, $options);
243
244 // make sure the driver is a valid instance
245 if (!$driver instanceof VAPOrderExportDriver)
246 {
247 throw new Exception(sprintf('Order export class [%s] is not a valid instance', $classname), 500);
248 }
249
250 return $driver;
251 }
252 }
253