3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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.
49 * Option value id, used when editing the Option
56 * Set variables up before form is built.
60 public function preProcess() {
61 $this->setPageTitle(ts('Price Option'));
62 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
65 $this->_oid
= CRM_Utils_Request
::retrieve('oid', 'Positive',
71 * Set default values for the form. Note that in edit/view mode
72 * the default values are retrieved from the database
74 * @return array|void array of default values
76 public function setDefaultValues() {
77 if ($this->_action
== CRM_Core_Action
::DELETE
) {
82 if (isset($this->_oid
)) {
83 $params = array('id' => $this->_oid
);
85 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $defaults);
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');
91 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
92 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
94 if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
95 $defaults['membership_num_terms'] = 1;
97 // set financial type used for price set to set default for new option
99 $defaults['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id', 'id');;
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;
111 * Build the form object.
115 public function buildQuickForm() {
116 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
117 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid
, 'financial_type_id');
118 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::UPDATE
);
119 if (!array_key_exists($finTypeId, $financialTypes)) {
120 CRM_Core_Error
::fatal(ts("You do not have permission to access this page"));
123 if ($this->_action
== CRM_Core_Action
::DELETE
) {
124 $this->addButtons(array(
127 'name' => ts('Delete'),
131 'name' => ts('Cancel'),
137 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
138 // lets trim all the whitespace
139 $this->applyFilter('__ALL__', 'trim');
141 // hidden Option Id for validation use
142 $this->add('hidden', 'optionId', $this->_oid
);
144 // Needed for i18n dialog
145 $this->assign('optionId', $this->_oid
);
147 //hidden field ID for validation use
148 $this->add('hidden', 'fieldId', $this->_fid
);
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);
156 elseif ($this->_action
== CRM_Core_Action
::ADD ||
157 $this->_action
== CRM_Core_Action
::VIEW
159 $this->_sid
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'price_set_id', 'id');
161 $this->isEvent
= FALSE;
162 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
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(
169 ) +
$membershipTypes, FALSE,
170 array('onClick' => "calculateRowValues( );"));
171 $this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
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;
179 $this->add('text', 'count', ts('Participant Count'));
180 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
182 $this->add('text', 'max_value', ts('Max Participants'));
183 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
188 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
190 if (count($financialType)) {
191 $this->assign('financialType', $financialType);
196 ts('Financial Type'),
197 array('' => ts('- select -')) +
$financialType,
201 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
202 // FIX ME: duplicate rule?
204 $this->addRule( 'label',
205 ts('Duplicate option label.'),
207 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
211 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
213 // the above value is used directly by QF, so the value has to be have a rule
214 // please check with Lobo before u comment this
215 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
216 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
218 $this->add('textarea', 'description', ts('Description'));
219 $this->add('textarea', 'help_pre', ts('Pre Option Help'));
220 $this->add('textarea', 'help_post', ts('Post Option Help'));
223 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
224 $this->addRule('weight', ts('is a numeric field'), 'numeric');
227 $this->add('checkbox', 'is_active', ts('Active?'));
230 $this->add('checkbox', 'is_default', ts('Default'));
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);
241 $this->addButtons(array(
244 'name' => ts('Save'),
248 'name' => ts('Cancel'),
252 // if view mode pls freeze it with the done button.
253 if ($this->_action
& CRM_Core_Action
::VIEW
) {
255 $this->addButtons(array(
258 'name' => ts('Done'),
265 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
269 * Global validation rules for the form.
271 * @param array $fields
272 * Posted values of the form.
275 * @param CRM_Core_Form $form
278 * if errors then list of errors to be posted back to the form,
281 public static function formRule($fields, $files, $form) {
283 if (!empty($fields['count']) && !empty($fields['max_value']) &&
284 $fields['count'] > $fields['max_value']
286 $errors['count'] = ts('Participant count can not be greater than max participants.');
290 CRM_Financial_BAO_FinancialAccount
::validateFinancialType($fields['financial_type_id'], $form->_fid
, 'PriceField');
292 catch (CRM_Core_Exception
$e) {
293 $errors['financial_type_id'] = $e->getMessage();
295 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);
325 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
328 $ids['id'] = $this->_oid
;
330 $optionValue = CRM_Price_BAO_PriceFieldValue
::create($params, $ids);
332 CRM_Core_Session
::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');