3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This class generates form components for Options.
37 class CRM_Admin_Form_Options
extends CRM_Admin_Form
{
40 * The option group name.
47 * The option group name in display format (capitalized, without underscores...etc)
56 public function preProcess() {
58 $session = CRM_Core_Session
::singleton();
59 if (!$this->_gName
&& !empty($this->urlPath
[3])) {
60 $this->_gName
= $this->urlPath
[3];
62 if (!$this->_gName
&& !empty($_GET['gid'])) {
63 $this->_gName
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', (int) $_GET['gid'], 'name');
66 $this->set('gName', $this->_gName
);
69 $this->_gName
= $this->get('gName');
71 $this->_gid
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
76 $this->_gLabel
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid
, 'title');
77 $url = "civicrm/admin/options/{$this->_gName}";
80 if (($this->_action
& CRM_Core_Action
::DELETE
) &&
81 in_array($this->_gName
, array('email_greeting', 'postal_greeting', 'addressee'))
83 // Don't allow delete if the option value belongs to addressee, postal or email greetings and is in use.
84 $findValue = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'value');
85 $queryParam = array(1 => array($findValue, 'Integer'));
86 $columnName = $this->_gName
. "_id";
87 $sql = "SELECT count(id) FROM civicrm_contact WHERE " . $columnName . " = %1";
88 $isInUse = CRM_Core_DAO
::singleValueQuery($sql, $queryParam);
90 $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>";
91 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(
94 )), ts('Sorry'), 'error');
95 $redirect = CRM_Utils_System
::url($url, $params);
96 CRM_Utils_System
::redirect($redirect);
100 $session->pushUserContext(CRM_Utils_System
::url($url, $params));
101 $this->assign('id', $this->_id
);
103 if ($this->_id
&& in_array($this->_gName
, CRM_Core_OptionGroup
::$_domainIDGroups)) {
104 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'domain_id', 'id');
105 if (CRM_Core_Config
::domainID() != $domainID) {
106 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
112 * Set default values for the form.
114 public function setDefaultValues() {
115 $defaults = parent
::setDefaultValues();
117 // Default weight & value
118 $fieldValues = array('option_group_id' => $this->_gid
);
119 foreach (array('weight', 'value') as $field) {
120 if (empty($defaults[$field])) {
121 $defaults[$field] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues, $field);
125 //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575
126 if (in_array($this->_gName
, array(
131 $defaults['contactOptions'] = (CRM_Utils_Array
::value('filter', $defaults)) ?
$defaults['filter'] : NULL;
134 if ($this->_gName
== 'payment_instrument' && $this->_id
) {
135 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount($this->_id
, 'civicrm_option_value', 'financial_account_id');
137 if (empty($this->_id
) ||
!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'color')) {
138 $defaults['color'] = '#ffffff';
144 * Build the form object.
146 public function buildQuickForm() {
147 parent
::buildQuickForm();
148 $this->setPageTitle(ts('%1 Option', array(1 => $this->_gLabel
)));
150 if ($this->_action
& CRM_Core_Action
::DELETE
) {
154 $this->applyFilter('__ALL__', 'trim');
158 $isReserved = (bool) CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'is_reserved');
164 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'),
168 if ($this->_gName
!= 'activity_type') {
172 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
177 $this->add('text', 'icon', ts('Icon'), array('class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE));
180 if ($this->_gName
== 'activity_status') {
181 $this->add('color', 'color', ts('Color'));
184 if (!in_array($this->_gName
, array(
190 $domainSpecificOptionGroups = array('from_email_address');
191 $domainSpecific = in_array($this->_gName
, $domainSpecificOptionGroups) ?
TRUE : FALSE;
192 $this->addRule('label',
193 ts('This Label already exists in the database for this option group. Please select a different Value.'),
195 array('CRM_Core_DAO_OptionValue', $this->_id
, $this->_gid
, 'label', $domainSpecific)
199 if ($this->_gName
== 'case_status') {
201 'Opened' => ts('Opened'),
202 'Closed' => ts('Closed'),
205 $grouping = $this->add('select',
215 if ($this->_gName
== 'payment_instrument') {
216 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
217 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
219 $this->add('select', 'financial_account_id', ts('Financial Account'),
220 array('' => ts('- select -')) +
$financialAccount,
226 if ($this->_gName
== 'custom_search') {
229 elseif ($this->_gName
== 'redaction_rule' ||
$this->_gName
== 'engagement_index') {
230 if ($this->_gName
== 'redaction_rule') {
231 $this->add('checkbox',
233 ts('Regular Expression?')
237 if ($this->_gName
== 'participant_listing') {
241 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description')
245 // Hard-coding attributes here since description is still stored as varchar and not text in the schema. dgg
246 $this->add('wysiwyg', 'description',
248 array('rows' => 4, 'cols' => 80),
253 if ($this->_gName
== 'event_badge') {
257 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'name')
264 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'),
267 $this->addRule('weight', ts('is a numeric field'), 'numeric');
269 // If CiviCase enabled AND "Add" mode OR "edit" mode for non-reserved activities, only allow user to pick Core or CiviCase component.
270 // FIXME: Each component should define whether adding new activity types is allowed.
271 $config = CRM_Core_Config
::singleton();
272 if ($this->_gName
== 'activity_type' && in_array("CiviCase", $config->enableComponents
) &&
273 (($this->_action
& CRM_Core_Action
::ADD
) ||
!$isReserved)
275 $caseID = CRM_Core_Component
::getComponentID('CiviCase');
276 $components = array('' => ts('Contacts AND Cases'), $caseID => ts('Cases Only'));
284 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
290 //fix for CRM-3552, CRM-4575
291 $showIsDefaultGroups = array(
295 'from_email_address',
299 'payment_instrument',
300 'communication_style',
305 if (in_array($this->_gName
, $showIsDefaultGroups)) {
306 $this->assign('showDefault', TRUE);
307 $this->add('checkbox', 'is_default', ts('Default Option?'));
310 //get contact type for which user want to create a new greeting/addressee type, CRM-4575
311 if (in_array($this->_gName
, array(
318 1 => ts('Individual'),
319 2 => ts('Household'),
320 3 => ts('Organization'),
321 4 => ts('Multiple Contact Merge'),
323 $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') +
$values, TRUE);
324 $this->assign('showContactFilter', TRUE);
327 if ($this->_gName
== 'participant_status') {
328 // For Participant Status options, expose the 'filter' field to track which statuses are "Counted", and the Visibility field
329 $element = $this->add('checkbox', 'filter', ts('Counted?'));
330 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
332 if ($this->_gName
== 'participant_role') {
333 // For Participant Role options, expose the 'filter' field to track which statuses are "Counted"
334 $this->add('checkbox', 'filter', ts('Counted?'));
337 $this->addFormRule(array('CRM_Admin_Form_Options', 'formRule'), $this);
343 * @param array $fields
344 * The input form values.
345 * @param array $files
346 * The uploaded files if any.
348 * Current form object.
351 * array of errors / empty array.
353 public static function formRule($fields, $files, $self) {
355 if ($self->_gName
== 'case_status' && empty($fields['grouping'])) {
356 $errors['grouping'] = ts('Status class is a required field');
359 if (in_array($self->_gName
, array(
363 )) && empty($self->_defaultValues
['is_reserved'])
365 $label = $fields['label'];
366 $condition = " AND v.label = '{$label}' ";
367 $values = CRM_Core_OptionGroup
::values($self->_gName
, FALSE, FALSE, FALSE, $condition, 'filter');
368 $checkContactOptions = TRUE;
370 if ($self->_id
&& ($self->_defaultValues
['contactOptions'] == $fields['contactOptions'])) {
371 $checkContactOptions = FALSE;
374 if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
375 $errors['label'] = ts('This Label already exists in the database for the selected contact type.');
379 if ($self->_gName
== 'from_email_address') {
380 $formEmail = CRM_Utils_Mail
::pluckEmailFromHeader($fields['label']);
381 if (!CRM_Utils_Rule
::email($formEmail)) {
382 $errors['label'] = ts('Please enter a valid email address.');
385 $formName = explode('"', $fields['label']);
386 if (empty($formName[1]) ||
count($formName) != 3) {
387 $errors['label'] = ts('Please follow the proper format for From Email Address');
391 $dataType = self
::getOptionGroupDataType($self->_gName
);
392 if ($dataType && $self->_gName
!== 'activity_type') {
393 $validate = CRM_Utils_Type
::validate($fields['value'], $dataType, FALSE);
395 CRM_Core_Session
::setStatus(
396 ts('Data Type of the value field for this option value does not match ' . $dataType),
397 ts('Value field Data Type mismatch'));
404 * Get the DataType for a specified Option Group.
406 * @param string $optionGroupName name of the option group
408 * @return string|null
410 public static function getOptionGroupDataType($optionGroupName) {
411 $optionGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $optionGroupName, 'id', 'name');
413 $dataType = CRM_Core_BAO_OptionGroup
::getDataType($optionGroupId);
418 * Process the form submission.
420 public function postProcess() {
421 if ($this->_action
& CRM_Core_Action
::DELETE
) {
422 $fieldValues = array('option_group_id' => $this->_gid
);
423 $wt = CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
425 if (CRM_Core_BAO_OptionValue
::del($this->_id
)) {
426 if ($this->_gName
== 'phone_type') {
427 CRM_Core_BAO_Phone
::setOptionToNull(CRM_Utils_Array
::value('value', $this->_defaultValues
));
430 CRM_Core_Session
::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel
)), ts('Record Deleted'), 'success');
433 CRM_Core_Session
::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel
)), ts('Sorry'), 'error');
434 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
439 $params = $this->exportValues();
441 // allow multiple defaults within group.
442 $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address');
443 if (in_array($this->_gName
, $allowMultiDefaults)) {
444 if ($this->_gName
== 'from_email_address') {
445 $params['reset_default_for'] = array('domain_id' => CRM_Core_Config
::domainID());
447 elseif ($filter = CRM_Utils_Array
::value('contactOptions', $params)) {
448 $params['filter'] = $filter;
449 $params['reset_default_for'] = array('filter' => "0, " . $params['filter']);
452 //make sure we should has to have space, CRM-6977
453 if ($this->_gName
== 'from_email_address') {
454 $params['label'] = str_replace('"<', '" <', $params['label']);
458 // set value of filter if not present in params
459 if ($this->_id
&& !array_key_exists('filter', $params)) {
460 if ($this->_gName
== 'participant_role') {
461 $params['filter'] = 0;
464 $params['filter'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'filter', 'id');
468 if (isset($params['color']) && strtolower($params['color']) == '#ffffff') {
469 $params['color'] = 'null';
472 $groupParams = array('name' => ($this->_gName
));
473 $optionValue = CRM_Core_OptionValue
::addOptionValue($params, $groupParams, $this->_action
, $this->_id
);
476 if (!empty($params['financial_account_id'])) {
477 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
479 'entity_table' => 'civicrm_option_value',
480 'entity_id' => $optionValue->id
,
481 'account_relationship' => $relationTypeId,
482 'financial_account_id' => $params['financial_account_id'],
484 CRM_Financial_BAO_FinancialTypeAccount
::add($params);
487 CRM_Core_Session
::setStatus(ts('The %1 \'%2\' has been saved.', array(
489 2 => $optionValue->label
,
490 )), ts('Saved'), 'success');
492 $this->ajaxResponse
['optionValue'] = $optionValue->toArray();