Merge pull request #22507 from colemanw/searchImportCleanup
[civicrm-core.git] / CRM / Custom / Form / MoveField.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is to build the form for Deleting Group
20 */
21 class CRM_Custom_Form_MoveField extends CRM_Core_Form {
22
23 /**
24 * The src group id.
25 *
26 * @var int
27 */
28 protected $_srcGID;
29
30 /**
31 * The src field id.
32 *
33 * @var int
34 */
35 protected $_srcFID;
36
37 /**
38 * The dst group id.
39 *
40 * @var int
41 */
42 protected $_dstGID;
43
44 /**
45 * The dst field id.
46 *
47 * @var int
48 */
49 protected $_dstFID;
50
51 /**
52 * The title of the field being moved.
53 *
54 * @var string
55 */
56 protected $_srcFieldLabel;
57
58 /**
59 * Set up variables to build the form.
60 *
61 * @return void
62 * @access protected
63 */
64 public function preProcess() {
65 $this->_srcFID = CRM_Utils_Request::retrieve('fid', 'Positive',
66 $this, TRUE
67 );
68
69 $this->_srcGID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
70 $this->_srcFID,
71 'custom_group_id'
72 );
73
74 $this->_srcFieldLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
75 $this->_srcFID,
76 'label'
77 );
78
79 $this->setTitle(ts('Custom Field Move: %1',
80 [1 => $this->_srcFieldLabel]
81 ));
82
83 $session = CRM_Core_Session::singleton();
84 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_srcGID}"));
85 }
86
87 /**
88 * Build the form object.
89 *
90 * @return void
91 */
92 public function buildQuickForm() {
93
94 $customGroup = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
95 unset($customGroup[$this->_srcGID]);
96 if (empty($customGroup)) {
97 CRM_Core_Error::statusBounce(ts('You need more than one custom group to move fields'));
98 }
99
100 $customGroup = [
101 '' => ts('- select -'),
102 ] + $customGroup;
103 $this->add('select',
104 'dst_group_id',
105 ts('Destination'),
106 $customGroup,
107 TRUE
108 );
109
110 $this->addButtons([
111 [
112 'type' => 'next',
113 'name' => ts('Move Custom Field'),
114 'isDefault' => TRUE,
115 ],
116 [
117 'type' => 'cancel',
118 'name' => ts('Cancel'),
119 ],
120 ]);
121
122 $this->addFormRule(['CRM_Custom_Form_MoveField', 'formRule'], $this);
123 }
124
125 /**
126 * @param $fields
127 * @param $files
128 * @param self $self
129 *
130 * @return array|bool
131 */
132 public static function formRule($fields, $files, $self) {
133 $self->_dstGID = $fields['dst_group_id'];
134 $tmp = CRM_Core_BAO_CustomField::_moveFieldValidate($self->_srcFID, $self->_dstGID);
135 $errors = [];
136 if ($tmp['newGroupID']) {
137 $errors['dst_group_id'] = $tmp['newGroupID'];
138 }
139 return empty($errors) ? TRUE : $errors;
140 }
141
142 /**
143 * Process the form when submitted.
144 *
145 * @return void
146 */
147 public function postProcess() {
148 CRM_Core_BAO_CustomField::moveField($this->_srcFID, $this->_dstGID);
149
150 $dstGroup = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
151 $this->_dstGID,
152 'title'
153 );
154 $srcUrl = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_dstGID}");
155 CRM_Core_Session::setStatus(ts("%1 has been moved to the custom set <a href='%3'>%2</a>.",
156 [
157 1 => $this->_srcFieldLabel,
158 2 => $dstGroup,
159 3 => $srcUrl,
160 ]), '', 'success');
161 }
162
163 }