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