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 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * form to process actions on the field aspect of Custom | |
38 | */ | |
39 | class CRM_Price_Form_Option extends CRM_Core_Form { | |
40 | ||
41 | /** | |
fe482240 | 42 | * The price field id saved to the session for an update. |
6a488035 TO |
43 | * |
44 | * @var int | |
6a488035 TO |
45 | */ |
46 | protected $_fid; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Option value id, used when editing the Option |
6a488035 TO |
50 | * |
51 | * @var int | |
6a488035 TO |
52 | */ |
53 | protected $_oid; | |
54 | ||
062913c2 SL |
55 | /** |
56 | * Array of Money fields | |
57 | * | |
58 | * @var array | |
59 | */ | |
60 | protected $_moneyFields = ['amount', 'non_deductible_amount']; | |
61 | ||
6a488035 | 62 | /** |
fe482240 | 63 | * Set variables up before form is built. |
6a488035 | 64 | * |
6a488035 | 65 | * @return void |
6a488035 TO |
66 | */ |
67 | public function preProcess() { | |
e5e8ab99 | 68 | $this->setPageTitle(ts('Price Option')); |
6a488035 TO |
69 | $this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', |
70 | $this | |
71 | ); | |
72 | $this->_oid = CRM_Utils_Request::retrieve('oid', 'Positive', | |
73 | $this | |
74 | ); | |
75 | } | |
76 | ||
77 | /** | |
c490a46a | 78 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
79 | * the default values are retrieved from the database |
80 | * | |
bed98343 | 81 | * @return array|void array of default values |
6a488035 | 82 | */ |
00be9182 | 83 | public function setDefaultValues() { |
a4c1913d | 84 | if ($this->_action == CRM_Core_Action::DELETE) { |
bed98343 | 85 | return NULL; |
a4c1913d | 86 | } |
be2fb01f | 87 | $defaults = []; |
6a488035 TO |
88 | |
89 | if (isset($this->_oid)) { | |
be2fb01f | 90 | $params = ['id' => $this->_oid]; |
6a488035 | 91 | |
9da8dc8c | 92 | CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults); |
6a488035 TO |
93 | |
94 | // fix the display of the monetary value, CRM-4038 | |
062913c2 SL |
95 | foreach ($this->_moneyFields as $field) { |
96 | $defaults[$field] = CRM_Utils_Money::format(CRM_Utils_Array::value($field, $defaults), NULL, '%a'); | |
97 | } | |
6a488035 TO |
98 | } |
99 | ||
100 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); | |
9da8dc8c | 101 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
102 | |
103 | if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) { | |
104 | $defaults['membership_num_terms'] = 1; | |
105 | } | |
106 | // set financial type used for price set to set default for new option | |
107 | if (!$this->_oid) { | |
9da8dc8c | 108 | $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');; |
6a488035 TO |
109 | } |
110 | if (!isset($defaults['weight']) || !$defaults['weight']) { | |
be2fb01f | 111 | $fieldValues = ['price_field_id' => $this->_fid]; |
9da8dc8c | 112 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues); |
6a488035 TO |
113 | $defaults['is_active'] = 1; |
114 | } | |
115 | ||
116 | return $defaults; | |
117 | } | |
118 | ||
119 | /** | |
fe482240 | 120 | * Build the form object. |
6a488035 | 121 | * |
6a488035 | 122 | * @return void |
6a488035 TO |
123 | */ |
124 | public function buildQuickForm() { | |
e3ca0d21 E |
125 | if ($this->_action == CRM_Core_Action::UPDATE) { |
126 | $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id'); | |
23dba589 | 127 | if (!CRM_Financial_BAO_FinancialType::checkPermissionToEditFinancialType($finTypeId)) { |
e3ca0d21 E |
128 | CRM_Core_Error::fatal(ts("You do not have permission to access this page")); |
129 | } | |
130 | } | |
6a488035 | 131 | if ($this->_action == CRM_Core_Action::DELETE) { |
be2fb01f CW |
132 | $this->addButtons([ |
133 | [ | |
c5c263ca AH |
134 | 'type' => 'next', |
135 | 'name' => ts('Delete'), | |
be2fb01f CW |
136 | ], |
137 | [ | |
c5c263ca AH |
138 | 'type' => 'cancel', |
139 | 'name' => ts('Cancel'), | |
be2fb01f CW |
140 | ], |
141 | ]); | |
bed98343 | 142 | return NULL; |
6a488035 TO |
143 | } |
144 | else { | |
353ffa53 | 145 | $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue'); |
6a488035 TO |
146 | // lets trim all the whitespace |
147 | $this->applyFilter('__ALL__', 'trim'); | |
148 | ||
149 | // hidden Option Id for validation use | |
150 | $this->add('hidden', 'optionId', $this->_oid); | |
151 | ||
94166e28 CW |
152 | // Needed for i18n dialog |
153 | $this->assign('optionId', $this->_oid); | |
154 | ||
6a488035 TO |
155 | //hidden field ID for validation use |
156 | $this->add('hidden', 'fieldId', $this->_fid); | |
157 | ||
158 | // label | |
159 | $this->add('text', 'label', ts('Option Label'), NULL, TRUE); | |
160 | $memberComponentId = CRM_Core_Component::getComponentID('CiviMember'); | |
161 | if ($this->_action == CRM_Core_Action::UPDATE) { | |
162 | $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this); | |
163 | } | |
164 | elseif ($this->_action == CRM_Core_Action::ADD || | |
165 | $this->_action == CRM_Core_Action::VIEW | |
166 | ) { | |
9da8dc8c | 167 | $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id'); |
6a488035 | 168 | } |
ba1dcfda | 169 | $this->isEvent = FALSE; |
9da8dc8c | 170 | $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id'); |
6a488035 TO |
171 | $this->assign('showMember', FALSE); |
172 | if ($memberComponentId == $extendComponentId) { | |
173 | $this->assign('showMember', TRUE); | |
174 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
be2fb01f | 175 | $this->add('select', 'membership_type_id', ts('Membership Type'), [ |
c5c263ca | 176 | '' => ' ', |
be2fb01f CW |
177 | ] + $membershipTypes, FALSE, |
178 | ['onClick' => "calculateRowValues( );"]); | |
d70ada2a | 179 | $this->add('number', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']); |
6a488035 TO |
180 | } |
181 | else { | |
182 | $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId); | |
183 | $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent'); | |
184 | if (in_array($eventComponentId, $allComponents)) { | |
185 | $this->isEvent = TRUE; | |
186 | // count | |
d70ada2a | 187 | $this->add('number', 'count', ts('Participant Count')); |
6a488035 | 188 | $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger'); |
366fe2a3 | 189 | |
d70ada2a | 190 | $this->add('number', 'max_value', ts('Max Participants')); |
6a488035 TO |
191 | $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger'); |
192 | } | |
366fe2a3 | 193 | |
6a488035 TO |
194 | } |
195 | //Financial Type | |
196 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); | |
366fe2a3 | 197 | |
6a488035 TO |
198 | if (count($financialType)) { |
199 | $this->assign('financialType', $financialType); | |
200 | } | |
201 | $this->add( | |
366fe2a3 | 202 | 'select', |
203 | 'financial_type_id', | |
204 | ts('Financial Type'), | |
be2fb01f | 205 | ['' => ts('- select -')] + $financialType, |
ba1dcfda | 206 | TRUE |
6a488035 | 207 | ); |
366fe2a3 | 208 | |
9da8dc8c | 209 | //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' ); |
6a488035 TO |
210 | // FIX ME: duplicate rule? |
211 | /* | |
e70a7fc0 TO |
212 | $this->addRule( 'label', |
213 | ts('Duplicate option label.'), | |
214 | 'optionExists', | |
215 | array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) ); | |
216 | */ | |
6a488035 | 217 | |
6a488035 TO |
218 | // value |
219 | $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE); | |
220 | ||
221 | // the above value is used directly by QF, so the value has to be have a rule | |
222 | // please check with Lobo before u comment this | |
223 | $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
224 | $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money'); | |
225 | ||
5afce5ad | 226 | $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL); |
227 | $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule'); | |
228 | $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money'); | |
229 | ||
6a488035 | 230 | $this->add('textarea', 'description', ts('Description')); |
08b8b2d9 | 231 | $this->add('textarea', 'help_pre', ts('Pre Option Help')); |
232 | $this->add('textarea', 'help_post', ts('Post Option Help')); | |
6a488035 TO |
233 | |
234 | // weight | |
f20d2b0d | 235 | $this->add('number', 'weight', ts('Order'), NULL, TRUE); |
6a488035 TO |
236 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); |
237 | ||
238 | // is active ? | |
239 | $this->add('checkbox', 'is_active', ts('Active?')); | |
240 | ||
2db35bf6 AF |
241 | // is public? |
242 | $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility()); | |
243 | ||
6a488035 TO |
244 | //is default |
245 | $this->add('checkbox', 'is_default', ts('Default')); | |
246 | ||
247 | if ($this->_fid) { | |
248 | //hide the default checkbox option for text field | |
9da8dc8c | 249 | $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type'); |
6a488035 TO |
250 | $this->assign('hideDefaultOption', FALSE); |
251 | if ($htmlType == 'Text') { | |
252 | $this->assign('hideDefaultOption', TRUE); | |
253 | } | |
254 | } | |
255 | // add buttons | |
be2fb01f CW |
256 | $this->addButtons([ |
257 | [ | |
c5c263ca AH |
258 | 'type' => 'next', |
259 | 'name' => ts('Save'), | |
be2fb01f CW |
260 | ], |
261 | [ | |
c5c263ca AH |
262 | 'type' => 'cancel', |
263 | 'name' => ts('Cancel'), | |
be2fb01f CW |
264 | ], |
265 | ]); | |
6a488035 TO |
266 | |
267 | // if view mode pls freeze it with the done button. | |
268 | if ($this->_action & CRM_Core_Action::VIEW) { | |
269 | $this->freeze(); | |
be2fb01f CW |
270 | $this->addButtons([ |
271 | [ | |
c5c263ca AH |
272 | 'type' => 'cancel', |
273 | 'name' => ts('Done'), | |
274 | 'isDefault' => TRUE, | |
be2fb01f CW |
275 | ], |
276 | ]); | |
6a488035 TO |
277 | } |
278 | } | |
279 | ||
be2fb01f | 280 | $this->addFormRule(['CRM_Price_Form_Option', 'formRule'], $this); |
6a488035 TO |
281 | } |
282 | ||
283 | /** | |
fe482240 | 284 | * Global validation rules for the form. |
6a488035 | 285 | * |
414c1420 TO |
286 | * @param array $fields |
287 | * Posted values of the form. | |
77b97be7 EM |
288 | * |
289 | * @param $files | |
c490a46a | 290 | * @param CRM_Core_Form $form |
6a488035 | 291 | * |
a6c01b45 CW |
292 | * @return array |
293 | * if errors then list of errors to be posted back to the form, | |
6a488035 | 294 | * true otherwise |
6a488035 | 295 | */ |
00be9182 | 296 | public static function formRule($fields, $files, $form) { |
be2fb01f | 297 | $errors = []; |
8cc574cf | 298 | if (!empty($fields['count']) && !empty($fields['max_value']) && |
6a488035 TO |
299 | $fields['count'] > $fields['max_value'] |
300 | ) { | |
301 | $errors['count'] = ts('Participant count can not be greater than max participants.'); | |
302 | } | |
a13c171d CR |
303 | |
304 | $priceField = CRM_Price_BAO_PriceField::findById($fields['fieldId']); | |
be2fb01f | 305 | $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, ['labelColumn' => 'name']); |
a13c171d CR |
306 | |
307 | $publicCount = 0; | |
308 | $options = CRM_Price_BAO_PriceField::getOptions($priceField->id); | |
309 | foreach ($options as $currentOption) { | |
310 | if ($fields['optionId'] == $currentOption['id'] && $visibilityOptions[$fields['visibility_id']] == 'public') { | |
311 | $publicCount++; | |
312 | } | |
313 | elseif ($fields['optionId'] != $currentOption['id'] && $visibilityOptions[$currentOption['visibility_id']] == 'public') { | |
314 | $publicCount++; | |
315 | } | |
316 | } | |
c8ac8c5b | 317 | if ($visibilityOptions[$priceField->visibility_id] == 'public' && $publicCount == 0 && $visibilityOptions[$fields['visibility_id']] == 'admin') { |
a13c171d CR |
318 | $errors['visibility_id'] = ts('All other options for this \'Public\' field have \'Admin\' visibility. There should at least be one \'Public\' option, or make the field \'Admin\' only.'); |
319 | } | |
c8ac8c5b | 320 | elseif ($visibilityOptions[$priceField->visibility_id] == 'admin' && $visibilityOptions[$fields['visibility_id']] == 'public') { |
a13c171d CR |
321 | $errors['visibility_id'] = ts('You must choose \'Admin\' visibility for this price option, as it belongs to a field with \'Admin\' visibility.'); |
322 | } | |
323 | ||
6a488035 TO |
324 | return empty($errors) ? TRUE : $errors; |
325 | } | |
326 | ||
327 | /** | |
fe482240 | 328 | * Process the form. |
6a488035 | 329 | * |
6a488035 | 330 | * @return void |
6a488035 TO |
331 | */ |
332 | public function postProcess() { | |
333 | if ($this->_action == CRM_Core_Action::DELETE) { | |
be2fb01f | 334 | $fieldValues = ['price_field_id' => $this->_fid]; |
353ffa53 TO |
335 | $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues); |
336 | $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', | |
6a488035 TO |
337 | $this->_oid, |
338 | 'label', 'id' | |
339 | ); | |
340 | ||
9da8dc8c | 341 | if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) { |
be2fb01f | 342 | CRM_Core_Session::setStatus(ts('%1 option has been deleted.', [1 => $label]), ts('Record Deleted'), 'success'); |
6a488035 | 343 | } |
bed98343 | 344 | return NULL; |
6a488035 TO |
345 | } |
346 | else { | |
be2fb01f | 347 | $params = $ids = []; |
353ffa53 | 348 | $params = $this->controller->exportValues('Option'); |
9da8dc8c | 349 | $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label'); |
6a488035 | 350 | |
062913c2 SL |
351 | foreach ($this->_moneyFields as $field) { |
352 | $params[$field] = CRM_Utils_Rule::cleanMoney(trim($params[$field])); | |
353 | } | |
6a488035 TO |
354 | $params['price_field_id'] = $this->_fid; |
355 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
ff33ddc8 | 356 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
2db35bf6 | 357 | $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE); |
be2fb01f | 358 | $ids = []; |
6a488035 | 359 | if ($this->_oid) { |
72b84ded | 360 | $params['id'] = $this->_oid; |
6a488035 | 361 | } |
9da8dc8c | 362 | $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids); |
6a488035 | 363 | |
be2fb01f | 364 | CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", [1 => $params['label']]), ts('Value Saved'), 'success'); |
6a488035 TO |
365 | } |
366 | } | |
96025800 | 367 | |
6a488035 | 368 | } |