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