PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.15
VikBooking Hotel Booking Engine & PMS v1.8.15
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 1.7.3 All 36 releases
vikbooking / admin / helpers / src / help / wizard / trait / report / configurable.php

configurable.php in VikBooking Hotel Booking Engine & PMS 1.8.15, at admin/helpers/src/help/wizard/trait/report/configurable.php

256 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
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 * Wizard help instruction configurable report trait.
16 *
17 * @since 1.18.2 (J) - 1.8.2 (WP)
18 */
19 trait VBOHelpWizardTraitReportConfigurable
20 {
21 /**
22 * The real class name of the report implementor (see reports folder).
23 *
24 * @var string
25 */
26 protected $reportId;
27
28 /**
29 * Checks whether the report supports an auto-export feature.
30 *
31 * @var bool
32 */
33 protected $isAutoExportSupported = true;
34
35 /**
36 * The format that will be used to auto-export the report.
37 *
38 * @var string
39 */
40 protected $autoExportFormat;
41
42 /**
43 * The payload that will be used to auto-export the report.
44 *
45 * @var array
46 */
47 protected $autoExportPayload;
48
49 /**
50 * Flag used to check whether the report already owns a basic setup.
51 *
52 * @var bool
53 */
54 private $hasSettingsConfigured = null;
55
56 /**
57 * Flag used to check whether the report already owns a configured cron job to auto-transmit the contents.
58 *
59 * @var bool
60 */
61 private $hasAutoExport = null;
62
63 /**
64 * @inheritDoc
65 *
66 * @see VBOHelpWizardInstruction::isProcessable()
67 */
68 public function isProcessable(?string &$btnText = null)
69 {
70 if ($this->isAutoExportSupported && !$this->hasAutoExport()) {
71 $btnText = JText::translate('VBO_SCHEDULE_CRONJOB');
72 }
73
74 if (!$this->hasSettingsConfigured()) {
75 $btnText = JText::translate('VBCONFIGURETASK');
76 }
77
78 return true;
79 }
80
81 /**
82 * @inheritDoc
83 *
84 * @see VBOHelpWizardInstruction::isConfigured()
85 */
86 public function isConfigured()
87 {
88 // check default configuration first
89 if (!$this->hasSettingsConfigured()) {
90 return false;
91 }
92
93 // in case the report supports an auto-export feature, check whether a cron job has been created
94 if ($this->isAutoExportSupported && !$this->hasAutoExport()) {
95 return false;
96 }
97
98 return true;
99 }
100
101 /**
102 * @inheritDoc
103 *
104 * @see VBOHelpWizardInstruction::process()
105 */
106 public function process(array $args = [])
107 {
108 if (!$this->hasSettingsConfigured()) {
109 return [
110 // open the page to configure the report settings
111 'redirect' => VBOFactory::getPlatform()->getUri()->admin('index.php?option=com_vikbooking&task=pmsreports&report=' . $this->reportId . '#settings', false),
112 ];
113 }
114
115 if ($this->isAutoExportSupported && !$this->hasAutoExport()) {
116
117 $this->preflight();
118
119 // create a new auto-export cron job for this report
120 $cronId = $this->saveAutoExport($this->autoExportFormat, $this->autoExportPayload);
121
122 $this->postflight();
123
124 return [
125 // redirect to the page of the newly created cron job
126 'redirect' => VBOFactory::getPlatform()->getUri()->admin('index.php?option=com_vikbooking&task=cronjob.edit&cid[]=' . $cronId, false),
127 ];
128 }
129 }
130
131 /**
132 * @inheritDoc
133 *
134 * @see VBOHelpWizardInstructionaware::getLayoutData()
135 */
136 protected function getLayoutData()
137 {
138 return [
139 'configured' => $this->hasSettingsConfigured(),
140 'autoexport' => $this->isAutoExportSupported,
141 ];
142 }
143
144 /**
145 * Checks whether the report already owns a basic setup.
146 *
147 * @return bool
148 */
149 protected function hasSettingsConfigured()
150 {
151 if (is_null($this->hasSettingsConfigured)) {
152 $this->hasSettingsConfigured = $this->checkSettingsConfigured();
153 }
154
155 return $this->hasSettingsConfigured;
156 }
157
158 /**
159 * Checks whether the report already owns a configured cron job to auto-transmit the contents.
160 *
161 * @return bool
162 */
163 protected function hasAutoExport()
164 {
165 if (is_null($this->hasAutoExport)) {
166 $this->hasAutoExport = (bool) VBOHelpWizardHelperCron::getAutoExport($this->reportId);
167 }
168
169 return $this->hasAutoExport;
170 }
171
172 /**
173 * Saves the auto-export report cron job.
174 *
175 * @param string $format
176 * @param array $payload
177 *
178 * @return int The cron ID.
179 *
180 * @throws RuntimeException
181 */
182 protected function saveAutoExport(string $format, array $payload)
183 {
184 if (empty($this->autoExportFormat)) {
185 throw new UnexpectedValueException('Missing auto-export format', 400);
186 }
187
188 // extract name from report ID
189 $name = ucwords(str_replace('_', ' ', $this->reportId)) . ' - ';
190
191 if (preg_match("/^[a-z]{2,2}\s/i", $name)) {
192 // get rid of the local country
193 $name = substr($name, 3);
194 }
195
196 // append the format type to the name
197 $name .= ucfirst(preg_replace("/([a-z])([A-Z])/", '$1 $2', $format));
198
199 // create the cron job
200 $cron = new stdClass;
201 $cron->cron_name = $name;
202 $cron->class_file = 'report_auto_exporter';
203 $cron->published = 1;
204 $cron->params = json_encode([
205 'report' => $this->reportId,
206 'format' => $format,
207 'payload' => json_encode($payload, JSON_PRETTY_PRINT),
208 'test_mode' => 0,
209 'recipient_email' => JFactory::getUser()->email,
210 'save_path' => JFactory::getApplication()->get('tmp_path'),
211 'rm_file' => 1,
212 ]);
213
214 $cronId = (new VBOModelCronjob)->save($cron);
215
216 // insert the cron job
217 if (!$cronId) {
218 throw new RuntimeException('Unable to save the pre-checkin cron!', 500);
219 }
220
221 return $cronId;
222 }
223
224 /**
225 * Runs some extra code before saving the auto-export cron job.
226 * Classes that use this trait can implement this method to execute
227 * custom preflight code.
228 *
229 * @return void
230 */
231 protected function preflight()
232 {
233 // do nothing by default
234 }
235
236 /**
237 * Runs some extra code after saving the auto-export cron job.
238 * Classes that use this trait can implement this method to execute
239 * custom postflight code.
240 *
241 * @return void
242 */
243 protected function postflight()
244 {
245 // do nothing by default
246 }
247
248 /**
249 * Concrete implementation to check whether the report has configuration settings
250 * properly set up.
251 *
252 * @return bool
253 */
254 abstract protected function checkSettingsConfigured();
255 }
256