3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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
55 * Mapper array of valid field type
58 protected $_htmlTypeTransitions;
61 * Set up variables to build the form.
66 public function preProcess() {
67 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
72 $params = ['id' => $this->_id
];
73 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
75 $this->_htmlTypeTransitions
= self
::fieldTypeTransitions(CRM_Utils_Array
::value('data_type', $this->_values
),
76 CRM_Utils_Array
::value('html_type', $this->_values
)
79 if (empty($this->_values
) ||
empty($this->_htmlTypeTransitions
)) {
80 CRM_Core_Error
::fatal(ts("Invalid custom field or can't change input type of this custom field."));
83 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/update',
84 "action=update&reset=1&gid={$this->_values['custom_group_id']}&id={$this->_id}"
86 $session = CRM_Core_Session
::singleton();
87 $session->pushUserContext($url);
89 CRM_Utils_System
::setTitle(ts('Change Field Type: %1',
90 [1 => $this->_values
['label']]
95 * Build the form object.
99 public function buildQuickForm() {
101 $srcHtmlType = $this->add('select',
103 ts('Current HTML Type'),
104 [$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 -'),
118 ] +
$this->_htmlTypeTransitions
,
125 'name' => ts('Change Field Type'),
127 'js' => ['onclick' => 'return checkCustomDataField();'],
131 'name' => ts('Cancel'),
137 * Process the form when submitted.
141 public function postProcess() {
142 $params = $this->controller
->exportValues($this->_name
);
144 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
145 $this->_values
['custom_group_id'],
153 'Autocomplete-Select',
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, [
171 'Autocomplete-Select',
173 $customField->option_group_id
= "NULL";
174 CRM_Core_BAO_CustomField
::checkOptionGroup($this->_values
['option_group_id']);
177 if (in_array($srcHtmlType, $mutliValueOps) &&
178 in_array($dstHtmlType, $singleValueOps)) {
179 $this->flattenToFirstValue($tableName, $this->_values
['column_name']);
181 elseif (in_array($srcHtmlType, $singleValueOps) &&
182 in_array($dstHtmlType, $mutliValueOps)) {
183 $this->firstValueToFlatten($tableName, $this->_values
['column_name']);
186 $customField->html_type
= $dstHtmlType;
187 $customField->save();
189 // Reset cache for custom fields
190 Civi
::cache('fields')->flush();
192 CRM_Core_Session
::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
193 [1 => $this->_values
['label'], 2 => $dstHtmlType]
194 ), ts('Field Type Changed'), 'success');
203 public static function fieldTypeTransitions($dataType, $htmlType) {
204 // Text field is single value field,
205 // can not be change to other single value option which contains option group
206 if ($htmlType == 'Text') {
212 'Select' => 'Select',
214 'Autocomplete-Select' => 'Autocomplete-Select',
218 'CheckBox' => 'CheckBox',
219 'Multi-Select' => 'Multi-Select',
224 if (in_array($htmlType, array_keys($singleValueOps))) {
225 unset($singleValueOps[$htmlType]);
226 return array_merge($singleValueOps, $mutliValueOps);
228 elseif (in_array($htmlType, array_keys($mutliValueOps))) {
229 unset($singleValueOps['Text']);
230 foreach ($singleValueOps as $type => $label) {
231 $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )";
233 unset($mutliValueOps[$htmlType]);
234 return array_merge($mutliValueOps, $singleValueOps);
242 if (in_array($htmlType, array_keys($singleValueOps))) {
243 unset($singleValueOps[$htmlType]);
244 return $singleValueOps;
250 'TextArea' => 'TextArea',
251 'RichTextEditor' => 'RichTextEditor',
253 if (in_array($htmlType, array_keys($ops))) {
254 unset($ops[$htmlType]);
264 * Take a single-value column (eg: a Radio or Select etc ) and convert
265 * value to the multi listed value (eg:"^Foo^")
267 * @param string $table
268 * @param string $column
270 public function firstValueToFlatten($table, $column) {
271 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
272 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
273 $dao = CRM_Core_DAO
::executeQuery($selectSql);
274 while ($dao->fetch()) {
275 if (!$dao->{$column}) {
278 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $dao->{$column} . CRM_Core_DAO
::VALUE_SEPARATOR
;
280 1 => [(string) $value, 'String'],
281 2 => [$dao->id
, 'Integer'],
283 CRM_Core_DAO
::executeQuery($updateSql, $params);
288 * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert
289 * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo")
291 * @param string $table
292 * @param string $column
294 public function flattenToFirstValue($table, $column) {
295 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
296 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
297 $dao = CRM_Core_DAO
::executeQuery($selectSql);
298 while ($dao->fetch()) {
299 $values = self
::explode($dao->{$column});
301 1 => [(string) array_shift($values), 'String'],
302 2 => [$dao->id
, 'Integer'],
304 CRM_Core_DAO
::executeQuery($updateSql, $params);
313 public static function explode($str) {
314 if (empty($str) ||
$str == CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Core_DAO
::VALUE_SEPARATOR
) {
318 return explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($str, CRM_Core_DAO
::VALUE_SEPARATOR
));