Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | * This class generates form components for Options | |
38 | * | |
39 | */ | |
40 | class CRM_Admin_Form_Options extends CRM_Admin_Form { | |
41 | ||
42 | /** | |
43 | * The option group name | |
44 | * | |
45 | * @var array | |
46 | * @static | |
47 | */ | |
48 | protected $_gName; | |
49 | ||
50 | /** | |
51 | * The option group name in display format (capitalized, without underscores...etc) | |
52 | * | |
53 | * @var array | |
54 | * @static | |
55 | */ | |
6c2473d5 | 56 | protected $_gLabel; |
6a488035 TO |
57 | |
58 | /** | |
59 | * Function to pre-process | |
60 | * | |
355ba699 | 61 | * @return void |
6a488035 TO |
62 | * @access public |
63 | */ | |
64 | public function preProcess() { | |
65 | parent::preProcess(); | |
66 | $session = CRM_Core_Session::singleton(); | |
118e964e CW |
67 | if (!$this->_gName && !empty($this->urlPath[3])) { |
68 | $this->_gName = $this->urlPath[3]; | |
69 | } | |
70 | if (!$this->_gName && !empty($_GET['gid'])) { | |
71 | $this->_gName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', (int) $_GET['gid'], 'name'); | |
6a488035 TO |
72 | } |
73 | if ($this->_gName) { | |
74 | $this->set('gName', $this->_gName); | |
75 | } | |
76 | else { | |
77 | $this->_gName = $this->get('gName'); | |
78 | } | |
118e964e CW |
79 | $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', |
80 | $this->_gName, | |
81 | 'id', | |
82 | 'name' | |
83 | ); | |
faa2cea0 | 84 | $this->_gLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid, 'title'); |
6a488035 | 85 | $url = "civicrm/admin/options/{$this->_gName}"; |
faa2cea0 | 86 | $params = "reset=1"; |
6a488035 TO |
87 | |
88 | if (($this->_action & CRM_Core_Action::DELETE) && | |
89 | in_array($this->_gName, array('email_greeting', 'postal_greeting', 'addressee')) | |
90 | ) { | |
91 | // Don't allow delete if the option value belongs to addressee, postal or email greetings and is in use. | |
92 | $findValue = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'value'); | |
93 | $queryParam = array(1 => array($findValue, 'Integer')); | |
94 | $columnName = $this->_gName . "_id"; | |
95 | $sql = "SELECT count(id) FROM civicrm_contact WHERE " . $columnName . " = %1"; | |
96 | $isInUse = CRM_Core_DAO::singleValueQuery($sql, $queryParam); | |
97 | if ($isInUse) { | |
98 | $scriptURL = "<a href='" . CRM_Utils_System::docURL2('Update Greetings and Address Data for Contacts', TRUE, NULL, NULL, NULL, "wiki") . "'>" . ts('Learn more about a script that can automatically update contact addressee and greeting options.') . "</a>"; | |
6c2473d5 | 99 | CRM_Core_Session::setStatus(ts('The selected %1 option has <strong>not been deleted</strong> because it is currently in use. Please update these contacts to use a different format before deleting this option. %2', array(1 => $this->_gLabel, 2 => $scriptURL)), ts('Sorry'), 'error'); |
6a488035 TO |
100 | $redirect = CRM_Utils_System::url($url, $params); |
101 | CRM_Utils_System::redirect($redirect); | |
102 | } | |
103 | } | |
104 | ||
105 | ||
106 | $session->pushUserContext(CRM_Utils_System::url($url, $params)); | |
107 | $this->assign('id', $this->_id); | |
108 | ||
109 | if ($this->_id && in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups)) { | |
110 | $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'domain_id', 'id'); | |
111 | if (CRM_Core_Config::domainID() != $domainID) { | |
112 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * This function sets the default values for the form. | |
119 | * the default values are retrieved from the database | |
120 | * | |
121 | * @access public | |
122 | * | |
355ba699 | 123 | * @return void |
6a488035 TO |
124 | */ |
125 | function setDefaultValues() { | |
126 | $defaults = parent::setDefaultValues(); | |
127 | ||
128 | if (!isset($defaults['weight']) || !$defaults['weight']) { | |
129 | $fieldValues = array('option_group_id' => $this->_gid); | |
130 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues); | |
131 | } | |
132 | ||
133 | //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575 | |
134 | if (in_array($this->_gName, array( | |
135 | 'email_greeting', 'postal_greeting', 'addressee'))) { | |
136 | $defaults['contactOptions'] = (CRM_Utils_Array::value('filter', $defaults)) ? $defaults['filter'] : NULL; | |
137 | } | |
138 | // CRM-11516 | |
139 | if ($this->_gName == 'payment_instrument' && $this->_id) { | |
140 | $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($this->_id, 'civicrm_option_value', 'financial_account_id'); | |
141 | } | |
142 | return $defaults; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Function to build the form | |
147 | * | |
355ba699 | 148 | * @return void |
6a488035 TO |
149 | * @access public |
150 | */ | |
151 | public function buildQuickForm() { | |
152 | parent::buildQuickForm(); | |
153 | if ($this->_action & CRM_Core_Action::DELETE) { | |
154 | return; | |
155 | } | |
156 | ||
157 | $this->applyFilter('__ALL__', 'trim'); | |
158 | ||
159 | $isReserved = FALSE; | |
160 | if ($this->_id) { | |
161 | $isReserved = (bool) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'is_reserved'); | |
162 | } | |
163 | ||
164 | $this->add('text', | |
165 | 'label', | |
166 | ts('Label'), | |
167 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'), | |
168 | TRUE | |
169 | ); | |
170 | ||
171 | if (!in_array($this->_gName, array( | |
172 | 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) { | |
173 | $this->addRule('label', | |
174 | ts('This Label already exists in the database for this option group. Please select a different Value.'), | |
175 | 'optionExists', | |
176 | array('CRM_Core_DAO_OptionValue', $this->_id, $this->_gid, 'label') | |
177 | ); | |
178 | } | |
179 | ||
180 | if ($this->_gName == 'case_status') { | |
181 | $classes = array('Opened' => ts('Opened'), | |
182 | 'Closed' => ts('Closed'), | |
183 | ); | |
184 | ||
185 | $grouping = $this->add('select', | |
186 | 'grouping', | |
187 | ts('Status Class'), | |
188 | $classes | |
189 | ); | |
190 | if ($isReserved) { | |
191 | $grouping->freeze(); | |
192 | } | |
193 | } | |
194 | // CRM-11516 | |
195 | if ($this->_gName == 'payment_instrument') { | |
f743a6eb | 196 | $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' "); |
6a488035 | 197 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType)); |
8ef12e64 | 198 | |
199 | $this->add('select', 'financial_account_id', ts('Financial Account'), | |
6a488035 TO |
200 | array('' => ts('- select -')) + $financialAccount |
201 | ); | |
202 | } | |
203 | ||
204 | $required = FALSE; | |
205 | if ($this->_gName == 'custom_search') { | |
206 | $required = TRUE; | |
207 | } | |
208 | elseif ($this->_gName == 'redaction_rule' || $this->_gName == 'engagement_index') { | |
209 | $this->add('text', | |
210 | 'value', | |
211 | ts('Value'), | |
212 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'), | |
213 | TRUE | |
214 | ); | |
215 | if ($this->_gName == 'redaction_rule') { | |
216 | $this->add('checkbox', | |
217 | 'filter', | |
218 | ts('Regular Expression?') | |
219 | ); | |
220 | } | |
221 | } | |
222 | if ($this->_gName == 'participant_listing') { | |
223 | $this->add('text', | |
224 | 'description', | |
225 | ts('Description'), | |
226 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description') | |
227 | ); | |
228 | } | |
229 | else { | |
230 | // Hard-coding attributes here since description is still stored as varchar and not text in the schema. dgg | |
231 | $this->addWysiwyg('description', | |
232 | ts('Description'), | |
233 | array('rows' => 4, 'cols' => 80), | |
234 | $required | |
235 | ); | |
236 | } | |
237 | ||
238 | if ($this->_gName == 'event_badge') { | |
239 | $this->add('text', | |
240 | 'name', | |
241 | ts('Class Name'), | |
242 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'name') | |
243 | ); | |
244 | } | |
245 | ||
246 | $this->add('text', | |
247 | 'weight', | |
248 | ts('Weight'), | |
249 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), | |
250 | TRUE | |
251 | ); | |
252 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); | |
253 | ||
254 | // If CiviCase enabled AND "Add" mode OR "edit" mode for non-reserved activities, only allow user to pick Core or CiviCase component. | |
255 | // FIXME: Each component should define whether adding new activity types is allowed. | |
256 | $config = CRM_Core_Config::singleton(); | |
257 | if ($this->_gName == 'activity_type' && in_array("CiviCase", $config->enableComponents) && | |
258 | (($this->_action & CRM_Core_Action::ADD) || !$isReserved) | |
259 | ) { | |
260 | $caseID = CRM_Core_Component::getComponentID('CiviCase'); | |
3c2f143f | 261 | $components = array('' => ts('Contacts OR Cases'), $caseID => ts('Cases Only')); |
6a488035 TO |
262 | $this->add('select', |
263 | 'component_id', | |
264 | ts('Component'), | |
265 | $components, FALSE | |
266 | ); | |
267 | } | |
268 | ||
269 | $enabled = $this->add('checkbox', 'is_active', ts('Enabled?')); | |
270 | ||
271 | if ($isReserved) { | |
272 | $enabled->freeze(); | |
273 | } | |
274 | ||
275 | //fix for CRM-3552, CRM-4575 | |
9eab4fdd | 276 | $showIsDefaultGroups = array( |
277 | 'email_greeting', | |
278 | 'postal_greeting', | |
279 | 'addressee', | |
280 | 'from_email_address', | |
281 | 'case_status', | |
282 | 'encounter_medium', | |
283 | 'case_type', | |
284 | 'payment_instrument', | |
285 | 'communication_style', | |
694ca46e | 286 | 'soft_credit_type', |
9eab4fdd | 287 | ); |
288 | ||
289 | if (in_array($this->_gName, $showIsDefaultGroups)) { | |
6a488035 TO |
290 | $this->assign('showDefault', TRUE); |
291 | $this->add('checkbox', 'is_default', ts('Default Option?')); | |
292 | } | |
293 | ||
294 | //get contact type for which user want to create a new greeting/addressee type, CRM-4575 | |
295 | if (in_array($this->_gName, array( | |
296 | 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) { | |
297 | $values = array(1 => ts('Individual'), | |
298 | 2 => ts('Household'), | |
299 | 3 => ts('Organization'), | |
300 | 4 => ts('Multiple Contact Merge'), | |
301 | ); | |
302 | $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') + $values, TRUE); | |
303 | $this->assign('showContactFilter', TRUE); | |
304 | } | |
305 | ||
306 | if ($this->_gName == 'participant_status') { | |
307 | // For Participant Status options, expose the 'filter' field to track which statuses are "Counted", and the Visibility field | |
308 | $element = $this->add('checkbox', 'filter', ts('Counted?')); | |
309 | $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility()); | |
310 | } | |
311 | if ($this->_gName == 'participant_role') { | |
312 | // For Participant Role options, expose the 'filter' field to track which statuses are "Counted" | |
313 | $this->add('checkbox', 'filter', ts('Counted?')); | |
314 | } | |
315 | ||
316 | $this->addFormRule(array('CRM_Admin_Form_Options', 'formRule'), $this); | |
317 | } | |
318 | ||
319 | /** | |
320 | * global form rule | |
321 | * | |
322 | * @param array $fields the input form values | |
323 | * @param array $files the uploaded files if any | |
324 | * @param array $self current form object. | |
325 | * | |
326 | * @return array array of errors / empty array. | |
327 | * @access public | |
328 | * @static | |
329 | */ | |
330 | static function formRule($fields, $files, $self) { | |
331 | $errors = array(); | |
8cc574cf | 332 | if ($self->_gName == 'case_status' && empty($fields['grouping'])) { |
6a488035 TO |
333 | $errors['grouping'] = ts('Status class is a required field'); |
334 | } | |
335 | ||
8cc574cf | 336 | if (in_array($self->_gName, array('email_greeting', 'postal_greeting', 'addressee')) && empty($self->_defaultValues['is_reserved'])) { |
6a488035 TO |
337 | $label = $fields['label']; |
338 | $condition = " AND v.label = '{$label}' "; | |
339 | $values = CRM_Core_OptionGroup::values($self->_gName, FALSE, FALSE, FALSE, $condition, 'filter'); | |
340 | $checkContactOptions = TRUE; | |
341 | ||
342 | if ($self->_id && ($self->_defaultValues['contactOptions'] == $fields['contactOptions'])) { | |
343 | $checkContactOptions = FALSE; | |
344 | } | |
345 | ||
346 | if ($checkContactOptions && in_array($fields['contactOptions'], $values)) { | |
347 | $errors['label'] = ts('This Label already exists in the database for the selected contact type.'); | |
348 | } | |
349 | } | |
350 | ||
351 | if ($self->_gName == 'from_email_address') { | |
352 | $formEmail = CRM_Utils_Mail::pluckEmailFromHeader($fields['label']); | |
353 | if (!CRM_Utils_Rule::email($formEmail)) { | |
354 | $errors['label'] = ts('Please enter the valid email address.'); | |
355 | } | |
356 | ||
357 | $formName = explode('"', $fields['label']); | |
a7488080 | 358 | if (empty($formName[1]) || count($formName) != 3) { |
6a488035 TO |
359 | $errors['label'] = ts('Please follow the proper format for From Email Address'); |
360 | } | |
361 | } | |
362 | ||
363 | return $errors; | |
364 | } | |
365 | ||
366 | /** | |
367 | * Function to process the form | |
368 | * | |
369 | * @access public | |
370 | * | |
355ba699 | 371 | * @return void |
6a488035 TO |
372 | */ |
373 | public function postProcess() { | |
374 | if ($this->_action & CRM_Core_Action::DELETE) { | |
375 | $fieldValues = array('option_group_id' => $this->_gid); | |
376 | $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); | |
377 | ||
378 | if (CRM_Core_BAO_OptionValue::del($this->_id)) { | |
379 | if ($this->_gName == 'phone_type') { | |
380 | CRM_Core_BAO_Phone::setOptionToNull(CRM_Utils_Array::value('value', $this->_defaultValues)); | |
381 | } | |
382 | ||
6c2473d5 | 383 | CRM_Core_Session::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel)), ts('Record Deleted'), 'success'); |
6a488035 TO |
384 | } |
385 | else { | |
6c2473d5 | 386 | CRM_Core_Session::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel)), ts('Sorry'), 'error'); |
6a488035 TO |
387 | CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues); |
388 | } | |
389 | } | |
390 | else { | |
391 | $params = $ids = array(); | |
392 | $params = $this->exportValues(); | |
393 | ||
394 | // allow multiple defaults within group. | |
395 | $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address'); | |
396 | if (in_array($this->_gName, $allowMultiDefaults)) { | |
397 | if ($this->_gName == 'from_email_address') { | |
398 | $params['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); | |
399 | } | |
400 | elseif ($filter = CRM_Utils_Array::value('contactOptions', $params)) { | |
401 | $params['filter'] = $filter; | |
402 | $params['reset_default_for'] = array('filter' => "0, " . $params['filter']); | |
403 | } | |
404 | ||
405 | //make sure we should has to have space, CRM-6977 | |
406 | if ($this->_gName == 'from_email_address') { | |
407 | $params['label'] = str_replace('"<', '" <', $params['label']); | |
408 | } | |
409 | } | |
410 | ||
74b187a2 KJ |
411 | // set value of filter if not present in params |
412 | if ($this->_id && !array_key_exists('filter', $params)) { | |
413 | if ($this->_gName == 'participant_role') { | |
414 | $params['filter'] = 0; | |
415 | } else { | |
416 | $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id'); | |
417 | } | |
6a488035 TO |
418 | } |
419 | ||
420 | $groupParams = array('name' => ($this->_gName)); | |
421 | $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id); | |
8ef12e64 | 422 | |
6a488035 | 423 | // CRM-11516 |
a7488080 | 424 | if (!empty($params['financial_account_id'])) { |
f743a6eb | 425 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); |
6a488035 TO |
426 | $params = array( |
427 | 'entity_table' => 'civicrm_option_value', | |
428 | 'entity_id' => $optionValue->id, | |
429 | 'account_relationship' => $relationTypeId, | |
430 | 'financial_account_id' => $params['financial_account_id'] | |
431 | ); | |
432 | CRM_Financial_BAO_FinancialTypeAccount::add($params); | |
433 | } | |
434 | ||
6c2473d5 | 435 | CRM_Core_Session::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_gLabel, 2 => $optionValue->label)), ts('Saved'), 'success'); |
6a488035 TO |
436 | } |
437 | } | |
438 | } |