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