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