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