3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 Option Value
40 class CRM_Admin_Form_OptionValue
extends CRM_Admin_Form
{
44 * The option group name
49 static $_gName = NULL;
52 * Function to for pre-processing
57 public function preProcess() {
59 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive',
62 //get optionGroup name in case of email/postal greeting or addressee, CRM-4575
63 if (!empty($this->_gid
)) {
64 $this->_gName
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid
, 'name');
66 $session = CRM_Core_Session
::singleton();
67 $url = CRM_Utils_System
::url('civicrm/admin/optionValue', 'reset=1&action=browse&gid=' . $this->_gid
);
68 $session->replaceUserContext($url);
70 $this->assign('id', $this->_id
);
72 if ($this->_id
&& in_array($this->_gName
, CRM_Core_OptionGroup
::$_domainIDGroups)) {
73 $domainID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'domain_id', 'id');
74 if (CRM_Core_Config
::domainID() != $domainID) {
75 CRM_Core_Error
::fatal(ts('You do not have permission to access this page'));
81 * This function sets the default values for the form.
82 * the default values are retrieved from the database
88 function setDefaultValues() {
90 $defaults = parent
::setDefaultValues();
91 if (!CRM_Utils_Array
::value('weight', $defaults)) {
92 $query = "SELECT max( `weight` ) as weight FROM `civicrm_option_value` where option_group_id=" . $this->_gid
;
93 $dao = new CRM_Core_DAO();
96 $defaults['weight'] = ($dao->weight +
1);
100 if ($this->_gName
== 'payment_instrument' && $this->_id
) {
101 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount($this->_id
, 'civicrm_option_value', 'financial_account_id');
103 //setDefault of contact types for email greeting, postal greeting, addressee, CRM-4575
104 if (in_array($this->_gName
, array(
105 'email_greeting', 'postal_greeting', 'addressee'))) {
106 $defaults['contactOptions'] = CRM_Utils_Array
::value('filter', $defaults);
112 * Function to build the form
117 public function buildQuickForm() {
121 $isReserved = (bool) CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id
, 'is_reserved');
123 parent
::buildQuickForm();
124 if ($this->_action
& CRM_Core_Action
::DELETE
) {
128 $this->applyFilter('__ALL__', 'trim');
129 $this->add('text', 'label', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
130 $this->add('text', 'value', ts('Value'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'value'), TRUE);
131 $this->add('text', 'name', ts('Name'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'name'));
132 if ($this->_gName
== 'custom_search') {
136 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description')
140 $this->addWysiwyg('description',
142 CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'description')
146 if ($this->_gName
== 'case_status') {
147 $grouping = $this->add('select', 'grouping', ts('Option Grouping Name'), array('Opened' => ts('Opened'),
148 'Closed' => ts('Closed'),
155 $this->add('text', 'grouping', ts('Grouping'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'grouping'));
158 $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
159 $this->add('checkbox', 'is_active', ts('Enabled?'));
160 $this->add('checkbox', 'is_default', ts('Default Option?'));
161 $this->add('checkbox', 'is_optgroup', ts('Is OptGroup?'));
164 if ($this->_gName
== 'payment_instrument') {
165 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
166 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
168 $this->add('select', 'financial_account_id', ts('Financial Account'),
169 array('' => ts('- select -')) +
$financialAccount
173 // we dont display this in the template, but the form sets the default values which are then saved
174 // this allow us to retain the previous values
175 $this->add('text', 'filter', ts('Filter'));
177 if ($this->_action
& CRM_Core_Action
::UPDATE
&& $isReserved) {
178 $this->freeze(array('name', 'description', 'is_active'));
180 //get contact type for which user want to create a new greeting/addressee type, CRM-4575
181 if (in_array($this->_gName
, array(
182 'email_greeting', 'postal_greeting', 'addressee')) && !$isReserved) {
183 $values = array(1 => ts('Individual'),
184 2 => ts('Household'),
185 3 => ts('Organization'),
186 4 => ts('Multiple Contact Merge'),
189 $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') +
$values, TRUE);
192 $this->addFormRule(array('CRM_Admin_Form_OptionValue', 'formRule'), $this);
193 $cancelURL = CRM_Utils_System
::url('civicrm/admin/optionValue', "gid={$this->_gid}&reset=1");
194 $cancelURL = str_replace('&', '&', $cancelURL);
199 'name' => ts('Save'),
204 'name' => ts('Cancel'),
205 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"),
214 * @param array $fields the input form values
215 * @param array $files the uploaded files if any
216 * @param array $self this object.
218 * @return true if no errors, else an array of errors
222 static function formRule($fields, $files, $self) {
225 //don't allow duplicate value within group.
226 $optionValues = array();
227 CRM_Core_OptionValue
::getValues(array('id' => $self->_gid
), $optionValues);
228 foreach ($optionValues as $values) {
229 if ($values['id'] != $self->_id
) {
230 if ($fields['value'] == $values['value']) {
231 $errors['value'] = ts('Value already exist in database.');
237 return empty($errors) ?
TRUE : $errors;
241 * Function to process the form
247 public function postProcess() {
248 CRM_Utils_System
::flushCache();
250 $params = $this->exportValues();
251 if ($this->_action
& CRM_Core_Action
::DELETE
) {
252 CRM_Core_BAO_OptionValue
::del($this->_id
);
253 CRM_Core_Session
::setStatus(ts('Selected option value has been deleted.'), ts('Record Deleted'), 'success');
257 $params = $ids = array();
258 // store the submitted values in an array
259 $params = $this->exportValues();
260 $params['option_group_id'] = $this->_gid
;
262 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
263 $ids['optionValue'] = $this->_id
;
266 //set defaultGreeting option in params to save default value as per contactOption-defaultValue mapping
267 if (CRM_Utils_Array
::value('contactOptions', $params)) {
268 $params['filter'] = CRM_Utils_Array
::value('contactOptions', $params);
269 $params['defaultGreeting'] = 1;
272 $optionValue = CRM_Core_BAO_OptionValue
::add($params, $ids);
274 if (CRM_Utils_Array
::value('financial_account_id', $params)) {
275 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
277 'entity_table' => 'civicrm_option_value',
278 'entity_id' => $optionValue->id
,
279 'account_relationship' => $relationTypeId,
280 'financial_account_id' => $params['financial_account_id']
282 CRM_Financial_BAO_FinancialTypeAccount
::add($params);
284 CRM_Core_Session
::setStatus(ts('The Option Value \'%1\' has been saved.', array(1 => $optionValue->label
)), ts('Saved'), 'success');