Passed
Pull Request — master (#2)
by Felix
04:48
created

getAdditionalFields()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 3
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Aoe\UpdateRefindex\Scheduler;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 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 2 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\Scheduler\AdditionalFieldProviderInterface;
29
30
/**
31
 * class to define additional fields
32
 *
33
 * @package update_refindex
34
 * @subpackage Scheduler
35
 */
36
class UpdateRefIndexAdditionalFields implements AdditionalFieldProviderInterface
37
{
38
    /**
39
     * Field name constants
40
     */
41
    const FIELD_ALL_TABLES = 'updateRefindexAllTables';
42
    const FIELD_SELECTED_TABLES = 'updateRefindexSelectedTables';
43
44
    /** Locallang reference */
45
    const LL_REFERENCE = 'LLL:EXT:update_refindex/Resources/Private/Language/locallang_db.xml';
46
47
    /**
48
     * @param array &$taskInfo
49
     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
50
     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject
51
     * @return array
52
     */
53
    public function getAdditionalFields(
54
        array &$taskInfo,
55
        $task,
56
        \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject
57
    ) {
58
        /** @var UpdateRefIndexTask $task */
59
60
        // define value for fields
61
        if ($parentObject->CMD == 'add') {
62
            $taskInfo[self::FIELD_ALL_TABLES] = false;
63
            $taskInfo[self::FIELD_SELECTED_TABLES] = array();
64
        } elseif ($parentObject->CMD == 'edit') {
65
            $taskInfo[self::FIELD_ALL_TABLES] = $task->isUpdateAllTables();
66
            $taskInfo[self::FIELD_SELECTED_TABLES] = $task->getSelectedTables();
67
        } else {
68
            $taskInfo[self::FIELD_ALL_TABLES] = false;
69
            $taskInfo[self::FIELD_SELECTED_TABLES] = array();
70
        }
71
72
        // Get configuration (markup & labels) for additional fields
73
        $additionalFields = array(
74
            self::FIELD_ALL_TABLES => array(
75
                'code' => $this->getCheckbox($taskInfo[self::FIELD_ALL_TABLES]),
76
                'label' => $GLOBALS['LANG']->sL(self::LL_REFERENCE . ':scheduler_task.updateRefindex.fieldUpdateAllTables.label')
77
            ),
78
            self::FIELD_SELECTED_TABLES => array(
79
                'code' => $this->getSelectBox($taskInfo[self::FIELD_SELECTED_TABLES]),
80
                'label' => $GLOBALS['LANG']->sL(self::LL_REFERENCE . ':scheduler_task.updateRefindex.fieldSelectedTables.label')
81
            ),
82
        );
83
84
        return $additionalFields;
85
    }
86
87
    /**
88
     * @param array $submittedData
89
     * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task
90
     */
91
    public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
92
    {
93
        /** @var UpdateRefIndexTask $task */
94
        $task->setUpdateAllTables((boolean)$submittedData[self::FIELD_ALL_TABLES]);
95
        $task->setSelectedTables((array)$submittedData[self::FIELD_SELECTED_TABLES]);
96
    }
97
98
    /**
99
     * @param array &$submittedData
100
     * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject
101
     * @return boolean
102
     */
103
    public function validateAdditionalFields(
104
        array &$submittedData,
105
        \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject
106
    ) {
107
        $isValid = true;
108
109
        if (!isset($submittedData[self::FIELD_ALL_TABLES])
110
            || !\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange((integer)$submittedData[self::FIELD_ALL_TABLES], 0, 1)
111
        ) {
112
            $isValid = false;
113
        }
114
115
        if (isset($submittedData[self::FIELD_SELECTED_TABLES])
116
            && count($submittedData[self::FIELD_SELECTED_TABLES]) === 0
117
        ) {
118
            $isValid = false;
119
        }
120
121
        return $isValid;
122
    }
123
124
    /**
125
     * Gets the HTML markup of a checkbox input field
126
     *
127
     * @param boolean $isChecked
128
     * @return string
129
     */
130
    private function getCheckbox($isChecked)
131
    {
132
        $checked = true === $isChecked ? 'checked="checked" ' : '';
133
        $content = '<input type="hidden" name="tx_scheduler[' . self::FIELD_ALL_TABLES . ']" value="0" />';
134
        $content .= '<input type="checkbox" ' . $checked . 'value="1"'
135
            . ' name="tx_scheduler[' . self::FIELD_ALL_TABLES . ']"'
136
            . ' id="task_' . self::FIELD_ALL_TABLES . '" />';
137
138
        return $content;
139
    }
140
141
    /**
142
     * Gets array with tables, which can be selected as options
143
     *
144
     * @return array
145
     */
146
    private function getOptionsForSelectBox()
147
    {
148
        $existingTables = array_keys($GLOBALS['TCA']);
149
        sort($existingTables);
150
151
        $optionsSelectedTables = array();
152
        foreach ($existingTables as $existingTable) {
153
            $optionsSelectedTables[$existingTable] = $existingTable;
154
        }
155
156
        return $optionsSelectedTables;
157
    }
158
159
    /**
160
     * Generates HTML selectbox for field 'selectedTables'
161
     *
162
     * @param array $selected
163
     * @return string
164
     */
165
    private function getSelectBox(array $selected)
166
    {
167
        $contentArray = array('<select id="task_' . self::FIELD_SELECTED_TABLES . '" name="tx_scheduler[' . self::FIELD_SELECTED_TABLES . '][]" size="20" multiple="multiple" class="form-control">');
168
169
        foreach ($this->getOptionsForSelectBox() as $value => $label) {
170
            $selectAttribute = in_array($value, $selected) ? ' selected="selected"' : '';
171
            $contentArray[] = '<option value="' . $value . '"' . $selectAttribute . '>' . $label . '</option>';
172
        }
173
174
        $contentArray[] = '</select>';
175
        $content = implode("\n", $contentArray);
176
177
        return $content;
178
    }
179
}
180