SearchKit - Improve field/operator/value selection UI
[civicrm-core.git] / CRM / Admin / Page / Options.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 * Page for displaying list of Gender.
20 */
21 class CRM_Admin_Page_Options extends CRM_Core_Page_Basic {
22
23 public $useLivePageJS = TRUE;
24
25 /**
26 * The action links that we need to display for the browse screen.
27 *
28 * @var array
29 */
30 public static $_links = NULL;
31
32 /**
33 * The option group name.
34 *
35 * @var string
36 */
37 public static $_gName = NULL;
38
39 /**
40 * The option group name in display format (capitalized, without underscores...etc)
41 *
42 * @var string
43 */
44 public static $_gLabel = NULL;
45
46 /**
47 * The option group id.
48 *
49 * @var int
50 */
51 public static $_gId = NULL;
52
53 /**
54 * A boolean determining if you can add options to this group in the GUI.
55 *
56 * @var bool
57 */
58 public static $_isLocked = FALSE;
59
60 /**
61 * Obtains the group name from url string or id from $_GET['gid'].
62 *
63 * Sets the title.
64 */
65 public function preProcess() {
66 if (!self::$_gName && !empty($this->urlPath[3])) {
67 self::$_gName = $this->urlPath[3];
68 self::$_isLocked = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gName, 'is_locked', 'name');
69 }
70 // If an id arg is passed instead of a group name in the path
71 elseif (!self::$_gName && !empty($_GET['gid'])) {
72 self::$_gId = (int) $_GET['gid'];
73 self::$_gName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gId, 'name');
74 self::$_isLocked = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gId, 'is_locked');
75 $breadCrumb = [
76 'title' => ts('Option Groups'),
77 'url' => CRM_Utils_System::url('civicrm/admin/options', 'reset=1'),
78 ];
79 CRM_Utils_System::appendBreadCrumb([$breadCrumb]);
80 }
81 if (!self::$_gName) {
82 self::$_gName = $this->get('gName');
83 }
84 // If we don't have a group we will browse all groups
85 if (!self::$_gName) {
86 return;
87 }
88 $this->set('gName', self::$_gName);
89 if (!self::$_gId) {
90 self::$_gId = (int) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gName, 'id', 'name');
91 }
92
93 self::$_gLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', self::$_gId, 'title');
94 if (!self::$_gLabel) {
95 self::$_gLabel = ts('Option');
96 }
97
98 if (self::$_gName == 'acl_role') {
99 CRM_Utils_System::setTitle(ts('Manage ACL Roles'));
100 // set breadcrumb to append to admin/access
101 $breadCrumb = [
102 [
103 'title' => ts('Access Control'),
104 'url' => CRM_Utils_System::url('civicrm/admin/access', 'reset=1'),
105 ],
106 ];
107 CRM_Utils_System::appendBreadCrumb($breadCrumb);
108 }
109 else {
110 CRM_Utils_System::setTitle(ts("%1 Options", [1 => self::$_gLabel]));
111 }
112 $this->assign('showIsDefault', in_array(self::$_gName,
113 [
114 'from_email_address',
115 'email_greeting',
116 'postal_greeting',
117 'addressee',
118 'communication_style',
119 'case_status',
120 'encounter_medium',
121 'case_type',
122 'payment_instrument',
123 'soft_credit_type',
124 'website_type',
125 ]
126 ));
127
128 $this->assign('showCounted', self::$_gName === 'participant_role');
129 $this->assign('isLocked', self::$_isLocked);
130 $this->assign('allowLoggedIn', Civi::settings()->get('allow_mail_from_logged_in_contact'));
131 $this->assign('showComponent', self::$_gName === 'activity_type');
132 $this->assign('gName', self::$_gName);
133 $this->assign('gLabel', self::$_gLabel);
134 }
135
136 /**
137 * Get BAO Name.
138 *
139 * @return string
140 * Classname of BAO.
141 */
142 public function getBAOName() {
143 return self::$_gName ? 'CRM_Core_BAO_OptionValue' : 'CRM_Core_BAO_OptionGroup';
144 }
145
146 /**
147 * Get action Links.
148 *
149 * @return array
150 * (reference) of action links
151 */
152 public function &links() {
153 if (!self::$_links) {
154 self::$_links = [
155 CRM_Core_Action::UPDATE => [
156 'name' => ts('Edit'),
157 'url' => 'civicrm/admin/options/' . self::$_gName,
158 'qs' => 'action=update&id=%%id%%&reset=1',
159 'title' => ts('Edit %1', [1 => self::$_gName]),
160 ],
161 CRM_Core_Action::DISABLE => [
162 'name' => ts('Disable'),
163 'ref' => 'crm-enable-disable',
164 'title' => ts('Disable %1', [1 => self::$_gName]),
165 ],
166 CRM_Core_Action::ENABLE => [
167 'name' => ts('Enable'),
168 'ref' => 'crm-enable-disable',
169 'title' => ts('Enable %1', [1 => self::$_gName]),
170 ],
171 CRM_Core_Action::DELETE => [
172 'name' => ts('Delete'),
173 'url' => 'civicrm/admin/options/' . self::$_gName,
174 'qs' => 'action=delete&id=%%id%%',
175 'title' => ts('Delete %1 Type', [1 => self::$_gName]),
176 ],
177 ];
178
179 if (self::$_gName == 'custom_search') {
180 $runLink = [
181 CRM_Core_Action::FOLLOWUP => [
182 'name' => ts('Run'),
183 'url' => 'civicrm/contact/search/custom',
184 'qs' => 'reset=1&csid=%%value%%',
185 'title' => ts('Run %1', [1 => self::$_gName]),
186 'class' => 'no-popup',
187 ],
188 ];
189 self::$_links = $runLink + self::$_links;
190 }
191 }
192 return self::$_links;
193 }
194
195 /**
196 * Run the basic page (run essentially starts execution for that page).
197 */
198 public function run() {
199 $this->preProcess();
200 return parent::run();
201 }
202
203 /**
204 * Browse all options.
205 */
206 public function browse() {
207 if (!self::$_gName) {
208 return parent::browse();
209 }
210 $groupParams = ['name' => self::$_gName];
211 $optionValue = CRM_Core_OptionValue::getRows($groupParams, $this->links(), 'component_id,weight');
212 $gName = self::$_gName;
213 $returnURL = CRM_Utils_System::url("civicrm/admin/options/$gName",
214 "reset=1&group=$gName"
215 );
216 $filter = "option_group_id = " . self::$_gId;
217 CRM_Utils_Weight::addOrder($optionValue, 'CRM_Core_DAO_OptionValue',
218 'id', $returnURL, $filter
219 );
220 $this->assign('hasIcons', FALSE);
221
222 // retrieve financial account name for the payment method page
223 foreach ($optionValue as $key => $option) {
224 if ($gName === 'payment_instrument') {
225 $optionValue[$key]['financial_account'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($key, NULL, 'civicrm_option_value', 'financial_account_id.name');
226 }
227 foreach (['weight', 'description', 'value', 'color', 'label', 'is_default', 'icon'] as $expectedKey) {
228 if (!array_key_exists($expectedKey, $option)) {
229 $optionValue[$key][$expectedKey] = NULL;
230 }
231 }
232 if ($option['icon']) {
233 $this->assign('hasIcons', TRUE);
234 }
235 }
236 $this->assign('rows', $optionValue);
237 }
238
239 /**
240 * Get name of edit form.
241 *
242 * @return string
243 * Classname of edit form.
244 */
245 public function editForm() {
246 return self::$_gName ? 'CRM_Admin_Form_Options' : 'CRM_Admin_Form_OptionGroup';
247 }
248
249 /**
250 * Get edit form name.
251 *
252 * @return string
253 * name of this page.
254 */
255 public function editName() {
256 return self::$_gLabel;
257 }
258
259 /**
260 * Get user context.
261 *
262 * @param null $mode
263 *
264 * @return string
265 * user context.
266 */
267 public function userContext($mode = NULL) {
268 return 'civicrm/admin/options' . (self::$_gName ? '/' . self::$_gName : '');
269 }
270
271 }