3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * form to process actions on the field aspect of Custom
39 class CRM_Price_Form_Option
extends CRM_Core_Form
{
42 * the price field id saved to the session for an update
50 * option value id, used when editing the Option
58 * Function to set variables up before form is built
65 public function preProcess() {
67 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
70 $this->_oid
= CRM_Utils_Request
::retrieve('oid', 'Positive',
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
81 * @return array array of default values
84 function setDefaultValues() {
85 if ($this->_action
== CRM_Core_Action
::DELETE
) {
90 if (isset($this->_oid
)) {
91 $params = array('id' => $this->_oid
);
93 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $defaults);
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');
99 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
100 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
102 if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
103 $defaults['membership_num_terms'] = 1;
105 // set financial type used for price set to set default for new option
107 $defaults['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id', 'id');;
109 if (!isset($defaults['weight']) ||
!$defaults['weight']) {
110 $fieldValues = array('price_field_id' => $this->_fid
);
111 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
112 $defaults['is_active'] = 1;
119 * Function to actually build the form
126 public function buildQuickForm() {
127 if ($this->_action
== CRM_Core_Action
::DELETE
) {
128 $this->addButtons(array(
131 'name' => ts('Delete'),
135 'name' => ts('Cancel'),
142 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
143 // lets trim all the whitespace
144 $this->applyFilter('__ALL__', 'trim');
146 // hidden Option Id for validation use
147 $this->add('hidden', 'optionId', $this->_oid
);
149 //hidden field ID for validation use
150 $this->add('hidden', 'fieldId', $this->_fid
);
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);
158 elseif ($this->_action
== CRM_Core_Action
::ADD ||
159 $this->_action
== CRM_Core_Action
::VIEW
161 $this->_sid
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'price_set_id', 'id');
163 $this->isEvent
= False;
164 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
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( );")
173 $this->add('text', 'membership_num_terms', ts('Number of terms'), $attributes['membership_num_terms']);
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;
181 $this->add('text', 'count', ts('Participants Count'));
182 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
184 $this->add('text', 'max_value', ts('Max Participants'));
185 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
190 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
192 if (count($financialType)) {
193 $this->assign('financialType', $financialType);
198 ts('Financial Type'),
199 array('' => ts('- select -')) +
$financialType,
203 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
204 // FIX ME: duplicate rule?
206 $this->addRule( 'label',
207 ts('Duplicate option label.'),
209 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
214 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
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');
221 $this->add('textarea', 'description', ts('Description'));
224 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
225 $this->addRule('weight', ts('is a numeric field'), 'numeric');
228 $this->add('checkbox', 'is_active', ts('Active?'));
231 $this->add('checkbox', 'is_default', ts('Default'));
234 //hide the default checkbox option for text field
235 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid
, 'html_type');
236 $this->assign('hideDefaultOption', FALSE);
237 if ($htmlType == 'Text') {
238 $this->assign('hideDefaultOption', TRUE);
242 $this->addButtons(array(
245 'name' => ts('Save'),
249 'name' => ts('Cancel'),
254 // if view mode pls freeze it with the done button.
255 if ($this->_action
& CRM_Core_Action
::VIEW
) {
257 $this->addButtons(array(
260 'name' => ts('Done'),
268 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
272 * global validation rules for the form
274 * @param array $fields (referance) posted values of the form
279 * @return array if errors then list of errors to be posted back to the form,
284 static function formRule($fields, $files, $form) {
286 if (!empty($fields['count']) && !empty($fields['max_value']) &&
287 $fields['count'] > $fields['max_value']
289 $errors['count'] = ts('Participant count can not be greater than max participants.');
292 return empty($errors) ?
TRUE : $errors;
303 public function postProcess() {
304 if ($this->_action
== CRM_Core_Action
::DELETE
) {
305 $fieldValues = array('price_field_id' => $this->_fid
);
306 $wt = CRM_Utils_Weight
::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid
, $fieldValues);
307 $label = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue',
312 if (CRM_Price_BAO_PriceFieldValue
::del($this->_oid
)) {
313 CRM_Core_Session
::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
318 $params = $ids = array();
319 $params = $this->controller
->exportValues('Option');
320 $fieldLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'label');
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);
327 $ids['id'] = $this->_oid
;
329 $optionValue = CRM_Price_BAO_PriceFieldValue
::create($params, $ids);
331 CRM_Core_Session
::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');