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