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