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