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