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
{
49 * Array of custom field values
54 * Mapper array of valid field type
56 protected $_htmlTypeTransitions;
59 * Set up variables to build the form
64 public function preProcess() {
65 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
69 $this->_values
= array();
70 $params = array('id' => $this->_id
);
71 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
73 $this->_htmlTypeTransitions
= self
::fieldTypeTransitions(CRM_Utils_Array
::value('data_type', $this->_values
),
74 CRM_Utils_Array
::value('html_type', $this->_values
)
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."));
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}"
84 $session = CRM_Core_Session
::singleton();
85 $session->pushUserContext($url);
87 CRM_Utils_System
::setTitle(ts('Change Field Type: %1',
88 array(1 => $this->_values
['label'])
93 * Build the form object
97 public function buildQuickForm() {
99 $srcHtmlType = $this->add('select',
101 ts('Current HTML Type'),
102 array($this->_values
['html_type'] => $this->_values
['html_type']),
106 $srcHtmlType->setValue($this->_values
['html_type']);
107 $srcHtmlType->freeze();
109 $this->assign('srcHtmlType', $this->_values
['html_type']);
111 $dstHtmlType = $this->add('select',
115 '' => ts('- select -')) +
$this->_htmlTypeTransitions
,
119 $this->addButtons(array(
122 'name' => ts('Change Field Type'),
124 'js' => array('onclick' => 'return checkCustomDataField();'),
128 'name' => ts('Cancel'),
135 * Process the form when submitted
139 public function postProcess() {
140 $params = $this->controller
->exportValues($this->_name
);
142 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
143 $this->_values
['custom_group_id'],
147 $singleValueOps = array(
151 'Autocomplete-Select',
154 $mutliValueOps = array(
160 $srcHtmlType = $this->_values
['html_type'];
161 $dstHtmlType = $params['dst_html_type'];
163 $customField = new CRM_Core_DAO_CustomField();
164 $customField->id
= $this->_id
;
165 $customField->find(TRUE);
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']);
173 if (in_array($srcHtmlType, $mutliValueOps) &&
174 in_array($dstHtmlType, $singleValueOps)
176 $this->flattenToFirstValue($tableName, $this->_values
['column_name']);
178 elseif (in_array($srcHtmlType, $singleValueOps) &&
179 in_array($dstHtmlType, $mutliValueOps)
181 $this->firstValueToFlatten($tableName, $this->_values
['column_name']);
184 $customField->html_type
= $dstHtmlType;
185 $customField->save();
187 // Reset cache for custom fields
188 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
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');
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') {
208 $singleValueOps = array(
210 'Select' => 'Select',
212 'Autocomplete-Select' => 'Autocomplete-Select',
215 $mutliValueOps = array(
216 'CheckBox' => 'CheckBox',
217 'Multi-Select' => 'Multi-Select',
218 'AdvMulti-Select' => 'AdvMulti-Select',
223 if (in_array($htmlType, array_keys($singleValueOps))) {
224 unset($singleValueOps[$htmlType]);
225 return array_merge($singleValueOps, $mutliValueOps);
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') . " )";
232 unset($mutliValueOps[$htmlType]);
233 return array_merge($mutliValueOps, $singleValueOps);
241 if (in_array($htmlType, array_keys($singleValueOps))) {
242 unset($singleValueOps[$htmlType]);
243 return $singleValueOps;
249 'TextArea' => 'TextArea',
250 'RichTextEditor' => 'RichTextEditor',
252 if (in_array($htmlType, array_keys($ops))) {
253 unset($ops[$htmlType]);
263 * Take a single-value column (eg: a Radio or Select etc ) and convert
264 * value to the multi listed value (eg:"^Foo^")
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}) {
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'),
278 CRM_Core_DAO
::executeQuery($updateSql, $params);
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")
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'),
295 CRM_Core_DAO
::executeQuery($updateSql, $params);
304 public static function explode($str) {
305 if (empty($str) ||
$str == CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Core_DAO
::VALUE_SEPARATOR
) {
309 return explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($str, CRM_Core_DAO
::VALUE_SEPARATOR
));