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