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