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