Merge pull request #18517 from JMAConsulting/financial-issue-150
[civicrm-core.git] / CRM / Custom / Page / Field.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 * Create a page for displaying Custom Fields.
20 *
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
23 * page.
24 *
25 */
26 class CRM_Custom_Page_Field extends CRM_Core_Page {
27
28 public $useLivePageJS = TRUE;
29
30 /**
31 * The group id of the field.
32 *
33 * @var int
34 */
35 protected $_gid;
36
37 /**
38 * The action links that we need to display for the browse screen.
39 *
40 * @var array
41 */
42 private static $_actionLinks;
43
44 /**
45 * Get the action links for this page.
46 *
47 * @return array
48 * array of action links that we need to display for the browse screen
49 */
50 public static function &actionLinks() {
51 if (!isset(self::$_actionLinks)) {
52 self::$_actionLinks = [
53 CRM_Core_Action::UPDATE => [
54 'name' => ts('Edit Field'),
55 'url' => 'civicrm/admin/custom/group/field/update',
56 'qs' => 'action=update&reset=1&gid=%%gid%%&id=%%id%%',
57 'title' => ts('Edit Custom Field'),
58 ],
59 CRM_Core_Action::BROWSE => [
60 'name' => ts('Edit Multiple Choice Options'),
61 'url' => 'civicrm/admin/custom/group/field/option',
62 'qs' => 'reset=1&action=browse&gid=%%gid%%&fid=%%id%%',
63 'title' => ts('List Custom Options'),
64 ],
65 CRM_Core_Action::PREVIEW => [
66 'name' => ts('Preview Field Display'),
67 'url' => 'civicrm/admin/custom/group/field',
68 'qs' => 'action=preview&reset=1&gid=%%gid%%&id=%%id%%',
69 'title' => ts('Preview Custom Field'),
70 ],
71 CRM_Core_Action::DISABLE => [
72 'name' => ts('Disable'),
73 'ref' => 'crm-enable-disable',
74 'title' => ts('Disable Custom Field'),
75 ],
76 CRM_Core_Action::ENABLE => [
77 'name' => ts('Enable'),
78 'ref' => 'crm-enable-disable',
79 'title' => ts('Enable Custom Field'),
80 ],
81 CRM_Core_Action::EXPORT => [
82 'name' => ts('Move'),
83 'url' => 'civicrm/admin/custom/group/field/move',
84 'class' => 'small-popup',
85 'qs' => 'reset=1&fid=%%id%%',
86 'title' => ts('Move Custom Field'),
87 ],
88 CRM_Core_Action::DELETE => [
89 'name' => ts('Delete'),
90 'url' => 'civicrm/admin/custom/group/field',
91 'qs' => 'action=delete&reset=1&gid=%%gid%%&id=%%id%%',
92 'title' => ts('Delete Custom Field'),
93 ],
94 ];
95 }
96 return self::$_actionLinks;
97 }
98
99 /**
100 * Browse all custom group fields.
101 *
102 * @return void
103 */
104 public function browse() {
105 $resourceManager = CRM_Core_Resources::singleton();
106 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
107 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
108 }
109
110 $customField = [];
111 $customFieldBAO = new CRM_Core_BAO_CustomField();
112
113 // fkey is gid
114 $customFieldBAO->custom_group_id = $this->_gid;
115 $customFieldBAO->orderBy('weight, label');
116 $customFieldBAO->find();
117
118 while ($customFieldBAO->fetch()) {
119 $customField[$customFieldBAO->id] = [];
120 CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
121 $action = array_sum(array_keys(self::actionLinks()));
122 if ($customFieldBAO->is_active) {
123 $action -= CRM_Core_Action::ENABLE;
124 }
125 else {
126 $action -= CRM_Core_Action::DISABLE;
127 }
128
129 switch ($customFieldBAO->data_type) {
130 case "String":
131 case "Int":
132 case "Float":
133 case "Money":
134 // if Multi Select field is selected in custom field
135 if ($customFieldBAO->html_type == 'Text') {
136 $action -= CRM_Core_Action::BROWSE;
137 }
138 break;
139
140 case "ContactReference":
141 case "Memo":
142 case "Date":
143 case "Boolean":
144 case "StateProvince":
145 case "Country":
146 case "File":
147 case "Link":
148 $action -= CRM_Core_Action::BROWSE;
149 break;
150 }
151
152 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
153 $customField[$customFieldBAO->id]['data_type'] = $customFieldDataType[$customField[$customFieldBAO->id]['data_type']];
154 $customField[$customFieldBAO->id]['order'] = $customField[$customFieldBAO->id]['weight'];
155 $customField[$customFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
156 [
157 'id' => $customFieldBAO->id,
158 'gid' => $this->_gid,
159 ],
160 ts('more'),
161 FALSE,
162 'customField.row.actions',
163 'CustomField',
164 $customFieldBAO->id
165 );
166 }
167
168 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_gid}");
169 $filter = "custom_group_id = {$this->_gid}";
170 CRM_Utils_Weight::addOrder($customField, 'CRM_Core_DAO_CustomField',
171 'id', $returnURL, $filter
172 );
173
174 $this->assign('customField', $customField);
175 }
176
177 /**
178 * Edit custom data.
179 *
180 * editing would involved modifying existing fields + adding data to new fields.
181 *
182 * @param string $action
183 * The action to be invoked.
184 *
185 * @return void
186 */
187 public function edit($action) {
188 // create a simple controller for editing custom dataCRM/Custom/Page/Field.php
189 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Field', ts('Custom Field'), $action);
190
191 // set the userContext stack
192 $session = CRM_Core_Session::singleton();
193 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
194
195 $controller->set('gid', $this->_gid);
196 $controller->setEmbedded(TRUE);
197 $controller->process();
198 $controller->run();
199 }
200
201 /**
202 * Run the page.
203 *
204 * This method is called after the page is created. It checks for the
205 * type of action and executes that action.
206 *
207 * @return void
208 */
209 public function run() {
210
211 $id = CRM_Utils_Request::retrieve('id', 'Positive',
212 $this, FALSE, 0
213 );
214
215 if ($id) {
216 $values = civicrm_api3('custom_field', 'getsingle', ['id' => $id]);
217 $this->_gid = $values['custom_group_id'];
218 }
219 // get the group id
220 else {
221 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
222 $this
223 );
224 }
225
226 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
227 CRM_Core_Error::statusBounce("You cannot add or edit fields in a reserved custom field-set.");
228 }
229
230 $action = CRM_Utils_Request::retrieve('action', 'String',
231 // default to 'browse'
232 $this, FALSE, 'browse'
233 );
234
235 if ($action & CRM_Core_Action::DELETE) {
236
237 $session = CRM_Core_Session::singleton();
238 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
239 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteField', "Delete Custom Field", '');
240 $id = CRM_Utils_Request::retrieve('id', 'Positive',
241 $this, FALSE, 0
242 );
243 $controller->set('id', $id);
244 $controller->setEmbedded(TRUE);
245 $controller->process();
246 $controller->run();
247 $fieldValues = ['custom_group_id' => $this->_gid];
248 $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues);
249 }
250
251 if ($this->_gid) {
252 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid);
253 $this->assign('gid', $this->_gid);
254 $this->assign('groupTitle', $groupTitle);
255 if ($action & CRM_Core_Action::BROWSE) {
256 CRM_Utils_System::setTitle(ts('%1 - Custom Fields', [1 => $groupTitle]));
257 }
258 }
259
260 // assign vars to templates
261 $this->assign('action', $action);
262
263 // what action to take ?
264 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
265 // no browse for edit/update/view
266 $this->edit($action);
267 }
268 elseif ($action & CRM_Core_Action::PREVIEW) {
269 $this->preview($id);
270 }
271 else {
272 $this->browse();
273 }
274
275 // Call the parents run method
276 return parent::run();
277 }
278
279 /**
280 * Preview custom field.
281 *
282 * @param int $id
283 * Custom field id.
284 *
285 * @return void
286 */
287 public function preview($id) {
288 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), CRM_Core_Action::PREVIEW);
289 $session = CRM_Core_Session::singleton();
290 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&action=browse&gid=' . $this->_gid));
291 $controller->set('fieldId', $id);
292 $controller->set('groupId', $this->_gid);
293 $controller->setEmbedded(TRUE);
294 $controller->process();
295 $controller->run();
296 }
297
298 }