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