CRM-13863 - Added setting to disable ajax popups
[civicrm-core.git] / CRM / Custom / Page / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
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
46 /**
47 * The Group id of the option
48 *
49 * @var int
50 * @access protected
51 */
52 protected $_gid;
53
54 /**
55 * The field id of the option
56 *
57 * @var int
58 * @access protected
59 */
60 protected $_fid;
61
62 /**
63 * The action links that we need to display for the browse screen
64 *
65 * @var array
66 * @access private
67 */
68 private static $_actionLinks;
69
70 /**
71 * Get the action links for this page.
72 *
73 * @param null
74 *
75 * @return array array of action links that we need to display for the browse screen
76 * @access public
77 */
78 function &actionLinks() {
79 if (!isset(self::$_actionLinks)) {
80 self::$_actionLinks = array(
81 CRM_Core_Action::UPDATE => array(
82 'name' => ts('Edit Option'),
83 'url' => 'civicrm/admin/custom/group/field/option',
84 'qs' => 'reset=1&action=update&id=%%id%%&fid=%%fid%%&gid=%%gid%%',
85 'title' => ts('Edit Multiple Choice Option'),
86 ),
87 CRM_Core_Action::VIEW => array(
88 'name' => ts('View'),
89 'url' => 'civicrm/admin/custom/group/field/option',
90 'qs' => 'action=view&id=%%id%%&fid=%%fid%%',
91 'title' => ts('View Multiple Choice Option'),
92 ),
93 CRM_Core_Action::DISABLE => array(
94 'name' => ts('Disable'),
95 'ref' => 'crm-enable-disable',
96 'title' => ts('Disable Mutliple Choice Option'),
97 ),
98 CRM_Core_Action::ENABLE => array(
99 'name' => ts('Enable'),
100 'ref' => 'crm-enable-disable',
101 'title' => ts('Enable Mutliple Choice Option'),
102 ),
103 CRM_Core_Action::DELETE => array(
104 'name' => ts('Delete'),
105 'url' => 'civicrm/admin/custom/group/field/option',
106 'qs' => 'action=delete&id=%%id%%&fid=%%fid%%',
107 'title' => ts('Disable Multiple Choice Option'),
108 ),
109 );
110 }
111 return self::$_actionLinks;
112 }
113
114 /**
115 * Browse all custom group fields.
116 *
117 * @param null
118 *
119 * @return void
120 * @access public
121 */
122 function browse() {
123 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'ajax_popups_enabled', NULL, TRUE)) {
124 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
125 }
126 //get the default value from custom fields
127 $customFieldBAO = new CRM_Core_BAO_CustomField();
128 $customFieldBAO->id = $this->_fid;
129 if ($customFieldBAO->find(TRUE)) {
130 $defaultValue = $customFieldBAO->default_value;
131 $fieldHtmlType = $customFieldBAO->html_type;
132 }
133 else {
134 CRM_Core_Error::fatal();
135 }
136 $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR,
137 substr($defaultValue, 1, -1)
138 );
139
140 // get the option group id
141 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
142 $this->_fid,
143 'option_group_id'
144 );
145
146 $query = "
147 SELECT id, label
148 FROM civicrm_custom_field
149 WHERE option_group_id = %1";
150 $params = array(1 => array($optionGroupID, 'Integer'),
151 2 => array($this->_fid, 'Integer'),
152 );
153 $dao = CRM_Core_DAO::executeQuery($query, $params);
154 $reusedNames = array();
155 if ($dao->N > 1) {
156 while ($dao->fetch()) {
157 $reusedNames[] = $dao->label;
158 }
159 $reusedNames = implode(', ', $reusedNames);
160 $newTitle = ts('%1 - Multiple Choice Options',
161 array(1 => $reusedNames)
162 );
163 CRM_Utils_System::setTitle($newTitle);
164 $this->assign('reusedNames', $reusedNames);
165 }
166
167 $query = "
168 SELECT *
169 FROM civicrm_option_value
170 WHERE option_group_id = %1
171 ORDER BY weight, label
172 ";
173 $params = array(1 => array($optionGroupID, 'Integer'));
174 $dao = CRM_Core_DAO::executeQuery($query, $params);
175
176 $customOption = array();
177 $fields = array('label', 'value', 'is_active', 'weight');
178 $config = CRM_Core_Config::singleton();
179 while ($dao->fetch()) {
180 $customOption[$dao->id] = array();
181 foreach ($fields as $field) {
182 $customOption[$dao->id][$field] = $dao->$field;
183 }
184
185 $action = array_sum(array_keys($this->actionLinks()));
186
187 // update enable/disable links depending on custom_field properties.
188 if ($dao->is_active) {
189 $action -= CRM_Core_Action::ENABLE;
190 }
191 else {
192 $action -= CRM_Core_Action::DISABLE;
193 }
194
195 if ($fieldHtmlType == 'CheckBox' ||
196 $fieldHtmlType == 'AdvMulti-Select' ||
197 $fieldHtmlType == 'Multi-Select'
198 ) {
199 if (in_array($dao->value, $defVal)) {
200 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
201 }
202 else {
203 $customOption[$dao->id]['default_value'] = '';
204 }
205 }
206 else {
207 if ($defaultValue == $dao->value) {
208 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
209 }
210 else {
211 $customOption[$dao->id]['default_value'] = '';
212 }
213 }
214
215 $customOption[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
216 $action,
217 array(
218 'id' => $dao->id,
219 'fid' => $this->_fid,
220 'gid' => $this->_gid,
221 ),
222 ts('more'),
223 FALSE,
224 'customOption.row.actions',
225 'customOption',
226 $dao->id
227 );
228 }
229
230 // Add order changing widget to selector
231 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
232 "reset=1&action=browse&gid={$this->_gid}&fid={$this->_fid}"
233 );
234 $filter = "option_group_id = {$optionGroupID}";
235 CRM_Utils_Weight::addOrder($customOption, 'CRM_Core_DAO_OptionValue',
236 'id', $returnURL, $filter
237 );
238 $this->assign('customOption', $customOption);
239 }
240
241 /**
242 * edit custom Option.
243 *
244 * editing would involved modifying existing fields + adding data to new fields.
245 *
246 * @param string $action the action to be invoked
247 *
248 * @return void
249 * @access public
250 */
251 function edit($action) {
252 // create a simple controller for editing custom data
253 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
254
255 // set the userContext stack
256 $session = CRM_Core_Session::singleton();
257 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
258 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
259 ));
260 $controller->setEmbedded(TRUE);
261 $controller->process();
262 $controller->run();
263 $this->browse();
264 }
265
266 /**
267 * Run the page.
268 *
269 * This method is called after the page is created. It checks for the
270 * type of action and executes that action.
271 *
272 * @param null
273 *
274 * @return void
275 * @access public
276 */
277 function run() {
278
279 // get the field id
280 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
281 $this, FALSE, 0
282 );
283 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
284 $this, FALSE, 0
285 );
286
287 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
288 CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
289 }
290
291 //as url contain $gid so append breadcrumb dynamically.
292 $breadcrumb = array(array('title' => ts('Custom Data Fields'),
293 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid),
294 ));
295 CRM_Utils_System::appendBreadCrumb($breadcrumb);
296
297 if ($this->_fid) {
298 $fieldTitle = CRM_Core_BAO_CustomField::getTitle($this->_fid);
299 $this->assign('fid', $this->_fid);
300 $this->assign('gid', $this->_gid);
301 $this->assign('fieldTitle', $fieldTitle);
302 CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle)));
303 }
304
305 // get the requested action
306 $action = CRM_Utils_Request::retrieve('action', 'String',
307 // default to 'browse'
308 $this, FALSE, 'browse'
309 );
310
311 // assign vars to templates
312 $this->assign('action', $action);
313
314 $id = CRM_Utils_Request::retrieve('id', 'Positive',
315 $this, FALSE, 0
316 );
317
318
319 // what action to take ?
320 if (($action &
321 (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
322 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
323 )
324 ) ||
325 !empty($_POST)
326 ) {
327 // no browse for edit/update/view
328 $this->edit($action);
329 }
330 else {
331 $this->browse();
332 }
333 // Call the parents run method
334 return parent::run();
335 }
336 }
337