3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
57 * The field id of the option
65 * The action links that we need to display for the browse screen
70 private static $_actionLinks;
73 * Get the action links for this page.
77 * @return array array of action links that we need to display for the browse screen
80 function &actionLinks() {
81 if (!isset(self
::$_actionLinks)) {
82 self
::$_actionLinks = array(
83 CRM_Core_Action
::UPDATE
=> array(
84 'name' => ts('Edit Option'),
85 'url' => 'civicrm/admin/custom/group/field/option',
86 'qs' => 'reset=1&action=update&id=%%id%%&fid=%%fid%%&gid=%%gid%%',
87 'title' => ts('Edit Multiple Choice Option'),
89 CRM_Core_Action
::VIEW
=> array(
91 'url' => 'civicrm/admin/custom/group/field/option',
92 'qs' => 'action=view&id=%%id%%&fid=%%fid%%',
93 'title' => ts('View Multiple Choice Option'),
95 CRM_Core_Action
::DISABLE
=> array(
96 'name' => ts('Disable'),
97 'ref' => 'crm-enable-disable',
98 'title' => ts('Disable Mutliple Choice Option'),
100 CRM_Core_Action
::ENABLE
=> array(
101 'name' => ts('Enable'),
102 'ref' => 'crm-enable-disable',
103 'title' => ts('Enable Mutliple Choice Option'),
105 CRM_Core_Action
::DELETE
=> array(
106 'name' => ts('Delete'),
107 'url' => 'civicrm/admin/custom/group/field/option',
108 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
109 'title' => ts('Disable Multiple Choice Option'),
113 return self
::$_actionLinks;
117 * Browse all custom group fields.
125 //get the default value from custom fields
126 $customFieldBAO = new CRM_Core_BAO_CustomField();
127 $customFieldBAO->id
= $this->_fid
;
128 if ($customFieldBAO->find(TRUE)) {
129 $defaultValue = $customFieldBAO->default_value
;
130 $fieldHtmlType = $customFieldBAO->html_type
;
133 CRM_Core_Error
::fatal();
135 $defVal = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
136 substr($defaultValue, 1, -1)
139 // get the option group id
140 $optionGroupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField',
147 FROM civicrm_custom_field
148 WHERE option_group_id = %1";
149 $params = array(1 => array($optionGroupID, 'Integer'),
150 2 => array($this->_fid
, 'Integer'),
152 $dao = CRM_Core_DAO
::executeQuery($query, $params);
153 $reusedNames = array();
155 while ($dao->fetch()) {
156 $reusedNames[] = $dao->label
;
158 $reusedNames = implode(', ', $reusedNames);
159 $newTitle = ts('%1 - Multiple Choice Options',
160 array(1 => $reusedNames)
162 CRM_Utils_System
::setTitle($newTitle);
163 $this->assign('reusedNames', $reusedNames);
168 FROM civicrm_option_value
169 WHERE option_group_id = %1
170 ORDER BY weight, label
172 $params = array(1 => array($optionGroupID, 'Integer'));
173 $dao = CRM_Core_DAO
::executeQuery($query, $params);
175 $customOption = array();
176 $fields = array('label', 'value', 'is_active', 'weight');
177 $config = CRM_Core_Config
::singleton();
178 while ($dao->fetch()) {
179 $customOption[$dao->id
] = array();
180 foreach ($fields as $field) {
181 $customOption[$dao->id
][$field] = $dao->$field;
184 $action = array_sum(array_keys($this->actionLinks()));
186 // update enable/disable links depending on custom_field properties.
187 if ($dao->is_active
) {
188 $action -= CRM_Core_Action
::ENABLE
;
191 $action -= CRM_Core_Action
::DISABLE
;
194 if ($fieldHtmlType == 'CheckBox' ||
195 $fieldHtmlType == 'AdvMulti-Select' ||
196 $fieldHtmlType == 'Multi-Select'
198 if (in_array($dao->value
, $defVal)) {
199 $customOption[$dao->id
]['default_value'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
202 $customOption[$dao->id
]['default_value'] = '';
206 if ($defaultValue == $dao->value
) {
207 $customOption[$dao->id
]['default_value'] = '<img src="' . $config->resourceBase
. 'i/check.gif" />';
210 $customOption[$dao->id
]['default_value'] = '';
214 $customOption[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(),
218 'fid' => $this->_fid
,
219 'gid' => $this->_gid
,
223 'customOption.row.actions',
229 // Add order changing widget to selector
230 $returnURL = CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
231 "reset=1&action=browse&gid={$this->_gid}&fid={$this->_fid}"
233 $filter = "option_group_id = {$optionGroupID}";
234 CRM_Utils_Weight
::addOrder($customOption, 'CRM_Core_DAO_OptionValue',
235 'id', $returnURL, $filter
237 $this->assign('customOption', $customOption);
241 * Edit custom Option.
243 * editing would involved modifying existing fields + adding data to new fields.
245 * @param string $action the action to be invoked
250 function edit($action) {
251 // create a simple controller for editing custom data
252 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
254 // set the userContext stack
255 $session = CRM_Core_Session
::singleton();
256 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/custom/group/field/option',
257 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
259 $controller->setEmbedded(TRUE);
260 $controller->process();
268 * This method is called after the page is created. It checks for the
269 * type of action and executes that action.
279 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
282 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
286 if ($isReserved = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid
, 'is_reserved', 'id')) {
287 CRM_Core_Error
::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
290 //as url contain $gid so append breadcrumb dynamically.
291 $breadcrumb = array(array('title' => ts('Custom Data Fields'),
292 'url' => CRM_Utils_System
::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid
),
294 CRM_Utils_System
::appendBreadCrumb($breadcrumb);
297 $fieldTitle = CRM_Core_BAO_CustomField
::getTitle($this->_fid
);
298 $this->assign('fid', $this->_fid
);
299 $this->assign('gid', $this->_gid
);
300 $this->assign('fieldTitle', $fieldTitle);
301 CRM_Utils_System
::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle)));
304 // get the requested action
305 $action = CRM_Utils_Request
::retrieve('action', 'String',
306 // default to 'browse'
307 $this, FALSE, 'browse'
310 // assign vars to templates
311 $this->assign('action', $action);
313 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
318 // what action to take ?
320 (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD |
321 CRM_Core_Action
::VIEW | CRM_Core_Action
::DELETE
326 // no browse for edit/update/view
327 $this->edit($action);
332 // Call the parents run method
333 return parent
::run();