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