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