3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for Options
40 class CRM_Admin_Form_Options
extends CRM_Admin_Form
{
43 * The option group name
51 * The option group name in display format (capitalized, without underscores...etc)
59 * Function to pre-process
64 public function preProcess() {
66 $session = CRM_Core_Session
::singleton();
68 $this->_gName
= CRM_Utils_Request
::retrieve('group', 'String', $this, FALSE, 0);
69 $this->_gid
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
76 $this->set('gName', $this->_gName
);
79 $this->_gName
= $this->get('gName');
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(1 => $this->_gLabel
, 2 => $scriptURL)), ts('Sorry'), 'error');
97 $redirect = CRM_Utils_System
::url($url, $params);
98 CRM_Utils_System
::redirect($redirect);
103 $session->pushUserContext(CRM_Utils_System
::url($url, $params));
104 $this->assign('id', $this->_id
);
106 if ($this->_id
&& in_array($this->_gName
, CRM_Core_OptionGroup
::$_domainIDGroups)) {
107 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'domain_id', 'id');
108 if (CRM_Core_Config
::domainID() != $domainID) {
109 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
115 * This function sets the default values for the form.
116 * the default values are retrieved from the database
122 function setDefaultValues() {
123 $defaults = parent
::setDefaultValues();
125 if (!isset($defaults['weight']) ||
!$defaults['weight']) {
126 $fieldValues = array('option_group_id' => $this->_gid
);
127 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
130 //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575
131 if (in_array($this->_gName
, array(
132 'email_greeting', 'postal_greeting', 'addressee'))) {
133 $defaults['contactOptions'] = (CRM_Utils_Array
::value('filter', $defaults)) ?
$defaults['filter'] : NULL;
136 if ($this->_gName
== 'payment_instrument' && $this->_id
) {
137 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount($this->_id
, 'civicrm_option_value', 'financial_account_id');
143 * Function to build the form
148 public function buildQuickForm() {
149 parent
::buildQuickForm();
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 (!in_array($this->_gName
, array(
169 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) {
170 $this->addRule('label',
171 ts('This Label already exists in the database for this option group. Please select a different Value.'),
173 array('CRM_Core_DAO_OptionValue', $this->_id
, $this->_gid
, 'label')
177 if ($this->_gName
== 'case_status') {
178 $classes = array('Opened' => ts('Opened'),
179 'Closed' => ts('Closed'),
182 $grouping = $this->add('select',
192 if ($this->_gName
== 'payment_instrument') {
193 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
194 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
196 $this->add('select', 'financial_account_id', ts('Financial Account'),
197 array('' => ts('- select -')) +
$financialAccount
202 if ($this->_gName
== 'custom_search') {
205 elseif ($this->_gName
== 'redaction_rule' ||
$this->_gName
== 'engagement_index') {
209 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
212 if ($this->_gName
== 'redaction_rule') {
213 $this->add('checkbox',
215 ts('Regular Expression?')
219 if ($this->_gName
== 'participant_listing') {
223 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description')
227 // Hard-coding attributes here since description is still stored as varchar and not text in the schema. dgg
228 $this->addWysiwyg('description',
230 array('rows' => 4, 'cols' => 80),
235 if ($this->_gName
== 'event_badge') {
239 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'name')
246 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'),
249 $this->addRule('weight', ts('is a numeric field'), 'numeric');
251 // If CiviCase enabled AND "Add" mode OR "edit" mode for non-reserved activities, only allow user to pick Core or CiviCase component.
252 // FIXME: Each component should define whether adding new activity types is allowed.
253 $config = CRM_Core_Config
::singleton();
254 if ($this->_gName
== 'activity_type' && in_array("CiviCase", $config->enableComponents
) &&
255 (($this->_action
& CRM_Core_Action
::ADD
) ||
!$isReserved)
257 $caseID = CRM_Core_Component
::getComponentID('CiviCase');
258 $components = array('' => ts('Contacts OR Cases'), $caseID => ts('Cases Only'));
266 $enabled = $this->add('checkbox', 'is_active', ts('Enabled?'));
272 //fix for CRM-3552, CRM-4575
273 $showIsDefaultGroups = array(
277 'from_email_address',
281 'payment_instrument',
282 'communication_style',
286 if (in_array($this->_gName
, $showIsDefaultGroups)) {
287 $this->assign('showDefault', TRUE);
288 $this->add('checkbox', 'is_default', ts('Default Option?'));
291 //get contact type for which user want to create a new greeting/addressee type, CRM-4575
292 if (in_array($this->_gName
, array(
293 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) {
294 $values = array(1 => ts('Individual'),
295 2 => ts('Household'),
296 3 => ts('Organization'),
297 4 => ts('Multiple Contact Merge'),
299 $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') +
$values, TRUE);
300 $this->assign('showContactFilter', TRUE);
303 if ($this->_gName
== 'participant_status') {
304 // For Participant Status options, expose the 'filter' field to track which statuses are "Counted", and the Visibility field
305 $element = $this->add('checkbox', 'filter', ts('Counted?'));
306 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
308 if ($this->_gName
== 'participant_role') {
309 // For Participant Role options, expose the 'filter' field to track which statuses are "Counted"
310 $this->add('checkbox', 'filter', ts('Counted?'));
313 $this->addFormRule(array('CRM_Admin_Form_Options', 'formRule'), $this);
319 * @param array $fields the input form values
320 * @param array $files the uploaded files if any
321 * @param array $self current form object.
323 * @return array array of errors / empty array.
327 static function formRule($fields, $files, $self) {
329 if ($self->_gName
== 'case_status' && empty($fields['grouping'])) {
330 $errors['grouping'] = ts('Status class is a required field');
333 if (in_array($self->_gName
, array('email_greeting', 'postal_greeting', 'addressee')) && empty($self->_defaultValues
['is_reserved'])) {
334 $label = $fields['label'];
335 $condition = " AND v.label = '{$label}' ";
336 $values = CRM_Core_OptionGroup
::values($self->_gName
, FALSE, FALSE, FALSE, $condition, 'filter');
337 $checkContactOptions = TRUE;
339 if ($self->_id
&& ($self->_defaultValues
['contactOptions'] == $fields['contactOptions'])) {
340 $checkContactOptions = FALSE;
343 if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
344 $errors['label'] = ts('This Label already exists in the database for the selected contact type.');
348 if ($self->_gName
== 'from_email_address') {
349 $formEmail = CRM_Utils_Mail
::pluckEmailFromHeader($fields['label']);
350 if (!CRM_Utils_Rule
::email($formEmail)) {
351 $errors['label'] = ts('Please enter the valid email address.');
354 $formName = explode('"', $fields['label']);
355 if (empty($formName[1]) ||
count($formName) != 3) {
356 $errors['label'] = ts('Please follow the proper format for From Email Address');
364 * Function to process the form
370 public function postProcess() {
371 if ($this->_action
& CRM_Core_Action
::DELETE
) {
372 $fieldValues = array('option_group_id' => $this->_gid
);
373 $wt = CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $this->_id
, $fieldValues);
375 if (CRM_Core_BAO_OptionValue
::del($this->_id
)) {
376 if ($this->_gName
== 'phone_type') {
377 CRM_Core_BAO_Phone
::setOptionToNull(CRM_Utils_Array
::value('value', $this->_defaultValues
));
380 CRM_Core_Session
::setStatus(ts('Selected %1 type has been deleted.', array(1 => $this->_gLabel
)), ts('Record Deleted'), 'success');
383 CRM_Core_Session
::setStatus(ts('Selected %1 type has not been deleted.', array(1 => $this->_gLabel
)), ts('Sorry'), 'error');
384 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $fieldValues);
388 $params = $ids = array();
389 $params = $this->exportValues();
391 // allow multiple defaults within group.
392 $allowMultiDefaults = array('email_greeting', 'postal_greeting', 'addressee', 'from_email_address');
393 if (in_array($this->_gName
, $allowMultiDefaults)) {
394 if ($this->_gName
== 'from_email_address') {
395 $params['reset_default_for'] = array('domain_id' => CRM_Core_Config
::domainID());
397 elseif ($filter = CRM_Utils_Array
::value('contactOptions', $params)) {
398 $params['filter'] = $filter;
399 $params['reset_default_for'] = array('filter' => "0, " . $params['filter']);
402 //make sure we should has to have space, CRM-6977
403 if ($this->_gName
== 'from_email_address') {
404 $params['label'] = str_replace('"<', '" <', $params['label']);
408 // set value of filter if not present in params
409 if ($this->_id
&& !array_key_exists('filter', $params)) {
410 if ($this->_gName
== 'participant_role') {
411 $params['filter'] = 0;
413 $params['filter'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'filter', 'id');
417 $groupParams = array('name' => ($this->_gName
));
418 $optionValue = CRM_Core_OptionValue
::addOptionValue($params, $groupParams, $this->_action
, $this->_id
);
421 if (!empty($params['financial_account_id'])) {
422 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
424 'entity_table' => 'civicrm_option_value',
425 'entity_id' => $optionValue->id
,
426 'account_relationship' => $relationTypeId,
427 'financial_account_id' => $params['financial_account_id']
429 CRM_Financial_BAO_FinancialTypeAccount
::add($params);
432 CRM_Core_Session
::setStatus(ts('The %1 \'%2\' has been saved.', array(1 => $this->_gLabel
, 2 => $optionValue->label
)), ts('Saved'), 'success');