[FEATURE] Make saltedpasswords conversion task options configurable
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / classes / tasks / class.tx_saltedpasswords_tasks_bulkupdate_additionalfieldprovider.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Philipp Gampe (typo3@philippgampe.info)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
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 /**
29 * Additional field for salted passwords bulk update task
30 *
31 * @autor Philipp Gampe <typo3@philippgampe.info>
32 * @package TYPO3
33 * @subpackage saltedpasswords
34 */
35 class tx_saltedpasswords_Tasks_BulkUpdate_AdditionalFieldProvider implements tx_scheduler_AdditionalFieldProvider {
36
37 /**
38 * Default value whether the task deactivates itself after last run.
39 * @var boolean Whether the task is allowed to deactivate itself after processing all existing user records.
40 */
41 protected $defaultCanDeactivateSelf = TRUE;
42
43 /**
44 * Default value for the number of records to handle at each run.
45 * @var integer Number of records
46 */
47 protected $defaultNumberOfRecords = 250;
48
49 /**
50 * Add a field for the number of records and if the task should deactivate itself after
51 * processing all records
52 *
53 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
54 * @param tx_saltedpasswords_Tasks_BulkUpdate $task When editing, reference to the current task object. Null when adding.
55 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
56 * @return array Array containing all the information pertaining to the additional fields
57 */
58 public function getAdditionalFields(array &$taskInfo, $task, tx_scheduler_Module $parentObject) {
59 // Initialize selected fields
60 if (!isset($taskInfo['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf'])) {
61 $taskInfo['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf'] = $this->defaultCanDeactivateSelf;
62 if ($parentObject->CMD === 'edit') {
63 $taskInfo['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf'] = $task->getCanDeactivateSelf();
64 }
65 }
66 if (!isset($taskInfo['scheduler_saltedpasswordsBulkUpdateNumberOfRecords'])) {
67 $taskInfo['scheduler_saltedpasswordsBulkUpdateNumberOfRecords'] = $this->defaultNumberOfRecords;
68 if ($parentObject->CMD === 'edit') {
69 $taskInfo['scheduler_saltedpasswordsBulkUpdateNumberOfRecords'] = $task->getNumberOfRecords();
70 }
71 }
72
73 // Configuration for canDeactivateSelf
74 $fieldName = 'tx_scheduler[scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf]';
75 $fieldId = 'task_saltedpasswordsBulkUpdateCanDeactivateSelf';
76 $fieldValue = 'IsChecked';
77 $fieldChecked = (bool)($taskInfo['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf']);
78 $fieldHtml = '<input type="checkbox"' .
79 ' name="' . $fieldName .'"' .
80 ' id="' . $fieldId .'"' .
81 ' value="' . $fieldValue .'"' .
82 ($fieldChecked ? ' checked="checked"' : '') .
83 ' />';
84
85 $additionalFields[$fieldId] = array(
86 'code' => $fieldHtml,
87 'label' => 'LLL:EXT:saltedpasswords/locallang.xml:ext.saltedpasswords.tasks.bulkupdate.label.canDeactivateSelf',
88 'cshKey' => '_txsaltedpasswords',
89 'cshLabel' => $fieldId,
90 );
91
92 // Configuration for numberOfRecords
93 $fieldName = 'tx_scheduler[scheduler_saltedpasswordsBulkUpdateNumberOfRecords]';
94 $fieldId = 'task_saltedpasswordsBulkUpdateNumberOfRecords';
95 $fieldValue = intval($taskInfo['scheduler_saltedpasswordsBulkUpdateNumberOfRecords']);
96 $fieldHtml = '<input type="text" name="' . $fieldName . '" id="' . $fieldId . '" value="' . htmlspecialchars($fieldValue) . '" />';
97
98 $additionalFields[$fieldId] = array(
99 'code' => $fieldHtml,
100 'label' => 'LLL:EXT:saltedpasswords/locallang.xml:ext.saltedpasswords.tasks.bulkupdate.label.numberOfRecords',
101 'cshKey' => '_txsaltedpasswords',
102 'cshLabel' => $fieldId,
103 );
104
105 return $additionalFields;
106 }
107
108 /**
109 * Checks if the given values are boolean and integer
110 *
111 * @param array $submittedData Reference to the array containing the data submitted by the user
112 * @param tx_scheduler_Module $parentObject Reference to the calling object (Scheduler's BE module)
113 * @return boolean TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
114 */
115 public function validateAdditionalFields(array &$submittedData, tx_scheduler_Module $parentObject) {
116 $result = TRUE;
117
118 // Check if number of records is indeed a number and greater or equals to 0
119 // If not, fail validation and issue error message
120 if (
121 !is_numeric($submittedData['scheduler_saltedpasswordsBulkUpdateNumberOfRecords']) ||
122 intval($submittedData['scheduler_saltedpasswordsBulkUpdateNumberOfRecords']) < 0
123 ) {
124 $result = FALSE;
125 $parentObject->addMessage($GLOBALS['LANG']->sL('LLL:EXT:saltedpasswords/locallang.xml:ext.saltedpasswords.tasks.bulkupdate.invalidNumberOfRecords'), t3lib_FlashMessage::ERROR);
126 }
127
128 return $result;
129 }
130
131 /**
132 * Saves given values in task object
133 *
134 * @param array $submittedData Contains data submitted by the user
135 * @param tx_scheduler_Task|tx_saltedpasswords_Tasks_BulkUpdate $task Reference to the current task object
136 * @return void
137 */
138 public function saveAdditionalFields(array $submittedData, tx_scheduler_Task $task) {
139 if (
140 isset($submittedData['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf'])
141 && $submittedData['scheduler_saltedpasswordsBulkUpdateCanDeactivateSelf'] === 'IsChecked'
142 ) {
143 $task->setCanDeactivateSelf(TRUE);
144 } else {
145 $task->setCanDeactivateSelf(FALSE);
146 }
147
148 $task->setNumberOfRecords(intval($submittedData['scheduler_saltedpasswordsBulkUpdateNumberOfRecords']));
149 }
150
151 }
152 ?>