TableGarbageCollectionAdditionalFieldProvider.php 12.7 KB
Newer Older
1
2
3
4
5
6
<?php
namespace TYPO3\CMS\Scheduler\Task;

/***************************************************************
 *  Copyright notice
 *
7
 *  (c) 2011-2013 Christian Kuhn <lolli@schwarzbu.ch>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
/**
 * Additional BE fields for sys log table garbage collection task.
 *
 * @author Christian Kuhn <lolli@schwarzbu.ch>
 */
class TableGarbageCollectionAdditionalFieldProvider implements \TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface {

	/**
	 * @var array Default number of days by table
	 */
	protected $defaultNumberOfDays = array();

	/**
	 * Add additional fields
	 *
	 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
	 * @param object $task When editing, reference to the current task object. Null when adding.
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return array Array containing all the information pertaining to the additional fields
	 */
	public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		$this->initDefaultNumberOfDays();
		$additionalFields['task_tableGarbageCollection_allTables'] = $this->getAllTablesAdditionalField($taskInfo, $task, $parentObject);
		$additionalFields['task_tableGarbageCollection_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
		$additionalFields['task_tableGarbageCollection_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
		return $additionalFields;
	}

	/**
	 * Initialize the default number of days for all configured tables
	 *
	 * @return void
	 */
	protected function initDefaultNumberOfDays() {
60
		$tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		foreach ($tableConfiguration as $tableName => $configuration) {
			if (isset($configuration['expirePeriod'])) {
				$this->defaultNumberOfDays[$tableName] = $configuration['expirePeriod'];
			}
		}
	}

	/**
	 * Add a select field of available tables.
	 *
	 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
	 * @param object $task When editing, reference to the current task object. Null when adding.
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return array Array containing all the information pertaining to the additional fields
	 */
	protected function getAllTablesAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		if ($parentObject->CMD === 'edit') {
			$checked = $task->allTables === TRUE ? 'checked="checked" ' : '';
		} else {
			$checked = '';
		}
		$fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_allTables]';
		$fieldId = 'task_tableGarbageCollection_allTables';
84
		$fieldHtml = '<input type="checkbox" ' . $checked . 'onChange="actOnChangeSchedulerTableGarbageCollectionAllTables(this)" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" />';
85
86
		$fieldConfiguration = array(
			'code' => $fieldHtml,
87
			'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.allTables',
88
			'cshKey' => '_MOD_system_txschedulerM1',
89
90
91
92
93
94
95
96
97
98
99
100
101
102
			'cshLabel' => $fieldId
		);
		return $fieldConfiguration;
	}

	/**
	 * Add a select field of available tables.
	 *
	 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
	 * @param object $task When editing, reference to the current task object. Null when adding.
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return array Array containing all the information pertaining to the additional fields
	 */
	protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
103
		$tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
104
105
106
107
108
109
110
111
112
		$options = array();
		// Add an empty option on top if an existing task is configured
		// with a table that can not be found in configuration anymore
		if ($parentObject->CMD === 'edit' && !array_key_exists($task->table, $tableConfiguration)) {
			$options[] = '<option value="" selected="selected"></option>';
		}
		foreach ($tableConfiguration as $tableName => $configuration) {
			if ($parentObject->CMD === 'add' && count($options) === 0) {
				// Select first table by default if adding a new task
113
				$options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
114
115
			} elseif ($task->table === $tableName) {
				// Select currently selected table
116
				$options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
117
			} else {
118
				$options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
119
120
121
122
123
124
125
			}
		}
		$disabled = $task->allTables === TRUE ? ' disabled="disabled"' : '';
		$fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_table]';
		$fieldId = 'task_tableGarbageCollection_table';
		$fieldHtml = array();
		// Add table drop down html
126
		$fieldHtml[] = '<select ' . 'name="' . $fieldName . '" ' . $disabled . 'onChange="actOnChangeSchedulerTableGarbageCollectionTable(this)"' . 'id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
127
128
		// Add js array for default 'number of days' values
		$fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
129
		$fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
