Merge pull request #13350 from francescbassas/patch-16
[civicrm-core.git] / CRM / Price / Form / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
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 /**
42 * The price field id saved to the session for an update.
43 *
44 * @var int
45 */
46 protected $_fid;
47
48 /**
49 * Option value id, used when editing the Option
50 *
51 * @var int
52 */
53 protected $_oid;
54
55 /**
56 * Array of Money fields
57 *
58 * @var array
59 */
60 protected $_moneyFields = ['amount', 'non_deductible_amount'];
61
62 /**
63 * Set variables up before form is built.
64 *
65 * @return void
66 */
67 public function preProcess() {
68 $this->setPageTitle(ts('Price Option'));
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 /**
78 * Set default values for the form. Note that in edit/view mode
79 * the default values are retrieved from the database
80 *
81 * @return array|void array of default values
82 */
83 public function setDefaultValues() {
84 if ($this->_action == CRM_Core_Action::DELETE) {
85 return NULL;
86 }
87 $defaults = array();
88
89 if (isset($this->_oid)) {
90 $params = array('id' => $this->_oid);
91
92 CRM_Price_BAO_PriceFieldValue::retrieve($params, $defaults);
93
94 // fix the display of the monetary value, CRM-4038
95 foreach ($this->_moneyFields as $field) {
96 $defaults[$field] = CRM_Utils_Money::format(CRM_Utils_Array::value($field, $defaults), NULL, '%a');
97 }
98 }
99
100 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
101 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
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) {
108 $defaults['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'financial_type_id', 'id');;
109 }
110 if (!isset($defaults['weight']) || !$defaults['weight']) {
111 $fieldValues = array('price_field_id' => $this->_fid);
112 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
113 $defaults['is_active'] = 1;
114 }
115
116 return $defaults;
117 }
118
119 /**
120 * Build the form object.
121 *
122 * @return void
123 */
124 public function buildQuickForm() {
125 if ($this->_action == CRM_Core_Action::UPDATE) {
126 $finTypeId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid, 'financial_type_id');
127 if (!CRM_Financial_BAO_FinancialType::checkPermissionToEditFinancialType($finTypeId)) {
128 CRM_Core_Error::fatal(ts("You do not have permission to access this page"));
129 }
130 }
131 if ($this->_action == CRM_Core_Action::DELETE) {
132 $this->addButtons(array(
133 array(
134 'type' => 'next',
135 'name' => ts('Delete'),
136 ),
137 array(
138 'type' => 'cancel',
139 'name' => ts('Cancel'),
140 ),
141 ));
142 return NULL;
143 }
144 else {
145 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
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
152 // Needed for i18n dialog
153 $this->assign('optionId', $this->_oid);
154
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 ) {
167 $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
168 }
169 $this->isEvent = FALSE;
170 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
171 $this->assign('showMember', FALSE);
172 if ($memberComponentId == $extendComponentId) {
173 $this->assign('showMember', TRUE);
174 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
175 $this->add('select', 'membership_type_id', ts('Membership Type'), array(
176 '' => ' ',
177 ) + $membershipTypes, FALSE,
178 array('onClick' => "calculateRowValues( );"));
179 $this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
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
187 $this->add('text', 'count', ts('Participant Count'));
188 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
189
190 $this->add('text', 'max_value', ts('Max Participants'));
191 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
192 }
193
194 }
195 //Financial Type
196 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
197
198 if (count($financialType)) {
199 $this->assign('financialType', $financialType);
200 }
201 $this->add(
202 'select',
203 'financial_type_id',
204 ts('Financial Type'),
205 array('' => ts('- select -')) + $financialType,
206 TRUE
207 );
208
209 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
210 // FIX ME: duplicate rule?
211 /*
212 $this->addRule( 'label',
213 ts('Duplicate option label.'),
214 'optionExists',
215 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
216 */
217
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
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
230 $this->add('textarea', 'description', ts('Description'));
231 $this->add('textarea', 'help_pre', ts('Pre Option Help'));
232 $this->add('textarea', 'help_post', ts('Post Option Help'));
233
234 // weight
235 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
236 $this->addRule('weight', ts('is a numeric field'), 'numeric');
237
238 // is active ?
239 $this->add('checkbox', 'is_active', ts('Active?'));
240
241 // is public?
242 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant::visibility());
243
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
249 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
250 $this->assign('hideDefaultOption', FALSE);
251 if ($htmlType == 'Text') {
252 $this->assign('hideDefaultOption', TRUE);
253 }
254 }
255 // add buttons
256 $this->addButtons(array(
257 array(
258 'type' => 'next',
259 'name' => ts('Save'),
260 ),
261 array(
262 'type' => 'cancel',
263 'name' => ts('Cancel'),
264 ),
265 ));
266
267 // if view mode pls freeze it with the done button.
268 if ($this->_action & CRM_Core_Action::VIEW) {
269 $this->freeze();
270 $this->addButtons(array(
271 array(
272 'type' => 'cancel',
273 'name' => ts('Done'),
274 'isDefault' => TRUE,
275 ),
276 ));
277 }
278 }
279
280 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
281 }
282
283 /**
284 * Global validation rules for the form.
285 *
286 * @param array $fields
287 * Posted values of the form.
288 *
289 * @param $files
290 * @param CRM_Core_Form $form
291 *
292 * @return array
293 * if errors then list of errors to be posted back to the form,
294 * true otherwise
295 */
296 public static function formRule($fields, $files, $form) {
297 $errors = array();
298 if (!empty($fields['count']) && !empty($fields['max_value']) &&
299 $fields['count'] > $fields['max_value']
300 ) {
301 $errors['count'] = ts('Participant count can not be greater than max participants.');
302 }
303
304 $priceField = CRM_Price_BAO_PriceField::findById($fields['fieldId']);
305 $visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array('labelColumn' => 'name'));
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 }
317 if ($visibilityOptions[$priceField->visibility_id] == 'public' && $publicCount == 0) {
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 }
320 elseif ($visibilityOptions[$priceField->visibility_id] == 'admin' && $publicCount > 0) {
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
324 return empty($errors) ? TRUE : $errors;
325 }
326
327 /**
328 * Process the form.
329 *
330 * @return void
331 */
332 public function postProcess() {
333 if ($this->_action == CRM_Core_Action::DELETE) {
334 $fieldValues = array('price_field_id' => $this->_fid);
335 $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
336 $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
337 $this->_oid,
338 'label', 'id'
339 );
340
341 if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
342 CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
343 }
344 return NULL;
345 }
346 else {
347 $params = $ids = array();
348 $params = $this->controller->exportValues('Option');
349 $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
350
351 foreach ($this->_moneyFields as $field) {
352 $params[$field] = CRM_Utils_Rule::cleanMoney(trim($params[$field]));
353 }
354 $params['price_field_id'] = $this->_fid;
355 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
356 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
357 $params['visibility_id'] = CRM_Utils_Array::value('visibility_id', $params, FALSE);
358 $ids = array();
359 if ($this->_oid) {
360 $ids['id'] = $this->_oid;
361 }
362 $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
363
364 CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');
365 }
366 }
367
368 }