3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * Create a page for displaying Custom Options.
23 * Heart of this class is the run method which checks
24 * for action type and then displays the appropriate
28 class CRM_Custom_Page_Option
extends CRM_Core_Page
{
30 public $useLivePageJS = TRUE;
33 * The Group id of the option
40 * The field id of the option
47 * The action links that we need to display for the browse screen
51 private static $_actionLinks;
54 * Get the action links for this page.
57 * array of action links that we need to display for the browse screen
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'),
68 CRM_Core_Action
::VIEW
=> [
70 'url' => 'civicrm/admin/custom/group/field/option',
71 'qs' => 'action=view&id=%%id%%&fid=%%fid%%',
72 'title' => ts('View Multiple Choice Option'),
74 CRM_Core_Action
::DISABLE
=> [
75 'name' => ts('Disable'),
76 'ref' => 'crm-enable-disable',
77 'title' => ts('Disable Multiple Choice Option'),
79 CRM_Core_Action
::ENABLE
=> [
80 'name' => ts('Enable'),
81 'ref' => 'crm-enable-disable',
82 'title' => ts('Enable Multiple Choice Option'),
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'),
92 return self
::$_actionLinks;
96 * Alphabetize multiple option values
100 public function alphabetize() {
101 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
107 FROM civicrm_option_value
108 WHERE option_group_id = %1";
110 1 => [$optionGroupID, 'Integer'],
112 $dao = CRM_Core_DAO
::executeQuery($query, $params);
114 while ($dao->fetch()) {
115 $optionValue[$dao->id
] = $dao->label
;
117 asort($optionValue, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL
);
120 foreach ($optionValue as $key => $_) {
121 $clause[] = "WHEN $key THEN $i";
125 $when = implode(' ', $clause);
127 UPDATE civicrm_option_value
131 WHERE option_group_id = %1";
133 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
137 * Browse all custom group fields.
141 public function browse() {
143 // get the option group id
144 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
151 FROM civicrm_custom_field
152 WHERE option_group_id = %1";
154 1 => [$optionGroupID, 'Integer'],
155 2 => [$this->_fid
, 'Integer'],
157 $dao = CRM_Core_DAO
::executeQuery($query, $params);
160 while ($dao->fetch()) {
161 $reusedNames[] = $dao->label
;
163 $reusedNames = implode(', ', $reusedNames);
164 $newTitle = ts('%1 - Multiple Choice Options',
167 CRM_Utils_System
::setTitle($newTitle);
168 $this->assign('reusedNames', $reusedNames);
170 $this->assign('optionGroupID', $optionGroupID);
174 * Edit custom Option.
176 * editing would involved modifying existing fields + adding data to new fields.
178 * @param string $action
179 * The action to be invoked.
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);
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}"
192 $controller->setEmbedded(TRUE);
193 $controller->process();
200 * This method is called after the page is created. It checks for the
201 * type of action and executes that action.
205 public function run() {
208 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
211 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
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.");
219 $optionGroupId = $this->getOptionGroupId($this->_fid
);
220 $isOptionGroupLocked = $optionGroupId ?
$this->isOptionGroupLocked($optionGroupId) : FALSE;
221 $this->assign('optionGroupId', $optionGroupId);
222 $this->assign('isOptionGroupLocked', $isOptionGroupLocked);
224 //as url contain $gid so append breadcrumb dynamically.
227 'title' => ts('Custom Data Fields'),
228 'url' => CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid
),
231 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
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]));
241 // get the requested action
242 $action = CRM_Utils_Request
::retrieve('action', 'String',
243 // default to 'browse'
244 $this, FALSE, 'browse'
247 // assign vars to templates
248 $this->assign('action', $action);
250 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
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
261 // no browse for edit/update/view
262 $this->edit($action);
264 elseif ($action & CRM_Core_Action
::MAP
) {
265 $this->alphabetize();
269 // Call the parents run method
270 return parent
::run();
274 * Gets the "is_locked" status for the provided option group
276 * @param int $optionGroupId
280 private function isOptionGroupLocked($optionGroupId) {
281 return (bool) CRM_Core_DAO
::getFieldValue(
282 CRM_Core_DAO_OptionGroup
::class,
289 * Gets the associated "option_group_id" for a custom field
291 * @param int $customFieldId
295 private function getOptionGroupId($customFieldId) {
296 return (int) CRM_Core_DAO
::getFieldValue(
297 CRM_Core_DAO_CustomField
::class,