Completed
Push — issue/190 ( 036298 )
by Tomas Norre
07:42
created

getAdditionalFields()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 18
nop 3
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
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\Core\Messaging\FlashMessage;
29
use TYPO3\CMS\Core\Utility\MathUtility;
30
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
31
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
32
use TYPO3\CMS\Scheduler\Task\AbstractTask;
33
34
/**
35
 * Class CrawlerTaskAdditionalFieldProvider
36
 *
37
 * @package AOE\Crawler\Task
38
 * @codeCoverageIgnore
39
 */
40
class CrawlerTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
41
{
42
43
    /**
44
     * Gets additional fields to render in the form to add/edit a task
45
     *
46
     * @param array $taskInfo
47
     * @param CrawlerTask $task
48
     * @param SchedulerModuleController $schedulerModule
49
     * @return array
50
     */
51
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
52
    {
53
        $additionalFields = [];
54
55
        if ($schedulerModule->CMD == 'add') {
56
            $task->sleepTime = $taskInfo['sleepTime'] ? $taskInfo['sleepTime'] : 1000;
57
            $task->sleepAfterFinish = $taskInfo['sleepAfterFinish'] ? $taskInfo['sleepAfterFinish'] : 10;
58
            $task->countInARun = $taskInfo['countInARun'] ? $taskInfo['countInARun'] : 100;
59
        }
60
61
        if ($schedulerModule->CMD == 'edit') {
62
            $taskInfo['sleepTime'] = $task->sleepTime;
63
            $taskInfo['sleepAfterFinish'] = $task->sleepAfterFinish;
64
            $taskInfo['countInARun'] = $task->countInARun;
65
        }
66
67
        // input for sleepTime
68
        $fieldId = 'task_sleepTime';
69
        $fieldCode = '<input type="text" name="tx_scheduler[sleepTime]" id="' . $fieldId . '" value="' . htmlentities($taskInfo['sleepTime']) . '" class="form-control" />';
70
        $additionalFields[$fieldId] = [
71
            'code' => $fieldCode,
72
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.sleepTime'
73
        ];
74
        // input for sleepAfterFinish
75
        $fieldId = 'task_sleepAfterFinish';
76
        $fieldCode = '<input type="text" name="tx_scheduler[sleepAfterFinish]" id="' . $fieldId . '" value="' . htmlentities($taskInfo['sleepAfterFinish']) . '" class="form-control" />';
77
        $additionalFields[$fieldId] = [
78
            'code' => $fieldCode,
79
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.sleepAfterFinish'
80
        ];
81
        // input for countInARun
82
        $fieldId = 'task_countInARun';
83
        $fieldCode = '<input type="text" name="tx_scheduler[countInARun]" id="' . $fieldId . '" value="' . htmlentities($taskInfo['countInARun']) . '" class="form-control" />';
84
        $additionalFields[$fieldId] = [
85
            'code' => $fieldCode,
86
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.countInARun'
87
        ];
88
89
        return $additionalFields;
90
    }
91
92
    /**
93
     * Validates the additional fields' values
94
     *
95
     * @param array $submittedData
96
     * @param SchedulerModuleController $schedulerModule
97
     * @return bool
98
     */
99
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
100
    {
101
        $isValid = false;
102
103
        if (MathUtility::convertToPositiveInteger($submittedData['sleepTime']) > 0) {
104
            $isValid = true;
105
        } else {
106
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidSleepTime'), FlashMessage::ERROR);
107
        }
108
109
        if (MathUtility::convertToPositiveInteger($submittedData['sleepAfterFinish']) === 0) {
110
            $isValid = false;
111
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidSleepAfterFinish'), FlashMessage::ERROR);
112
        }
113
114
        if (MathUtility::convertToPositiveInteger($submittedData['countInARun']) === 0) {
115
            $isValid = false;
116
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidCountInARun'), FlashMessage::ERROR);
117
        }
118
119
        return $isValid;
120
    }
121
122
    /**
123
     * Takes care of saving the additional fields' values in the task's object
124
     *
125
     * @param array $submittedData
126
     * @param CrawlerTask|AbstractTask $task
127
     * @return void
128
     */
129
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
130
    {
131
        $task->sleepTime = intval($submittedData['sleepTime']);
0 ignored issues
show
Bug introduced by
The property sleepTime 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...
132
        $task->sleepAfterFinish = intval($submittedData['sleepAfterFinish']);
0 ignored issues
show
Bug introduced by
The property sleepAfterFinish 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...
133
        $task->countInARun = intval($submittedData['countInARun']);
0 ignored issues
show
Bug introduced by
The property countInARun 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...
134
    }
135
}
136