3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Create a page for displaying Custom Fields.
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
28 class CRM_Custom_Page_Field
extends CRM_Core_Page
{
30 public $useLivePageJS = TRUE;
33 * The group id of the field.
40 * The action links that we need to display for the browse screen.
44 private static $_actionLinks;
47 * Get the action links for this page.
50 * array of action links that we need to display for the browse screen
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'),
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'),
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'),
73 CRM_Core_Action
::DISABLE
=> [
74 'name' => ts('Disable'),
75 'ref' => 'crm-enable-disable',
76 'title' => ts('Disable Custom Field'),
78 CRM_Core_Action
::ENABLE
=> [
79 'name' => ts('Enable'),
80 'ref' => 'crm-enable-disable',
81 'title' => ts('Enable Custom Field'),
83 CRM_Core_Action
::EXPORT
=> [
85 'url' => 'civicrm/admin/custom/group/field/move',
86 'class' => 'small-popup',
87 'qs' => 'reset=1&fid=%%id%%',
88 'title' => ts('Move Custom Field'),
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'),
98 return self
::$_actionLinks;
102 * Browse all custom group fields.
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');
113 $customFieldBAO = new CRM_Core_BAO_CustomField();
116 $customFieldBAO->custom_group_id
= $this->_gid
;
117 $customFieldBAO->orderBy('weight, label');
118 $customFieldBAO->find();
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
;
128 $action -= CRM_Core_Action
::DISABLE
;
131 switch ($customFieldBAO->data_type
) {
136 // if Multi Select field is selected in custom field
137 if ($customFieldBAO->html_type
== 'Text') {
138 $action -= CRM_Core_Action
::BROWSE
;
142 case "ContactReference":
146 case "StateProvince":
150 $action -= CRM_Core_Action
::BROWSE
;
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,
159 'id' => $customFieldBAO->id
,
160 'gid' => $this->_gid
,
164 'customField.row.actions',
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
176 $this->assign('customField', $customField);
182 * editing would involved modifying existing fields + adding data to new fields.
184 * @param string $action
185 * The action to be invoked.
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);
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
));
197 $controller->set('gid', $this->_gid
);
198 $controller->setEmbedded(TRUE);
199 $controller->process();
206 * This method is called after the page is created. It checks for the
207 * type of action and executes that action.
211 public function run() {
213 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
218 $values = civicrm_api3('custom_field', 'getsingle', ['id' => $id]);
219 $this->_gid
= $values['custom_group_id'];
223 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
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.");
232 $action = CRM_Utils_Request
::retrieve('action', 'String',
233 // default to 'browse'
234 $this, FALSE, 'browse'
237 if ($action & CRM_Core_Action
::DELETE
) {
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',
245 $controller->set('id', $id);
246 $controller->setEmbedded(TRUE);
247 $controller->process();
249 $fieldValues = ['custom_group_id' => $this->_gid
];
250 $wt = CRM_Utils_Weight
::delWeight('CRM_Core_DAO_CustomField', $id, $fieldValues);
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]));
262 // assign vars to templates
263 $this->assign('action', $action);
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);
270 elseif ($action & CRM_Core_Action
::PREVIEW
) {
277 // Call the parents run method
278 return parent
::run();
282 * Preview custom field.
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();