Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Option Value | |
38 | * | |
39 | */ | |
40 | class CRM_Admin_Form_OptionValue extends CRM_Admin_Form { | |
b76a9e3e | 41 | protected $_gid = NULL; |
6a488035 TO |
42 | |
43 | /** | |
44 | * The option group name | |
45 | * | |
46 | * @var string | |
47 | * @static | |
48 | */ | |
b76a9e3e | 49 | protected $_gName = NULL; |
6a488035 TO |
50 | |
51 | /** | |
52 | * Function to for pre-processing | |
53 | * | |
54 | * @return None | |
55 | * @access public | |
56 | */ | |
57 | public function preProcess() { | |
58 | parent::preProcess(); | |
59 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', | |
60 | $this, FALSE, 0 | |
61 | ); | |
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'); | |
65 | } | |
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); | |
69 | ||
70 | $this->assign('id', $this->_id); | |
71 | ||
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')); | |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
81 | * This function sets the default values for the form. | |
82 | * the default values are retrieved from the database | |
83 | * | |
84 | * @access public | |
85 | * | |
86 | * @return None | |
87 | */ | |
88 | function setDefaultValues() { | |
89 | $defaults = array(); | |
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(); | |
94 | $dao->query($query); | |
95 | $dao->fetch(); | |
96 | $defaults['weight'] = ($dao->weight + 1); | |
97 | } | |
eea16664 | 98 | |
6a488035 TO |
99 | // CRM-11516 |
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'); | |
102 | } | |
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); | |
107 | } | |
108 | return $defaults; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Function to build the form | |
113 | * | |
114 | * @return None | |
115 | * @access public | |
116 | */ | |
117 | public function buildQuickForm() { | |
118 | //CRM-4575 | |
119 | $isReserved = FALSE; | |
120 | if ($this->_id) { | |
121 | $isReserved = (bool) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'is_reserved'); | |
122 | } | |
123 | parent::buildQuickForm(); | |
124 | if ($this->_action & CRM_Core_Action::DELETE) { | |
125 | return; | |
126 | } | |
127 | ||
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') { | |
133 | $this->add('text', | |
134 | 'description', | |
135 | ts('Description'), | |
136 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description') | |
137 | ); | |
138 | } | |
139 | else { | |
140 | $this->addWysiwyg('description', | |
141 | ts('Description'), | |
142 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description') | |
143 | ); | |
144 | } | |
145 | ||
146 | if ($this->_gName == 'case_status') { | |
147 | $grouping = $this->add('select', 'grouping', ts('Option Grouping Name'), array('Opened' => ts('Opened'), | |
148 | 'Closed' => ts('Closed'), | |
149 | )); | |
150 | if ($isReserved) { | |
151 | $grouping->freeze(); | |
152 | } | |
153 | } | |
154 | else { | |
155 | $this->add('text', 'grouping', ts('Grouping'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'grouping')); | |
156 | } | |
157 | ||
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?')); | |
162 | ||
163 | // CRM-11516 | |
164 | if ($this->_gName == 'payment_instrument') { | |
f743a6eb | 165 | $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' "); |
6a488035 | 166 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType)); |
eea16664 | 167 | |
168 | $this->add('select', 'financial_account_id', ts('Financial Account'), | |
6a488035 TO |
169 | array('' => ts('- select -')) + $financialAccount |
170 | ); | |
171 | } | |
172 | // CRM-9953 | |
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')); | |
176 | ||
177 | if ($this->_action & CRM_Core_Action::UPDATE && $isReserved) { | |
178 | $this->freeze(array('name', 'description', 'is_active')); | |
179 | } | |
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'), | |
187 | ); | |
188 | ||
189 | $this->add('select', 'contactOptions', ts('Contact Type'), array('' => '-select-') + $values, TRUE); | |
190 | } | |
191 | ||
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); | |
195 | $this->addButtons( | |
196 | array( | |
197 | array( | |
198 | 'type' => 'next', | |
199 | 'name' => ts('Save'), | |
200 | 'isDefault' => TRUE, | |
201 | ), | |
202 | array( | |
203 | 'type' => 'cancel', | |
204 | 'name' => ts('Cancel'), | |
205 | 'js' => array('onclick' => "location.href='{$cancelURL}'; return false;"), | |
206 | ), | |
207 | ) | |
208 | ); | |
209 | } | |
210 | ||
211 | /** | |
212 | * global form rule | |
213 | * | |
214 | * @param array $fields the input form values | |
215 | * @param array $files the uploaded files if any | |
216 | * @param array $self this object. | |
217 | * | |
218 | * @return true if no errors, else an array of errors | |
219 | * @access public | |
220 | * @static | |
221 | */ | |
222 | static function formRule($fields, $files, $self) { | |
223 | $errors = array(); | |
224 | ||
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.'); | |
232 | break; | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | return empty($errors) ? TRUE : $errors; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Function to process the form | |
242 | * | |
243 | * @access public | |
244 | * | |
245 | * @return None | |
246 | */ | |
247 | public function postProcess() { | |
248 | CRM_Utils_System::flushCache(); | |
249 | ||
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'); | |
254 | } | |
255 | else { | |
256 | ||
257 | $params = $ids = array(); | |
258 | // store the submitted values in an array | |
259 | $params = $this->exportValues(); | |
260 | $params['option_group_id'] = $this->_gid; | |
261 | ||
262 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
263 | $ids['optionValue'] = $this->_id; | |
264 | } | |
265 | ||
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; | |
270 | } | |
271 | ||
272 | $optionValue = CRM_Core_BAO_OptionValue::add($params, $ids); | |
273 | // CRM-11516 | |
274 | if (CRM_Utils_Array::value('financial_account_id', $params)) { | |
f743a6eb | 275 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); |
6a488035 TO |
276 | $params = array( |
277 | 'entity_table' => 'civicrm_option_value', | |
278 | 'entity_id' => $optionValue->id, | |
279 | 'account_relationship' => $relationTypeId, | |
280 | 'financial_account_id' => $params['financial_account_id'] | |
281 | ); | |
282 | CRM_Financial_BAO_FinancialTypeAccount::add($params); | |
283 | } | |
284 | CRM_Core_Session::setStatus(ts('The Option Value \'%1\' has been saved.', array(1 => $optionValue->label)), ts('Saved'), 'success'); | |
285 | } | |
286 | } | |
287 | } |