Convert the contact fields cache group to standard cache backend
[civicrm-core.git] / CRM / Custom / Form / ChangeFieldType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is to build the form for Deleting Group
38 */
39 class CRM_Custom_Form_ChangeFieldType extends CRM_Core_Form {
40
41 /**
42 * The field id
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * Array of custom field values
50 * @var array
51 */
52 protected $_values;
53
54 /**
55 * Mapper array of valid field type
56 * @var array
57 */
58 protected $_htmlTypeTransitions;
59
60 /**
61 * Set up variables to build the form.
62 *
63 * @return void
64 * @access protected
65 */
66 public function preProcess() {
67 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
68 $this, TRUE
69 );
70
71 $this->_values = [];
72 $params = ['id' => $this->_id];
73 CRM_Core_BAO_CustomField::retrieve($params, $this->_values);
74
75 $this->_htmlTypeTransitions = self::fieldTypeTransitions(CRM_Utils_Array::value('data_type', $this->_values),
76 CRM_Utils_Array::value('html_type', $this->_values)
77 );
78
79 if (empty($this->_values) || empty($this->_htmlTypeTransitions)) {
80 CRM_Core_Error::fatal(ts("Invalid custom field or can't change input type of this custom field."));
81 }
82
83 $url = CRM_Utils_System::url('civicrm/admin/custom/group/field/update',
84 "action=update&reset=1&gid={$this->_values['custom_group_id']}&id={$this->_id}"
85 );
86 $session = CRM_Core_Session::singleton();
87 $session->pushUserContext($url);
88
89 CRM_Utils_System::setTitle(ts('Change Field Type: %1',
90 [1 => $this->_values['label']]
91 ));
92 }
93
94 /**
95 * Build the form object.
96 *
97 * @return void
98 */
99 public function buildQuickForm() {
100
101 $srcHtmlType = $this->add('select',
102 'src_html_type',
103 ts('Current HTML Type'),
104 [$this->_values['html_type'] => $this->_values['html_type']],
105 TRUE
106 );
107
108 $srcHtmlType->setValue($this->_values['html_type']);
109 $srcHtmlType->freeze();
110
111 $this->assign('srcHtmlType', $this->_values['html_type']);
112
113 $dstHtmlType = $this->add('select',
114 'dst_html_type',
115 ts('New HTML Type'),
116 [
117 '' => ts('- select -'),
118 ] + $this->_htmlTypeTransitions,
119 TRUE
120 );
121
122 $this->addButtons([
123 [
124 'type' => 'next',
125 'name' => ts('Change Field Type'),
126 'isDefault' => TRUE,
127 'js' => ['onclick' => 'return checkCustomDataField();'],
128 ],
129 [
130 'type' => 'cancel',
131 'name' => ts('Cancel'),
132 ],
133 ]);
134 }
135
136 /**
137 * Process the form when submitted.
138 *
139 * @return void
140 */
141 public function postProcess() {
142 $params = $this->controller->exportValues($this->_name);
143
144 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
145 $this->_values['custom_group_id'],
146 'table_name'
147 );
148
149 $singleValueOps = [
150 'Text',
151 'Select',
152 'Radio',
153 'Autocomplete-Select',
154 ];
155
156 $mutliValueOps = [
157 'CheckBox',
158 'Multi-Select',
159 ];
160
161 $srcHtmlType = $this->_values['html_type'];
162 $dstHtmlType = $params['dst_html_type'];
163
164 $customField = new CRM_Core_DAO_CustomField();
165 $customField->id = $this->_id;
166 $customField->find(TRUE);
167
168 if ($dstHtmlType == 'Text' && in_array($srcHtmlType, [
169 'Select',
170 'Radio',
171 'Autocomplete-Select',
172 ])) {
173 $customField->option_group_id = "NULL";
174 CRM_Core_BAO_CustomField::checkOptionGroup($this->_values['option_group_id']);
175 }
176
177 if (in_array($srcHtmlType, $mutliValueOps) &&
178 in_array($dstHtmlType, $singleValueOps)) {
179 $this->flattenToFirstValue($tableName, $this->_values['column_name']);
180 }
181 elseif (in_array($srcHtmlType, $singleValueOps) &&
182 in_array($dstHtmlType, $mutliValueOps)) {
183 $this->firstValueToFlatten($tableName, $this->_values['column_name']);
184 }
185
186 $customField->html_type = $dstHtmlType;
187 $customField->save();
188
189 // Reset cache for custom fields
190 Civi::cache('fields')->flush();
191
192 CRM_Core_Session::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
193 [1 => $this->_values['label'], 2 => $dstHtmlType]
194 ), ts('Field Type Changed'), 'success');
195 }
196
197 /**
198 * @param $dataType
199 * @param $htmlType
200 *
201 * @return array|null
202 */
203 public static function fieldTypeTransitions($dataType, $htmlType) {
204 // Text field is single value field,
205 // can not be change to other single value option which contains option group
206 if ($htmlType == 'Text') {
207 return NULL;
208 }
209
210 $singleValueOps = [
211 'Text' => 'Text',
212 'Select' => 'Select',
213 'Radio' => 'Radio',
214 'Autocomplete-Select' => 'Autocomplete-Select',
215 ];
216
217 $mutliValueOps = [
218 'CheckBox' => 'CheckBox',
219 'Multi-Select' => 'Multi-Select',
220 ];
221
222 switch ($dataType) {
223 case 'String':
224 if (in_array($htmlType, array_keys($singleValueOps))) {
225 unset($singleValueOps[$htmlType]);
226 return array_merge($singleValueOps, $mutliValueOps);
227 }
228 elseif (in_array($htmlType, array_keys($mutliValueOps))) {
229 unset($singleValueOps['Text']);
230 foreach ($singleValueOps as $type => $label) {
231 $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )";
232 }
233 unset($mutliValueOps[$htmlType]);
234 return array_merge($mutliValueOps, $singleValueOps);
235 }
236 break;
237
238 case 'Int':
239 case 'Float':
240 case 'Int':
241 case 'Money':
242 if (in_array($htmlType, array_keys($singleValueOps))) {
243 unset($singleValueOps[$htmlType]);
244 return $singleValueOps;
245 }
246 break;
247
248 case 'Memo':
249 $ops = [
250 'TextArea' => 'TextArea',
251 'RichTextEditor' => 'RichTextEditor',
252 ];
253 if (in_array($htmlType, array_keys($ops))) {
254 unset($ops[$htmlType]);
255 return $ops;
256 }
257 break;
258 }
259
260 return NULL;
261 }
262
263 /**
264 * Take a single-value column (eg: a Radio or Select etc ) and convert
265 * value to the multi listed value (eg:"^Foo^")
266 *
267 * @param string $table
268 * @param string $column
269 */
270 public function firstValueToFlatten($table, $column) {
271 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
272 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
273 $dao = CRM_Core_DAO::executeQuery($selectSql);
274 while ($dao->fetch()) {
275 if (!$dao->{$column}) {
276 continue;
277 }
278 $value = CRM_Core_DAO::VALUE_SEPARATOR . $dao->{$column} . CRM_Core_DAO::VALUE_SEPARATOR;
279 $params = [
280 1 => [(string) $value, 'String'],
281 2 => [$dao->id, 'Integer'],
282 ];
283 CRM_Core_DAO::executeQuery($updateSql, $params);
284 }
285 }
286
287 /**
288 * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert
289 * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo")
290 *
291 * @param string $table
292 * @param string $column
293 */
294 public function flattenToFirstValue($table, $column) {
295 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
296 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
297 $dao = CRM_Core_DAO::executeQuery($selectSql);
298 while ($dao->fetch()) {
299 $values = self::explode($dao->{$column});
300 $params = [
301 1 => [(string) array_shift($values), 'String'],
302 2 => [$dao->id, 'Integer'],
303 ];
304 CRM_Core_DAO::executeQuery($updateSql, $params);
305 }
306 }
307
308 /**
309 * @param $str
310 *
311 * @return array
312 */
313 public static function explode($str) {
314 if (empty($str) || $str == CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::VALUE_SEPARATOR) {
315 return [];
316 }
317 else {
318 return explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($str, CRM_Core_DAO::VALUE_SEPARATOR));
319 }
320 }
321
322 }