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