Merge pull request #1074 from kurund/CRM-12916
[civicrm-core.git] / CRM / Custom / Page / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
44class 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 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_OptionValue' . '\',\'' . 'enable-disable' . '\' );"',
96 'ref' => 'disable-action',
97 'title' => ts('Disable Mutliple Choice Option'),
98 ),
99 CRM_Core_Action::ENABLE => array(
100 'name' => ts('Enable'),
101 'extra' => 'onclick = "enableDisable( %%id%%,\'' . 'CRM_Core_BAO_OptionValue' . '\',\'' . 'disable-enable' . '\' );"',
102 'ref' => 'enable-action',
103 'title' => ts('Enable Mutliple Choice Option'),
104 ),
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'),
110 ),
111 );
112 }
113 return self::$_actionLinks;
114 }
115
116 /**
117 * Browse all custom group fields.
118 *
119 * @param null
120 *
121 * @return void
122 * @access public
123 */
124 function browse() {
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;
131 }
132 else {
133 CRM_Core_Error::fatal();
134 }
135 $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR,
136 substr($defaultValue, 1, -1)
137 );
138
139 // get the option group id
140 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
141 $this->_fid,
142 'option_group_id'
143 );
144
145 $query = "
146SELECT id, label
147FROM civicrm_custom_field
148WHERE option_group_id = %1";
149 $params = array(1 => array($optionGroupID, 'Integer'),
150 2 => array($this->_fid, 'Integer'),
151 );
152 $dao = CRM_Core_DAO::executeQuery($query, $params);
153 $reusedNames = array();
154 if ($dao->N > 1) {
155 while ($dao->fetch()) {
156 $reusedNames[] = $dao->label;
157 }
158 $reusedNames = implode(', ', $reusedNames);
159 $newTitle = ts('%1 - Multiple Choice Options',
160 array(1 => $reusedNames)
161 );
162 CRM_Utils_System::setTitle($newTitle);
163 $this->assign('reusedNames', $reusedNames);
164 }
165
166 $query = "
167SELECT *
168 FROM civicrm_option_value
169 WHERE option_group_id = %1
170ORDER BY weight, label
171";
172 $params = array(1 => array($optionGroupID, 'Integer'));
173 $dao = CRM_Core_DAO::executeQuery($query, $params);
174
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;
182 }
183
184 $action = array_sum(array_keys($this->actionLinks()));
185
186 // update enable/disable links depending on custom_field properties.
187 if ($dao->is_active) {
188 $action -= CRM_Core_Action::ENABLE;
189 }
190 else {
191 $action -= CRM_Core_Action::DISABLE;
192 }
193
194 if ($fieldHtmlType == 'CheckBox' ||
195 $fieldHtmlType == 'AdvMulti-Select' ||
196 $fieldHtmlType == 'Multi-Select'
197 ) {
198 if (in_array($dao->value, $defVal)) {
199 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
200 }
201 else {
202 $customOption[$dao->id]['default_value'] = '';
203 }
204 }
205 else {
206 if ($defaultValue == $dao->value) {
207 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
208 }
209 else {
210 $customOption[$dao->id]['default_value'] = '';
211 }
212 }
213
214 $customOption[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
215 $action,
216 array(
217 'id' => $dao->id,
218 'fid' => $this->_fid,
219 'gid' => $this->_gid,
220 )
221 );
222 }
223
224 // Add order changing widget to selector
225 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
226 "reset=1&action=browse&gid={$this->_gid}&fid={$this->_fid}"
227 );
228 $filter = "option_group_id = {$optionGroupID}";
229 CRM_Utils_Weight::addOrder($customOption, 'CRM_Core_DAO_OptionValue',
230 'id', $returnURL, $filter
231 );
232 $this->assign('customOption', $customOption);
233 }
234
235 /**
236 * edit custom Option.
237 *
238 * editing would involved modifying existing fields + adding data to new fields.
239 *
240 * @param string $action the action to be invoked
241 *
242 * @return void
243 * @access public
244 */
245 function edit($action) {
246 // create a simple controller for editing custom data
247 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
248
249 // set the userContext stack
250 $session = CRM_Core_Session::singleton();
251 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
252 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
253 ));
254 $controller->setEmbedded(TRUE);
255 $controller->process();
256 $controller->run();
257 $this->browse();
258 }
259
260 /**
261 * Run the page.
262 *
263 * This method is called after the page is created. It checks for the
264 * type of action and executes that action.
265 *
266 * @param null
267 *
268 * @return void
269 * @access public
270 */
271 function run() {
272
273 // get the field id
274 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
275 $this, FALSE, 0
276 );
277 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
278 $this, FALSE, 0
279 );
280
281 //as url contain $gid so append breadcrumb dynamically.
282 $breadcrumb = array(array('title' => ts('Custom Data Fields'),
283 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid),
284 ));
285 CRM_Utils_System::appendBreadCrumb($breadcrumb);
286
287 if ($this->_fid) {
288 $fieldTitle = CRM_Core_BAO_CustomField::getTitle($this->_fid);
289 $this->assign('fid', $this->_fid);
290 $this->assign('gid', $this->_gid);
291 $this->assign('fieldTitle', $fieldTitle);
292 CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle)));
293 }
294
295 // get the requested action
296 $action = CRM_Utils_Request::retrieve('action', 'String',
297 // default to 'browse'
298 $this, FALSE, 'browse'
299 );
300
301 // assign vars to templates
302 $this->assign('action', $action);
303
304 $id = CRM_Utils_Request::retrieve('id', 'Positive',
305 $this, FALSE, 0
306 );
307
308
309 // what action to take ?
310 if (($action &
311 (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
312 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
313 )
314 ) ||
315 !empty($_POST)
316 ) {
317 // no browse for edit/update/view
318 $this->edit($action);
319 }
320 else {
321 $this->browse();
322 }
323 // Call the parents run method
324 return parent::run();
325 }
326}
327