Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
96f50de2 CW |
46 | public $useLivePageJS = TRUE; |
47 | ||
6a488035 TO |
48 | /** |
49 | * The Group id of the option | |
50 | * | |
51 | * @var int | |
52 | * @access protected | |
53 | */ | |
54 | protected $_gid; | |
55 | ||
56 | /** | |
57 | * The field id of the option | |
58 | * | |
59 | * @var int | |
60 | * @access protected | |
61 | */ | |
62 | protected $_fid; | |
63 | ||
64 | /** | |
65 | * The action links that we need to display for the browse screen | |
66 | * | |
67 | * @var array | |
68 | * @access private | |
69 | */ | |
70 | private static $_actionLinks; | |
71 | ||
72 | /** | |
73 | * Get the action links for this page. | |
74 | * | |
75 | * @param null | |
76 | * | |
77 | * @return array array of action links that we need to display for the browse screen | |
78 | * @access public | |
79 | */ | |
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'), | |
88 | ), | |
89 | CRM_Core_Action::VIEW => array( | |
90 | 'name' => ts('View'), | |
91 | 'url' => 'civicrm/admin/custom/group/field/option', | |
92 | 'qs' => 'action=view&id=%%id%%&fid=%%fid%%', | |
93 | 'title' => ts('View Multiple Choice Option'), | |
94 | ), | |
95 | CRM_Core_Action::DISABLE => array( | |
96 | 'name' => ts('Disable'), | |
4d17a233 | 97 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
98 | 'title' => ts('Disable Mutliple Choice Option'), |
99 | ), | |
100 | CRM_Core_Action::ENABLE => array( | |
101 | 'name' => ts('Enable'), | |
4d17a233 | 102 | 'ref' => 'crm-enable-disable', |
6a488035 TO |
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 = " | |
146 | SELECT id, label | |
147 | FROM civicrm_custom_field | |
148 | WHERE 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 = " | |
167 | SELECT * | |
168 | FROM civicrm_option_value | |
169 | WHERE option_group_id = %1 | |
170 | ORDER 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, | |
87dab4a4 AH |
220 | ), |
221 | ts('more'), | |
222 | FALSE, | |
223 | 'customOption.row.actions', | |
224 | 'customOption', | |
225 | $dao->id | |
6a488035 TO |
226 | ); |
227 | } | |
228 | ||
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}" | |
232 | ); | |
233 | $filter = "option_group_id = {$optionGroupID}"; | |
234 | CRM_Utils_Weight::addOrder($customOption, 'CRM_Core_DAO_OptionValue', | |
235 | 'id', $returnURL, $filter | |
236 | ); | |
237 | $this->assign('customOption', $customOption); | |
238 | } | |
239 | ||
240 | /** | |
100fef9d | 241 | * Edit custom Option. |
6a488035 TO |
242 | * |
243 | * editing would involved modifying existing fields + adding data to new fields. | |
244 | * | |
245 | * @param string $action the action to be invoked | |
246 | * | |
247 | * @return void | |
248 | * @access public | |
249 | */ | |
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); | |
253 | ||
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}" | |
258 | )); | |
259 | $controller->setEmbedded(TRUE); | |
260 | $controller->process(); | |
261 | $controller->run(); | |
262 | $this->browse(); | |
263 | } | |
264 | ||
265 | /** | |
266 | * Run the page. | |
267 | * | |
268 | * This method is called after the page is created. It checks for the | |
269 | * type of action and executes that action. | |
270 | * | |
271 | * @param null | |
272 | * | |
273 | * @return void | |
274 | * @access public | |
275 | */ | |
276 | function run() { | |
277 | ||
278 | // get the field id | |
279 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', | |
280 | $this, FALSE, 0 | |
281 | ); | |
282 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', | |
283 | $this, FALSE, 0 | |
284 | ); | |
285 | ||
d06700a7 | 286 | if ($isReserved = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_gid, 'is_reserved', 'id')) { |
e89941dc | 287 | CRM_Core_Error::fatal("You cannot add or edit muliple choice options in a reserved custom field-set."); |
d06700a7 RN |
288 | } |
289 | ||
6a488035 TO |
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), | |
293 | )); | |
294 | CRM_Utils_System::appendBreadCrumb($breadcrumb); | |
295 | ||
296 | if ($this->_fid) { | |
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))); | |
302 | } | |
303 | ||
304 | // get the requested action | |
305 | $action = CRM_Utils_Request::retrieve('action', 'String', | |
306 | // default to 'browse' | |
307 | $this, FALSE, 'browse' | |
308 | ); | |
309 | ||
310 | // assign vars to templates | |
311 | $this->assign('action', $action); | |
312 | ||
313 | $id = CRM_Utils_Request::retrieve('id', 'Positive', | |
314 | $this, FALSE, 0 | |
315 | ); | |
316 | ||
317 | ||
318 | // what action to take ? | |
319 | if (($action & | |
320 | (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | | |
321 | CRM_Core_Action::VIEW | CRM_Core_Action::DELETE | |
322 | ) | |
323 | ) || | |
324 | !empty($_POST) | |
325 | ) { | |
326 | // no browse for edit/update/view | |
327 | $this->edit($action); | |
328 | } | |
329 | else { | |
330 | $this->browse(); | |
331 | } | |
332 | // Call the parents run method | |
333 | return parent::run(); | |
334 | } | |
335 | } | |
336 |