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