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