3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for Options
40 class CRM_Admin_Form_Options
extends CRM_Admin_Form
{
43 * The option group name.
50 * The option group name in display format (capitalized, without underscores...etc)
61 public function preProcess() {
63 $session = CRM_Core_Session
::singleton();
64 if (!$this->_gName
&& !empty($this->urlPath
[3])) {
65 $this->_gName
= $this->urlPath
[3];
67 if (!$this->_gName
&& !empty($_GET['gid'])) {
68 $this->_gName
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', (int) $_GET['gid'], 'name');
71 $this->set('gName', $this->_gName
);
74 $this->_gName
= $this->get('gName');
76 $this->_gid
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
81 $this->_gLabel
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid
, 'title');
82 $url = "civicrm/admin/options/{$this->_gName}";
85 if (($this->_action
& CRM_Core_Action
::DELETE
) &&
86 in_array($this->_gName
, array('email_greeting', 'postal_greeting', 'addressee'))
88 // Don't allow delete if the option value belongs to addressee, postal or email greetings and is in use.
89 $findValue = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'value');
90 $queryParam = array(1 => array($findValue, 'Integer'));
91 $columnName = $this->_gName
. "_id";
92 $sql = "SELECT count(id) FROM civicrm_contact WHERE " . $columnName . " = %1";
93 $isInUse = CRM_Core_DAO
::singleValueQuery($sql, $queryParam);
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>";
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(
99 )), ts('Sorry'), 'error');
100 $redirect = CRM_Utils_System
::url($url, $params);
101 CRM_Utils_System
::redirect($redirect);
105 $session->pushUserContext(CRM_Utils_System
::url($url, $params));
106 $this->assign('id', $this->_id
);
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) {
111 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
117 * Set default values for the form.
118 * the default values are retrieved from the database
123 public function setDefaultValues() {
124 $defaults = parent
::setDefaultValues();
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);
131 //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575
132 if (in_array($this->_gName
, array(
137 $defaults['contactOptions'] = (CRM_Utils_Array
::value('filter', $defaults)) ?
$defaults['filter'] : NULL;
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');
147 * Build the form object.
151 public function buildQuickForm() {
152 parent
::buildQuickForm();
153 $this->setPageTitle(ts('%1 Option', array(1 => $this->_gLabel
)));
155 if ($this->_action
& CRM_Core_Action
::DELETE
) {
159 $this->applyFilter('__ALL__', 'trim');
163 $isReserved = (bool) CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'is_reserved');
169 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'),
173 if (!in_array($this->_gName
, array(
179 $this->addRule('label',
180 ts('This Label already exists in the database for this option group. Please select a different Value.'),
182 array('CRM_Core_DAO_OptionValue', $this->_id
, $this->_gid
, 'label')
186 if ($this->_gName
== 'case_status') {
188 'Opened' => ts('Opened'),
189 'Closed' => ts('Closed'),
192 $grouping = $this->add('select',
202 if ($this->_gName
== 'payment_instrument') {
203 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
204 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
206 $this->add('select', 'financial_account_id', ts('Financial Account'),
207 array('' => ts('- select -')) +
$financialAccount,
213 if ($this->_gName
== 'custom_search') {
216 elseif ($this->_gName
== 'redaction_rule' ||
$this->_gName
== 'engagement_index') {
220 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
223 if ($this->_gName
== 'redaction_rule') {
224 $this->add('checkbox',
226 ts('Regular Expression?')
230 if ($this->_gName
== 'participant_listing') {
234 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description')
238 // Hard-coding attributes here since description is still stored as varchar and not text in the schema. dgg
239 $this->addWysiwyg('description',
241 array('rows' => 4, 'cols' => 80),
246 if ($this->_gName
== 'event_badge') {
250 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'name')
257 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'),
260 $this->addRule('weight', ts('is a numeric field'), 'numeric');
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)
268 $caseID = CRM_Core_Component
::getComponentID('CiviCase');
269 $components = array('' => ts('Contacts AND Cases'), $caseID => ts('Cases Only'));
277 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
283 //fix for CRM-3552, CRM-4575
284 $showIsDefaultGroups = array(
288 'from_email_address',
292 'payment_instrument',
293 'communication_style',
298 if (in_array($this->_gName
, $showIsDefaultGroups)) {
299 $this->assign('showDefault', TRUE);
300 $this->add('checkbox', 'is_default', ts('Default Option?'));
303 //get contact type for which user want to create a new greeting/addressee type, CRM-4575
304 if (in_array($this->_gName
, array(
311 1 => ts('Individual'),
312 2 => ts('Household'),
313 3 => ts('Organization'),
314 4 => ts('Multiple Contact Merge'),
316 $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') +
$values, TRUE);
317 $this->assign('showContactFilter', TRUE);
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());
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?'));
330 $this->addFormRule(array('CRM_Admin_Form_Options', 'formRule'), $this);
336 * @param array $fields
337 * The input form values.
338 * @param array $files
339 * The uploaded files if any.
341 * Current form object.
344 * array of errors / empty array.
346 public static function formRule($fields, $files, $self) {
348 if ($self->_gName
== 'case_status' && empty($fields['grouping'])) {
349 $errors['grouping'] = ts('Status class is a required field');
352 if (in_array($self->_gName
, array(
356 )) && empty($self->_defaultValues
['is_reserved'])
358 $label = $fields['label'];
359 $condition = " AND v.label = '{$label}' ";
360 $values = CRM_Core_OptionGroup
::values($self->_gName
, FALSE, FALSE, FALSE, $condition, 'filter');
361 $checkContactOptions = TRUE;
363 if ($self->_id
&& ($self->_defaultValues
['contactOptions'] == $fields['contactOptions'])) {
364 $checkContactOptions = FALSE;
367 if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
368 $errors['label'] = ts('This Label already exists in the database for the selected contact type.');
372 if ($self->_gName
== 'from_email_address') {
373 $formEmail = CRM_Utils_Mail
::pluckEmailFromHeader($fields['label']);
374 if (!CRM_Utils_Rule
::email($formEmail)) {
375 $errors['label'] = ts('Please enter a valid email address.');
378 $formName = explode('"', $fields['label']);
379 if (empty($formName[1]) ||
count($formName) != 3) {
380 $errors['label'] = ts('Please follow the proper format for From Email Address');
388 * Process the form submission.
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);
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
));
403 CRM_Core_Session
::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel
)), ts('Record Deleted'), 'success');
406 CRM_Core_Session
::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel
)), ts('Sorry'), 'error');
407 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
411 $params = $ids = array();
412 $params = $this->exportValues();
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());
420 elseif ($filter = CRM_Utils_Array
::value('contactOptions', $params)) {
421 $params['filter'] = $filter;
422 $params['reset_default_for'] = array('filter' => "0, " . $params['filter']);
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']);
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;
437 $params['filter'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'filter', 'id');
441 $groupParams = array('name' => ($this->_gName
));
442 $optionValue = CRM_Core_OptionValue
::addOptionValue($params, $groupParams, $this->_action
, $this->_id
);
445 if (!empty($params['financial_account_id'])) {
446 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
448 'entity_table' => 'civicrm_option_value',
449 'entity_id' => $optionValue->id
,
450 'account_relationship' => $relationTypeId,
451 'financial_account_id' => $params['financial_account_id'],
453 CRM_Financial_BAO_FinancialTypeAccount
::add($params);
456 CRM_Core_Session
::setStatus(ts('The %1 \'%2\' has been saved.', array(
458 2 => $optionValue->label
,
459 )), ts('Saved'), 'success');