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