Merge pull request #17516 from eileenmcnaughton/pf_field
[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 * $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 */
28 class CRM_Custom_Page_Field extends CRM_Core_Page {
29
30 public $useLivePageJS = TRUE;
31
32 /**
33 * The group id of the field.
34 *
35 * @var int
36 */
37 protected $_gid;
38
39 /**
40 * The action links that we need to display for the browse screen.
41 *
42 * @var array
43 */
44 private static $_actionLinks;
45
46 /**
47 * Get the action links for this page.
48 *
49 * @return array
50 * array of action links that we need to display for the browse screen
51 */
52 public static function &actionLinks() {
53 if (!isset(self::$_actionLinks)) {
54 self::$_actionLinks = [
55 CRM_Core_Action::UPDATE => [
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'),
60 ],
61 CRM_Core_Action::BROWSE => [
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'),
66 ],
67 CRM_Core_Action::PREVIEW => [
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'),
72 ],
73 CRM_Core_Action::DISABLE => [
74 'name' => ts('Disable'),
75 'ref' => 'crm-enable-disable',
76 'title' => ts('Disable Custom Field'),
77 ],
78 CRM_Core_Action::ENABLE => [
79 'name' => ts('Enable'),
80 'ref' => 'crm-enable-disable',
81 'title' => ts('Enable Custom Field'),
82 ],
83 CRM_Core_Action::EXPORT => [
84 'name' => ts('Move'),
85 'url' => 'civicrm/admin/custom/group/field/move',
86 'class' => 'small-popup',
87 'qs' => 'reset=1&fid=%%id%%',
88 'title' => ts('Move Custom Field'),
89 ],
90 CRM_Core_Action::DELETE => [
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'),
95 ],
96 ];
97 }
98 return self::$_actionLinks;
99 }
100
101 /**
102 * Browse all custom group fields.
103 *
104 * @return void
105 */
106 public function browse() {
107 $resourceManager = CRM_Core_Resources::singleton();
108 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
109 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
110 }
111
112 $customField = [];
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()) {
121 $customField[$customFieldBAO->id] = [];
122 CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
123 $action = array_sum(array_keys(self::actionLinks()));
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,
158 [
159 'id' => $customFieldBAO->id,
160 'gid' => $this->_gid,
161 ],
162 ts('more'),
163 FALSE,
164 'customField.row.actions',
165 'CustomField',
166 $customFieldBAO->id
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 /**
180 * Edit custom data.
181 *
182 * editing would involved modifying existing fields + adding data to new fields.
183 *
184 * @param string $action
185 * The action to be invoked.
186 *
187 * @return void
188 */
189 public function edit($action) {
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 *
209 * @return void
210 */
211 public function run() {
212
213 $id = CRM_Utils_Request::retrieve('id', 'Positive',
214 $this, FALSE, 0
215 );
216
217 if ($id) {
218 $values = civicrm_api3('custom_field', 'getsingle', ['id' => $id]);
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
228 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
229 CRM_Core_Error::statusBounce("You cannot add or edit fields in a reserved custom field-set.");
230 }
231
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();
249 $fieldValues = ['custom_group_id' => $this->_gid];
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);
257 if ($action & CRM_Core_Action::BROWSE) {
258 CRM_Utils_System::setTitle(ts('%1 - Custom Fields', [1 => $groupTitle]));
259 }
260 }
261
262 // assign vars to templates
263 $this->assign('action', $action);
264
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 /**
282 * Preview custom field.
283 *
284 * @param int $id
285 * Custom field id.
286 *
287 * @return void
288 */
289 public function preview($id) {
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 }
299
300 }