130
131
132
		$fieldHtml[] = '// -->/*]]>*/</script>';
		$fieldConfiguration = array(
			'code' => implode(LF, $fieldHtml),
133
			'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.table',
134
			'cshKey' => '_MOD_system_txschedulerM1',
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
			'cshLabel' => $fieldId
		);
		return $fieldConfiguration;
	}

	/**
	 * Add a input field to get the number of days.
	 *
	 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
	 * @param object $task When editing, reference to the current task object. Null when adding.
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return array Array containing all the information pertaining to the additional fields
	 */
	protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		// Initialize selected fields
		$disabled = '';
		if (empty($taskInfo['scheduler_tableGarbageCollection_numberOfDays'])) {
			if ($parentObject->CMD === 'add') {
				// In case of new task, set to 180 days
				$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = 180;
			} elseif ($parentObject->CMD === 'edit') {
				// In case of editing the task, set to currently selected value
				$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] = $task->numberOfDays;
				if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
					$disabled = ' disabled="disabled"';
				}
			}
		}
		if ($task->allTables === TRUE) {
			$disabled = ' disabled="disabled"';
		}
		$fieldName = 'tx_scheduler[scheduler_tableGarbageCollection_numberOfDays]';
		$fieldId = 'task_tableGarbageCollection_numberOfDays';
168
		$fieldHtml = '<input type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_tableGarbageCollection_numberOfDays'] . '" ' . 'size="4" />';
169
170
		$fieldConfiguration = array(
			'code' => $fieldHtml,
171
			'label' => 'LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.numberOfDays',
172
			'cshKey' => '_MOD_system_txschedulerM1',
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
			'cshLabel' => $fieldId
		);
		return $fieldConfiguration;
	}

	/**
	 * Validate additional fields
	 *
	 * @param array $submittedData Reference to the array containing the data submitted by the user
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
	 */
	public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		$validData = $this->validateAllTablesAdditionalField($submittedData, $parentObject);
		$validData &= $this->validateTableAdditionalField($submittedData, $parentObject);
		$validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
		return $validData;
	}

	/**
	 * Checks if all table field is correct
	 *
	 * @param array $submittedData Reference to the array containing the data submitted by the user
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return boolean True if data is valid
	 */
	public function validateAllTablesAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		$validData = FALSE;
		if (!isset($submittedData['scheduler_tableGarbageCollection_allTables'])) {
			$validData = TRUE;
		} elseif ($submittedData['scheduler_tableGarbageCollection_allTables'] === 'on') {
			$validData = TRUE;
		}
		return $validData;
	}

	/**
	 * Checks given table for existence in configuration array
	 *
	 * @param array $submittedData Reference to the array containing the data submitted by the user
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return boolean True if table exists in configuration, false otherwise
	 */
	public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		$validData = FALSE;
218
		$tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
		if (!isset($submittedData['scheduler_tableGarbageCollection_table'])) {
			$validData = TRUE;
		} elseif (array_key_exists($submittedData['scheduler_tableGarbageCollection_table'], $tableConfiguration)) {
			$validData = TRUE;
		}
		return $validData;
	}

	/**
	 * Checks if given number of days is a positive integer
	 *
	 * @param array $submittedData Reference to the array containing the data submitted by the user
	 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
	 * @return boolean True if validation was ok (or selected class is not relevant), false otherwise
	 */
	public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject) {
		$validData = FALSE;
		if (!isset($submittedData['scheduler_tableGarbageCollection_numberOfDays'])) {
			$validData = TRUE;
238
		} elseif ((int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'] >= 0) {
239
240
241
			$validData = TRUE;
		} else {
			// Issue error message
242
			$parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
243
244
245
246
247
248
249
250
		}
		return $validData;
	}

	/**
	 * Save additional field in task
	 *
	 * @param array $submittedData Contains data submitted by the user
251
	 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
252
253
	 * @return void
	 */
254
	public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task) {
255
256
		$task->allTables = $submittedData['scheduler_tableGarbageCollection_allTables'] === 'on' ? TRUE : FALSE;
		$task->table = $submittedData['scheduler_tableGarbageCollection_table'];
257
		$task->numberOfDays = (int)$submittedData['scheduler_tableGarbageCollection_numberOfDays'];
258
259
260
	}

}