Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * form to process actions on the field aspect of Custom | |
38 | */ | |
39 | class CRM_Price_Form_Option extends CRM_Core_Form { | |
40 | ||
41 | /** | |
fe482240 | 42 | * The price field id saved to the session for an update. |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | protected $_fid; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Option value id, used when editing the Option |
6a488035 TO |
50 | * |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | protected $_oid; | |
54 | ||
55 | /** | |
fe482240 | 56 | * Set variables up before form is built. |
6a488035 | 57 | * |
6a488035 | 58 | * @return void |
6a488035 TO |
59 | */ |
60 | public function preProcess() { | |
e5e8ab99 | 61 | $this->setPageTitle(ts('Price Option')); |
6a488035 TO |
62 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', |
63 | $this | |
64 | ); | |
65 | $this->_oid = CRM_Utils_Request::retrieve('oid', 'Positive', | |
66 | $this | |
67 | ); | |
68 | } | |
69 | ||
70 | /** | |
c490a46a | 71 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
72 | * the default values are retrieved from the database |
73 | * | |
bed98343 | 74 | * @return array|void array of default values |
6a488035 | 75 | */ |
00be9182 | 76 | public function setDefaultValues() { |
a4c1913d | 77 | if ($this->_action == CRM_Core_Action::DELETE) { |
bed98343 | 78 | return NULL; |
a4c1913d | 79 | } |
6a488035 TO |
80 | $defaults = array(); |
81 | ||
82 | if (isset($this->_oid)) { | |
83 | $params = array('id' => $this->_oid); | |
84 | ||
9da8dc8c | 85 | CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults); |
6a488035 TO |
86 | |
87 | // fix the display of the monetary value, CRM-4038 | |
88 | $defaults['value'] = CRM_Utils_Money::format(CRM_Utils_Array::value('value', $defaults), NULL, '%a'); | |
89 | } | |
90 | ||
91 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); | |
9da8dc8c | 92 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
93 | |
94 | if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) { | |
95 | $defaults['membership_num_terms'] = 1; | |
96 | } | |
97 | // set financial type used for price set to set default for new option | |
98 | if (!$this->_oid) { | |
9da8dc8c | 99 | $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');; |
6a488035 TO |
100 | } |
101 | if (!isset($defaults['weight']) || !$defaults['weight']) { | |
102 | $fieldValues = array('price_field_id' => $this->_fid); | |
9da8dc8c | 103 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues); |
6a488035 TO |
104 | $defaults['is_active'] = 1; |
105 | } | |
106 | ||
107 | return $defaults; | |
108 | } | |
109 | ||
110 | /** | |
fe482240 | 111 | * Build the form object. |
6a488035 | 112 | * |
6a488035 | 113 | * @return void |
6a488035 TO |
114 | */ |
115 | public function buildQuickForm() { | |
e3ca0d21 E |
116 | if ($this->_action == CRM_Core_Action::UPDATE) { |
117 | $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id'); | |
23dba589 | 118 | if (!CRM_Financial_BAO_FinancialType::checkPermissionToEditFinancialType($finTypeId)) { |
e3ca0d21 E |
119 | CRM_Core_Error::fatal(ts("You do not have permission to access this page")); |
120 | } | |
121 | } | |
6a488035 TO |
122 | if ($this->_action == CRM_Core_Action::DELETE) { |
123 | $this->addButtons(array( | |
c5c263ca AH |
124 | array( |
125 | 'type' => 'next', | |
126 | 'name' => ts('Delete'), | |
127 | ), | |
128 | array( | |
129 | 'type' => 'cancel', | |
130 | 'name' => ts('Cancel'), | |
131 | ), | |
132 | )); | |
bed98343 | 133 | return NULL; |
6a488035 TO |
134 | } |
135 | else { | |
353ffa53 | 136 | $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue'); |
6a488035 TO |
137 | // lets trim all the whitespace |
138 | $this->applyFilter('__ALL__', 'trim'); | |
139 | ||
140 | // hidden Option Id for validation use | |
141 | $this->add('hidden', 'optionId', $this->_oid); | |
142 | ||
94166e28 CW |
143 | // Needed for i18n dialog |
144 | $this->assign('optionId', $this->_oid); | |
145 | ||
6a488035 TO |
146 | //hidden field ID for validation use |
147 | $this->add('hidden', 'fieldId', $this->_fid); | |
148 | ||
149 | // label | |
150 | $this->add('text', 'label', ts('Option Label'), NULL, TRUE); | |
151 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); | |
152 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
153 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this); | |
154 | } | |
155 | elseif ($this->_action == CRM_Core_Action::ADD || | |
156 | $this->_action == CRM_Core_Action::VIEW | |
157 | ) { | |
9da8dc8c | 158 | $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id'); |
6a488035 | 159 | } |
ba1dcfda | 160 | $this->isEvent = FALSE; |
9da8dc8c | 161 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
162 | $this->assign('showMember', FALSE); |
163 | if ($memberComponentId == $extendComponentId) { | |
164 | $this->assign('showMember', TRUE); | |
165 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
166 | $this->add('select', 'membership_type_id', ts('Membership Type'), array( | |
c5c263ca AH |
167 | '' => ' ', |
168 | ) + $membershipTypes, FALSE, | |
169 | array('onClick' => "calculateRowValues( );")); | |
896bd22c | 170 | $this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']); |
6a488035 TO |
171 | } |
172 | else { | |
173 | $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId); | |
174 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); | |
175 | if (in_array($eventComponentId, $allComponents)) { | |
176 | $this->isEvent = TRUE; | |
177 | // count | |
bbb36eb8 | 178 | $this->add('text', 'count', ts('Participant Count')); |
6a488035 | 179 | $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger'); |
366fe2a3 | 180 | |
6a488035 TO |
181 | $this->add('text', 'max_value', ts('Max Participants')); |
182 | $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger'); | |
183 | } | |
366fe2a3 | 184 | |
6a488035 TO |
185 | } |
186 | //Financial Type | |
187 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
366fe2a3 | 188 | |
6a488035 TO |
189 | if (count($financialType)) { |
190 | $this->assign('financialType', $financialType); | |
191 | } | |
192 | $this->add( | |
366fe2a3 | 193 | 'select', |
194 | 'financial_type_id', | |
195 | ts('Financial Type'), | |
6a488035 | 196 | array('' => ts('- select -')) + $financialType, |
ba1dcfda | 197 | TRUE |
6a488035 | 198 | ); |
366fe2a3 | 199 | |
9da8dc8c | 200 | //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' ); |
6a488035 TO |
201 | // FIX ME: duplicate rule? |
202 | /* | |
e70a7fc0 TO |
203 | $this->addRule( 'label', |
204 | ts('Duplicate option label.'), | |
205 | 'optionExists', | |
206 | array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) ); | |
207 | */ | |
6a488035 | 208 | |
6a488035 TO |
209 | // value |
210 | $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE); | |
211 | ||
212 | // the above value is used directly by QF, so the value has to be have a rule | |
213 | // please check with Lobo before u comment this | |
214 | $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
215 | $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money'); | |
216 | ||
5afce5ad | 217 | $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL); |
218 | $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
219 | $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money'); | |
220 | ||
6a488035 | 221 | $this->add('textarea', 'description', ts('Description')); |
08b8b2d9 | 222 | $this->add('textarea', 'help_pre', ts('Pre Option Help')); |
223 | $this->add('textarea', 'help_post', ts('Post Option Help')); | |
6a488035 TO |
224 | |
225 | // weight | |
226 | $this->add('text', 'weight', ts('Order'), NULL, TRUE); | |
227 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); | |
228 | ||
229 | // is active ? | |
230 | $this->add('checkbox', 'is_active', ts('Active?')); | |
231 | ||
2db35bf6 AF |
232 | // is public? |
233 | $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility()); | |
234 | ||
6a488035 TO |
235 | //is default |
236 | $this->add('checkbox', 'is_default', ts('Default')); | |
237 | ||
238 | if ($this->_fid) { | |
239 | //hide the default checkbox option for text field | |
9da8dc8c | 240 | $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type'); |
6a488035 TO |
241 | $this->assign('hideDefaultOption', FALSE); |
242 | if ($htmlType == 'Text') { | |
243 | $this->assign('hideDefaultOption', TRUE); | |
244 | } | |
245 | } | |
246 | // add buttons | |
247 | $this->addButtons(array( | |
c5c263ca AH |
248 | array( |
249 | 'type' => 'next', | |
250 | 'name' => ts('Save'), | |
251 | ), | |
252 | array( | |
253 | 'type' => 'cancel', | |
254 | 'name' => ts('Cancel'), | |
255 | ), | |
256 | )); | |
6a488035 TO |
257 | |
258 | // if view mode pls freeze it with the done button. | |
259 | if ($this->_action & CRM_Core_Action::VIEW) { | |
260 | $this->freeze(); | |
261 | $this->addButtons(array( | |
c5c263ca AH |
262 | array( |
263 | 'type' => 'cancel', | |
264 | 'name' => ts('Done'), | |
265 | 'isDefault' => TRUE, | |
266 | ), | |
267 | )); | |
6a488035 TO |
268 | } |
269 | } | |
270 | ||
271 | $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this); | |
272 | } | |
273 | ||
274 | /** | |
fe482240 | 275 | * Global validation rules for the form. |
6a488035 | 276 | * |
414c1420 TO |
277 | * @param array $fields |
278 | * Posted values of the form. | |
77b97be7 EM |
279 | * |
280 | * @param $files | |
c490a46a | 281 | * @param CRM_Core_Form $form |
6a488035 | 282 | * |
a6c01b45 CW |
283 | * @return array |
284 | * if errors then list of errors to be posted back to the form, | |
6a488035 | 285 | * true otherwise |
6a488035 | 286 | */ |
00be9182 | 287 | public static function formRule($fields, $files, $form) { |
6a488035 | 288 | $errors = array(); |
8cc574cf | 289 | if (!empty($fields['count']) && !empty($fields['max_value']) && |
6a488035 TO |
290 | $fields['count'] > $fields['max_value'] |
291 | ) { | |
292 | $errors['count'] = ts('Participant count can not be greater than max participants.'); | |
293 | } | |
a13c171d CR |
294 | |
295 | $priceField = CRM_Price_BAO_PriceField::findById($fields['fieldId']); | |
296 | $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array('labelColumn' => 'name')); | |
297 | ||
298 | $publicCount = 0; | |
299 | $options = CRM_Price_BAO_PriceField::getOptions($priceField->id); | |
300 | foreach ($options as $currentOption) { | |
301 | if ($fields['optionId'] == $currentOption['id'] && $visibilityOptions[$fields['visibility_id']] == 'public') { | |
302 | $publicCount++; | |
303 | } | |
304 | elseif ($fields['optionId'] != $currentOption['id'] && $visibilityOptions[$currentOption['visibility_id']] == 'public') { | |
305 | $publicCount++; | |
306 | } | |
307 | } | |
308 | if ($visibilityOptions[$priceField->visibility_id] == 'public' && $publicCount == 0) { | |
309 | $errors['visibility_id'] = ts('All other options for this \'Public\' field have \'Admin\' visibility. There should at least be one \'Public\' option, or make the field \'Admin\' only.'); | |
310 | } | |
311 | elseif ($visibilityOptions[$priceField->visibility_id] == 'admin' && $publicCount > 0) { | |
312 | $errors['visibility_id'] = ts('You must choose \'Admin\' visibility for this price option, as it belongs to a field with \'Admin\' visibility.'); | |
313 | } | |
314 | ||
6a488035 TO |
315 | return empty($errors) ? TRUE : $errors; |
316 | } | |
317 | ||
318 | /** | |
fe482240 | 319 | * Process the form. |
6a488035 | 320 | * |
6a488035 | 321 | * @return void |
6a488035 TO |
322 | */ |
323 | public function postProcess() { | |
324 | if ($this->_action == CRM_Core_Action::DELETE) { | |
325 | $fieldValues = array('price_field_id' => $this->_fid); | |
353ffa53 TO |
326 | $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues); |
327 | $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
6a488035 TO |
328 | $this->_oid, |
329 | 'label', 'id' | |
330 | ); | |
331 | ||
9da8dc8c | 332 | if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) { |
6a488035 TO |
333 | CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success'); |
334 | } | |
bed98343 | 335 | return NULL; |
6a488035 TO |
336 | } |
337 | else { | |
353ffa53 TO |
338 | $params = $ids = array(); |
339 | $params = $this->controller->exportValues('Option'); | |
9da8dc8c | 340 | $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label'); |
6a488035 TO |
341 | |
342 | $params['amount'] = CRM_Utils_Rule::cleanMoney(trim($params['amount'])); | |
343 | $params['price_field_id'] = $this->_fid; | |
344 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
ff33ddc8 | 345 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
2db35bf6 | 346 | $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE); |
6a488035 TO |
347 | $ids = array(); |
348 | if ($this->_oid) { | |
349 | $ids['id'] = $this->_oid; | |
350 | } | |
9da8dc8c | 351 | $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids); |
6a488035 TO |
352 | |
353 | CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success'); | |
354 | } | |
355 | } | |
96025800 | 356 | |
6a488035 | 357 | } |