3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 * Create a page for displaying Custom Options.
39 * Heart of this class is the run method which checks
40 * for action type and then displays the appropriate
44 class CRM_Custom_Page_Option
extends CRM_Core_Page
{
46 public $useLivePageJS = TRUE;
49 * The Group id of the option
56 * The field id of the option
63 * The action links that we need to display for the browse screen
67 private static $_actionLinks;
70 * Get the action links for this page.
73 * array of action links that we need to display for the browse screen
75 public static function &actionLinks() {
76 if (!isset(self
::$_actionLinks)) {
77 self
::$_actionLinks = array(
78 CRM_Core_Action
::UPDATE
=> array(
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'),
84 CRM_Core_Action
::VIEW
=> array(
86 'url' => 'civicrm/admin/custom/group/field/option',
87 'qs' => 'action=view&id=%%id%%&fid=%%fid%%',
88 'title' => ts('View Multiple Choice Option'),
90 CRM_Core_Action
::DISABLE
=> array(
91 'name' => ts('Disable'),
92 'ref' => 'crm-enable-disable',
93 'title' => ts('Disable Multiple Choice Option'),
95 CRM_Core_Action
::ENABLE
=> array(
96 'name' => ts('Enable'),
97 'ref' => 'crm-enable-disable',
98 'title' => ts('Enable Multiple Choice Option'),
100 CRM_Core_Action
::DELETE
=> array(
101 'name' => ts('Delete'),
102 'url' => 'civicrm/admin/custom/group/field/option',
103 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
104 'title' => ts('Delete Multiple Choice Option'),
108 return self
::$_actionLinks;
112 * Alphabetize multiple option values
116 public function alphabetize() {
117 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
123 FROM civicrm_option_value
124 WHERE option_group_id = %1";
126 1 => array($optionGroupID, 'Integer'),
128 $dao = CRM_Core_DAO
::executeQuery($query, $params);
129 $optionValue = array();
130 while ($dao->fetch()) {
131 $optionValue[$dao->id
] = $dao->label
;
133 asort($optionValue, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL
);
136 foreach ($optionValue as $key => $_) {
137 $clause[] = "WHEN $key THEN $i";
141 $when = implode(' ', $clause);
143 UPDATE civicrm_option_value
147 WHERE option_group_id = %1";
149 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
153 * Browse all custom group fields.
157 public function browse() {
159 // get the option group id
160 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
167 FROM civicrm_custom_field
168 WHERE option_group_id = %1";
170 1 => array($optionGroupID, 'Integer'),
171 2 => array($this->_fid
, 'Integer'),
173 $dao = CRM_Core_DAO
::executeQuery($query, $params);
174 $reusedNames = array();
176 while ($dao->fetch()) {
177 $reusedNames[] = $dao->label
;
179 $reusedNames = implode(', ', $reusedNames);
180 $newTitle = ts('%1 - Multiple Choice Options',
181 array(1 => $reusedNames)
183 CRM_Utils_System
::setTitle($newTitle);
184 $this->assign('reusedNames', $reusedNames);
186 $this->assign('optionGroupID', $optionGroupID);
190 * Edit custom Option.
192 * editing would involved modifying existing fields + adding data to new fields.
194 * @param string $action
195 * The action to be invoked.
199 public function edit($action) {
200 // create a simple controller for editing custom data
201 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
203 // set the userContext stack
204 $session = CRM_Core_Session
::singleton();
205 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
206 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
208 $controller->setEmbedded(TRUE);
209 $controller->process();
216 * This method is called after the page is created. It checks for the
217 * type of action and executes that action.
221 public function run() {
224 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
227 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
231 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
232 CRM_Core_Error
::fatal("You cannot add or edit multiple choice options in a reserved custom field-set.");
235 $optionGroupId = $this->getOptionGroupId($this->_fid
);
236 $isOptionGroupLocked = $optionGroupId ?
$this->isOptionGroupLocked($optionGroupId) : FALSE;
237 $this->assign('optionGroupId', $optionGroupId);
238 $this->assign('isOptionGroupLocked', $isOptionGroupLocked);
240 //as url contain $gid so append breadcrumb dynamically.
243 'title' => ts('Custom Data Fields'),
244 'url' => CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid
),
247 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
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);
254 CRM_Utils_System
::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle)));
257 // get the requested action
258 $action = CRM_Utils_Request
::retrieve('action', 'String',
259 // default to 'browse'
260 $this, FALSE, 'browse'
263 // assign vars to templates
264 $this->assign('action', $action);
266 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
270 // take action in addition to default browse ?
271 if (($action & (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD |
272 CRM_Core_Action
::VIEW | CRM_Core_Action
::DELETE
277 // no browse for edit/update/view
278 $this->edit($action);
280 elseif ($action & CRM_Core_Action
::MAP
) {
281 $this->alphabetize();
285 // Call the parents run method
286 return parent
::run();
290 * Gets the "is_locked" status for the provided option group
292 * @param int $optionGroupId
296 private function isOptionGroupLocked($optionGroupId) {
297 return (bool) CRM_Core_DAO
::getFieldValue(
298 CRM_Core_DAO_OptionGroup
::class,
305 * Gets the associated "option_group_id" for a custom field
307 * @param int $customFieldId
311 private function getOptionGroupId($customFieldId) {
312 return (int) CRM_Core_DAO
::getFieldValue(
313 CRM_Core_DAO_CustomField
::class,