Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * form to process actions on the field aspect of Price | |
36 | */ | |
37 | class CRM_Price_Form_Field extends CRM_Core_Form { | |
38 | ||
39 | /** | |
40 | * Constants for number of options for data types of multiple option. | |
41 | */ | |
7da04cde | 42 | const NUM_OPTION = 15; |
6a488035 TO |
43 | |
44 | /** | |
fe482240 | 45 | * The custom set id saved to the session for an update. |
6a488035 TO |
46 | * |
47 | * @var int | |
6a488035 TO |
48 | */ |
49 | protected $_sid; | |
50 | ||
51 | /** | |
52 | * The field id, used when editing the field | |
53 | * | |
54 | * @var int | |
6a488035 TO |
55 | */ |
56 | protected $_fid; | |
57 | ||
58 | /** | |
fe482240 | 59 | * The extended component Id. |
6a488035 TO |
60 | * |
61 | * @var array | |
6a488035 TO |
62 | */ |
63 | protected $_extendComponentId; | |
64 | ||
65 | /** | |
fe482240 | 66 | * Variable is set if price set is used for membership. |
6a488035 TO |
67 | */ |
68 | protected $_useForMember; | |
69 | ||
70 | /** | |
fe482240 | 71 | * Set variables up before form is built. |
6a488035 TO |
72 | */ |
73 | public function preProcess() { | |
74 | ||
75 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST'); | |
76 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST'); | |
353ffa53 | 77 | $url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}"); |
704f21c0 | 78 | $breadCrumb = array(array('title' => ts('Price Set Fields'), 'url' => $url)); |
6a488035 TO |
79 | |
80 | $this->_extendComponentId = array(); | |
9da8dc8c | 81 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
82 | if ($extendComponentId) { |
83 | $this->_extendComponentId = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId); | |
84 | } | |
85 | ||
86 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
704f21c0 | 87 | |
e2046b33 | 88 | $this->setPageTitle(ts('Price Field')); |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
bf15c191 | 92 | * Set default values for the form. |
6a488035 | 93 | * |
a6c01b45 CW |
94 | * @return array |
95 | * array of default values | |
6a488035 | 96 | */ |
00be9182 | 97 | public function setDefaultValues() { |
6a488035 TO |
98 | $defaults = array(); |
99 | // is it an edit operation ? | |
100 | if (isset($this->_fid)) { | |
101 | $params = array('id' => $this->_fid); | |
102 | $this->assign('fid', $this->_fid); | |
9da8dc8c | 103 | CRM_Price_BAO_PriceField::retrieve($params, $defaults); |
6a488035 TO |
104 | $this->_sid = $defaults['price_set_id']; |
105 | ||
106 | // if text, retrieve price | |
107 | if ($defaults['html_type'] == 'Text') { | |
fc8edfbf | 108 | $isActive = $defaults['is_active']; |
6a488035 TO |
109 | $valueParams = array('price_field_id' => $this->_fid); |
110 | ||
9da8dc8c | 111 | CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults); |
6a488035 TO |
112 | |
113 | // fix the display of the monetary value, CRM-4038 | |
114 | $defaults['price'] = CRM_Utils_Money::format($defaults['amount'], NULL, '%a'); | |
ff33ddc8 | 115 | $defaults['is_active'] = $isActive; |
6a488035 TO |
116 | } |
117 | ||
6a488035 TO |
118 | } |
119 | else { | |
120 | $defaults['is_active'] = 1; | |
121 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
122 | $defaults['option_status[' . $i . ']'] = 1; | |
123 | $defaults['option_weight[' . $i . ']'] = $i; | |
2db35bf6 | 124 | $defaults['option_visibility_id[' . $i . ']'] = 1; |
6a488035 TO |
125 | } |
126 | } | |
127 | ||
128 | if ($this->_action & CRM_Core_Action::ADD) { | |
129 | $fieldValues = array('price_set_id' => $this->_sid); | |
9da8dc8c | 130 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues); |
6a488035 TO |
131 | $defaults['options_per_line'] = 1; |
132 | $defaults['is_display_amounts'] = 1; | |
133 | } | |
0495a6a4 | 134 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
135 | $eventComponentId = NULL; | |
ba1dcfda | 136 | if (array_key_exists('CiviEvent', $enabledComponents)) { |
353ffa53 | 137 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); |
0495a6a4 | 138 | } |
6a488035 TO |
139 | |
140 | if (isset($this->_sid) && $this->_action == CRM_Core_Action::ADD) { | |
9da8dc8c | 141 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id'); |
6a488035 TO |
142 | $defaults['financial_type_id'] = $financialTypeId; |
143 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
144 | $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId; | |
145 | } | |
146 | } | |
147 | return $defaults; | |
148 | } | |
149 | ||
150 | /** | |
fe482240 | 151 | * Build the form object. |
6a488035 TO |
152 | */ |
153 | public function buildQuickForm() { | |
154 | // lets trim all the whitespace | |
155 | $this->applyFilter('__ALL__', 'trim'); | |
156 | ||
157 | // add a hidden field to remember the price set id | |
158 | // this get around the browser tab issue | |
159 | $this->add('hidden', 'sid', $this->_sid); | |
160 | $this->add('hidden', 'fid', $this->_fid); | |
161 | ||
162 | // label | |
9da8dc8c | 163 | $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE); |
6a488035 TO |
164 | |
165 | // html_type | |
166 | $javascript = 'onchange="option_html_type(this.form)";'; | |
167 | ||
9da8dc8c | 168 | $htmlTypes = CRM_Price_BAO_PriceField::htmlTypes(); |
6a488035 TO |
169 | |
170 | // Text box for Participant Count for a field | |
171 | ||
172 | // Financial Type | |
173 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
53df1142 | 174 | foreach ($financialType as $finTypeId => $type) { |
40c655aa | 175 | if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() |
66af7c48 PN |
176 | && !CRM_Core_Permission::check('add contributions of type ' . $type) |
177 | ) { | |
53df1142 E |
178 | unset($financialType[$finTypeId]); |
179 | } | |
180 | } | |
6a488035 TO |
181 | if (count($financialType)) { |
182 | $this->assign('financialType', $financialType); | |
183 | } | |
2db35bf6 AF |
184 | |
185 | //Visibility Type Options | |
186 | $visibilityType = CRM_Core_PseudoConstant::visibility(); | |
187 | $this->assign('visibilityType', $visibilityType); | |
188 | ||
0495a6a4 | 189 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
190 | $eventComponentId = $memberComponentId = NULL; | |
ba1dcfda | 191 | if (array_key_exists('CiviEvent', $enabledComponents)) { |
353ffa53 | 192 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); |
0495a6a4 | 193 | } |
ba1dcfda | 194 | if (array_key_exists('CiviMember', $enabledComponents)) { |
0495a6a4 | 195 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); |
196 | } | |
366fe2a3 | 197 | |
353ffa53 | 198 | $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue'); |
366fe2a3 | 199 | |
200 | $this->add('select', 'financial_type_id', | |
201 | ts('Financial Type'), | |
ba1dcfda | 202 | array(' ' => ts('- select -')) + $financialType |
6a488035 TO |
203 | ); |
204 | ||
205 | $this->assign('useForMember', FALSE); | |
206 | if (in_array($eventComponentId, $this->_extendComponentId)) { | |
207 | $this->add('text', 'count', ts('Participant Count'), $attributes['count']); | |
208 | ||
209 | $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger'); | |
210 | ||
211 | $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']); | |
212 | $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger'); | |
213 | ||
214 | $this->assign('useForEvent', TRUE); | |
215 | } | |
216 | else { | |
217 | if (in_array($memberComponentId, $this->_extendComponentId)) { | |
218 | $this->_useForMember = 1; | |
219 | $this->assign('useForMember', $this->_useForMember); | |
220 | } | |
221 | $this->assign('useForEvent', FALSE); | |
222 | } | |
223 | ||
224 | $sel = $this->add('select', 'html_type', ts('Input Field Type'), | |
225 | $htmlTypes, TRUE, $javascript | |
226 | ); | |
227 | ||
228 | // price (for text inputs) | |
229 | $this->add('text', 'price', ts('Price')); | |
230 | $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule'); | |
231 | $this->addRule('price', ts('must be a monetary value'), 'money'); | |
232 | ||
5afce5ad | 233 | $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL); |
234 | $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
235 | $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money'); | |
236 | ||
6a488035 TO |
237 | if ($this->_action == CRM_Core_Action::UPDATE) { |
238 | $this->freeze('html_type'); | |
239 | } | |
240 | ||
241 | // form fields of Custom Option rows | |
242 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
243 | ||
244 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
245 | ||
246 | //the show hide blocks | |
247 | $showBlocks = 'optionField_' . $i; | |
248 | if ($i > 2) { | |
249 | $_showHide->addHide($showBlocks); | |
250 | if ($i == self::NUM_OPTION) { | |
251 | $_showHide->addHide('additionalOption'); | |
252 | } | |
253 | } | |
254 | else { | |
255 | $_showHide->addShow($showBlocks); | |
256 | } | |
257 | // label | |
258 | $attributes['label']['size'] = 25; | |
259 | $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']); | |
260 | ||
261 | // amount | |
262 | $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']); | |
263 | $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money'); | |
264 | ||
265 | //Financial Type | |
266 | $this->add( | |
366fe2a3 | 267 | 'select', |
92fcb95f | 268 | 'option_financial_type_id[' . $i . ']', |
366fe2a3 | 269 | ts('Financial Type'), |
270 | array('' => ts('- select -')) + $financialType | |
6a488035 TO |
271 | ); |
272 | if (in_array($eventComponentId, $this->_extendComponentId)) { | |
273 | // count | |
274 | $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']); | |
275 | $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger'); | |
276 | ||
277 | // max_value | |
278 | $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']); | |
279 | $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger'); | |
280 | ||
281 | // description | |
282 | //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 )); | |
283 | } | |
284 | elseif (in_array($memberComponentId, $this->_extendComponentId)) { | |
285 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
286 | $js = array('onchange' => "calculateRowValues( $i );"); | |
287 | ||
288 | $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'), | |
ae5ffbb7 | 289 | array('' => ' ') + $membershipTypes, FALSE, $js |
353ffa53 | 290 | ); |
896bd22c | 291 | $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array::value('membership_num_terms', $attributes)); |
6a488035 TO |
292 | } |
293 | ||
294 | // weight | |
295 | $this->add('text', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']); | |
296 | ||
297 | // is active ? | |
298 | $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?')); | |
299 | ||
2bc7601f | 300 | $this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), $visibilityType); |
6a488035 TO |
301 | $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); |
302 | ||
303 | //for checkbox handling of default option | |
304 | $this->add('checkbox', "default_checkbox_option[$i]", NULL); | |
305 | } | |
306 | //default option selection | |
307 | $this->addGroup($defaultOption, 'default_option'); | |
308 | $_showHide->addToTemplate(); | |
309 | ||
310 | // is_display_amounts | |
311 | $this->add('checkbox', 'is_display_amounts', ts('Display Amount?')); | |
312 | ||
313 | // weight | |
9da8dc8c | 314 | $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE); |
6a488035 TO |
315 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); |
316 | ||
317 | // checkbox / radio options per line | |
318 | $this->add('text', 'options_per_line', ts('Options Per Line')); | |
319 | $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric'); | |
320 | ||
139f5f76 JP |
321 | $this->add('textarea', 'help_pre', ts('Pre Field Help'), |
322 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post') | |
323 | ); | |
324 | ||
6a488035 | 325 | // help post, mask, attributes, javascript ? |
139f5f76 | 326 | $this->add('textarea', 'help_post', ts('Post Field Help'), |
9da8dc8c | 327 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'help_post') |
6a488035 TO |
328 | ); |
329 | ||
70a44aee | 330 | $this->add('datepicker', 'active_on', ts('Active On'), [], FALSE, array('time' => TRUE)); |
6a488035 TO |
331 | |
332 | // expire_on | |
70a44aee | 333 | $this->add('datepicker', 'expire_on', ts('Expire On'), [], FALSE, array('time' => TRUE)); |
6a488035 TO |
334 | |
335 | // is required ? | |
336 | $this->add('checkbox', 'is_required', ts('Required?')); | |
337 | ||
338 | // is active ? | |
339 | $this->add('checkbox', 'is_active', ts('Active?')); | |
340 | ||
341 | // add buttons | |
342 | $this->addButtons(array( | |
c5c263ca AH |
343 | array( |
344 | 'type' => 'next', | |
345 | 'name' => ts('Save'), | |
346 | 'isDefault' => TRUE, | |
347 | ), | |
348 | array( | |
349 | 'type' => 'next', | |
350 | 'name' => ts('Save and New'), | |
351 | 'subName' => 'new', | |
352 | ), | |
353 | array( | |
354 | 'type' => 'cancel', | |
355 | 'name' => ts('Cancel'), | |
356 | ), | |
357 | )); | |
6a488035 TO |
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) && | |
e68e1c9a | 399 | $fields['html_type'] == 'Text' |
6a488035 | 400 | ) { |
e68e1c9a PN |
401 | if ($fields['price'] == NULL) { |
402 | $errors['price'] = ts('Price is a required field'); | |
403 | } | |
404 | if ($fields['financial_type_id'] == '') { | |
405 | $errors['financial_type_id'] = ts('Financial Type is a required field'); | |
406 | } | |
6a488035 | 407 | } |
366fe2a3 | 408 | |
6a488035 | 409 | //avoid the same price field label in Within PriceSet |
9da8dc8c | 410 | $priceFieldLabel = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
411 | $priceFieldLabel->label = $fields['label']; |
412 | $priceFieldLabel->price_set_id = $form->_sid; | |
413 | ||
414 | $dupeLabel = FALSE; | |
415 | if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) { | |
416 | $dupeLabel = TRUE; | |
417 | } | |
418 | ||
419 | if ($dupeLabel) { | |
420 | $errors['label'] = ts('Name already exists in Database.'); | |
421 | } | |
422 | ||
423 | if ((is_numeric(CRM_Utils_Array::value('count', $fields)) && | |
424 | CRM_Utils_Array::value('count', $fields) == 0 | |
425 | ) && | |
426 | (CRM_Utils_Array::value('html_type', $fields) == 'Text') | |
427 | ) { | |
428 | $errors['count'] = ts('Participant Count must be greater than zero.'); | |
429 | } | |
430 | ||
431 | if ($form->_action & CRM_Core_Action::ADD) { | |
432 | if ($fields['html_type'] != 'Text') { | |
433 | $countemptyrows = 0; | |
a13c171d | 434 | $publicOptionCount = $_flagOption = $_rowError = 0; |
6a488035 TO |
435 | |
436 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
a13c171d | 437 | $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array('labelColumn' => 'name')); |
6a488035 TO |
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 | ||
a13c171d CR |
523 | if (!empty($fields['option_visibility_id'][$index]) && (!$noLabel || !$noAmount)) { |
524 | if ($visibilityOptions[$fields['option_visibility_id'][$index]] == 'public') { | |
525 | $publicOptionCount++; | |
526 | } | |
527 | } | |
528 | ||
6a488035 TO |
529 | $_flagOption = $_emptyRow = 0; |
530 | } | |
531 | ||
532 | if (!empty($memTypesIDS)) { | |
533 | // check for checkboxes allowing user to select multiple memberships from same membership organization | |
534 | if ($fields['html_type'] == 'CheckBox') { | |
ba1dcfda TO |
535 | $foundDuplicate = FALSE; |
536 | $orgIds = array(); | |
537 | foreach ($memTypesIDS as $key => $val) { | |
538 | $org = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization($val); | |
539 | if (in_array($org[$val], $orgIds)) { | |
540 | $foundDuplicate = TRUE; | |
541 | break; | |
542 | } | |
543 | $orgIds[$val] = $org[$val]; | |
6a488035 | 544 | |
ba1dcfda TO |
545 | } |
546 | if ($foundDuplicate) { | |
547 | $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.'); | |
548 | } | |
6a488035 | 549 | } |
366fe2a3 | 550 | |
6a488035 TO |
551 | // CRM-10390 - Only one price field in a set can include auto-renew membership options |
552 | $foundAutorenew = FALSE; | |
553 | foreach ($memTypesIDS as $key => $val) { | |
554 | // see if any price field option values in this price field are for memberships with autorenew | |
555 | $memTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($val); | |
a7488080 | 556 | if (!empty($memTypeDetails['auto_renew'])) { |
6a488035 TO |
557 | $foundAutorenew = TRUE; |
558 | break; | |
559 | } | |
560 | } | |
561 | ||
562 | if ($foundAutorenew) { | |
563 | // if so, check for other fields in this price set which also have auto-renew membership options | |
9da8dc8c | 564 | $otherFieldAutorenew = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_sid); |
6a488035 TO |
565 | if ($otherFieldAutorenew) { |
566 | $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.'); | |
567 | } | |
568 | } | |
569 | } | |
570 | $_showHide->addToTemplate(); | |
571 | ||
831e0fa5 | 572 | if ($countemptyrows == 15) { |
6a488035 TO |
573 | $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.'); |
574 | $_flagOption = 1; | |
575 | } | |
a13c171d CR |
576 | |
577 | if ($visibilityOptions[$fields['visibility_id']] == 'public' && $publicOptionCount == 0) { | |
578 | $errors['visibility_id'] = ts('You have selected to make this field public but have not enabled any public price options. Please update your selections to include a public price option, or make this field admin visibility only.'); | |
579 | for ($index = 1; $index <= self::NUM_OPTION; $index++) { | |
580 | if (!empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index])) { | |
581 | $errors["option_visibility_id[{$index}]"] = ts('Public field should at least have one public option.'); | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
586 | if ($visibilityOptions[$fields['visibility_id']] == 'admin' && $publicOptionCount > 0) { | |
587 | $errors['visibility_id'] = ts('Field with \'Admin\' visibility should only contain \'Admin\' options.'); | |
588 | ||
589 | for ($index = 1; $index <= self::NUM_OPTION; $index++) { | |
590 | ||
591 | $isOptionSet = !empty($fields['option_label'][$index]) || !empty($fields['option_amount'][$index]); | |
592 | $currentOptionVisibility = CRM_Utils_Array::value($fields['option_visibility_id'][$index], $visibilityOptions); | |
593 | ||
594 | if ($isOptionSet && $currentOptionVisibility == 'public') { | |
595 | $errors["option_visibility_id[{$index}]"] = ts('\'Admin\' field should only have \'Admin\' visibility options.'); | |
596 | } | |
597 | } | |
598 | } | |
6a488035 TO |
599 | } |
600 | elseif (!empty($fields['max_value']) && | |
601 | !empty($fields['count']) && | |
602 | ($fields['count'] > $fields['max_value']) | |
603 | ) { | |
604 | $errors['max_value'] = ts('Participant count can not be greater than max participants.'); | |
605 | } | |
606 | ||
607 | // do not process if no option rows were submitted | |
608 | if (empty($fields['option_amount']) && empty($fields['option_label'])) { | |
609 | return TRUE; | |
610 | } | |
611 | ||
612 | if (empty($fields['option_name'])) { | |
613 | $fields['option_amount'] = array(); | |
614 | } | |
615 | ||
616 | if (empty($fields['option_label'])) { | |
617 | $fields['option_label'] = array(); | |
618 | } | |
619 | } | |
620 | ||
621 | return empty($errors) ? TRUE : $errors; | |
622 | } | |
623 | ||
624 | /** | |
fe482240 | 625 | * Process the form. |
6a488035 TO |
626 | */ |
627 | public function postProcess() { | |
628 | // store the submitted values in an array | |
629 | $params = $this->controller->exportValues('Field'); | |
630 | ||
6a488035 TO |
631 | $params['is_display_amounts'] = CRM_Utils_Array::value('is_display_amounts', $params, FALSE); |
632 | $params['is_required'] = CRM_Utils_Array::value('is_required', $params, FALSE); | |
633 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
353ffa53 | 634 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE); |
6a488035 TO |
635 | $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE); |
636 | $params['count'] = CRM_Utils_Array::value('count', $params, FALSE); | |
637 | ||
638 | // need the FKEY - price set id | |
639 | $params['price_set_id'] = $this->_sid; | |
640 | ||
641 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) { | |
642 | $fieldValues = array('price_set_id' => $this->_sid); | |
643 | $oldWeight = NULL; | |
644 | if ($this->_fid) { | |
9da8dc8c | 645 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id'); |
6a488035 | 646 | } |
9da8dc8c | 647 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues); |
6a488035 TO |
648 | } |
649 | ||
650 | // make value <=> name consistency. | |
651 | if (isset($params['option_name'])) { | |
652 | $params['option_value'] = $params['option_name']; | |
653 | } | |
654 | $params['is_enter_qty'] = CRM_Utils_Array::value('is_enter_qty', $params, FALSE); | |
655 | ||
656 | if ($params['html_type'] == 'Text') { | |
657 | // if html type is Text, force is_enter_qty on | |
658 | $params['is_enter_qty'] = 1; | |
659 | // modify params values as per the option group and option | |
660 | // value | |
661 | $params['option_amount'] = array(1 => $params['price']); | |
662 | $params['option_label'] = array(1 => $params['label']); | |
663 | $params['option_count'] = array(1 => $params['count']); | |
664 | $params['option_max_value'] = array(1 => CRM_Utils_Array::value('max_value', $params)); | |
665 | //$params['option_description'] = array( 1 => $params['description'] ); | |
666 | $params['option_weight'] = array(1 => $params['weight']); | |
667 | $params['option_financial_type_id'] = array(1 => $params['financial_type_id']); | |
2db35bf6 | 668 | $params['option_visibility_id'] = array(1 => CRM_Utils_Array::value('visibility_id', $params)); |
6a488035 TO |
669 | } |
670 | ||
671 | if ($this->_fid) { | |
672 | $params['id'] = $this->_fid; | |
673 | } | |
366fe2a3 | 674 | |
6a488035 TO |
675 | $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL; |
676 | ||
9da8dc8c | 677 | $priceField = CRM_Price_BAO_PriceField::create($params); |
6a488035 TO |
678 | |
679 | if (!is_a($priceField, 'CRM_Core_Error')) { | |
680 | CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label)), ts('Saved'), 'success'); | |
681 | } | |
682 | $buttonName = $this->controller->getButtonName(); | |
683 | $session = CRM_Core_Session::singleton(); | |
684 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
685 | CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info'); | |
686 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid)); | |
687 | } | |
688 | else { | |
689 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid)); | |
690 | } | |
691 | } | |
96025800 | 692 | |
6a488035 | 693 | } |