3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 -'),
116 ) +
$this->_htmlTypeTransitions
,
120 $this->addButtons(array(
123 'name' => ts('Change Field Type'),
125 'js' => array('onclick' => 'return checkCustomDataField();'),
129 'name' => ts('Cancel'),
136 * Process the form when submitted.
140 public function postProcess() {
141 $params = $this->controller
->exportValues($this->_name
);
143 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
144 $this->_values
['custom_group_id'],
148 $singleValueOps = array(
152 'Autocomplete-Select',
155 $mutliValueOps = array(
161 $srcHtmlType = $this->_values
['html_type'];
162 $dstHtmlType = $params['dst_html_type'];
164 $customField = new CRM_Core_DAO_CustomField();
165 $customField->id
= $this->_id
;
166 $customField->find(TRUE);
168 if ($dstHtmlType == 'Text' && in_array($srcHtmlType, array(
171 'Autocomplete-Select',
174 $customField->option_group_id
= "NULL";
175 CRM_Core_BAO_CustomField
::checkOptionGroup($this->_values
['option_group_id']);
178 if (in_array($srcHtmlType, $mutliValueOps) &&
179 in_array($dstHtmlType, $singleValueOps)
181 $this->flattenToFirstValue($tableName, $this->_values
['column_name']);
183 elseif (in_array($srcHtmlType, $singleValueOps) &&
184 in_array($dstHtmlType, $mutliValueOps)
186 $this->firstValueToFlatten($tableName, $this->_values
['column_name']);
189 $customField->html_type
= $dstHtmlType;
190 $customField->save();
192 // Reset cache for custom fields
193 CRM_Core_BAO_Cache
::deleteGroup('contact fields');
195 CRM_Core_Session
::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
196 array(1 => $this->_values
['label'], 2 => $dstHtmlType)
197 ), ts('Field Type Changed'), 'success');
206 public static function fieldTypeTransitions($dataType, $htmlType) {
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') {
213 $singleValueOps = array(
215 'Select' => 'Select',
217 'Autocomplete-Select' => 'Autocomplete-Select',
220 $mutliValueOps = array(
221 'CheckBox' => 'CheckBox',
222 'Multi-Select' => 'Multi-Select',
223 'AdvMulti-Select' => 'AdvMulti-Select',
228 if (in_array($htmlType, array_keys($singleValueOps))) {
229 unset($singleValueOps[$htmlType]);
230 return array_merge($singleValueOps, $mutliValueOps);
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') . " )";
237 unset($mutliValueOps[$htmlType]);
238 return array_merge($mutliValueOps, $singleValueOps);
246 if (in_array($htmlType, array_keys($singleValueOps))) {
247 unset($singleValueOps[$htmlType]);
248 return $singleValueOps;
254 'TextArea' => 'TextArea',
255 'RichTextEditor' => 'RichTextEditor',
257 if (in_array($htmlType, array_keys($ops))) {
258 unset($ops[$htmlType]);
268 * Take a single-value column (eg: a Radio or Select etc ) and convert
269 * value to the multi listed value (eg:"^Foo^")
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";
274 $dao = CRM_Core_DAO
::executeQuery($selectSql);
275 while ($dao->fetch()) {
276 if (!$dao->{$column}) {
279 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $dao->{$column} . CRM_Core_DAO
::VALUE_SEPARATOR
;
281 1 => array((string) $value, 'String'),
282 2 => array($dao->id
, 'Integer'),
284 CRM_Core_DAO
::executeQuery($updateSql, $params);
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")
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";
295 $dao = CRM_Core_DAO
::executeQuery($selectSql);
296 while ($dao->fetch()) {
297 $values = self
::explode($dao->{$column});
299 1 => array((string) array_shift($values), 'String'),
300 2 => array($dao->id
, 'Integer'),
302 CRM_Core_DAO
::executeQuery($updateSql, $params);
311 public static function explode($str) {
312 if (empty($str) ||
$str == CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Core_DAO
::VALUE_SEPARATOR
) {
316 return explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($str, CRM_Core_DAO
::VALUE_SEPARATOR
));