3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class is to build the form for Deleting Group
23 class CRM_Custom_Form_ChangeFieldType
extends CRM_Core_Form
{
33 * Array of custom field values
39 * Mapper array of valid field type
42 protected $_htmlTypeTransitions;
45 * Set up variables to build the form.
50 public function preProcess() {
51 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
56 $params = ['id' => $this->_id
];
57 CRM_Core_BAO_CustomField
::retrieve($params, $this->_values
);
59 $this->_htmlTypeTransitions
= self
::fieldTypeTransitions(CRM_Utils_Array
::value('data_type', $this->_values
),
60 CRM_Utils_Array
::value('html_type', $this->_values
)
63 if (empty($this->_values
) ||
empty($this->_htmlTypeTransitions
)) {
64 CRM_Core_Error
::fatal(ts("Invalid custom field or can't change input type of this custom field."));
67 $url = CRM_Utils_System
::url('civicrm/admin/custom/group/field/update',
68 "action=update&reset=1&gid={$this->_values['custom_group_id']}&id={$this->_id}"
70 $session = CRM_Core_Session
::singleton();
71 $session->pushUserContext($url);
73 CRM_Utils_System
::setTitle(ts('Change Field Type: %1',
74 [1 => $this->_values
['label']]
79 * Build the form object.
83 public function buildQuickForm() {
85 $srcHtmlType = $this->add('select',
87 ts('Current HTML Type'),
88 [$this->_values
['html_type'] => $this->_values
['html_type']],
92 $srcHtmlType->setValue($this->_values
['html_type']);
93 $srcHtmlType->freeze();
95 $this->assign('srcHtmlType', $this->_values
['html_type']);
97 $dstHtmlType = $this->add('select',
101 '' => ts('- select -'),
102 ] +
$this->_htmlTypeTransitions
,
109 'name' => ts('Change Field Type'),
111 'js' => ['onclick' => 'return checkCustomDataField();'],
115 'name' => ts('Cancel'),
121 * Process the form when submitted.
125 public function postProcess() {
126 $params = $this->controller
->exportValues($this->_name
);
128 $tableName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup',
129 $this->_values
['custom_group_id'],
137 'Autocomplete-Select',
145 $srcHtmlType = $this->_values
['html_type'];
146 $dstHtmlType = $params['dst_html_type'];
148 $customField = new CRM_Core_DAO_CustomField();
149 $customField->id
= $this->_id
;
150 $customField->find(TRUE);
152 if ($dstHtmlType == 'Text' && in_array($srcHtmlType, [
155 'Autocomplete-Select',
157 $customField->option_group_id
= "NULL";
158 CRM_Core_BAO_CustomField
::checkOptionGroup($this->_values
['option_group_id']);
161 if (in_array($srcHtmlType, $mutliValueOps) &&
162 in_array($dstHtmlType, $singleValueOps)) {
163 $this->flattenToFirstValue($tableName, $this->_values
['column_name']);
165 elseif (in_array($srcHtmlType, $singleValueOps) &&
166 in_array($dstHtmlType, $mutliValueOps)) {
167 $this->firstValueToFlatten($tableName, $this->_values
['column_name']);
170 $customField->html_type
= $dstHtmlType;
171 $customField->save();
173 // Reset cache for custom fields
174 Civi
::cache('fields')->flush();
175 // reset ACL and system caches.
176 CRM_Core_BAO_Cache
::resetCaches();
178 CRM_Core_Session
::setStatus(ts('Input type of custom field \'%1\' has been successfully changed to \'%2\'.',
179 [1 => $this->_values
['label'], 2 => $dstHtmlType]
180 ), ts('Field Type Changed'), 'success');
189 public static function fieldTypeTransitions($dataType, $htmlType) {
190 // Text field is single value field,
191 // can not be change to other single value option which contains option group
192 if ($htmlType == 'Text') {
198 'Select' => 'Select',
200 'Autocomplete-Select' => 'Autocomplete-Select',
204 'CheckBox' => 'CheckBox',
205 'Multi-Select' => 'Multi-Select',
210 if (in_array($htmlType, array_keys($singleValueOps))) {
211 unset($singleValueOps[$htmlType]);
212 return array_merge($singleValueOps, $mutliValueOps);
214 elseif (in_array($htmlType, array_keys($mutliValueOps))) {
215 unset($singleValueOps['Text']);
216 foreach ($singleValueOps as $type => $label) {
217 $singleValueOps[$type] = "{$label} ( " . ts('Not Safe') . " )";
219 unset($mutliValueOps[$htmlType]);
220 return array_merge($mutliValueOps, $singleValueOps);
228 if (in_array($htmlType, array_keys($singleValueOps))) {
229 unset($singleValueOps[$htmlType]);
230 return $singleValueOps;
236 'TextArea' => 'TextArea',
237 'RichTextEditor' => 'RichTextEditor',
239 if (in_array($htmlType, array_keys($ops))) {
240 unset($ops[$htmlType]);
250 * Take a single-value column (eg: a Radio or Select etc ) and convert
251 * value to the multi listed value (eg:"^Foo^")
253 * @param string $table
254 * @param string $column
256 public function firstValueToFlatten($table, $column) {
257 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
258 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
259 $dao = CRM_Core_DAO
::executeQuery($selectSql);
260 while ($dao->fetch()) {
261 if (!$dao->{$column}) {
264 $value = CRM_Core_DAO
::VALUE_SEPARATOR
. $dao->{$column} . CRM_Core_DAO
::VALUE_SEPARATOR
;
266 1 => [(string) $value, 'String'],
267 2 => [$dao->id
, 'Integer'],
269 CRM_Core_DAO
::executeQuery($updateSql, $params);
274 * Take a multi-value column (e.g. a Multi-Select or CheckBox column), and convert
275 * all values (of the form "^^" or "^Foo^" or "^Foo^Bar^") to the first listed value ("Foo")
277 * @param string $table
278 * @param string $column
280 public function flattenToFirstValue($table, $column) {
281 $selectSql = "SELECT id, $column FROM $table WHERE $column IS NOT NULL";
282 $updateSql = "UPDATE $table SET $column = %1 WHERE id = %2";
283 $dao = CRM_Core_DAO
::executeQuery($selectSql);
284 while ($dao->fetch()) {
285 $values = self
::explode($dao->{$column});
287 1 => [(string) array_shift($values), 'String'],
288 2 => [$dao->id
, 'Integer'],
290 CRM_Core_DAO
::executeQuery($updateSql, $params);
299 public static function explode($str) {
300 if (empty($str) ||
$str == CRM_Core_DAO
::VALUE_SEPARATOR
. CRM_Core_DAO
::VALUE_SEPARATOR
) {
304 return explode(CRM_Core_DAO
::VALUE_SEPARATOR
, trim($str, CRM_Core_DAO
::VALUE_SEPARATOR
));