OptionValues - Specify id+name+label for option callbacks that use it
[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 */
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 */
26class CRM_Custom_Page_Field extends CRM_Core_Page {
27
96f50de2
CW
28 public $useLivePageJS = TRUE;
29
6a488035 30 /**
fe482240 31 * The group id of the field.
6a488035
TO
32 *
33 * @var int
6a488035
TO
34 */
35 protected $_gid;
36
37 /**
fe482240 38 * The action links that we need to display for the browse screen.
6a488035
TO
39 *
40 * @var array
6a488035
TO
41 */
42 private static $_actionLinks;
43
44 /**
45 * Get the action links for this page.
46 *
a6c01b45
CW
47 * @return array
48 * array of action links that we need to display for the browse screen
eea16664 49 */
d1424f8f 50 public static function &actionLinks() {
6a488035 51 if (!isset(self::$_actionLinks)) {
be2fb01f
CW
52 self::$_actionLinks = [
53 CRM_Core_Action::UPDATE => [
6a488035
TO
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'),
be2fb01f
CW
58 ],
59 CRM_Core_Action::BROWSE => [
6a488035
TO
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'),
be2fb01f
CW
64 ],
65 CRM_Core_Action::PREVIEW => [
6a488035 66 'name' => ts('Preview Field Display'),
aca7613b
CW
67 'url' => 'civicrm/admin/custom/group/preview',
68 'qs' => 'action=preview&reset=1&fid=%%id%%',
6a488035 69 'title' => ts('Preview Custom Field'),
be2fb01f
CW
70 ],
71 CRM_Core_Action::DISABLE => [
6a488035 72 'name' => ts('Disable'),
12798ddc 73 'ref' => 'crm-enable-disable',
6a488035 74 'title' => ts('Disable Custom Field'),
be2fb01f
CW
75 ],
76 CRM_Core_Action::ENABLE => [
6a488035 77 'name' => ts('Enable'),
12798ddc 78 'ref' => 'crm-enable-disable',
6a488035 79 'title' => ts('Enable Custom Field'),
be2fb01f
CW
80 ],
81 CRM_Core_Action::EXPORT => [
6a488035
TO
82 'name' => ts('Move'),
83 'url' => 'civicrm/admin/custom/group/field/move',
704f21c0 84 'class' => 'small-popup',
6a488035
TO
85 'qs' => 'reset=1&fid=%%id%%',
86 'title' => ts('Move Custom Field'),
be2fb01f
CW
87 ],
88 CRM_Core_Action::DELETE => [
6a488035 89 'name' => ts('Delete'),
aca7613b
CW
90 'url' => 'civicrm/admin/custom/group/field/delete',
91 'qs' => 'reset=1&id=%%id%%',
6a488035 92 'title' => ts('Delete Custom Field'),
be2fb01f
CW
93 ],
94 ];
6a488035
TO
95 }
96 return self::$_actionLinks;
97 }
98
99 /**
100 * Browse all custom group fields.
101 *
6a488035 102 * @return void
6a488035 103 */
00be9182 104 public function browse() {
6f231148
CW
105 $resourceManager = CRM_Core_Resources::singleton();
106 if (!empty($_GET['new']) && $resourceManager->ajaxPopupsEnabled) {
96ed17aa 107 $resourceManager->addScriptFile('civicrm', 'js/crm.addNew.js', 999, 'html-header');
6f231148
CW
108 }
109
be2fb01f 110 $customField = [];
6a488035
TO
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()) {
be2fb01f 119 $customField[$customFieldBAO->id] = [];
6a488035 120 CRM_Core_DAO::storeValues($customFieldBAO, $customField[$customFieldBAO->id]);
d1424f8f 121 $action = array_sum(array_keys(self::actionLinks()));
6a488035
TO
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
c44d3d25 152 $customFieldDataType = array_column(CRM_Core_BAO_CustomField::dataType(), 'label', 'id');
6a488035
TO
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,
be2fb01f 156 [
6a488035
TO
157 'id' => $customFieldBAO->id,
158 'gid' => $this->_gid,
be2fb01f 159 ],
87dab4a4
AH
160 ts('more'),
161 FALSE,
162 'customField.row.actions',
163 'CustomField',
164 $customFieldBAO->id
6a488035
TO
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
6a488035
TO
177 /**
178 * Run the page.
179 *
180 * This method is called after the page is created. It checks for the
181 * type of action and executes that action.
182 *
6a488035 183 * @return void
6a488035 184 */
00be9182 185 public function run() {
aca7613b 186 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, TRUE);
6a488035 187
aca7613b 188 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved')) {
79e11805 189 CRM_Core_Error::statusBounce("You cannot add or edit fields in a reserved custom field-set.");
d06700a7
RN
190 }
191
aca7613b
CW
192 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_gid);
193 $this->assign('gid', $this->_gid);
194 $this->assign('groupTitle', $groupTitle);
6a488035
TO
195
196 // assign vars to templates
aca7613b 197 $this->assign('action', 'browse');
6a488035 198
aca7613b 199 $this->browse();
6a488035 200
6a488035
TO
201 return parent::run();
202 }
203
6a488035 204}