Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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', | |
158 | 'AdvMulti-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, array( | |
353ffa53 TO |
169 | 'Select', |
170 | 'Radio', | |
389bcebf | 171 | 'Autocomplete-Select', |
353ffa53 TO |
172 | )) |
173 | ) { | |
6a488035 TO |
174 | $customField->option_group_id = "NULL"; |
175 | CRM_Core_BAO_CustomField::checkOptionGroup($this->_values['option_group_id']); | |
176 | } | |
177 | ||
178 | if (in_array($srcHtmlType, $mutliValueOps) && | |
179 | in_array($dstHtmlType, $singleValueOps) | |
180 | ) { | |
181 | $this->flattenToFirstValue($tableName, $this->_values['column_name']); | |
182 | } | |
183 | elseif (in_array($srcHtmlType, $singleValueOps) && | |
184 | in_array($dstHtmlType, $mutliValueOps) | |
185 | ) { | |
186 | $this->firstValueToFlatten($tableName, $this->_values['column_name']); | |
187 | } | |
188 | ||
189 | $customField->html_type = $dstHtmlType; | |
190 | $customField->save(); | |
191 | ||
192 | // Reset cache for custom fields | |
193 | CRM_Core_BAO_Cache::deleteGroup('contact fields'); | |
194 | ||
195 | CRM_Core_Session::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.', | |
353ffa53 TO |
196 | array(1 => $this->_values['label'], 2 => $dstHtmlType) |
197 | ), ts('Field Type Changed'), 'success'); | |
6a488035 TO |
198 | } |
199 | ||
e0ef6999 EM |
200 | /** |
201 | * @param $dataType | |
202 | * @param $htmlType | |
203 | * | |
204 | * @return array|null | |
205 | */ | |
00be9182 | 206 | public static function fieldTypeTransitions($dataType, $htmlType) { |
6a488035 TO |
207 | // Text field is single value field, |
208 | // can not be change to other single value option which contains option group | |
209 | if ($htmlType == 'Text') { | |
210 | return NULL; | |
211 | } | |
212 | ||
213 | $singleValueOps = array( | |
214 | 'Text' => 'Text', | |
215 | 'Select' => 'Select', | |
216 | 'Radio' => 'Radio', | |
217 | 'Autocomplete-Select' => 'Autocomplete-Select', | |
218 | ); | |
219 | ||
220 | $mutliValueOps = array( | |
221 | 'CheckBox' => 'CheckBox', | |
222 | 'Multi-Select' => 'Multi-Select', | |
223 | 'AdvMulti-Select' => 'AdvMulti-Select', | |
224 | ); | |
225 | ||
226 | switch ($dataType) { | |
227 | case 'String': | |
228 | if (in_array($htmlType, array_keys($singleValueOps))) { | |
229 | unset($singleValueOps[$htmlType]); | |
230 | return array_merge($singleValueOps, $mutliValueOps); | |
231 | } | |
232 | elseif (in_array($htmlType, array_keys($mutliValueOps))) { | |
233 | unset($singleValueOps['Text']); | |
234 | foreach ($singleValueOps as $type => $label) { | |
235 | $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )"; | |
236 | } | |
237 | unset($mutliValueOps[$htmlType]); | |
238 | return array_merge($mutliValueOps, $singleValueOps); | |
239 | } | |
240 | break; | |
241 | ||
242 | case 'Int': | |
243 | case 'Float': | |
244 | case 'Int': | |
245 | case 'Money': | |
246 | if (in_array($htmlType, array_keys($singleValueOps))) { | |
247 | unset($singleValueOps[$htmlType]); | |
248 | return $singleValueOps; | |
249 | } | |
250 | break; | |
251 | ||
252 | case 'Memo': | |
253 | $ops = array( | |
254 | 'TextArea' => 'TextArea', | |
255 | 'RichTextEditor' => 'RichTextEditor', | |
256 | ); | |
257 | if (in_array($htmlType, array_keys($ops))) { | |
258 | unset($ops[$htmlType]); | |
259 | return $ops; | |
260 | } | |
261 | break; | |
262 | } | |
263 | ||
264 | return NULL; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Take a single-value column (eg: a Radio or Select etc ) and convert | |
269 | * value to the multi listed value (eg:"^Foo^") | |
270 | */ | |
271 | public function firstValueToFlatten($table, $column) { | |
272 | $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL"; | |
273 | $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2"; | |
353ffa53 | 274 | $dao = CRM_Core_DAO::executeQuery($selectSql); |
6a488035 TO |
275 | while ($dao->fetch()) { |
276 | if (!$dao->{$column}) { | |
277 | continue; | |
278 | } | |
279 | $value = CRM_Core_DAO::VALUE_SEPARATOR . $dao->{$column} . CRM_Core_DAO::VALUE_SEPARATOR; | |
e8646905 | 280 | $params = array( |
353ffa53 | 281 | 1 => array((string) $value, 'String'), |
6a488035 TO |
282 | 2 => array($dao->id, 'Integer'), |
283 | ); | |
284 | CRM_Core_DAO::executeQuery($updateSql, $params); | |
285 | } | |
286 | } | |
287 | ||
288 | /** | |
289 | * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert | |
290 | * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo") | |
291 | */ | |
292 | public function flattenToFirstValue($table, $column) { | |
293 | $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL"; | |
294 | $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2"; | |
353ffa53 | 295 | $dao = CRM_Core_DAO::executeQuery($selectSql); |
6a488035 TO |
296 | while ($dao->fetch()) { |
297 | $values = self::explode($dao->{$column}); | |
e8646905 | 298 | $params = array( |
353ffa53 | 299 | 1 => array((string) array_shift($values), 'String'), |
6a488035 TO |
300 | 2 => array($dao->id, 'Integer'), |
301 | ); | |
302 | CRM_Core_DAO::executeQuery($updateSql, $params); | |
303 | } | |
304 | } | |
305 | ||
e0ef6999 EM |
306 | /** |
307 | * @param $str | |
308 | * | |
309 | * @return array | |
310 | */ | |
00be9182 | 311 | public static function explode($str) { |
6a488035 TO |
312 | if (empty($str) || $str == CRM_Core_DAO::VALUE_SEPARATOR . CRM_Core_DAO::VALUE_SEPARATOR) { |
313 | return array(); | |
314 | } | |
315 | else { | |
316 | return explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($str, CRM_Core_DAO::VALUE_SEPARATOR)); | |
317 | } | |
318 | } | |
96025800 | 319 | |
6a488035 | 320 | } |