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