Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * form to process actions on the field aspect of Price | |
20 | */ | |
21 | class CRM_Price_Form_Field extends CRM_Core_Form { | |
22 | ||
23 | /** | |
24 | * Constants for number of options for data types of multiple option. | |
25 | */ | |
7da04cde | 26 | const NUM_OPTION = 15; |
6a488035 TO |
27 | |
28 | /** | |
fe482240 | 29 | * The custom set id saved to the session for an update. |
6a488035 TO |
30 | * |
31 | * @var int | |
6a488035 TO |
32 | */ |
33 | protected $_sid; | |
34 | ||
35 | /** | |
36 | * The field id, used when editing the field | |
37 | * | |
38 | * @var int | |
6a488035 TO |
39 | */ |
40 | protected $_fid; | |
41 | ||
42 | /** | |
fe482240 | 43 | * The extended component Id. |
6a488035 TO |
44 | * |
45 | * @var array | |
6a488035 TO |
46 | */ |
47 | protected $_extendComponentId; | |
48 | ||
49 | /** | |
fe482240 | 50 | * Variable is set if price set is used for membership. |
971e129b | 51 | * @var bool |
6a488035 TO |
52 | */ |
53 | protected $_useForMember; | |
54 | ||
55 | /** | |
fe482240 | 56 | * Set variables up before form is built. |
6a488035 TO |
57 | */ |
58 | public function preProcess() { | |
59 | ||
60 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST'); | |
61 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST'); | |
353ffa53 | 62 | $url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}"); |
be2fb01f | 63 | $breadCrumb = [['title' => ts('Price Set Fields'), 'url' => $url]]; |
6a488035 | 64 | |
be2fb01f | 65 | $this->_extendComponentId = []; |
9da8dc8c | 66 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
67 | if ($extendComponentId) { |
68 | $this->_extendComponentId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId); | |
69 | } | |
70 | ||
71 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
704f21c0 | 72 | |
e2046b33 | 73 | $this->setPageTitle(ts('Price Field')); |
6a488035 TO |
74 | } |
75 | ||
76 | /** | |
bf15c191 | 77 | * Set default values for the form. |
6a488035 | 78 | * |
a6c01b45 CW |
79 | * @return array |
80 | * array of default values | |
6a488035 | 81 | */ |
00be9182 | 82 | public function setDefaultValues() { |
be2fb01f | 83 | $defaults = []; |
6a488035 TO |
84 | // is it an edit operation ? |
85 | if (isset($this->_fid)) { | |
be2fb01f | 86 | $params = ['id' => $this->_fid]; |
6a488035 | 87 | $this->assign('fid', $this->_fid); |
9da8dc8c | 88 | CRM_Price_BAO_PriceField::retrieve($params, $defaults); |
6a488035 TO |
89 | $this->_sid = $defaults['price_set_id']; |
90 | ||
91 | // if text, retrieve price | |
92 | if ($defaults['html_type'] == 'Text') { | |
fc8edfbf | 93 | $isActive = $defaults['is_active']; |
be2fb01f | 94 | $valueParams = ['price_field_id' => $this->_fid]; |
6a488035 | 95 | |
9da8dc8c | 96 | CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults); |
6a488035 TO |
97 | |
98 | // fix the display of the monetary value, CRM-4038 | |
99 | $defaults['price'] = CRM_Utils_Money::format($defaults['amount'], NULL, '%a'); | |
ff33ddc8 | 100 | $defaults['is_active'] = $isActive; |
6a488035 TO |
101 | } |
102 | ||
6a488035 TO |
103 | } |
104 | else { | |
105 | $defaults['is_active'] = 1; | |
106 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
107 | $defaults['option_status[' . $i . ']'] = 1; | |
108 | $defaults['option_weight[' . $i . ']'] = $i; | |
2db35bf6 | 109 | $defaults['option_visibility_id[' . $i . ']'] = 1; |
6a488035 TO |
110 | } |
111 | } | |
112 | ||
113 | if ($this->_action & CRM_Core_Action::ADD) { | |
be2fb01f | 114 | $fieldValues = ['price_set_id' => $this->_sid]; |
9da8dc8c | 115 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues); |
6a488035 TO |
116 | $defaults['options_per_line'] = 1; |
117 | $defaults['is_display_amounts'] = 1; | |
118 | } | |
0495a6a4 | 119 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
120 | $eventComponentId = NULL; | |
ba1dcfda | 121 | if (array_key_exists('CiviEvent', $enabledComponents)) { |
353ffa53 | 122 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); |
0495a6a4 | 123 | } |
6a488035 TO |
124 | |
125 | if (isset($this->_sid) && $this->_action == CRM_Core_Action::ADD) { | |
9da8dc8c | 126 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id'); |
6a488035 TO |
127 | $defaults['financial_type_id'] = $financialTypeId; |
128 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
129 | $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId; | |
130 | } | |
131 | } | |
132 | return $defaults; | |
133 | } | |
134 | ||
135 | /** | |
fe482240 | 136 | * Build the form object. |
6a488035 TO |
137 | */ |
138 | public function buildQuickForm() { | |
139 | // lets trim all the whitespace | |
140 | $this->applyFilter('__ALL__', 'trim'); | |
141 | ||
142 | // add a hidden field to remember the price set id | |
143 | // this get around the browser tab issue | |
144 | $this->add('hidden', 'sid', $this->_sid); | |
145 | $this->add('hidden', 'fid', $this->_fid); | |
146 | ||
147 | // label | |
9da8dc8c | 148 | $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE); |
6a488035 TO |
149 | |
150 | // html_type | |
151 | $javascript = 'onchange="option_html_type(this.form)";'; | |
152 | ||
9da8dc8c | 153 | $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes(); |
6a488035 TO |
154 | |
155 | // Text box for Participant Count for a field | |
156 | ||
157 | // Financial Type | |
158 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
53df1142 | 159 | foreach ($financialType as $finTypeId => $type) { |
40c655aa | 160 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
161 | && !CRM_Core_Permission::check('add contributions of type ' . $type) |
162 | ) { | |
53df1142 E |
163 | unset($financialType[$finTypeId]); |
164 | } | |
165 | } | |
6a488035 TO |
166 | if (count($financialType)) { |
167 | $this->assign('financialType', $financialType); | |
168 | } | |
2db35bf6 AF |
169 | |
170 | //Visibility Type Options | |
171 | $visibilityType = CRM_Core_PseudoConstant::visibility(); | |
172 | $this->assign('visibilityType', $visibilityType); | |
173 | ||
0495a6a4 | 174 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
175 | $eventComponentId = $memberComponentId = NULL; | |
ba1dcfda | 176 | if (array_key_exists('CiviEvent', $enabledComponents)) { |
353ffa53 | 177 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); |
0495a6a4 | 178 | } |
ba1dcfda | 179 | if (array_key_exists('CiviMember', $enabledComponents)) { |
0495a6a4 | 180 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); |
181 | } | |
366fe2a3 | 182 | |
353ffa53 | 183 | $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue'); |
366fe2a3 | 184 | |
185 | $this->add('select', 'financial_type_id', | |
186 | ts('Financial Type'), | |
be2fb01f | 187 | [' ' => ts('- select -')] + $financialType |
6a488035 TO |
188 | ); |
189 | ||
190 | $this->assign('useForMember', FALSE); | |
191 | if (in_array($eventComponentId, $this->_extendComponentId)) { | |
192 | $this->add('text', 'count', ts('Participant Count'), $attributes['count']); | |
193 | ||
194 | $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger'); | |
195 | ||
196 | $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']); | |
197 | $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger'); | |
198 | ||
199 | $this->assign('useForEvent', TRUE); | |
200 | } | |
201 | else { | |
202 | if (in_array($memberComponentId, $this->_extendComponentId)) { | |
203 | $this->_useForMember = 1; | |
204 | $this->assign('useForMember', $this->_useForMember); | |
205 | } | |
206 | $this->assign('useForEvent', FALSE); | |
207 | } | |
208 | ||
209 | $sel = $this->add('select', 'html_type', ts('Input Field Type'), | |
210 | $htmlTypes, TRUE, $javascript | |
211 | ); | |
212 | ||
213 | // price (for text inputs) | |
214 | $this->add('text', 'price', ts('Price')); | |
215 | $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule'); | |
216 | $this->addRule('price', ts('must be a monetary value'), 'money'); | |
217 | ||
5afce5ad | 218 | $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL); |
219 | $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
220 | $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money'); | |
221 | ||
6a488035 TO |
222 | if ($this->_action == CRM_Core_Action::UPDATE) { |
223 | $this->freeze('html_type'); | |
224 | } | |
225 | ||
226 | // form fields of Custom Option rows | |
227 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
228 | ||
229 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
230 | ||
231 | //the show hide blocks | |
232 | $showBlocks = 'optionField_' . $i; | |
233 | if ($i > 2) { | |
234 | $_showHide->addHide($showBlocks); | |
235 | if ($i == self::NUM_OPTION) { | |
236 | $_showHide->addHide('additionalOption'); | |
237 | } | |
238 | } | |
239 | else { | |
240 | $_showHide->addShow($showBlocks); | |
241 | } | |
242 | // label | |
243 | $attributes['label']['size'] = 25; | |
244 | $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']); | |
245 | ||
246 | // amount | |
247 | $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']); | |
248 | $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money'); | |
249 | ||
250 | //Financial Type | |
251 | $this->add( | |
366fe2a3 | 252 | 'select', |
92fcb95f | 253 | 'option_financial_type_id[' . $i . ']', |
366fe2a3 | 254 | ts('Financial Type'), |
be2fb01f | 255 | ['' => ts('- select -')] + $financialType |
6a488035 TO |
256 | ); |
257 | if (in_array($eventComponentId, $this->_extendComponentId)) { | |
258 | // count | |
259 | $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']); | |
260 | $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger'); | |
261 | ||
262 | // max_value | |
263 | $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']); | |
264 | $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger'); | |
265 | ||
266 | // description | |
267 | //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 )); | |
268 | } | |
269 | elseif (in_array($memberComponentId, $this->_extendComponentId)) { | |
270 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
be2fb01f | 271 | $js = ['onchange' => "calculateRowValues( $i );"]; |
6a488035 TO |
272 | |
273 | $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'), | |
be2fb01f | 274 | ['' => ' '] + $membershipTypes, FALSE, $js |
353ffa53 | 275 | ); |
896bd22c | 276 | $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array::value('membership_num_terms', $attributes)); |
6a488035 TO |
277 | } |
278 | ||
279 | // weight | |
f20d2b0d | 280 | $this->add('number', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']); |
6a488035 TO |
281 | |
282 | // is active ? | |
283 | $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?')); | |
284 | ||
2bc7601f | 285 | $this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), $visibilityType); |
6a488035 TO |
286 | $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); |
287 | ||
288 | //for checkbox handling of default option | |
289 | $this->add('checkbox', "default_checkbox_option[$i]", NULL); | |
290 | } | |
291 | //default option selection | |
292 | $this->addGroup($defaultOption, 'default_option'); | |
293 | $_showHide->addToTemplate(); | |
294 | ||
295 | // is_display_amounts | |
296 | $this->add('checkbox', 'is_display_amounts', ts('Display Amount?')); | |
297 | ||
298 | // weight | |
f20d2b0d | 299 | $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE); |
6a488035 TO |
300 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); |
301 | ||
302 | // checkbox / radio options per line | |
303 | $this->add('text', 'options_per_line', ts('Options Per Line')); | |
304 | $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric'); | |
305 | ||
139f5f76 JP |
306 | $this->add('textarea', 'help_pre', ts('Pre Field Help'), |
307 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post') | |
308 | ); | |
309 | ||
6a488035 | 310 | // help post, mask, attributes, javascript ? |
139f5f76 | 311 | $this->add('textarea', 'help_post', ts('Post Field Help'), |
9da8dc8c | 312 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post') |
6a488035 TO |
313 | ); |
314 | ||
be2fb01f | 315 | $this->add('datepicker', 'active_on', ts('Active On'), [], FALSE, ['time' => TRUE]); |
6a488035 TO |
316 | |
317 | // expire_on | |
be2fb01f | 318 | $this->add('datepicker', 'expire_on', ts('Expire On'), [], FALSE, ['time' => TRUE]); |
6a488035 TO |
319 | |
320 | // is required ? | |
321 | $this->add('checkbox', 'is_required', ts('Required?')); | |
322 | ||
323 | // is active ? | |
324 | $this->add('checkbox', 'is_active', ts('Active?')); | |
325 | ||
326 | // add buttons | |
be2fb01f CW |
327 | $this->addButtons([ |
328 | [ | |
c5c263ca AH |
329 | 'type' => 'next', |
330 | 'name' => ts('Save'), | |
331 | 'isDefault' => TRUE, | |
be2fb01f CW |
332 | ], |
333 | [ | |
c5c263ca AH |
334 | 'type' => 'next', |
335 | 'name' => ts('Save and New'), | |
336 | 'subName' => 'new', | |
be2fb01f CW |
337 | ], |
338 | [ | |
c5c263ca AH |
339 | 'type' => 'cancel', |
340 | 'name' => ts('Cancel'), | |
be2fb01f CW |
341 | ], |
342 | ]); | |
6a488035 TO |
343 | // is public? |
344 | $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility()); | |
345 | ||
346 | // add a form rule to check default value | |
be2fb01f | 347 | $this->addFormRule(['CRM_Price_Form_Field', 'formRule'], $this); |
6a488035 TO |
348 | |
349 | // if view mode pls freeze it with the done button. | |
350 | if ($this->_action & CRM_Core_Action::VIEW) { | |
351 | $this->freeze(); | |
352 | $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid); | |
353 | $this->addElement('button', | |
354 | 'done', | |
355 | ts('Done'), | |
be2fb01f | 356 | ['onclick' => "location.href='$url'"] |
6a488035 TO |
357 | ); |
358 | } | |
359 | } | |
360 | ||
361 | /** | |
fe482240 | 362 | * Global validation rules for the form. |
6a488035 | 363 | * |
414c1420 TO |
364 | * @param array $fields |
365 | * Posted values of the form. | |
77b97be7 EM |
366 | * |
367 | * @param $files | |
c490a46a | 368 | * @param CRM_Core_Form $form |
6a488035 | 369 | * |
a6c01b45 CW |
370 | * @return array |
371 | * if errors then list of errors to be posted back to the form, | |
6a488035 | 372 | * true otherwise |
6a488035 | 373 | */ |
00be9182 | 374 | public static function formRule($fields, $files, $form) { |
6a488035 TO |
375 | |
376 | // all option fields are of type "money" | |
be2fb01f | 377 | $errors = []; |
6a488035 TO |
378 | |
379 | /** Check the option values entered | |
380 | * Appropriate values are required for the selected datatype | |
381 | * Incomplete row checking is also required. | |
382 | */ | |
383 | if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) && | |
e68e1c9a | 384 | $fields['html_type'] == 'Text' |
6a488035 | 385 | ) { |
e68e1c9a PN |
386 | if ($fields['price'] == NULL) { |
387 | $errors['price'] = ts('Price is a required field'); | |
388 | } | |
389 | if ($fields['financial_type_id'] == '') { | |
390 | $errors['financial_type_id'] = ts('Financial Type is a required field'); | |
391 | } | |
6a488035 | 392 | } |
366fe2a3 | 393 | |
6a488035 | 394 | //avoid the same price field label in Within PriceSet |
9da8dc8c | 395 | $priceFieldLabel = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
396 | $priceFieldLabel->label = $fields['label']; |
397 | $priceFieldLabel->price_set_id = $form->_sid; | |
398 | ||
399 | $dupeLabel = FALSE; | |
400 | if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) { | |
401 | $dupeLabel = TRUE; | |
402 | } | |
403 | ||
404 | if ($dupeLabel) { | |
405 | $errors['label'] = ts('Name already exists in Database.'); | |
406 | } | |
407 | ||
408 | if ((is_numeric(CRM_Utils_Array::value('count', $fields)) && | |
de6c59ca | 409 | empty($fields['count']) |
6a488035 TO |
410 | ) && |
411 | (CRM_Utils_Array::value('html_type', $fields) == 'Text') | |
412 | ) { | |
413 | $errors['count'] = ts('Participant Count must be greater than zero.'); | |
414 | } | |
415 | ||
416 | if ($form->_action & CRM_Core_Action::ADD) { | |
417 | if ($fields['html_type'] != 'Text') { | |
418 | $countemptyrows = 0; | |
a13c171d | 419 | $publicOptionCount = $_flagOption = $_rowError = 0; |
6a488035 TO |
420 | |
421 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
be2fb01f | 422 | $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, ['labelColumn' => 'name']); |
6a488035 TO |
423 | |
424 | for ($index = 1; $index <= self::NUM_OPTION; $index++) { | |
425 | ||
426 | $noLabel = $noAmount = $noWeight = 1; | |
427 | if (!empty($fields['option_label'][$index])) { | |
428 | $noLabel = 0; | |
429 | $duplicateIndex = CRM_Utils_Array::key($fields['option_label'][$index], | |
430 | $fields['option_label'] | |
431 | ); | |
432 | ||
433 | if ((!($duplicateIndex === FALSE)) && | |
434 | (!($duplicateIndex == $index)) | |
435 | ) { | |
436 | $errors["option_label[{$index}]"] = ts('Duplicate label value'); | |
437 | $_flagOption = 1; | |
438 | } | |
439 | } | |
440 | if ($form->_useForMember) { | |
441 | if (!empty($fields['membership_type_id'][$index])) { | |
442 | $memTypesIDS[] = $fields['membership_type_id'][$index]; | |
443 | } | |
444 | } | |
445 | ||
446 | // allow for 0 value. | |
447 | if (!empty($fields['option_amount'][$index]) || | |
448 | strlen($fields['option_amount'][$index]) > 0 | |
449 | ) { | |
450 | $noAmount = 0; | |
451 | } | |
452 | ||
453 | if (!empty($fields['option_weight'][$index])) { | |
454 | $noWeight = 0; | |
455 | $duplicateIndex = CRM_Utils_Array::key($fields['option_weight'][$index], | |
456 | $fields['option_weight'] | |
457 | ); | |
458 | ||
459 | if ((!($duplicateIndex === FALSE)) && | |
460 | (!($duplicateIndex == $index)) | |
461 | ) { | |
462 | $errors["option_weight[{$index}]"] = ts('Duplicate weight value'); | |
463 | $_flagOption = 1; | |
464 | } | |
465 | } | |
8cc574cf | 466 | if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') { |
6a488035 | 467 | $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.'); |
366fe2a3 | 468 | } |
6a488035 TO |
469 | if ($noLabel && !$noAmount) { |
470 | $errors["option_label[{$index}]"] = ts('Label cannot be empty.'); | |
471 | $_flagOption = 1; | |
472 | } | |
473 | ||
474 | if (!$noLabel && $noAmount) { | |
475 | $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.'); | |
476 | $_flagOption = 1; | |
477 | } | |
478 | ||
479 | if ($noLabel && $noAmount) { | |
480 | $countemptyrows++; | |
481 | $_emptyRow = 1; | |
482 | } | |
483 | elseif (!empty($fields['option_max_value'][$index]) && | |
484 | !empty($fields['option_count'][$index]) && | |
485 | ($fields['option_count'][$index] > $fields['option_max_value'][$index]) | |
486 | ) { | |
487 | $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.'); | |
488 | $_flagOption = 1; | |
489 | } | |
490 | ||
491 | $showBlocks = 'optionField_' . $index; | |
492 | if ($_flagOption) { | |
493 | $_showHide->addShow($showBlocks); | |
494 | $_rowError = 1; | |
495 | } | |
496 | ||
497 | if (!empty($_emptyRow)) { | |
498 | $_showHide->addHide($showBlocks); | |
499 | } | |
500 | else { | |
501 | $_showHide->addShow($showBlocks); | |
502 | } | |
503 | if ($index == self::NUM_OPTION) { | |
504 | $hideBlock = 'additionalOption'; | |
505 | $_showHide->addHide($hideBlock); | |
506 | } | |
507 | ||
a13c171d CR |
508 | if (!empty($fields['option_visibility_id'][$index]) && (!$noLabel || !$noAmount)) { |
509 | if ($visibilityOptions[$fields['option_visibility_id'][$index]] == 'public') { | |
510 | $publicOptionCount++; | |
511 | } | |
512 | } | |
513 | ||
6a488035 TO |
514 | $_flagOption = $_emptyRow = 0; |
515 | } | |
516 | ||
517 | if (!empty($memTypesIDS)) { | |
518 | // check for checkboxes allowing user to select multiple memberships from same membership organization | |
519 | if ($fields['html_type'] == 'CheckBox') { | |
ba1dcfda | 520 | $foundDuplicate = FALSE; |
be2fb01f | 521 | $orgIds = []; |
ba1dcfda TO |
522 | foreach ($memTypesIDS as $key => $val) { |
523 | $org = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization($val); | |
524 | if (in_array($org[$val], $orgIds)) { | |
525 | $foundDuplicate = TRUE; | |
526 | break; | |
527 | } | |
528 | $orgIds[$val] = $org[$val]; | |
6a488035 | 529 | |
ba1dcfda TO |
530 | } |
531 | if ($foundDuplicate) { | |
532 | $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity.'); | |
533 | } | |
6a488035 | 534 | } |
366fe2a3 | 535 | |
6a488035 TO |
536 | // CRM-10390 - Only one price field in a set can include auto-renew membership options |
537 | $foundAutorenew = FALSE; | |
538 | foreach ($memTypesIDS as $key => $val) { | |
539 | // see if any price field option values in this price field are for memberships with autorenew | |
540 | $memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($val); | |
a7488080 | 541 | if (!empty($memTypeDetails['auto_renew'])) { |
6a488035 TO |
542 | $foundAutorenew = TRUE; |
543 | break; | |
544 | } | |
545 | } | |
546 | ||
547 | if ($foundAutorenew) { | |
548 | // if so, check for other fields in this price set which also have auto-renew membership options | |
9da8dc8c | 549 | $otherFieldAutorenew = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_sid); |
6a488035 TO |
550 | if ($otherFieldAutorenew) { |
551 | $errors['_qf_default'] = ts('You can include auto-renew membership choices for only one price field in a price set. Another field in this set already contains one or more auto-renew membership options.'); | |
552 | } | |
553 | } | |
554 | } | |
555 | $_showHide->addToTemplate(); | |
556 | ||
831e0fa5 | 557 | if ($countemptyrows == 15) { |
6a488035 TO |
558 | $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.'); |
559 | $_flagOption = 1; | |
560 | } | |
a13c171d CR |
561 | |
562 | if ($visibilityOptions[$fields['visibility_id']] == 'public' && $publicOptionCount == 0) { | |
563 | $errors['visibility_id'] = ts('You have selected to make this field public but have not enabled any public price options. Please update your selections to include a public price option, or make this field admin visibility only.'); | |
564 | for ($index = 1; $index <= self::NUM_OPTION; $index++) { | |
565 | if (!empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index])) { | |
566 | $errors["option_visibility_id[{$index}]"] = ts('Public field should at least have one public option.'); | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | if ($visibilityOptions[$fields['visibility_id']] == 'admin' && $publicOptionCount > 0) { | |
572 | $errors['visibility_id'] = ts('Field with \'Admin\' visibility should only contain \'Admin\' options.'); | |
573 | ||
574 | for ($index = 1; $index <= self::NUM_OPTION; $index++) { | |
575 | ||
576 | $isOptionSet = !empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index]); | |
577 | $currentOptionVisibility = CRM_Utils_Array::value($fields['option_visibility_id'][$index], $visibilityOptions); | |
578 | ||
579 | if ($isOptionSet && $currentOptionVisibility == 'public') { | |
580 | $errors["option_visibility_id[{$index}]"] = ts('\'Admin\' field should only have \'Admin\' visibility options.'); | |
581 | } | |
582 | } | |
583 | } | |
6a488035 TO |
584 | } |
585 | elseif (!empty($fields['max_value']) && | |
586 | !empty($fields['count']) && | |
587 | ($fields['count'] > $fields['max_value']) | |
588 | ) { | |
589 | $errors['max_value'] = ts('Participant count can not be greater than max participants.'); | |
590 | } | |
591 | ||
592 | // do not process if no option rows were submitted | |
593 | if (empty($fields['option_amount']) && empty($fields['option_label'])) { | |
594 | return TRUE; | |
595 | } | |
596 | ||
597 | if (empty($fields['option_name'])) { | |
be2fb01f | 598 | $fields['option_amount'] = []; |
6a488035 TO |
599 | } |
600 | ||
601 | if (empty($fields['option_label'])) { | |
be2fb01f | 602 | $fields['option_label'] = []; |
6a488035 TO |
603 | } |
604 | } | |
605 | ||
606 | return empty($errors) ? TRUE : $errors; | |
607 | } | |
608 | ||
609 | /** | |
fe482240 | 610 | * Process the form. |
a72a9544 | 611 | * |
612 | * @throws \CRM_Core_Exception | |
431c56b2 | 613 | * @throws \CiviCRM_API3_Exception |
6a488035 TO |
614 | */ |
615 | public function postProcess() { | |
616 | // store the submitted values in an array | |
617 | $params = $this->controller->exportValues('Field'); | |
431c56b2 | 618 | $params['price'] = CRM_Utils_Rule::cleanMoney($params['price']); |
6a488035 | 619 | |
6a488035 TO |
620 | $params['is_display_amounts'] = CRM_Utils_Array::value('is_display_amounts', $params, FALSE); |
621 | $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE); | |
622 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
353ffa53 | 623 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE); |
6a488035 TO |
624 | $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE); |
625 | $params['count'] = CRM_Utils_Array::value('count', $params, FALSE); | |
626 | ||
627 | // need the FKEY - price set id | |
628 | $params['price_set_id'] = $this->_sid; | |
629 | ||
630 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { | |
be2fb01f | 631 | $fieldValues = ['price_set_id' => $this->_sid]; |
6a488035 TO |
632 | $oldWeight = NULL; |
633 | if ($this->_fid) { | |
9da8dc8c | 634 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id'); |
6a488035 | 635 | } |
9da8dc8c | 636 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues); |
6a488035 TO |
637 | } |
638 | ||
639 | // make value <=> name consistency. | |
640 | if (isset($params['option_name'])) { | |
641 | $params['option_value'] = $params['option_name']; | |
642 | } | |
643 | $params['is_enter_qty'] = CRM_Utils_Array::value('is_enter_qty', $params, FALSE); | |
644 | ||
a72a9544 | 645 | if ($params['html_type'] === 'Text') { |
6a488035 TO |
646 | // if html type is Text, force is_enter_qty on |
647 | $params['is_enter_qty'] = 1; | |
648 | // modify params values as per the option group and option | |
649 | // value | |
be2fb01f CW |
650 | $params['option_amount'] = [1 => $params['price']]; |
651 | $params['option_label'] = [1 => $params['label']]; | |
652 | $params['option_count'] = [1 => $params['count']]; | |
653 | $params['option_max_value'] = [1 => CRM_Utils_Array::value('max_value', $params)]; | |
6a488035 | 654 | //$params['option_description'] = array( 1 => $params['description'] ); |
be2fb01f CW |
655 | $params['option_weight'] = [1 => $params['weight']]; |
656 | $params['option_financial_type_id'] = [1 => $params['financial_type_id']]; | |
657 | $params['option_visibility_id'] = [1 => CRM_Utils_Array::value('visibility_id', $params)]; | |
6a488035 TO |
658 | } |
659 | ||
660 | if ($this->_fid) { | |
661 | $params['id'] = $this->_fid; | |
662 | } | |
366fe2a3 | 663 | |
6a488035 TO |
664 | $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL; |
665 | ||
9da8dc8c | 666 | $priceField = CRM_Price_BAO_PriceField::create($params); |
6a488035 TO |
667 | |
668 | if (!is_a($priceField, 'CRM_Core_Error')) { | |
be2fb01f | 669 | CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', [1 => $priceField->label]), ts('Saved'), 'success'); |
6a488035 TO |
670 | } |
671 | $buttonName = $this->controller->getButtonName(); | |
672 | $session = CRM_Core_Session::singleton(); | |
673 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
674 | CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info'); | |
675 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid)); | |
676 | } | |
677 | else { | |
678 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
679 | } | |
680 | } | |
96025800 | 681 | |
6a488035 | 682 | } |