Merge pull request #17506 from mattwire/propertybagremovewarning
[civicrm-core.git] / CRM / Custom / Page / Option.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 * $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 */
28 class CRM_Custom_Page_Option extends CRM_Core_Page {
29
30 public $useLivePageJS = TRUE;
31
32 /**
33 * The Group id of the option
34 *
35 * @var int
36 */
37 protected $_gid;
38
39 /**
40 * The field id of the option
41 *
42 * @var int
43 */
44 protected $_fid;
45
46 /**
47 * The action links that we need to display for the browse screen
48 *
49 * @var array
50 */
51 private static $_actionLinks;
52
53 /**
54 * Get the action links for this page.
55 *
56 * @return array
57 * array of action links that we need to display for the browse screen
58 */
59 public static function &actionLinks() {
60 if (!isset(self::$_actionLinks)) {
61 self::$_actionLinks = [
62 CRM_Core_Action::UPDATE => [
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'),
67 ],
68 CRM_Core_Action::VIEW => [
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'),
73 ],
74 CRM_Core_Action::DISABLE => [
75 'name' => ts('Disable'),
76 'ref' => 'crm-enable-disable',
77 'title' => ts('Disable Multiple Choice Option'),
78 ],
79 CRM_Core_Action::ENABLE => [
80 'name' => ts('Enable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Enable Multiple Choice Option'),
83 ],
84 CRM_Core_Action::DELETE => [
85 'name' => ts('Delete'),
86 'url' => 'civicrm/admin/custom/group/field/option',
87 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
88 'title' => ts('Delete Multiple Choice Option'),
89 ],
90 ];
91 }
92 return self::$_actionLinks;
93 }
94
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 = "
106 SELECT id, label
107 FROM civicrm_option_value
108 WHERE option_group_id = %1";
109 $params = [
110 1 => [$optionGroupID, 'Integer'],
111 ];
112 $dao = CRM_Core_DAO::executeQuery($query, $params);
113 $optionValue = [];
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 = "
127 UPDATE civicrm_option_value
128 SET weight = CASE id
129 $when
130 END
131 WHERE option_group_id = %1";
132
133 $dao = CRM_Core_DAO::executeQuery($sql, $params);
134 }
135
136 /**
137 * Browse all custom group fields.
138 *
139 * @return void
140 */
141 public function browse() {
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 = "
150 SELECT id, label
151 FROM civicrm_custom_field
152 WHERE option_group_id = %1";
153 $params = [
154 1 => [$optionGroupID, 'Integer'],
155 2 => [$this->_fid, 'Integer'],
156 ];
157 $dao = CRM_Core_DAO::executeQuery($query, $params);
158 $reusedNames = [];
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',
165 [1 => $reusedNames]
166 );
167 CRM_Utils_System::setTitle($newTitle);
168 $this->assign('reusedNames', $reusedNames);
169 }
170 $this->assign('optionGroupID', $optionGroupID);
171 }
172
173 /**
174 * Edit custom Option.
175 *
176 * editing would involved modifying existing fields + adding data to new fields.
177 *
178 * @param string $action
179 * The action to be invoked.
180 *
181 * @return void
182 */
183 public function edit($action) {
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',
190 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
191 ));
192 $controller->setEmbedded(TRUE);
193 $controller->process();
194 $controller->run();
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 *
203 * @return void
204 */
205 public function run() {
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
215 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
216 CRM_Core_Error::statusBounce("You cannot add or edit multiple choice options in a reserved custom field-set.");
217 }
218
219 $optionGroupId = $this->getOptionGroupId($this->_fid);
220 $isOptionGroupLocked = $optionGroupId ? $this->isOptionGroupLocked($optionGroupId) : FALSE;
221 $this->assign('optionGroupId', $optionGroupId);
222 $this->assign('isOptionGroupLocked', $isOptionGroupLocked);
223
224 //as url contain $gid so append breadcrumb dynamically.
225 $breadcrumb = [
226 [
227 'title' => ts('Custom Data Fields'),
228 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid),
229 ],
230 ];
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);
238 CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', [1 => $fieldTitle]));
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
254 // take action in addition to default browse ?
255 if (($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
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 }
264 elseif ($action & CRM_Core_Action::MAP) {
265 $this->alphabetize();
266 }
267 $this->browse();
268
269 // Call the parents run method
270 return parent::run();
271 }
272
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
303 }