TYPO3  7.6
FileStorageExtractionAdditionalFieldProvider.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
25 
31 {
41  public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $parentObject)
42  {
43  if ($task !== null && !$task instanceof FileStorageExtractionTask) {
44  throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275695);
45  }
46  $additionalFields['scheduler_fileStorageIndexing_storage'] = $this->getAllStoragesField($task);
47  $additionalFields['scheduler_fileStorageIndexing_fileCount'] = $this->getFileCountField($task);
48  $additionalFields['scheduler_fileStorageIndexing_registeredExtractors'] = $this->getRegisteredExtractorsField($task);
49  return $additionalFields;
50  }
51 
58  protected function getAllStoragesField(FileStorageExtractionTask $task = null)
59  {
61  $storages = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();
62  $options = array();
63  foreach ($storages as $storage) {
64  if ($task !== null && $task->storageUid === $storage->getUid()) {
65  $options[] = '<option value="' . $storage->getUid() . '" selected="selected">' . $storage->getName() . '</option>';
66  } else {
67  $options[] = '<option value="' . $storage->getUid() . '">' . $storage->getName() . '</option>';
68  }
69  }
70 
71  $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_storage]';
72  $fieldId = 'scheduler_fileStorageIndexing_storage';
73  $fieldHtml = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode("\n", $options) . '</select>';
74 
75  $fieldConfiguration = array(
76  'code' => $fieldHtml,
77  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageIndexing.storage',
78  'cshKey' => '_MOD_system_txschedulerM1',
79  'cshLabel' => $fieldId
80  );
81  return $fieldConfiguration;
82  }
83 
90  protected function getFileCountField(FileStorageExtractionTask $task = null)
91  {
92  $fieldName = 'tx_scheduler[scheduler_fileStorageIndexing_fileCount]';
93  $fieldId = 'scheduler_fileStorageIndexing_fileCount';
94  $fieldValue = $task !== null ? (int)$task->maxFileCount : 100;
95  $fieldHtml = '<input type="text" class="form-control" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '">';
96 
97  $fieldConfiguration = array(
98  'code' => $fieldHtml,
99  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.fileCount',
100  'cshKey' => '_MOD_system_txschedulerM1',
101  'cshLabel' => $fieldId
102  );
103  return $fieldConfiguration;
104  }
105 
112  protected function getRegisteredExtractorsField(FileStorageExtractionTask $task = null)
113  {
114  $extractors = ExtractorRegistry::getInstance()->getExtractors();
115 
116  if (empty($extractors)) {
117  $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.without_extractors';
119  $flashMessage = GeneralUtility::makeInstance(
120  FlashMessage::class,
121  $this->getLanguageService()->sL($labelKey),
122  '',
124  );
125  $content = $flashMessage->render();
126  } else {
127  // Assemble the extractor bullet list first.
128  $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.extractor';
129  $bullets = array();
130  foreach ($extractors as $extractor) {
131  $bullets[] = sprintf(
132  '<li title="%s">%s</li>',
133  get_class($extractor),
134  sprintf($this->getLanguageService()->sL($labelKey), $this->formatExtractorClassName($extractor), $extractor->getPriority())
135  );
136  }
137 
138  // Finalize content assembling.
139  $labelKey = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors.with_extractors';
141  $flashMessage = GeneralUtility::makeInstance(
142  FlashMessage::class,
143  '<ul>' . implode(LF, $bullets) . '</ul>',
144  $this->getLanguageService()->sL($labelKey),
146  );
147  $content = $flashMessage->render();
148  }
149 
150  $fieldConfiguration = array(
151  'code' => $content,
152  'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.fileStorageExtraction.registeredExtractors',
153  'cshKey' => '_MOD_system_txschedulerM1',
154  'cshLabel' => 'scheduler_fileStorageIndexing_registeredExtractors'
155  );
156  return $fieldConfiguration;
157  }
158 
166  public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $parentObject)
167  {
168  if (
169  !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_storage'])
170  || !MathUtility::canBeInterpretedAsInteger($submittedData['scheduler_fileStorageIndexing_fileCount'])
171  ) {
172  return false;
173  } elseif (ResourceFactory::getInstance()->getStorageObject($submittedData['scheduler_fileStorageIndexing_storage']) === null) {
174  return false;
175  } elseif (!MathUtility::isIntegerInRange($submittedData['scheduler_fileStorageIndexing_fileCount'], 1, 9999)) {
176  return false;
177  }
178  return true;
179  }
180 
189  public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
190  {
191  if ($task !== null && !$task instanceof FileStorageExtractionTask) {
192  throw new \InvalidArgumentException('Task not of type FileStorageExtractionTask', 1384275698);
193  }
194  $task->storageUid = (int)$submittedData['scheduler_fileStorageIndexing_storage'];
195  $task->maxFileCount = (int)$submittedData['scheduler_fileStorageIndexing_fileCount'];
196  }
197 
205  protected function formatExtractorClassName(ExtractorInterface $extractor)
206  {
207  $extractorParts = explode('\\', get_class($extractor));
208  return array_pop($extractorParts);
209  }
210 
214  protected function getLanguageService()
215  {
216  return $GLOBALS['LANG'];
217  }
218 }