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