Merge pull request #4607 from samuelsov/CRM-15637
[civicrm-core.git] / CRM / Custom / Form / ChangeFieldType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 */
51 protected $_values;
52
53 /**
54 * Mapper array of valid field type
55 */
56 protected $_htmlTypeTransitions;
57
58 /**
59 * Set up variables to build the form
60 *
61 * @return void
62 * @acess protected
63 */
64 public function preProcess() {
65 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
66 $this, TRUE
67 );
68
69 $this->_values = array();
70 $params = array('id' => $this->_id);
71 CRM_Core_BAO_CustomField::retrieve($params, $this->_values);
72
73 $this->_htmlTypeTransitions = self::fieldTypeTransitions(CRM_Utils_Array::value('data_type', $this->_values),
74 CRM_Utils_Array::value('html_type', $this->_values)
75 );
76
77 if (empty($this->_values) || empty($this->_htmlTypeTransitions)) {
78 CRM_Core_Error::fatal(ts("Invalid custom field or can't change input type of this custom field."));
79 }
80
81 $url = CRM_Utils_System::url('civicrm/admin/custom/group/field/update',
82 "action=update&reset=1&gid={$this->_values['custom_group_id']}&id={$this->_id}"
83 );
84 $session = CRM_Core_Session::singleton();
85 $session->pushUserContext($url);
86
87 CRM_Utils_System::setTitle(ts('Change Field Type: %1',
88 array(1 => $this->_values['label'])
89 ));
90 }
91
92 /**
93 * Build the form object
94 *
95 * @return void
96 */
97 public function buildQuickForm() {
98
99 $srcHtmlType = $this->add('select',
100 'src_html_type',
101 ts('Current HTML Type'),
102 array($this->_values['html_type'] => $this->_values['html_type']),
103 TRUE
104 );
105
106 $srcHtmlType->setValue($this->_values['html_type']);
107 $srcHtmlType->freeze();
108
109 $this->assign('srcHtmlType', $this->_values['html_type']);
110
111 $dstHtmlType = $this->add('select',
112 'dst_html_type',
113 ts('New HTML Type'),
114 array(
115 '' => ts('- select -')) + $this->_htmlTypeTransitions,
116 TRUE
117 );
118
119 $this->addButtons(array(
120 array(
121 'type' => 'next',
122 'name' => ts('Change Field Type'),
123 'isDefault' => TRUE,
124 'js' => array('onclick' => 'return checkCustomDataField();'),
125 ),
126 array(
127 'type' => 'cancel',
128 'name' => ts('Cancel'),
129 ),
130 )
131 );
132 }
133
134 /**
135 * Process the form when submitted
136 *
137 * @return void
138 */
139 public function postProcess() {
140 $params = $this->controller->exportValues($this->_name);
141
142 $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
143 $this->_values['custom_group_id'],
144 'table_name'
145 );
146
147 $singleValueOps = array(
148 'Text',
149 'Select',
150 'Radio',
151 'Autocomplete-Select',
152 );
153
154 $mutliValueOps = array(
155 'CheckBox',
156 'Multi-Select',
157 'AdvMulti-Select',
158 );
159
160 $srcHtmlType = $this->_values['html_type'];
161 $dstHtmlType = $params['dst_html_type'];
162
163 $customField = new CRM_Core_DAO_CustomField();
164 $customField->id = $this->_id;
165 $customField->find(TRUE);
166
167 if ($dstHtmlType == 'Text' && in_array($srcHtmlType, array(
168 'Select', 'Radio', 'Autocomplete-Select'))) {
169 $customField->option_group_id = "NULL";
170 CRM_Core_BAO_CustomField::checkOptionGroup($this->_values['option_group_id']);
171 }
172
173 if (in_array($srcHtmlType, $mutliValueOps) &&
174 in_array($dstHtmlType, $singleValueOps)
175 ) {
176 $this->flattenToFirstValue($tableName, $this->_values['column_name']);
177 }
178 elseif (in_array($srcHtmlType, $singleValueOps) &&
179 in_array($dstHtmlType, $mutliValueOps)
180 ) {
181 $this->firstValueToFlatten($tableName, $this->_values['column_name']);
182 }
183
184 $customField->html_type = $dstHtmlType;
185 $customField->save();
186
187 // Reset cache for custom fields
188 CRM_Core_BAO_Cache::deleteGroup('contact fields');
189
190 CRM_Core_Session::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
191 array(1 => $this->_values['label'], 2 => $dstHtmlType)
192 ), ts('Field Type Changed'), 'success');
193 }
194
195 /**
196 * @param $dataType
197 * @param $htmlType
198 *
199 * @return array|null
200 */
201 public static function fieldTypeTransitions($dataType, $htmlType) {
202 // Text field is single value field,
203 // can not be change to other single value option which contains option group
204 if ($htmlType == 'Text') {
205 return NULL;
206 }
207
208 $singleValueOps = array(
209 'Text' => 'Text',
210 'Select' => 'Select',
211 'Radio' => 'Radio',
212 'Autocomplete-Select' => 'Autocomplete-Select',
213 );
214
215 $mutliValueOps = array(
216 'CheckBox' => 'CheckBox',
217 'Multi-Select' => 'Multi-Select',
218 'AdvMulti-Select' => 'AdvMulti-Select',
219 );
220
221 switch ($dataType) {
222 case 'String':
223 if (in_array($htmlType, array_keys($singleValueOps))) {
224 unset($singleValueOps[$htmlType]);
225 return array_merge($singleValueOps, $mutliValueOps);
226 }
227 elseif (in_array($htmlType, array_keys($mutliValueOps))) {
228 unset($singleValueOps['Text']);
229 foreach ($singleValueOps as $type => $label) {
230 $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )";
231 }
232 unset($mutliValueOps[$htmlType]);
233 return array_merge($mutliValueOps, $singleValueOps);
234 }
235 break;
236
237 case 'Int':
238 case 'Float':
239 case 'Int':
240 case 'Money':
241 if (in_array($htmlType, array_keys($singleValueOps))) {
242 unset($singleValueOps[$htmlType]);
243 return $singleValueOps;
244 }
245 break;
246
247 case 'Memo':
248 $ops = array(
249 'TextArea' => 'TextArea',
250 'RichTextEditor' => 'RichTextEditor',
251 );
252 if (in_array($htmlType, array_keys($ops))) {
253 unset($ops[$htmlType]);
254 return $ops;
255 }
256 break;
257 }
258
259 return NULL;
260 }
261
262 /**
263 * Take a single-value column (eg: a Radio or Select etc ) and convert
264 * value to the multi listed value (eg:"^Foo^")
265 */
266 public function firstValueToFlatten($table, $column) {
267 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
268 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
269 $dao = CRM_Core_DAO::executeQuery($selectSql);
270 while ($dao->fetch()) {
271 if (!$dao->{$column}) {
272 continue;
273 }
274 $value = CRM_Core_DAO::VALUE_SEPARATOR . $dao->{$column} . CRM_Core_DAO::VALUE_SEPARATOR;
275 $params = array(1 => array((string)$value, 'String'),
276 2 => array($dao->id, 'Integer'),
277 );
278 CRM_Core_DAO::executeQuery($updateSql, $params);
279 }
280 }
281
282 /**
283 * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert
284 * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo")
285 */
286 public function flattenToFirstValue($table, $column) {
287 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
288 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
289 $dao = CRM_Core_DAO::executeQuery($selectSql);
290 while ($dao->fetch()) {
291 $values = self::explode($dao->{$column});
292 $params = array(1 => array((string)array_shift($values), 'String'),
293 2 => array($dao->id, 'Integer'),
294 );
295 CRM_Core_DAO::executeQuery($updateSql, $params);
296 }
297 }
298
299 /**
300 * @param $str
301 *
302 * @return array
303 */
304 public static function explode($str) {
305 if (empty($str) || $str == CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::VALUE_SEPARATOR) {
306 return array();
307 }
308 else {
309 return explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($str, CRM_Core_DAO::VALUE_SEPARATOR));
310 }
311 }
312 }