Merge pull request #4227 from rohankatkar/CRM_15120
[civicrm-core.git] / CRM / Price / Form / Option.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 /**
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() {
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 /**
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() {
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 /**
119 * Function to actually build the form
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
149 //hidden field ID for validation use
150 $this->add('hidden', 'fieldId', $this->_fid);
151
152 // label
153 $this->add('text', 'label', ts('Option Label'), NULL, TRUE);
154 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
155 if ($this->_action == CRM_Core_Action::UPDATE) {
156 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
157 }
158 elseif ($this->_action == CRM_Core_Action::ADD ||
159 $this->_action == CRM_Core_Action::VIEW
160 ) {
9da8dc8c 161 $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
6a488035
TO
162 }
163 $this->isEvent = False;
9da8dc8c 164 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
6a488035
TO
165 $this->assign('showMember', FALSE);
166 if ($memberComponentId == $extendComponentId) {
167 $this->assign('showMember', TRUE);
168 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
169 $this->add('select', 'membership_type_id', ts('Membership Type'), array(
170 '' => ' ') + $membershipTypes, FALSE,
171 array('onClick' => "calculateRowValues( );")
172 );
173 $this->add('text', 'membership_num_terms', ts('Number of terms'), $attributes['membership_num_terms']);
174 }
175 else {
176 $allComponents = explode(CRM_Core_DAO::VALUE_SEPARATOR, $extendComponentId);
177 $eventComponentId = CRM_Core_Component::getComponentID('CiviEvent');
178 if (in_array($eventComponentId, $allComponents)) {
179 $this->isEvent = TRUE;
180 // count
181 $this->add('text', 'count', ts('Participants Count'));
182 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
366fe2a3 183
6a488035
TO
184 $this->add('text', 'max_value', ts('Max Participants'));
185 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
186 }
366fe2a3 187
6a488035
TO
188 }
189 //Financial Type
190 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
366fe2a3 191
6a488035
TO
192 if (count($financialType)) {
193 $this->assign('financialType', $financialType);
194 }
195 $this->add(
366fe2a3 196 'select',
197 'financial_type_id',
198 ts('Financial Type'),
6a488035
TO
199 array('' => ts('- select -')) + $financialType,
200 true
201 );
366fe2a3 202
9da8dc8c 203 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
6a488035
TO
204 // FIX ME: duplicate rule?
205 /*
206 $this->addRule( 'label',
207 ts('Duplicate option label.'),
208 'optionExists',
209 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
210 */
211
212
213 // value
214 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
215
216 // the above value is used directly by QF, so the value has to be have a rule
217 // please check with Lobo before u comment this
218 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
219 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
220
221 $this->add('textarea', 'description', ts('Description'));
222
223 // weight
224 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
225 $this->addRule('weight', ts('is a numeric field'), 'numeric');
226
227 // is active ?
228 $this->add('checkbox', 'is_active', ts('Active?'));
229
230 //is default
231 $this->add('checkbox', 'is_default', ts('Default'));
232
233 if ($this->_fid) {
234 //hide the default checkbox option for text field
9da8dc8c 235 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
6a488035
TO
236 $this->assign('hideDefaultOption', FALSE);
237 if ($htmlType == 'Text') {
238 $this->assign('hideDefaultOption', TRUE);
239 }
240 }
241 // add buttons
242 $this->addButtons(array(
243 array(
244 'type' => 'next',
245 'name' => ts('Save'),
246 ),
247 array(
248 'type' => 'cancel',
249 'name' => ts('Cancel'),
250 ),
251 )
252 );
253
254 // if view mode pls freeze it with the done button.
255 if ($this->_action & CRM_Core_Action::VIEW) {
256 $this->freeze();
257 $this->addButtons(array(
258 array(
259 'type' => 'cancel',
260 'name' => ts('Done'),
261 'isDefault' => TRUE,
262 ),
263 )
264 );
265 }
266 }
267
268 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
269 }
270
271 /**
272 * global validation rules for the form
273 *
c206647d 274 * @param array $fields posted values of the form
77b97be7
EM
275 *
276 * @param $files
277 * @param $form
6a488035
TO
278 *
279 * @return array if errors then list of errors to be posted back to the form,
280 * true otherwise
281 * @static
282 * @access public
283 */
284 static function formRule($fields, $files, $form) {
285 $errors = array();
8cc574cf 286 if (!empty($fields['count']) && !empty($fields['max_value']) &&
6a488035
TO
287 $fields['count'] > $fields['max_value']
288 ) {
289 $errors['count'] = ts('Participant count can not be greater than max participants.');
290 }
291
292 return empty($errors) ? TRUE : $errors;
293 }
294
295 /**
296 * Process the form
297 *
298 * @param null
299 *
300 * @return void
301 * @access public
302 */
303 public function postProcess() {
304 if ($this->_action == CRM_Core_Action::DELETE) {
305 $fieldValues = array('price_field_id' => $this->_fid);
9da8dc8c 306 $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
307 $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
6a488035
TO
308 $this->_oid,
309 'label', 'id'
310 );
311
9da8dc8c 312 if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
6a488035
TO
313 CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
314 }
315 return;
316 }
317 else {
318 $params = $ids = array();
319 $params = $this->controller->exportValues('Option');
9da8dc8c 320 $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
6a488035
TO
321
322 $params['amount'] = CRM_Utils_Rule::cleanMoney(trim($params['amount']));
323 $params['price_field_id'] = $this->_fid;
324 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
325 $ids = array();
326 if ($this->_oid) {
327 $ids['id'] = $this->_oid;
328 }
9da8dc8c 329 $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
6a488035
TO
330
331 CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');
332 }
333 }
334}
335