Merge pull request #17679 from civicrm/5.27
[civicrm-core.git] / CRM / Custom / Page / Option.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 Options.
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_Option extends CRM_Core_Page {
29
96f50de2
CW
30 public $useLivePageJS = TRUE;
31
6a488035
TO
32 /**
33 * The Group id of the option
34 *
35 * @var int
6a488035
TO
36 */
37 protected $_gid;
38
39 /**
40 * The field id of the option
41 *
42 * @var int
6a488035
TO
43 */
44 protected $_fid;
45
46 /**
47 * The action links that we need to display for the browse screen
48 *
49 * @var array
6a488035
TO
50 */
51 private static $_actionLinks;
52
53 /**
54 * Get the action links for this page.
55 *
a6c01b45
CW
56 * @return array
57 * array of action links that we need to display for the browse screen
6a488035 58 */
d1424f8f 59 public static function &actionLinks() {
6a488035 60 if (!isset(self::$_actionLinks)) {
be2fb01f
CW
61 self::$_actionLinks = [
62 CRM_Core_Action::UPDATE => [
6a488035
TO
63 'name' => ts('Edit Option'),
64 'url' => 'civicrm/admin/custom/group/field/option',
65 'qs' => 'reset=1&action=update&id=%%id%%&fid=%%fid%%&gid=%%gid%%',
66 'title' => ts('Edit Multiple Choice Option'),
be2fb01f
CW
67 ],
68 CRM_Core_Action::VIEW => [
6a488035
TO
69 'name' => ts('View'),
70 'url' => 'civicrm/admin/custom/group/field/option',
71 'qs' => 'action=view&id=%%id%%&fid=%%fid%%',
72 'title' => ts('View Multiple Choice Option'),
be2fb01f
CW
73 ],
74 CRM_Core_Action::DISABLE => [
6a488035 75 'name' => ts('Disable'),
4d17a233 76 'ref' => 'crm-enable-disable',
aa594a79 77 'title' => ts('Disable Multiple Choice Option'),
be2fb01f
CW
78 ],
79 CRM_Core_Action::ENABLE => [
6a488035 80 'name' => ts('Enable'),
4d17a233 81 'ref' => 'crm-enable-disable',
aa594a79 82 'title' => ts('Enable Multiple Choice Option'),
be2fb01f
CW
83 ],
84 CRM_Core_Action::DELETE => [
6a488035
TO
85 'name' => ts('Delete'),
86 'url' => 'civicrm/admin/custom/group/field/option',
87 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
aa594a79 88 'title' => ts('Delete Multiple Choice Option'),
be2fb01f
CW
89 ],
90 ];
6a488035
TO
91 }
92 return self::$_actionLinks;
93 }
94
5d129bb1 95 /**
96 * Alphabetize multiple option values
97 *
98 * @return void
99 */
100 public function alphabetize() {
101 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
102 $this->_fid,
103 'option_group_id'
104 );
105 $query = "
106SELECT id, label
107FROM civicrm_option_value
108WHERE option_group_id = %1";
be2fb01f
CW
109 $params = [
110 1 => [$optionGroupID, 'Integer'],
111 ];
5d129bb1 112 $dao = CRM_Core_DAO::executeQuery($query, $params);
be2fb01f 113 $optionValue = [];
5d129bb1 114 while ($dao->fetch()) {
115 $optionValue[$dao->id] = $dao->label;
116 }
117 asort($optionValue, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL);
118
119 $i = 1;
120 foreach ($optionValue as $key => $_) {
121 $clause[] = "WHEN $key THEN $i";
122 $i++;
123 }
124
125 $when = implode(' ', $clause);
126 $sql = "
127UPDATE civicrm_option_value
128SET weight = CASE id
129$when
130END
131WHERE option_group_id = %1";
132
133 $dao = CRM_Core_DAO::executeQuery($sql, $params);
134 }
135
6a488035
TO
136 /**
137 * Browse all custom group fields.
138 *
6a488035 139 * @return void
6a488035 140 */
00be9182 141 public function browse() {
6a488035
TO
142
143 // get the option group id
144 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
145 $this->_fid,
146 'option_group_id'
147 );
148
149 $query = "
150SELECT id, label
151FROM civicrm_custom_field
152WHERE option_group_id = %1";
be2fb01f
CW
153 $params = [
154 1 => [$optionGroupID, 'Integer'],
155 2 => [$this->_fid, 'Integer'],
156 ];
6a488035 157 $dao = CRM_Core_DAO::executeQuery($query, $params);
be2fb01f 158 $reusedNames = [];
6a488035
TO
159 if ($dao->N > 1) {
160 while ($dao->fetch()) {
161 $reusedNames[] = $dao->label;
162 }
163 $reusedNames = implode(', ', $reusedNames);
164 $newTitle = ts('%1 - Multiple Choice Options',
be2fb01f 165 [1 => $reusedNames]
6a488035
TO
166 );
167 CRM_Utils_System::setTitle($newTitle);
168 $this->assign('reusedNames', $reusedNames);
169 }
46227b8d 170 $this->assign('optionGroupID', $optionGroupID);
6a488035
TO
171 }
172
173 /**
100fef9d 174 * Edit custom Option.
6a488035
TO
175 *
176 * editing would involved modifying existing fields + adding data to new fields.
177 *
c4ca4892
TO
178 * @param string $action
179 * The action to be invoked.
6a488035
TO
180 *
181 * @return void
6a488035 182 */
00be9182 183 public function edit($action) {
6a488035
TO
184 // create a simple controller for editing custom data
185 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
186
187 // set the userContext stack
188 $session = CRM_Core_Session::singleton();
189 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
353ffa53
TO
190 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
191 ));
6a488035
TO
192 $controller->setEmbedded(TRUE);
193 $controller->process();
194 $controller->run();
6a488035
TO
195 }
196
197 /**
198 * Run the page.
199 *
200 * This method is called after the page is created. It checks for the
201 * type of action and executes that action.
202 *
6a488035 203 * @return void
6a488035 204 */
00be9182 205 public function run() {
6a488035
TO
206
207 // get the field id
208 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
209 $this, FALSE, 0
210 );
211 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
212 $this, FALSE, 0
213 );
214
d06700a7 215 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
79e11805 216 CRM_Core_Error::statusBounce("You cannot add or edit multiple choice options in a reserved custom field-set.");
d06700a7
RN
217 }
218
c352113c
MD
219 $optionGroupId = $this->getOptionGroupId($this->_fid);
220 $isOptionGroupLocked = $optionGroupId ? $this->isOptionGroupLocked($optionGroupId) : FALSE;
221 $this->assign('optionGroupId', $optionGroupId);
222 $this->assign('isOptionGroupLocked', $isOptionGroupLocked);
223
6a488035 224 //as url contain $gid so append breadcrumb dynamically.
be2fb01f
CW
225 $breadcrumb = [
226 [
353ffa53 227 'title' => ts('Custom Data Fields'),
6a488035 228 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid),
be2fb01f
CW
229 ],
230 ];
6a488035
TO
231 CRM_Utils_System::appendBreadCrumb($breadcrumb);
232
233 if ($this->_fid) {
234 $fieldTitle = CRM_Core_BAO_CustomField::getTitle($this->_fid);
235 $this->assign('fid', $this->_fid);
236 $this->assign('gid', $this->_gid);
237 $this->assign('fieldTitle', $fieldTitle);
be2fb01f 238 CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', [1 => $fieldTitle]));
6a488035
TO
239 }
240
241 // get the requested action
242 $action = CRM_Utils_Request::retrieve('action', 'String',
243 // default to 'browse'
244 $this, FALSE, 'browse'
245 );
246
247 // assign vars to templates
248 $this->assign('action', $action);
249
250 $id = CRM_Utils_Request::retrieve('id', 'Positive',
251 $this, FALSE, 0
252 );
253
49217a42 254 // take action in addition to default browse ?
e8646905 255 if (($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
6a488035
TO
256 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
257 )
258 ) ||
259 !empty($_POST)
260 ) {
261 // no browse for edit/update/view
262 $this->edit($action);
263 }
1672a5d6 264 elseif ($action & CRM_Core_Action::MAP) {
5d129bb1 265 $this->alphabetize();
266 }
9e5724d5
ME
267 $this->browse();
268
6a488035
TO
269 // Call the parents run method
270 return parent::run();
271 }
96025800 272
c352113c
MD
273 /**
274 * Gets the "is_locked" status for the provided option group
275 *
276 * @param int $optionGroupId
277 *
278 * @return bool
279 */
280 private function isOptionGroupLocked($optionGroupId) {
281 return (bool) CRM_Core_DAO::getFieldValue(
282 CRM_Core_DAO_OptionGroup::class,
283 $optionGroupId,
284 'is_locked'
285 );
286 }
287
288 /**
289 * Gets the associated "option_group_id" for a custom field
290 *
291 * @param int $customFieldId
292 *
293 * @return int
294 */
295 private function getOptionGroupId($customFieldId) {
296 return (int) CRM_Core_DAO::getFieldValue(
297 CRM_Core_DAO_CustomField::class,
298 $customFieldId,
299 'option_group_id'
300 );
301 }
302
6a488035 303}