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