Merge pull request #4925 from KarinG/patch-1
[civicrm-core.git] / CRM / Price / Form / Option.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * Set variables up before form is built
57 *
58 * @param null
59 *
60 * @return void
61 */
62 public function preProcess() {
63 $this->setPageTitle(ts('Price Option'));
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 /**
73 * Set default values for the form. Note that in edit/view mode
74 * the default values are retrieved from the database
75 *
76 * @param null
77 *
78 * @return array
79 * array of default values
80 */
81 public function setDefaultValues() {
82 if ($this->_action == CRM_Core_Action::DELETE) {
83 return;
84 }
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 * Build the form object
117 *
118 * @param null
119 *
120 * @return void
121 */
122 public function buildQuickForm() {
123 if ($this->_action == CRM_Core_Action::DELETE) {
124 $this->addButtons(array(
125 array(
126 'type' => 'next',
127 'name' => ts('Delete'),
128 ),
129 array(
130 'type' => 'cancel',
131 'name' => ts('Cancel'),
132 ),
133 )
134 );
135 return;
136 }
137 else {
138 $attributes = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceFieldValue');
139 // lets trim all the whitespace
140 $this->applyFilter('__ALL__', 'trim');
141
142 // hidden Option Id for validation use
143 $this->add('hidden', 'optionId', $this->_oid);
144
145 // Needed for i18n dialog
146 $this->assign('optionId', $this->_oid);
147
148 //hidden field ID for validation use
149 $this->add('hidden', 'fieldId', $this->_fid);
150
151 // label
152 $this->add('text', 'label', ts('Option Label'), NULL, TRUE);
153 $memberComponentId = CRM_Core_Component::getComponentID('CiviMember');
154 if ($this->_action == CRM_Core_Action::UPDATE) {
155 $this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
156 }
157 elseif ($this->_action == CRM_Core_Action::ADD ||
158 $this->_action == CRM_Core_Action::VIEW
159 ) {
160 $this->_sid = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'price_set_id', 'id');
161 }
162 $this->isEvent = FALSE;
163 $extendComponentId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid, 'extends', 'id');
164 $this->assign('showMember', FALSE);
165 if ($memberComponentId == $extendComponentId) {
166 $this->assign('showMember', TRUE);
167 $membershipTypes = CRM_Member_PseudoConstant::membershipType();
168 $this->add('select', 'membership_type_id', ts('Membership Type'), array(
169 '' => ' '
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('Participant Count'));
182 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
183
184 $this->add('text', 'max_value', ts('Max Participants'));
185 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
186 }
187
188 }
189 //Financial Type
190 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
191
192 if (count($financialType)) {
193 $this->assign('financialType', $financialType);
194 }
195 $this->add(
196 'select',
197 'financial_type_id',
198 ts('Financial Type'),
199 array('' => ts('- select -')) + $financialType,
200 TRUE
201 );
202
203 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
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 // value
213 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
214
215 // the above value is used directly by QF, so the value has to be have a rule
216 // please check with Lobo before u comment this
217 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
218 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
219
220 $this->add('textarea', 'description', ts('Description'));
221
222 // weight
223 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
224 $this->addRule('weight', ts('is a numeric field'), 'numeric');
225
226 // is active ?
227 $this->add('checkbox', 'is_active', ts('Active?'));
228
229 //is default
230 $this->add('checkbox', 'is_default', ts('Default'));
231
232 if ($this->_fid) {
233 //hide the default checkbox option for text field
234 $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid, 'html_type');
235 $this->assign('hideDefaultOption', FALSE);
236 if ($htmlType == 'Text') {
237 $this->assign('hideDefaultOption', TRUE);
238 }
239 }
240 // add buttons
241 $this->addButtons(array(
242 array(
243 'type' => 'next',
244 'name' => ts('Save'),
245 ),
246 array(
247 'type' => 'cancel',
248 'name' => ts('Cancel'),
249 ),
250 )
251 );
252
253 // if view mode pls freeze it with the done button.
254 if ($this->_action & CRM_Core_Action::VIEW) {
255 $this->freeze();
256 $this->addButtons(array(
257 array(
258 'type' => 'cancel',
259 'name' => ts('Done'),
260 'isDefault' => TRUE,
261 ),
262 )
263 );
264 }
265 }
266
267 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
268 }
269
270 /**
271 * Global validation rules for the form
272 *
273 * @param array $fields
274 * Posted values of the form.
275 *
276 * @param $files
277 * @param CRM_Core_Form $form
278 *
279 * @return array
280 * if errors then list of errors to be posted back to the form,
281 * true otherwise
282 */
283 public static function formRule($fields, $files, $form) {
284 $errors = array();
285 if (!empty($fields['count']) && !empty($fields['max_value']) &&
286 $fields['count'] > $fields['max_value']
287 ) {
288 $errors['count'] = ts('Participant count can not be greater than max participants.');
289 }
290
291 return empty($errors) ? TRUE : $errors;
292 }
293
294 /**
295 * Process the form
296 *
297 * @param null
298 *
299 * @return void
300 */
301 public function postProcess() {
302 if ($this->_action == CRM_Core_Action::DELETE) {
303 $fieldValues = array('price_field_id' => $this->_fid);
304 $wt = CRM_Utils_Weight::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid, $fieldValues);
305 $label = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue',
306 $this->_oid,
307 'label', 'id'
308 );
309
310 if (CRM_Price_BAO_PriceFieldValue::del($this->_oid)) {
311 CRM_Core_Session::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
312 }
313 return;
314 }
315 else {
316 $params = $ids = array();
317 $params = $this->controller->exportValues('Option');
318 $fieldLabel = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'label');
319
320 $params['amount'] = CRM_Utils_Rule::cleanMoney(trim($params['amount']));
321 $params['price_field_id'] = $this->_fid;
322 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
323 $ids = array();
324 if ($this->_oid) {
325 $ids['id'] = $this->_oid;
326 }
327 $optionValue = CRM_Price_BAO_PriceFieldValue::create($params, $ids);
328
329 CRM_Core_Session::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');
330 }
331 }
332 }