Merge pull request #4866 from totten/master-phpcs
[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 array of default values
104 */
105 public function setDefaultValues() {
106 $defaults = array();
107 // is it an edit operation ?
108 if (isset($this->_fid)) {
109 $params = array('id' => $this->_fid);
110 $this->assign('fid', $this->_fid);
111 CRM_Price_BAO_PriceField::retrieve($params, $defaults);
112 $this->_sid = $defaults['price_set_id'];
113
114 // if text, retrieve price
115 if ($defaults['html_type'] == 'Text') {
116 $valueParams = array('price_field_id' => $this->_fid);
117
118 CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults);
119
120 // fix the display of the monetary value, CRM-4038
121 $defaults['price'] = CRM_Utils_Money::format($defaults['amount'], NULL, '%a');
122 }
123
124 if (!empty($defaults['active_on'])) {
125 list($defaults['active_on'],
126 $defaults['active_on_time']
127 ) = CRM_Utils_Date::setDateDefaults($defaults['active_on'], 'activityDateTime');
128 }
129
130 if (!empty($defaults['expire_on'])) {
131 list($defaults['expire_on'],
132 $defaults['expire_on_time']
133 ) = CRM_Utils_Date::setDateDefaults($defaults['expire_on'], 'activityDateTime');
134 }
135 }
136 else {
137 $defaults['is_active'] = 1;
138 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
139 $defaults['option_status[' . $i . ']'] = 1;
140 $defaults['option_weight[' . $i . ']'] = $i;
141 }
142 }
143
144 if ($this->_action & CRM_Core_Action::ADD) {
145 $fieldValues = array('price_set_id' => $this->_sid);
146 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
147 $defaults['options_per_line'] = 1;
148 $defaults['is_display_amounts'] = 1;
149 }
150 $enabledComponents = CRM_Core_Component::getEnabledComponents();
151 $eventComponentId = NULL;
152 if (array_key_exists('CiviEvent', $enabledComponents)) {
153 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
154 }
155
156 if (isset($this->_sid) && $this->_action == CRM_Core_Action::ADD) {
157 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id');
158 $defaults['financial_type_id'] = $financialTypeId;
159 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
160 $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
161 }
162 }
163 return $defaults;
164 }
165
166 /**
167 * Build the form object
168 *
169 * @param null
170 *
171 * @return void
172 */
173 public function buildQuickForm() {
174 // lets trim all the whitespace
175 $this->applyFilter('__ALL__', 'trim');
176
177 // add a hidden field to remember the price set id
178 // this get around the browser tab issue
179 $this->add('hidden', 'sid', $this->_sid);
180 $this->add('hidden', 'fid', $this->_fid);
181
182 // label
183 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
184
185 // html_type
186 $javascript = 'onchange="option_html_type(this.form)";';
187
188 $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes();
189
190 // Text box for Participant Count for a field
191
192 // Financial Type
193 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
194 if (count($financialType)) {
195 $this->assign('financialType', $financialType);
196 }
197 $enabledComponents = CRM_Core_Component::getEnabledComponents();
198 $eventComponentId = $memberComponentId = NULL;
199 if (array_key_exists('CiviEvent', $enabledComponents)) {
200 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
201 }
202 if (array_key_exists('CiviMember', $enabledComponents)) {
203 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
204 }
205
206 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
207
208 $this->add('select', 'financial_type_id',
209 ts('Financial Type'),
210 array(' ' => ts('- select -')) + $financialType
211 );
212
213 $this->assign('useForMember', FALSE);
214 if (in_array($eventComponentId, $this->_extendComponentId)) {
215 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
216
217 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
218
219 $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
220 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
221
222 $this->assign('useForEvent', TRUE);
223 }
224 else {
225 if (in_array($memberComponentId, $this->_extendComponentId)) {
226 $this->_useForMember = 1;
227 $this->assign('useForMember', $this->_useForMember);
228 }
229 $this->assign('useForEvent', FALSE);
230 }
231
232 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
233 $htmlTypes, TRUE, $javascript
234 );
235
236 // price (for text inputs)
237 $this->add('text', 'price', ts('Price'));
238 $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
239 $this->addRule('price', ts('must be a monetary value'), 'money');
240
241 if ($this->_action == CRM_Core_Action::UPDATE) {
242 $this->freeze('html_type');
243 }
244
245 // form fields of Custom Option rows
246 $_showHide = new CRM_Core_ShowHideBlocks('', '');
247
248 for ($i = 1; $i <= self::NUM_OPTION; $i++) {
249
250 //the show hide blocks
251 $showBlocks = 'optionField_' . $i;
252 if ($i > 2) {
253 $_showHide->addHide($showBlocks);
254 if ($i == self::NUM_OPTION) {
255 $_showHide->addHide('additionalOption');
256 }
257 }
258 else {
259 $_showHide->addShow($showBlocks);
260 }
261 // label
262 $attributes['label']['size'] = 25;
263 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
264
265 // amount
266 $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
267 $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
268
269 //Financial Type
270 $this->add(
271 'select',
272 'option_financial_type_id[' . $i . ']',
273 ts('Financial Type'),
274 array('' => ts('- select -')) + $financialType
275 );
276 if (in_array($eventComponentId, $this->_extendComponentId)) {
277 // count
278 $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
279 $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
280
281 // max_value
282 $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
283 $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
284
285 // description
286 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
287 }
288 elseif (in_array($memberComponentId, $this->_extendComponentId)) {
289 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
290 $js = array('onchange' => "calculateRowValues( $i );");
291
292 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
293 array(
294 '' => ' ') + $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 );
366 // is public?
367 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
368
369 // add a form rule to check default value
370 $this->addFormRule(array('CRM_Price_Form_Field', 'formRule'), $this);
371
372 // if view mode pls freeze it with the done button.
373 if ($this->_action & CRM_Core_Action::VIEW) {
374 $this->freeze();
375 $url = CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid);
376 $this->addElement('button',
377 'done',
378 ts('Done'),
379 array('onclick' => "location.href='$url'")
380 );
381 }
382 }
383
384 /**
385 * Global validation rules for the form
386 *
387 * @param array $fields
388 * Posted values of the form.
389 *
390 * @param $files
391 * @param CRM_Core_Form $form
392 *
393 * @return array if errors then list of errors to be posted back to the form,
394 * true otherwise
395 * @static
396 */
397 public static function formRule($fields, $files, $form) {
398
399 // all option fields are of type "money"
400 $errors = array();
401
402 /** Check the option values entered
403 * Appropriate values are required for the selected datatype
404 * Incomplete row checking is also required.
405 */
406 if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) &&
407 $fields['html_type'] == 'Text' && $fields['price'] == NULL
408 ) {
409 $errors['price'] = ts('Price is a required field');
410 }
411
412 if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) &&
413 $fields['html_type'] == 'Text' && $fields['financial_type_id'] == '') {
414 $errors['financial_type_id'] = ts('Financial Type is a required field');
415 }
416
417 //avoid the same price field label in Within PriceSet
418 $priceFieldLabel = new CRM_Price_DAO_PriceField();
419 $priceFieldLabel->label = $fields['label'];
420 $priceFieldLabel->price_set_id = $form->_sid;
421
422 $dupeLabel = FALSE;
423 if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) {
424 $dupeLabel = TRUE;
425 }
426
427 if ($dupeLabel) {
428 $errors['label'] = ts('Name already exists in Database.');
429 }
430
431 if ((is_numeric(CRM_Utils_Array::value('count', $fields)) &&
432 CRM_Utils_Array::value('count', $fields) == 0
433 ) &&
434 (CRM_Utils_Array::value('html_type', $fields) == 'Text')
435 ) {
436 $errors['count'] = ts('Participant Count must be greater than zero.');
437 }
438
439 if ($form->_action & CRM_Core_Action::ADD) {
440 if ($fields['html_type'] != 'Text') {
441 $countemptyrows = 0;
442 $_flagOption = $_rowError = 0;
443
444 $_showHide = new CRM_Core_ShowHideBlocks('', '');
445
446 for ($index = 1; $index <= self::NUM_OPTION; $index++) {
447
448 $noLabel = $noAmount = $noWeight = 1;
449 if (!empty($fields['option_label'][$index])) {
450 $noLabel = 0;
451 $duplicateIndex = CRM_Utils_Array::key($fields['option_label'][$index],
452 $fields['option_label']
453 );
454
455 if ((!($duplicateIndex === FALSE)) &&
456 (!($duplicateIndex == $index))
457 ) {
458 $errors["option_label[{$index}]"] = ts('Duplicate label value');
459 $_flagOption = 1;
460 }
461 }
462 if ($form->_useForMember) {
463 if (!empty($fields['membership_type_id'][$index])) {
464 $memTypesIDS[] = $fields['membership_type_id'][$index];
465 }
466 }
467
468 // allow for 0 value.
469 if (!empty($fields['option_amount'][$index]) ||
470 strlen($fields['option_amount'][$index]) > 0
471 ) {
472 $noAmount = 0;
473 }
474
475 if (!empty($fields['option_weight'][$index])) {
476 $noWeight = 0;
477 $duplicateIndex = CRM_Utils_Array::key($fields['option_weight'][$index],
478 $fields['option_weight']
479 );
480
481 if ((!($duplicateIndex === FALSE)) &&
482 (!($duplicateIndex == $index))
483 ) {
484 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
485 $_flagOption = 1;
486 }
487 }
488 if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
489 $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
490 }
491 if ($noLabel && !$noAmount) {
492 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
493 $_flagOption = 1;
494 }
495
496 if (!$noLabel && $noAmount) {
497 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
498 $_flagOption = 1;
499 }
500
501 if ($noLabel && $noAmount) {
502 $countemptyrows++;
503 $_emptyRow = 1;
504 }
505 elseif (!empty($fields['option_max_value'][$index]) &&
506 !empty($fields['option_count'][$index]) &&
507 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
508 ) {
509 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
510 $_flagOption = 1;
511 }
512
513 $showBlocks = 'optionField_' . $index;
514 if ($_flagOption) {
515 $_showHide->addShow($showBlocks);
516 $_rowError = 1;
517 }
518
519 if (!empty($_emptyRow)) {
520 $_showHide->addHide($showBlocks);
521 }
522 else {
523 $_showHide->addShow($showBlocks);
524 }
525 if ($index == self::NUM_OPTION) {
526 $hideBlock = 'additionalOption';
527 $_showHide->addHide($hideBlock);
528 }
529
530 $_flagOption = $_emptyRow = 0;
531 }
532
533 if (!empty($memTypesIDS)) {
534 // check for checkboxes allowing user to select multiple memberships from same membership organization
535 if ($fields['html_type'] == 'CheckBox') {
536 $foundDuplicate = FALSE;
537 $orgIds = array();
538 foreach ($memTypesIDS as $key => $val) {
539 $org = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization($val);
540 if (in_array($org[$val], $orgIds)) {
541 $foundDuplicate = TRUE;
542 break;
543 }
544 $orgIds[$val] = $org[$val];
545
546 }
547 if ($foundDuplicate) {
548 $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.');
549 }
550 }
551
552 // CRM-10390 - Only one price field in a set can include auto-renew membership options
553 $foundAutorenew = FALSE;
554 foreach ($memTypesIDS as $key => $val) {
555 // see if any price field option values in this price field are for memberships with autorenew
556 $memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($val);
557 if (!empty($memTypeDetails['auto_renew'])) {
558 $foundAutorenew = TRUE;
559 break;
560 }
561 }
562
563 if ($foundAutorenew) {
564 // if so, check for other fields in this price set which also have auto-renew membership options
565 $otherFieldAutorenew = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_sid);
566 if ($otherFieldAutorenew) {
567 $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.');
568 }
569 }
570 }
571 $_showHide->addToTemplate();
572
573 if ($countemptyrows == 15) {
574 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
575 $_flagOption = 1;
576 }
577 }
578 elseif (!empty($fields['max_value']) &&
579 !empty($fields['count']) &&
580 ($fields['count'] > $fields['max_value'])
581 ) {
582 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
583 }
584
585 // do not process if no option rows were submitted
586 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
587 return TRUE;
588 }
589
590 if (empty($fields['option_name'])) {
591 $fields['option_amount'] = array();
592 }
593
594 if (empty($fields['option_label'])) {
595 $fields['option_label'] = array();
596 }
597 }
598
599 return empty($errors) ? TRUE : $errors;
600 }
601
602 /**
603 * Process the form
604 *
605 * @param null
606 *
607 * @return void
608 */
609 public function postProcess() {
610 // store the submitted values in an array
611 $params = $this->controller->exportValues('Field');
612
613 $params['name'] = CRM_Utils_String::titleToVar($params['label']);
614 $params['is_display_amounts'] = CRM_Utils_Array::value('is_display_amounts', $params, FALSE);
615 $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE);
616 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
617 $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE);
618 if (isset($params['active_on'])) {
619 $params['active_on'] = CRM_Utils_Date::processDate($params['active_on'],
620 CRM_Utils_Array::value('active_on_time', $params),
621 TRUE
622 );
623 }
624 if (isset($params['expire_on'])) {
625 $params['expire_on'] = CRM_Utils_Date::processDate($params['expire_on'],
626 CRM_Utils_Array::value('expire_on_time', $params),
627 TRUE
628 );
629 }
630 $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
631 $params['count'] = CRM_Utils_Array::value('count', $params, FALSE);
632
633 // need the FKEY - price set id
634 $params['price_set_id'] = $this->_sid;
635
636 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
637 $fieldValues = array('price_set_id' => $this->_sid);
638 $oldWeight = NULL;
639 if ($this->_fid) {
640 $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id');
641 }
642 $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
643 }
644
645 // make value <=> name consistency.
646 if (isset($params['option_name'])) {
647 $params['option_value'] = $params['option_name'];
648 }
649 $params['is_enter_qty'] = CRM_Utils_Array::value('is_enter_qty', $params, FALSE);
650
651 if ($params['html_type'] == 'Text') {
652 // if html type is Text, force is_enter_qty on
653 $params['is_enter_qty'] = 1;
654 // modify params values as per the option group and option
655 // value
656 $params['option_amount'] = array(1 => $params['price']);
657 $params['option_label'] = array(1 => $params['label']);
658 $params['option_count'] = array(1 => $params['count']);
659 $params['option_max_value'] = array(1 => CRM_Utils_Array::value('max_value', $params));
660 //$params['option_description'] = array( 1 => $params['description'] );
661 $params['option_weight'] = array(1 => $params['weight']);
662 $params['option_financial_type_id'] = array(1 => $params['financial_type_id']);
663 $params['is_active'] = array(1 => 1);
664 }
665
666 if ($this->_fid) {
667 $params['id'] = $this->_fid;
668 }
669
670 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL;
671
672 $priceField = CRM_Price_BAO_PriceField::create($params);
673
674 if (!is_a($priceField, 'CRM_Core_Error')) {
675 CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label)), ts('Saved'), 'success');
676 }
677 $buttonName = $this->controller->getButtonName();
678 $session = CRM_Core_Session::singleton();
679 if ($buttonName == $this->getButtonName('next', 'new')) {
680 CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info');
681 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid));
682 }
683 else {
684 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
685 }
686 }
687 }