Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
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 | /** | |
100fef9d | 42 | * The field id |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | protected $_id; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Array of custom field values |
6a488035 TO |
50 | */ |
51 | protected $_values; | |
52 | ||
53 | /** | |
100fef9d | 54 | * Mapper array of valid field type |
6a488035 TO |
55 | */ |
56 | protected $_htmlTypeTransitions; | |
57 | ||
58 | /** | |
fe482240 | 59 | * Set up variables to build the form. |
6a488035 TO |
60 | * |
61 | * @return void | |
b44e3f84 | 62 | * @access protected |
8ef12e64 | 63 | */ |
00be9182 | 64 | public function preProcess() { |
6a488035 TO |
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', | |
353ffa53 TO |
88 | array(1 => $this->_values['label']) |
89 | )); | |
6a488035 TO |
90 | } |
91 | ||
92 | /** | |
fe482240 | 93 | * Build the form object. |
6a488035 | 94 | * |
355ba699 | 95 | * @return void |
6a488035 TO |
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( | |
389bcebf | 115 | '' => ts('- select -'), |
353ffa53 | 116 | ) + $this->_htmlTypeTransitions, |
6a488035 TO |
117 | TRUE |
118 | ); | |
119 | ||
120 | $this->addButtons(array( | |
121 | array( | |
122 | 'type' => 'next', | |
123 | 'name' => ts('Change Field Type'), | |
124 | 'isDefault' => TRUE, | |
125 | 'js' => array('onclick' => 'return checkCustomDataField();'), | |
126 | ), | |
127 | array( | |
128 | 'type' => 'cancel', | |
129 | 'name' => ts('Cancel'), | |
130 | ), | |
131 | ) | |
132 | ); | |
133 | } | |
134 | ||
135 | /** | |
fe482240 | 136 | * Process the form when submitted. |
6a488035 TO |
137 | * |
138 | * @return void | |
6a488035 TO |
139 | */ |
140 | public function postProcess() { | |
141 | $params = $this->controller->exportValues($this->_name); | |
142 | ||
143 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
144 | $this->_values['custom_group_id'], | |
145 | 'table_name' | |
146 | ); | |
147 | ||
148 | $singleValueOps = array( | |
149 | 'Text', | |
150 | 'Select', | |
151 | 'Radio', | |
152 | 'Autocomplete-Select', | |
153 | ); | |
154 | ||
155 | $mutliValueOps = array( | |
156 | 'CheckBox', | |
157 | 'Multi-Select', | |
6a488035 TO |
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( | |
353ffa53 TO |
168 | 'Select', |
169 | 'Radio', | |
389bcebf | 170 | 'Autocomplete-Select', |
353ffa53 TO |
171 | )) |
172 | ) { | |
6a488035 TO |
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 | ) { | |
180 | $this->flattenToFirstValue($tableName, $this->_values['column_name']); | |
181 | } | |
182 | elseif (in_array($srcHtmlType, $singleValueOps) && | |
183 | in_array($dstHtmlType, $mutliValueOps) | |
184 | ) { | |
185 | $this->firstValueToFlatten($tableName, $this->_values['column_name']); | |
186 | } | |
187 | ||
188 | $customField->html_type = $dstHtmlType; | |
189 | $customField->save(); | |
190 | ||
191 | // Reset cache for custom fields | |
192 | CRM_Core_BAO_Cache::deleteGroup('contact fields'); | |
193 | ||
194 | CRM_Core_Session::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.', | |
353ffa53 TO |
195 | array(1 => $this->_values['label'], 2 => $dstHtmlType) |
196 | ), ts('Field Type Changed'), 'success'); | |
6a488035 TO |
197 | } |
198 | ||
e0ef6999 EM |
199 | /** |
200 | * @param $dataType | |
201 | * @param $htmlType | |
202 | * | |
203 | * @return array|null | |
204 | */ | |
00be9182 | 205 | public static function fieldTypeTransitions($dataType, $htmlType) { |
6a488035 TO |
206 | // Text field is single value field, |
207 | // can not be change to other single value option which contains option group | |
208 | if ($htmlType == 'Text') { | |
209 | return NULL; | |
210 | } | |
211 | ||
212 | $singleValueOps = array( | |
213 | 'Text' => 'Text', | |
214 | 'Select' => 'Select', | |
215 | 'Radio' => 'Radio', | |
216 | 'Autocomplete-Select' => 'Autocomplete-Select', | |
217 | ); | |
218 | ||
219 | $mutliValueOps = array( | |
220 | 'CheckBox' => 'CheckBox', | |
221 | 'Multi-Select' => 'Multi-Select', | |
6a488035 TO |
222 | ); |
223 | ||
224 | switch ($dataType) { | |
225 | case 'String': | |
226 | if (in_array($htmlType, array_keys($singleValueOps))) { | |
227 | unset($singleValueOps[$htmlType]); | |
228 | return array_merge($singleValueOps, $mutliValueOps); | |
229 | } | |
230 | elseif (in_array($htmlType, array_keys($mutliValueOps))) { | |
231 | unset($singleValueOps['Text']); | |
232 | foreach ($singleValueOps as $type => $label) { | |
233 | $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )"; | |
234 | } | |
235 | unset($mutliValueOps[$htmlType]); | |
236 | return array_merge($mutliValueOps, $singleValueOps); | |
237 | } | |
238 | break; | |
239 | ||
240 | case 'Int': | |
241 | case 'Float': | |
242 | case 'Int': | |
243 | case 'Money': | |
244 | if (in_array($htmlType, array_keys($singleValueOps))) { | |
245 | unset($singleValueOps[$htmlType]); | |
246 | return $singleValueOps; | |
247 | } | |
248 | break; | |
249 | ||
250 | case 'Memo': | |
251 | $ops = array( | |
252 | 'TextArea' => 'TextArea', | |
253 | 'RichTextEditor' => 'RichTextEditor', | |
254 | ); | |
255 | if (in_array($htmlType, array_keys($ops))) { | |
256 | unset($ops[$htmlType]); | |
257 | return $ops; | |
258 | } | |
259 | break; | |
260 | } | |
261 | ||
262 | return NULL; | |
263 | } | |
264 | ||
265 | /** | |
266 | * Take a single-value column (eg: a Radio or Select etc ) and convert | |
267 | * value to the multi listed value (eg:"^Foo^") | |
ea3ddccf | 268 | * |
269 | * @param string $table | |
270 | * @param string $column | |
6a488035 TO |
271 | */ |
272 | public function firstValueToFlatten($table, $column) { | |
273 | $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL"; | |
274 | $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2"; | |
353ffa53 | 275 | $dao = CRM_Core_DAO::executeQuery($selectSql); |
6a488035 TO |
276 | while ($dao->fetch()) { |
277 | if (!$dao->{$column}) { | |
278 | continue; | |
279 | } | |
280 | $value = CRM_Core_DAO::VALUE_SEPARATOR . $dao->{$column} . CRM_Core_DAO::VALUE_SEPARATOR; | |
e8646905 | 281 | $params = array( |
353ffa53 | 282 | 1 => array((string) $value, 'String'), |
6a488035 TO |
283 | 2 => array($dao->id, 'Integer'), |
284 | ); | |
285 | CRM_Core_DAO::executeQuery($updateSql, $params); | |
286 | } | |
287 | } | |
288 | ||
289 | /** | |
290 | * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert | |
291 | * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo") | |
ea3ddccf | 292 | * |
293 | * @param string $table | |
294 | * @param string $column | |
6a488035 TO |
295 | */ |
296 | public function flattenToFirstValue($table, $column) { | |
297 | $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL"; | |
298 | $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2"; | |
353ffa53 | 299 | $dao = CRM_Core_DAO::executeQuery($selectSql); |
6a488035 TO |
300 | while ($dao->fetch()) { |
301 | $values = self::explode($dao->{$column}); | |
e8646905 | 302 | $params = array( |
353ffa53 | 303 | 1 => array((string) array_shift($values), 'String'), |
6a488035 TO |
304 | 2 => array($dao->id, 'Integer'), |
305 | ); | |
306 | CRM_Core_DAO::executeQuery($updateSql, $params); | |
307 | } | |
308 | } | |
309 | ||
e0ef6999 EM |
310 | /** |
311 | * @param $str | |
312 | * | |
313 | * @return array | |
314 | */ | |
00be9182 | 315 | public static function explode($str) { |
6a488035 TO |
316 | if (empty($str) || $str == CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::VALUE_SEPARATOR) { |
317 | return array(); | |
318 | } | |
319 | else { | |
320 | return explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($str, CRM_Core_DAO::VALUE_SEPARATOR)); | |
321 | } | |
322 | } | |
96025800 | 323 | |
6a488035 | 324 | } |