3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class is to build the form for Deleting Group
39 class CRM_Custom_Form_ChangeFieldType
extends CRM_Core_Form
{
50 * Array of custom field values
55 * Mapper array of valid field type
57 protected $_htmlTypeTransitions;
60 * Set up variables to build the form
65 function preProcess() {
66 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
70 $this->_values
= array();
71 $params = array('id' => $this->_id
);
72 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
74 $this->_htmlTypeTransitions
= self
::fieldTypeTransitions(CRM_Utils_Array
::value('data_type', $this->_values
),
75 CRM_Utils_Array
::value('html_type', $this->_values
)
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."));
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}"
85 $session = CRM_Core_Session
::singleton();
86 $session->pushUserContext($url);
88 CRM_Utils_System
::setTitle(ts('Change Field Type: %1',
89 array(1 => $this->_values
['label'])
94 * Build the form object
99 public function buildQuickForm() {
101 $srcHtmlType = $this->add('select',
103 ts('Current HTML Type'),
104 array($this->_values
['html_type'] => $this->_values
['html_type']),
108 $srcHtmlType->setValue($this->_values
['html_type']);
109 $srcHtmlType->freeze();
111 $this->assign('srcHtmlType', $this->_values
['html_type']);
113 $dstHtmlType = $this->add('select',
117 '' => ts('- select -')) +
$this->_htmlTypeTransitions
,
121 $this->addButtons(array(
124 'name' => ts('Change Field Type'),
126 'js' => array('onclick' => 'return checkCustomDataField();'),
130 'name' => ts('Cancel'),
137 * Process the form when submitted
142 public function postProcess() {
143 $params = $this->controller
->exportValues($this->_name
);
145 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
146 $this->_values
['custom_group_id'],
150 $singleValueOps = array(
154 'Autocomplete-Select',
157 $mutliValueOps = array(
163 $srcHtmlType = $this->_values
['html_type'];
164 $dstHtmlType = $params['dst_html_type'];
166 $customField = new CRM_Core_DAO_CustomField();
167 $customField->id
= $this->_id
;
168 $customField->find(TRUE);
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']);
176 if (in_array($srcHtmlType, $mutliValueOps) &&
177 in_array($dstHtmlType, $singleValueOps)
179 $this->flattenToFirstValue($tableName, $this->_values
['column_name']);
181 elseif (in_array($srcHtmlType, $singleValueOps) &&
182 in_array($dstHtmlType, $mutliValueOps)
184 $this->firstValueToFlatten($tableName, $this->_values
['column_name']);
187 $customField->html_type
= $dstHtmlType;
188 $customField->save();
190 // Reset cache for custom fields
191 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
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');
204 static function fieldTypeTransitions($dataType, $htmlType) {
205 // Text field is single value field,
206 // can not be change to other single value option which contains option group
207 if ($htmlType == 'Text') {
211 $singleValueOps = array(
213 'Select' => 'Select',
215 'Autocomplete-Select' => 'Autocomplete-Select',
218 $mutliValueOps = array(
219 'CheckBox' => 'CheckBox',
220 'Multi-Select' => 'Multi-Select',
221 'AdvMulti-Select' => 'AdvMulti-Select',
226 if (in_array($htmlType, array_keys($singleValueOps))) {
227 unset($singleValueOps[$htmlType]);
228 return array_merge($singleValueOps, $mutliValueOps);
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') . " )";
235 unset($mutliValueOps[$htmlType]);
236 return array_merge($mutliValueOps, $singleValueOps);
244 if (in_array($htmlType, array_keys($singleValueOps))) {
245 unset($singleValueOps[$htmlType]);
246 return $singleValueOps;
252 'TextArea' => 'TextArea',
253 'RichTextEditor' => 'RichTextEditor',
255 if (in_array($htmlType, array_keys($ops))) {
256 unset($ops[$htmlType]);
266 * Take a single-value column (eg: a Radio or Select etc ) and convert
267 * value to the multi listed value (eg:"^Foo^")
269 public function firstValueToFlatten($table, $column) {
270 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
271 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
272 $dao = CRM_Core_DAO
::executeQuery($selectSql);
273 while ($dao->fetch()) {
274 if (!$dao->{$column}) {
277 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $dao->{$column} . CRM_Core_DAO
::VALUE_SEPARATOR
;
278 $params = array(1 => array((string)$value, 'String'),
279 2 => array($dao->id
, 'Integer'),
281 CRM_Core_DAO
::executeQuery($updateSql, $params);
286 * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert
287 * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo")
289 public function flattenToFirstValue($table, $column) {
290 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
291 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
292 $dao = CRM_Core_DAO
::executeQuery($selectSql);
293 while ($dao->fetch()) {
294 $values = self
::explode($dao->{$column});
295 $params = array(1 => array((string)array_shift($values), 'String'),
296 2 => array($dao->id
, 'Integer'),
298 CRM_Core_DAO
::executeQuery($updateSql, $params);
307 static function explode($str) {
308 if (empty($str) ||
$str == CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Core_DAO
::VALUE_SEPARATOR
) {
312 return explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($str, CRM_Core_DAO
::VALUE_SEPARATOR
));