Completed
Branch master (b7ffcb)
by Tomas Norre
17:57
created

CrawlerQueueTaskAdditionalFieldProvider   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 3
dl 0
loc 183
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
F getAdditionalFields() 0 84 14
A getSelectedState() 0 11 3
A getCrawlerConfigurationRecords() 0 16 2
B validateAdditionalFields() 0 23 5
A saveAdditionalFields() 0 6 1
1
<?php
2
namespace AOE\Crawler\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Backend\Utility\BackendUtility;
29
use TYPO3\CMS\Core\Messaging\FlashMessage;
30
use TYPO3\CMS\Core\Utility\MathUtility;
31
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
32
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
33
use TYPO3\CMS\Scheduler\Task\AbstractTask;
34
35
/**
36
 * Class CrawlerQueueTaskAdditionalFieldProvider
37
 *
38
 * @package AOE\Crawler\Task
39
 * @codeCoverageIgnore
40
 */
41
class CrawlerQueueTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
42
{
43
    /**
44
     * Gets additional fields to render in the form to add/edit a task
45
     *
46
     * @param array $taskInfo
47
     * @param AbstractTask $task
48
     * @param SchedulerModuleController $schedulerModule
49
     *
50
     * @return array
51
     */
52
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
53
    {
54
        $additionalFields = [];
55
56
        if (empty($taskInfo['configuration'])) {
57
            if ($schedulerModule->CMD == 'add') {
58
                $taskInfo['configuration'] = [];
59
            } elseif ($schedulerModule->CMD == 'edit') {
60
                $taskInfo['configuration'] = $task->configuration;
0 ignored issues
show
Bug introduced by
The property configuration does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
            } else {
62
                $taskInfo['configuration'] = $task->configuration;
63
            }
64
        }
65
66
        if (empty($taskInfo['startPage'])) {
67
            if ($schedulerModule->CMD == 'add') {
68
                $taskInfo['startPage'] = 0;
69
                if ($task instanceof \TYPO3\CMS\Scheduler\Task\AbstractTask) {
70
                    $task->startPage = 0;
0 ignored issues
show
Bug introduced by
The property startPage does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
                }
72
            } elseif ($schedulerModule->CMD == 'edit') {
73
                $taskInfo['startPage'] = $task->startPage;
74
            } else {
75
                $taskInfo['startPage'] = $task->startPage;
76
            }
77
        }
78
79
        if (empty($taskInfo['depth'])) {
80
            if ($schedulerModule->CMD == 'add') {
81
                $taskInfo['depth'] = [];
82
            } elseif ($schedulerModule->CMD == 'edit') {
83
                $taskInfo['depth'] = $task->depth;
0 ignored issues
show
Bug introduced by
The property depth does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
84
            } else {
85
                $taskInfo['depth'] = $task->depth;
86
            }
87
        }
88
89
        // input for startPage
90
        $fieldId = 'task_startPage';
91
        $fieldCode = '<input name="tx_scheduler[startPage]" type="text" id="' . $fieldId . '" value="' . $task->startPage . '" class="form-control" />';
92
        $additionalFields[$fieldId] = [
93
            'code' => $fieldCode,
94
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.startPage'
95
        ];
96
97
        // input for depth
98
        $fieldId = 'task_depth';
99
        $fieldValueArray = [
100
            '0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
101
            '1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
102
            '2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
103
            '3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
104
            '4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
105
            '99' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
106
        ];
107
        $fieldCode = '<select name="tx_scheduler[depth]" id="' . $fieldId . '" class="form-control">';
108
109
        foreach ($fieldValueArray as $key => $label) {
110
            $fieldCode .= "\t" . '<option value="' . $key . '"' . (($key == $task->depth) ? ' selected="selected"' : '') . '>' . $label . '</option>';
111
        }
112
113
        $fieldCode .= '</select>';
114
        $additionalFields[$fieldId] = [
115
            'code' => $fieldCode,
116
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.depth'
117
        ];
118
119
        // combobox for configuration records
120
        $recordsArray = $this->getCrawlerConfigurationRecords();
121
        $fieldId = 'task_configuration';
122
        $fieldCode = '<select name="tx_scheduler[configuration][]" multiple="multiple" id="' . $fieldId . '" class="form-control">';
123
        $fieldCode .= "\t" . '<option value=""></option>';
124
        for ($i = 0; $i < count($recordsArray); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
125
            $fieldCode .= "\t" . '<option ' . $this->getSelectedState($task->configuration, $recordsArray[$i]['name']) . 'value="' . $recordsArray[$i]['name'] . '">' . $recordsArray[$i]['name'] . '</option>';
126
        }
127
        $fieldCode .= '</select>';
128
129
        $additionalFields[$fieldId] = [
130
            'code' => $fieldCode,
131
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.conf'
132
        ];
133
134
        return $additionalFields;
135
    }
136
137
    /**
138
     * Mark current value as selected by returning the "selected" attribute
139
     *
140
     * @param $configurationArray
141
     * @param $currentValue
142
     *
143
     * @return string
144
     */
145
    protected function getSelectedState($configurationArray, $currentValue)
146
    {
147
        $selected = '';
148
        for ($i = 0; $i < count($configurationArray); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
149
            if (strcmp($configurationArray[$i], $currentValue) === 0) {
150
                $selected = 'selected="selected" ';
151
            }
152
        }
153
154
        return $selected;
155
    }
156
157
    /**
158
     * Get all available configuration records.
159
     *
160
     * @return array which contains the available configuration records.
161
     */
162
    protected function getCrawlerConfigurationRecords()
163
    {
164
        $records = [];
165
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
166
            '*',
167
            'tx_crawler_configuration',
168
            '1=1' . BackendUtility::deleteClause('tx_crawler_configuration')
169
        );
170
171
        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
172
            $records[] = $row;
173
        }
174
        $GLOBALS['TYPO3_DB']->sql_free_result($result);
175
176
        return $records;
177
    }
178
179
    /**
180
     * Validates the additional fields' values
181
     *
182
     * @param array $submittedData
183
     * @param SchedulerModuleController $schedulerModule
184
     * @return bool
185
     */
186
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
187
    {
188
        $isValid = false;
189
190
        //!TODO add validation to validate the $submittedData['configuration'] which is normally a comma separated string
191
        if (is_array($submittedData['configuration'])) {
192
            $isValid = true;
193
        } else {
194
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidConfiguration'), FlashMessage::ERROR);
195
        }
196
197
        if ($submittedData['depth'] < 0) {
198
            $isValid = false;
199
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidDepth'), FlashMessage::ERROR);
200
        }
201
202
        if (!MathUtility::canBeInterpretedAsInteger($submittedData['startPage']) || $submittedData['startPage'] < 0) {
203
            $isValid = false;
204
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidStartPage'), FlashMessage::ERROR);
205
        }
206
207
        return $isValid;
208
    }
209
210
    /**
211
     * Takes care of saving the additional fields' values in the task's object
212
     *
213
     * @param array $submittedData
214
     * @param AbstractTask $task
215
     * @return void
216
     */
217
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
218
    {
219
        $task->depth = intval($submittedData['depth']);
0 ignored issues
show
Bug introduced by
The property depth does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
220
        $task->configuration = $submittedData['configuration'];
0 ignored issues
show
Bug introduced by
The property configuration does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
221
        $task->startPage = intval($submittedData['startPage']);
0 ignored issues
show
Bug introduced by
The property startPage does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
222
    }
223
}
224