Total Complexity | 42 |
Total Lines | 271 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaseAdditionalFieldProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseAdditionalFieldProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class BaseAdditionalFieldProvider implements AdditionalFieldProviderInterface |
||
33 | { |
||
34 | /** |
||
35 | * Gets additional fields to render in the form to add/edit a task |
||
36 | * |
||
37 | * @param array $taskInfo Values of the fields from the add/edit task form |
||
38 | * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task The task object being edited. Null when adding a task! |
||
39 | * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module |
||
40 | * @return array A two dimensional array, array('Identifier' => array('fieldId' => array('code' => '', 'label' => '', 'cshKey' => '', 'cshLabel' => '')) |
||
41 | */ |
||
42 | public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule) |
||
43 | { |
||
44 | return []; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Validates the additional fields' values |
||
49 | * |
||
50 | * @param array $submittedData An array containing the data submitted by the add/edit task form |
||
51 | * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $schedulerModule Reference to the scheduler backend module |
||
52 | * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise |
||
53 | */ |
||
54 | public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule) |
||
55 | { |
||
56 | $fieldsValid = true; |
||
57 | |||
58 | Helper::getLanguageService()->includeLLFile('EXT:dlf/Resources/Private/Language/locallang_tasks.xlf'); |
||
59 | |||
60 | $messageTitle = Helper::getLanguageService()->getLL('additionalFields.error'); |
||
61 | $messageSeverity = FlashMessage::ERROR; |
||
62 | |||
63 | if (isset($submittedData['doc']) && empty($submittedData['doc'])) { |
||
64 | Helper::addMessage( |
||
65 | Helper::getLanguageService()->getLL('additionalFields.doc') . ' ' . Helper::getLanguageService()->getLL('additionalFields.valid'), |
||
66 | $messageTitle, |
||
67 | $messageSeverity, |
||
68 | true, |
||
69 | 'core.template.flashMessages' |
||
70 | ); |
||
71 | $fieldsValid = false; |
||
72 | } |
||
73 | |||
74 | if ((isset($submittedData['pid']) && (int) $submittedData['pid'] <= 0) || !isset($submittedData['pid'])) { |
||
75 | Helper::addMessage( |
||
76 | Helper::getLanguageService()->getLL('additionalFields.pid') . ' ' . Helper::getLanguageService()->getLL('additionalFields.valid'), |
||
77 | $messageTitle, |
||
78 | $messageSeverity, |
||
79 | true, |
||
80 | 'core.template.flashMessages' |
||
81 | ); |
||
82 | $fieldsValid = false; |
||
83 | } |
||
84 | |||
85 | if (!$submittedData['uid']) { |
||
86 | $messageTitle = Helper::getLanguageService()->getLL('additionalFields.warning'); |
||
87 | $messageSeverity = FlashMessage::WARNING; |
||
88 | } |
||
89 | |||
90 | if ((isset($submittedData['lib']) && (int) $submittedData['lib'] <= 0)) { |
||
91 | Helper::addMessage( |
||
92 | Helper::getLanguageService()->getLL('additionalFields.lib') . ' ' . Helper::getLanguageService()->getLL('additionalFields.valid'), |
||
93 | $messageTitle, |
||
94 | $messageSeverity, |
||
95 | true, |
||
96 | 'core.template.flashMessages' |
||
97 | ); |
||
98 | $fieldsValid = false; |
||
99 | } |
||
100 | |||
101 | if ((isset($submittedData['solr']) && (int) $submittedData['solr'] <= 0) || !isset($submittedData['solr'])) { |
||
102 | Helper::addMessage( |
||
103 | Helper::getLanguageService()->getLL('additionalFields.solr') . ' ' . Helper::getLanguageService()->getLL('additionalFields.valid'), |
||
104 | $messageTitle, |
||
105 | $messageSeverity, |
||
106 | true, |
||
107 | 'core.template.flashMessages' |
||
108 | ); |
||
109 | $fieldsValid = false; |
||
110 | } |
||
111 | |||
112 | if (((isset($submittedData['coll']) && isset($submittedData['all'])) || (!isset($submittedData['coll']) && !isset($submittedData['all']))) |
||
113 | && !isset($submittedData['doc']) && !isset($submittedData['lib'])) { |
||
114 | Helper::addMessage( |
||
115 | Helper::getLanguageService()->getLL('additionalFields.collOrAll'), |
||
116 | $messageTitle, |
||
117 | $messageSeverity, |
||
118 | true, |
||
119 | 'core.template.flashMessages' |
||
120 | ); |
||
121 | $fieldsValid = false; |
||
122 | } |
||
123 | return $fieldsValid; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Takes care of saving the additional fields' values in the task's object |
||
128 | * |
||
129 | * @param array $submittedData An array containing the data submitted by the add/edit task form |
||
130 | * @param BaseTask $task Reference to the scheduler backend module |
||
131 | * @return void |
||
132 | */ |
||
133 | public function saveAdditionalFields(array $submittedData, AbstractTask $task) |
||
134 | { |
||
135 | /** @var BaseTask $task */ |
||
136 | $task->setDryRun(!empty($submittedData['dryRun'])); |
||
137 | if (isset($submittedData['doc'])) { |
||
138 | $task->setDoc(htmlspecialchars($submittedData['doc'])); |
||
139 | } |
||
140 | if (isset($submittedData['lib'])) { |
||
141 | $task->setLib((int) $submittedData['lib']); |
||
142 | } |
||
143 | if (isset($submittedData['coll']) && is_array($submittedData['coll'])) { |
||
144 | $task->setColl($submittedData['coll']); |
||
145 | } else { |
||
146 | $task->setColl([]); |
||
147 | } |
||
148 | if (isset($submittedData['pid'])) { |
||
149 | $task->setPid((int) $submittedData['pid']); |
||
150 | } |
||
151 | if (isset($submittedData['solr'])) { |
||
152 | $task->setSolr((int) $submittedData['solr']); |
||
153 | } |
||
154 | if (isset($submittedData['owner'])) { |
||
155 | $task->setOwner(htmlspecialchars($submittedData['owner'])); |
||
156 | } |
||
157 | $task->setAll(!empty($submittedData['all'])); |
||
158 | if (isset($submittedData['from'])) { |
||
159 | $task->setFrom(htmlspecialchars($submittedData['from'])); |
||
160 | } |
||
161 | if (isset($submittedData['until'])) { |
||
162 | $task->setUntil(htmlspecialchars($submittedData['until'])); |
||
163 | } |
||
164 | if (isset($submittedData['set'])) { |
||
165 | $task->setSet(htmlspecialchars($submittedData['set'])); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Return HTML for dry run checkbox |
||
171 | * |
||
172 | * @access protected |
||
173 | * |
||
174 | * @param bool $dryRun |
||
175 | * |
||
176 | * @return array additional field dry run checkbox |
||
177 | */ |
||
178 | protected function getDryRunField(bool $dryRun): array |
||
179 | { |
||
180 | $fieldName = 'dryRun'; |
||
181 | $fieldId = 'task_' . $fieldName; |
||
182 | $fieldHtml = '<input type="checkbox" name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '" value="1"' . ($dryRun ? ' checked="checked"' : '') . '>'; |
||
183 | return [ |
||
184 | 'code' => $fieldHtml, |
||
185 | 'label' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:additionalFields.dryRun', |
||
186 | 'cshKey' => '_MOD_system_txschedulerM1', |
||
187 | 'cshLabel' => $fieldId |
||
188 | ]; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Return HTML for solr dropdown |
||
193 | * |
||
194 | * @access protected |
||
195 | * |
||
196 | * @param int $solr UID of the selected Solr core |
||
197 | * @param int $pid UID of the selected storage page |
||
198 | * |
||
199 | * @return array additional field solr dropdown |
||
200 | */ |
||
201 | protected function getSolrField(int $solr, int $pid): array |
||
202 | { |
||
203 | $fieldName = 'solr'; |
||
204 | $fieldId = 'task_' . $fieldName; |
||
205 | |||
206 | $allSolrCores = $this->getSolrCores($pid); |
||
207 | $options = []; |
||
208 | $options[] = '<option value="-1"></option>'; |
||
209 | foreach ($allSolrCores as $label => $uid) { |
||
210 | $options[] = '<option value="' . $uid . '" ' . ($solr == $uid ? 'selected' : '') . ' >' . $label . '</option>'; |
||
211 | }; |
||
212 | $fieldHtml = '<select name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '">' . implode("\n", $options) . '</select>'; |
||
213 | return [ |
||
214 | 'code' => $fieldHtml, |
||
215 | 'label' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:additionalFields.solr', |
||
216 | 'cshKey' => '_MOD_system_txschedulerM1', |
||
217 | 'cshLabel' => $fieldId |
||
218 | ]; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Return html for page dropdown |
||
223 | * |
||
224 | * @access protected |
||
225 | * |
||
226 | * @param int $pid UID of the selected storage page |
||
227 | * |
||
228 | * @return array additional field storage page dropdown |
||
229 | */ |
||
230 | protected function getPidField(int $pid): array |
||
231 | { |
||
232 | $fieldName = 'pid'; |
||
233 | $fieldId = 'task_' . $fieldName; |
||
234 | |||
235 | $pageRepository = GeneralUtility::makeInstance(PageTreeRepository::class); |
||
236 | $pages = $pageRepository->getTree(0); |
||
237 | |||
238 | $options = []; |
||
239 | foreach ($pages['_children'] as $page) { |
||
240 | if ($page['doktype'] == 254) { |
||
241 | $options[] = '<option value="' . $page['uid'] . '" ' . ($pid == $page['uid'] ? 'selected' : '') . ' >' . $page['title'] . '</option>'; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | $fieldHtml = '<select name="tx_scheduler[' . $fieldName . ']" id="' . $fieldId . '">' . implode("\n", $options) . '</select>'; |
||
246 | return [ |
||
247 | 'code' => $fieldHtml, |
||
248 | 'label' => 'LLL:EXT:dlf/Resources/Private/Language/locallang_tasks.xlf:additionalFields.pid', |
||
249 | 'cshKey' => '_MOD_system_txschedulerM1', |
||
250 | 'cshLabel' => $fieldId |
||
251 | ]; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Return HTML for owner text field |
||
256 | * |
||
257 | * @access protected |
||
258 | * |
||
259 | * @param string $owner registered owner |
||
260 | * |
||
261 | * @return array additional field owner text field |
||
262 | */ |
||
263 | protected function getOwnerField(string $owner): array |
||
273 | ]; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Fetches all Solr cores on given page. |
||
278 | * |
||
279 | * @access protected |
||
280 | * |
||
281 | * @param int $pid UID of storage page |
||
282 | * |
||
283 | * @return array Array of valid Solr cores |
||
284 | */ |
||
285 | private function getSolrCores(int $pid): array |
||
303 | } |
||
304 | } |
||
305 |