3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 * Array of Money fields
60 protected $_moneyFields = ['amount', 'non_deductible_amount'];
63 * Set variables up before form is built.
67 public function preProcess() {
68 $this->setPageTitle(ts('Price Option'));
69 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive',
72 $this->_oid
= CRM_Utils_Request
::retrieve('oid', 'Positive',
78 * Set default values for the form. Note that in edit/view mode
79 * the default values are retrieved from the database
81 * @return array|void array of default values
83 public function setDefaultValues() {
84 if ($this->_action
== CRM_Core_Action
::DELETE
) {
89 if (isset($this->_oid
)) {
90 $params = array('id' => $this->_oid
);
92 CRM_Price_BAO_PriceFieldValue
::retrieve($params, $defaults);
94 // fix the display of the monetary value, CRM-4038
95 foreach ($this->_moneyFields
as $field) {
96 $defaults[$field] = CRM_Utils_Money
::format(CRM_Utils_Array
::value($field, $defaults), NULL, '%a');
100 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
101 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
103 if (!isset($defaults['membership_num_terms']) && $memberComponentId == $extendComponentId) {
104 $defaults['membership_num_terms'] = 1;
106 // set financial type used for price set to set default for new option
108 $defaults['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id', 'id');;
110 if (!isset($defaults['weight']) ||
!$defaults['weight']) {
111 $fieldValues = array('price_field_id' => $this->_fid
);
112 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Price_DAO_PriceFieldValue', $fieldValues);
113 $defaults['is_active'] = 1;
120 * Build the form object.
124 public function buildQuickForm() {
125 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
126 $finTypeId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_oid
, 'financial_type_id');
127 if (!CRM_Financial_BAO_FinancialType
::checkPermissionToEditFinancialType($finTypeId)) {
128 CRM_Core_Error
::fatal(ts("You do not have permission to access this page"));
131 if ($this->_action
== CRM_Core_Action
::DELETE
) {
132 $this->addButtons(array(
135 'name' => ts('Delete'),
139 'name' => ts('Cancel'),
145 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
146 // lets trim all the whitespace
147 $this->applyFilter('__ALL__', 'trim');
149 // hidden Option Id for validation use
150 $this->add('hidden', 'optionId', $this->_oid
);
152 // Needed for i18n dialog
153 $this->assign('optionId', $this->_oid
);
155 //hidden field ID for validation use
156 $this->add('hidden', 'fieldId', $this->_fid
);
159 $this->add('text', 'label', ts('Option Label'), NULL, TRUE);
160 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
161 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
162 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this);
164 elseif ($this->_action
== CRM_Core_Action
::ADD ||
165 $this->_action
== CRM_Core_Action
::VIEW
167 $this->_sid
= CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'price_set_id', 'id');
169 $this->isEvent
= FALSE;
170 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
171 $this->assign('showMember', FALSE);
172 if ($memberComponentId == $extendComponentId) {
173 $this->assign('showMember', TRUE);
174 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
175 $this->add('select', 'membership_type_id', ts('Membership Type'), array(
177 ) +
$membershipTypes, FALSE,
178 array('onClick' => "calculateRowValues( );"));
179 $this->add('text', 'membership_num_terms', ts('Number of Terms'), $attributes['membership_num_terms']);
182 $allComponents = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
183 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
184 if (in_array($eventComponentId, $allComponents)) {
185 $this->isEvent
= TRUE;
187 $this->add('text', 'count', ts('Participant Count'));
188 $this->addRule('count', ts('Please enter a valid Max Participants.'), 'positiveInteger');
190 $this->add('text', 'max_value', ts('Max Participants'));
191 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
196 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
198 if (count($financialType)) {
199 $this->assign('financialType', $financialType);
204 ts('Financial Type'),
205 array('' => ts('- select -')) +
$financialType,
209 //CRM_Core_DAO::getFieldValue( 'CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id' );
210 // FIX ME: duplicate rule?
212 $this->addRule( 'label',
213 ts('Duplicate option label.'),
215 array( 'CRM_Core_DAO_OptionValue', $this->_oid, $this->_ogId, 'label' ) );
219 $this->add('text', 'amount', ts('Option Amount'), NULL, TRUE);
221 // the above value is used directly by QF, so the value has to be have a rule
222 // please check with Lobo before u comment this
223 $this->registerRule('amount', 'callback', 'money', 'CRM_Utils_Rule');
224 $this->addRule('amount', ts('Please enter a monetary value for this field.'), 'money');
226 $this->add('text', 'non_deductible_amount', ts('Non-deductible Amount'), NULL);
227 $this->registerRule('non_deductible_amount', 'callback', 'money', 'CRM_Utils_Rule');
228 $this->addRule('non_deductible_amount', ts('Please enter a monetary value for this field.'), 'money');
230 $this->add('textarea', 'description', ts('Description'));
231 $this->add('textarea', 'help_pre', ts('Pre Option Help'));
232 $this->add('textarea', 'help_post', ts('Post Option Help'));
235 $this->add('text', 'weight', ts('Order'), NULL, TRUE);
236 $this->addRule('weight', ts('is a numeric field'), 'numeric');
239 $this->add('checkbox', 'is_active', ts('Active?'));
242 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
245 $this->add('checkbox', 'is_default', ts('Default'));
248 //hide the default checkbox option for text field
249 $htmlType = CRM_Core_DAO
::getFieldValue('CRM_Price_BAO_PriceField', $this->_fid
, 'html_type');
250 $this->assign('hideDefaultOption', FALSE);
251 if ($htmlType == 'Text') {
252 $this->assign('hideDefaultOption', TRUE);
256 $this->addButtons(array(
259 'name' => ts('Save'),
263 'name' => ts('Cancel'),
267 // if view mode pls freeze it with the done button.
268 if ($this->_action
& CRM_Core_Action
::VIEW
) {
270 $this->addButtons(array(
273 'name' => ts('Done'),
280 $this->addFormRule(array('CRM_Price_Form_Option', 'formRule'), $this);
284 * Global validation rules for the form.
286 * @param array $fields
287 * Posted values of the form.
290 * @param CRM_Core_Form $form
293 * if errors then list of errors to be posted back to the form,
296 public static function formRule($fields, $files, $form) {
298 if (!empty($fields['count']) && !empty($fields['max_value']) &&
299 $fields['count'] > $fields['max_value']
301 $errors['count'] = ts('Participant count can not be greater than max participants.');
304 $priceField = CRM_Price_BAO_PriceField
::findById($fields['fieldId']);
305 $visibilityOptions = CRM_Price_BAO_PriceFieldValue
::buildOptions('visibility_id', NULL, array('labelColumn' => 'name'));
308 $options = CRM_Price_BAO_PriceField
::getOptions($priceField->id
);
309 foreach ($options as $currentOption) {
310 if ($fields['optionId'] == $currentOption['id'] && $visibilityOptions[$fields['visibility_id']] == 'public') {
313 elseif ($fields['optionId'] != $currentOption['id'] && $visibilityOptions[$currentOption['visibility_id']] == 'public') {
317 if ($visibilityOptions[$priceField->visibility_id
] == 'public' && $publicCount == 0) {
318 $errors['visibility_id'] = ts('All other options for this \'Public\' field have \'Admin\' visibility. There should at least be one \'Public\' option, or make the field \'Admin\' only.');
320 elseif ($visibilityOptions[$priceField->visibility_id
] == 'admin' && $publicCount > 0) {
321 $errors['visibility_id'] = ts('You must choose \'Admin\' visibility for this price option, as it belongs to a field with \'Admin\' visibility.');
324 return empty($errors) ?
TRUE : $errors;
332 public function postProcess() {
333 if ($this->_action
== CRM_Core_Action
::DELETE
) {
334 $fieldValues = array('price_field_id' => $this->_fid
);
335 $wt = CRM_Utils_Weight
::delWeight('CRM_Price_DAO_PriceFieldValue', $this->_oid
, $fieldValues);
336 $label = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue',
341 if (CRM_Price_BAO_PriceFieldValue
::del($this->_oid
)) {
342 CRM_Core_Session
::setStatus(ts('%1 option has been deleted.', array(1 => $label)), ts('Record Deleted'), 'success');
347 $params = $ids = array();
348 $params = $this->controller
->exportValues('Option');
349 $fieldLabel = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'label');
351 foreach ($this->_moneyFields
as $field) {
352 $params[$field] = CRM_Utils_Rule
::cleanMoney(trim($params[$field]));
354 $params['price_field_id'] = $this->_fid
;
355 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, FALSE);
356 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
357 $params['visibility_id'] = CRM_Utils_Array
::value('visibility_id', $params, FALSE);
360 $ids['id'] = $this->_oid
;
362 $optionValue = CRM_Price_BAO_PriceFieldValue
::create($params, $ids);
364 CRM_Core_Session
::setStatus(ts("The option '%1' has been saved.", array(1 => $params['label'])), ts('Value Saved'), 'success');