-- HR-235 Import multi-value custom data
[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 CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js');
124 //get the default value from custom fields
125 $customFieldBAO = new CRM_Core_BAO_CustomField();
126 $customFieldBAO->id = $this->_fid;
127 if ($customFieldBAO->find(TRUE)) {
128 $defaultValue = $customFieldBAO->default_value;
129 $fieldHtmlType = $customFieldBAO->html_type;
130 }
131 else {
132 CRM_Core_Error::fatal();
133 }
134 $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR,
135 substr($defaultValue, 1, -1)
136 );
137
138 // get the option group id
139 $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
140 $this->_fid,
141 'option_group_id'
142 );
143
144 $query = "
145 SELECT id, label
146 FROM civicrm_custom_field
147 WHERE option_group_id = %1";
148 $params = array(1 => array($optionGroupID, 'Integer'),
149 2 => array($this->_fid, 'Integer'),
150 );
151 $dao = CRM_Core_DAO::executeQuery($query, $params);
152 $reusedNames = array();
153 if ($dao->N > 1) {
154 while ($dao->fetch()) {
155 $reusedNames[] = $dao->label;
156 }
157 $reusedNames = implode(', ', $reusedNames);
158 $newTitle = ts('%1 - Multiple Choice Options',
159 array(1 => $reusedNames)
160 );
161 CRM_Utils_System::setTitle($newTitle);
162 $this->assign('reusedNames', $reusedNames);
163 }
164
165 $query = "
166 SELECT *
167 FROM civicrm_option_value
168 WHERE option_group_id = %1
169 ORDER BY weight, label
170 ";
171 $params = array(1 => array($optionGroupID, 'Integer'));
172 $dao = CRM_Core_DAO::executeQuery($query, $params);
173
174 $customOption = array();
175 $fields = array('label', 'value', 'is_active', 'weight');
176 $config = CRM_Core_Config::singleton();
177 while ($dao->fetch()) {
178 $customOption[$dao->id] = array();
179 foreach ($fields as $field) {
180 $customOption[$dao->id][$field] = $dao->$field;
181 }
182
183 $action = array_sum(array_keys($this->actionLinks()));
184
185 // update enable/disable links depending on custom_field properties.
186 if ($dao->is_active) {
187 $action -= CRM_Core_Action::ENABLE;
188 }
189 else {
190 $action -= CRM_Core_Action::DISABLE;
191 }
192
193 if ($fieldHtmlType == 'CheckBox' ||
194 $fieldHtmlType == 'AdvMulti-Select' ||
195 $fieldHtmlType == 'Multi-Select'
196 ) {
197 if (in_array($dao->value, $defVal)) {
198 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
199 }
200 else {
201 $customOption[$dao->id]['default_value'] = '';
202 }
203 }
204 else {
205 if ($defaultValue == $dao->value) {
206 $customOption[$dao->id]['default_value'] = '<img src="' . $config->resourceBase . 'i/check.gif" />';
207 }
208 else {
209 $customOption[$dao->id]['default_value'] = '';
210 }
211 }
212
213 $customOption[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
214 $action,
215 array(
216 'id' => $dao->id,
217 'fid' => $this->_fid,
218 'gid' => $this->_gid,
219 ),
220 ts('more'),
221 FALSE,
222 'customOption.row.actions',
223 'customOption',
224 $dao->id
225 );
226 }
227
228 // Add order changing widget to selector
229 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
230 "reset=1&action=browse&gid={$this->_gid}&fid={$this->_fid}"
231 );
232 $filter = "option_group_id = {$optionGroupID}";
233 CRM_Utils_Weight::addOrder($customOption, 'CRM_Core_DAO_OptionValue',
234 'id', $returnURL, $filter
235 );
236 $this->assign('customOption', $customOption);
237 }
238
239 /**
240 * edit custom Option.
241 *
242 * editing would involved modifying existing fields + adding data to new fields.
243 *
244 * @param string $action the action to be invoked
245 *
246 * @return void
247 * @access public
248 */
249 function edit($action) {
250 // create a simple controller for editing custom data
251 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', ts('Custom Option'), $action);
252
253 // set the userContext stack
254 $session = CRM_Core_Session::singleton();
255 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field/option',
256 "reset=1&action=browse&fid={$this->_fid}&gid={$this->_gid}"
257 ));
258 $controller->setEmbedded(TRUE);
259 $controller->process();
260 $controller->run();
261 $this->browse();
262 }
263
264 /**
265 * Run the page.
266 *
267 * This method is called after the page is created. It checks for the
268 * type of action and executes that action.
269 *
270 * @param null
271 *
272 * @return void
273 * @access public
274 */
275 function run() {
276
277 // get the field id
278 $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive',
279 $this, FALSE, 0
280 );
281 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive',
282 $this, FALSE, 0
283 );
284
285 if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) {
286 CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set.");
287 }
288
289 //as url contain $gid so append breadcrumb dynamically.
290 $breadcrumb = array(array('title' => ts('Custom Data Fields'),
291 'url' => CRM_Utils_System::url('civicrm/admin/custom/group/field', 'reset=1&gid=' . $this->_gid),
292 ));
293 CRM_Utils_System::appendBreadCrumb($breadcrumb);
294
295 if ($this->_fid) {
296 $fieldTitle = CRM_Core_BAO_CustomField::getTitle($this->_fid);
297 $this->assign('fid', $this->_fid);
298 $this->assign('gid', $this->_gid);
299 $this->assign('fieldTitle', $fieldTitle);
300 CRM_Utils_System::setTitle(ts('%1 - Multiple Choice Options', array(1 => $fieldTitle)));
301 }
302
303 // get the requested action
304 $action = CRM_Utils_Request::retrieve('action', 'String',
305 // default to 'browse'
306 $this, FALSE, 'browse'
307 );
308
309 // assign vars to templates
310 $this->assign('action', $action);
311
312 $id = CRM_Utils_Request::retrieve('id', 'Positive',
313 $this, FALSE, 0
314 );
315
316
317 // what action to take ?
318 if (($action &
319 (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD |
320 CRM_Core_Action::VIEW | CRM_Core_Action::DELETE
321 )
322 ) ||
323 !empty($_POST)
324 ) {
325 // no browse for edit/update/view
326 $this->edit($action);
327 }
328 else {
329 $this->browse();
330 }
331 // Call the parents run method
332 return parent::run();
333 }
334 }
335