/
AbstractProvider.php
253 lines (198 loc) · 10.6 KB
/
AbstractProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* @license GPLv3, http://www.gnu.org/copyleft/gpl.html
* @copyright Metaways Infosystems GmbH, 2014
* @copyright Aimeos (aimeos.org), 2014-2016
* @package TYPO3
*/
namespace Aimeos\Aimeos\Scheduler\Provider;
use Aimeos\Aimeos\Base;
use Aimeos\Aimeos\Scheduler;
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
/**
* Common methods for Aimeos' additional field providers.
*
* @package TYPO3
*/
abstract class AbstractProvider extends \TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider
{
private $fieldSite = 'aimeos_sitecode';
private $fieldController = 'aimeos_controller';
private $fieldTSconfig = 'aimeos_config';
/**
* Fields generation.
* This method is used to define new fields for adding or editing a task
* In this case, it adds a page ID field
*
* @param array $taskInfo Reference to the array containing the info used in the add/edit form
* @param object $task When editing, reference to the current task object. Null when adding.
* @param object $parentObject Reference to the calling object (Scheduler's BE module)
* @return array Array containg all the information pertaining to the additional fields
* The array is multidimensional, keyed to the task class name and each field's id
* For each field it provides an associative sub-array with the following:
* ['code'] => The HTML code for the field
* ['label'] => The label of the field (possibly localized)
* ['cshKey'] => The CSH key for the field
* ['cshLabel'] => The code of the CSH label
*/
protected function getFields(array &$taskInfo, $task, $parentObject)
{
$additionalFields = [];
// In case of editing a task, set to the internal value if data wasn't already submitted
if( empty( $taskInfo[$this->fieldController] ) && $parentObject->getCurrentAction()->equals( Action::EDIT ) ) {
$taskInfo[$this->fieldController] = $task->{$this->fieldController} ?? [];
}
$taskInfo[$this->fieldController] = (array) ($taskInfo[$this->fieldController] ?? []);
$fieldCode = sprintf('<select class="form-control" name="tx_scheduler[%1$s][]" id="%1$s" multiple="multiple" size="10" />', $this->fieldController);
$fieldCode .= $this->getControllerOptions($taskInfo[$this->fieldController]);
$fieldCode .= '</select>';
$additionalFields[$this->fieldController] = [
'code' => $fieldCode,
'label' => 'LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.label.controller',
'cshKey' => 'xMOD_tx_aimeos',
'cshLabel' => $this->fieldController
];
// In case of editing a task, set to the internal value if data wasn't already submitted
if( empty( $taskInfo[$this->fieldSite] ) && $parentObject->getCurrentAction()->equals( Action::EDIT ) ) {
$taskInfo[$this->fieldSite] = $task->{$this->fieldSite} ?? [];
}
$taskInfo[$this->fieldSite] = (array) ($taskInfo[$this->fieldSite] ?? []);
$fieldCode = sprintf('<select class="form-control" name="tx_scheduler[%1$s][]" id="%1$s" multiple="multiple" size="10" />', $this->fieldSite);
$fieldCode .= $this->getSiteOptions($this->getAvailableSites(), $taskInfo[$this->fieldSite], 0);
$fieldCode .= '</select>';
$additionalFields[$this->fieldSite] = [
'code' => $fieldCode,
'label' => 'LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.label.sitecode',
'cshKey' => 'xMOD_tx_aimeos',
'cshLabel' => $this->fieldSite
];
// In case of editing a task, set to the internal value if data wasn't already submitted
if (empty($taskInfo[$this->fieldTSconfig]) && $parentObject->getCurrentAction()->equals(Action::EDIT)) {
$taskInfo[$this->fieldTSconfig] = $task->{$this->fieldTSconfig} ?? '';
}
$taskInfo[$this->fieldTSconfig] = htmlspecialchars( $taskInfo[$this->fieldTSconfig] ?? '', ENT_QUOTES, 'UTF-8' );
$fieldStr = '<textarea class="form-control" name="tx_scheduler[%1$s]" id="%1$s" rows="20" cols="80" >%2$s</textarea>';
$fieldCode = sprintf($fieldStr, $this->fieldTSconfig, $taskInfo[$this->fieldTSconfig]);
$additionalFields[$this->fieldTSconfig] = [
'code' => $fieldCode,
'label' => 'LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.label.tsconfig',
'cshKey' => 'xMOD_tx_aimeos',
'cshLabel' => $this->fieldTSconfig
];
return $additionalFields;
}
/**
* Store fields.
* This method is used to save any additional input into the current task object
* if the task class matches
*
* @param array $submittedData Array containing the data submitted by the user
* @param object $task Reference to the current task object
*/
protected function saveFields(array $submittedData, $task)
{
$task->{$this->fieldSite} = $submittedData[$this->fieldSite] ?? '';
$task->{$this->fieldController} = $submittedData[$this->fieldController] ?? '';
$task->{$this->fieldTSconfig} = $submittedData[$this->fieldTSconfig] ?? '';
}
/**
* Fields validation.
* This method checks if page id given in the 'Hide content' specific task is int+
* If the task class is not relevant, the method is expected to return true
*
* @param array $submittedData Reference to the array containing the data submitted by the user
* @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
* @return boolean True if validation was ok (or selected class is not relevant), false otherwise
*/
protected function validateFields( array &$submittedData, $parentObject )
{
if( count( (array) $submittedData[$this->fieldController] ?? [] ) < 1 ) {
throw new \InvalidArgumentException( $GLOBALS['LANG']->sL( 'LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.error.controller.missing' ) );
}
if( count( (array) $submittedData[$this->fieldSite] ?? [] ) < 1 ) {
throw new \InvalidArgumentException( $GLOBALS['LANG']->sL( 'LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.error.sitecode.missing' ) );
}
Base::parseTS( $submittedData[$this->fieldTSconfig] ?? '' );
$context = Scheduler\Base::context();
$submittedData[$this->fieldSite] = array_unique( (array) $submittedData[$this->fieldSite] ?? [] );
$siteItems = Scheduler\Base::getSiteItems( $context, $submittedData[$this->fieldSite] );
if (count($siteItems) !== count($submittedData[$this->fieldSite])) {
throw new \RuntimeException($GLOBALS['LANG']->sL('LLL:EXT:aimeos/Resources/Private/Language/scheduler.xlf:default.error.sitecode'));
}
$aimeos = Base::aimeos();
$cntlPaths = $aimeos->getCustomPaths( 'controller/jobs' );
$submittedData[$this->fieldController] = array_unique( (array) $submittedData[$this->fieldController] ?? [] );
foreach ($submittedData[$this->fieldController] as $name) {
\Aimeos\Controller\Jobs::create($context, $aimeos, $name);
}
return true;
}
/**
* Returns the list of site trees.
*
* @return \Aimeos\Map Associative list of items and children implementing \Aimeos\MShop\Locale\Item\Site\Iface
*/
protected function getAvailableSites() : \Aimeos\Map
{
$manager = \Aimeos\MShop::create(Scheduler\Base::context(), 'locale/site');
$search = $manager->filter();
$search->setConditions($search->compare('==', 'locale.site.level', 0));
$search->setSortations(array($search->sort('+', 'locale.site.label')));
$sites = $manager->search($search);
foreach ($sites as $id => $siteItem) {
$sites[$id] = $manager->getTree($id, [], \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE);
}
return $sites;
}
/**
* Returns the HTML code for the select control.
* The method adds every site and its children recursively.
*
* @param \Aimeos\Map $siteItems List of items implementing \Aimeos\MShop\Locale\Item\Site\Iface
* @param array $selected List of site codes that were previously selected by the user
* @param int $level Nesting level of the sites (should start with 0)
* @return string HTML code with <option> tags for the select box
*/
protected function getSiteOptions(\Aimeos\Map $siteItems, array $selected, int $level) : string
{
$html = '';
$prefix = str_repeat('-', $level) . ' ';
foreach ($siteItems as $item) {
$active = (in_array($item->getCode(), $selected) ? 'selected="selected"' : '');
$disabled = ($item->getStatus() > 0 ? '' : 'disabled="disabled"');
$string = '<option value="%1$s" %2$s %3$s>%4$s</option>';
$html .= sprintf($string, $item->getCode(), $active, $disabled, $prefix . $item->getLabel());
$html .= $this->getSiteOptions($item->getChildren(), $selected, $level + 1);
}
return $html;
}
/**
* Returns the HTML code for the controller control.
*
* @param array $selected List of site codes that were previously selected by the user
* @return string HTML code with <option> tags for the select box
*/
protected function getControllerOptions(array $selected) : string
{
$html = '';
$aimeos = Base::aimeos();
$context = Scheduler\Base::context();
$cntlPaths = $aimeos->getCustomPaths('controller/jobs');
$langid = 'en';
if (isset($GLOBALS['BE_USER']->uc['lang']) && !in_array($GLOBALS['BE_USER']->uc['lang'], ['', 'default'])) {
$langid = $GLOBALS['BE_USER']->uc['lang'];
}
$localeItem = \Aimeos\MShop::create($context, 'locale')->create();
$localeItem->setLanguageId($langid);
$context->setLocale($localeItem);
$controllers = \Aimeos\Controller\Jobs::get($context, $aimeos, $cntlPaths);
foreach ($controllers as $name => $controller) {
$active = (in_array($name, $selected) ? 'selected="selected"' : '');
$title = htmlspecialchars($controller->getDescription(), ENT_QUOTES, 'UTF-8');
$cntl = htmlspecialchars(sprintf('%1$s: %2$s', $name, $controller->getName()), ENT_QUOTES, 'UTF-8');
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
$html .= sprintf('<option value="%1$s" title="%2$s" %3$s>%4$s</option>', $name, $title, $active, $cntl);
}
return $html;
}
}