3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
37 * form to process actions on the field aspect of Price
39 class CRM_Price_Form_Field
extends CRM_Core_Form
{
42 * Constants for number of options for data types of multiple option.
44 const NUM_OPTION
= 15;
47 * The custom set id saved to the session for an update.
54 * The field id, used when editing the field
61 * The extended component Id.
65 protected $_extendComponentId;
68 * Variable is set if price set is used for membership.
70 protected $_useForMember;
73 * Set variables up before form is built.
75 public function preProcess() {
77 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
78 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
79 $url = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
80 $breadCrumb = array(array('title' => ts('Price Set Fields'), 'url' => $url));
82 $this->_extendComponentId
= array();
83 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
84 if ($extendComponentId) {
85 $this->_extendComponentId
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
88 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
90 $this->setPageTitle(ts('Price Field'));
94 * Set default values for the form. Note that in edit/view mode
95 * the default values are retrieved from the database
98 * array of default values
100 public function setDefaultValues() {
102 // is it an edit operation ?
103 if (isset($this->_fid
)) {
104 $params = array('id' => $this->_fid
);
105 $this->assign('fid', $this->_fid
);
106 CRM_Price_BAO_PriceField
::retrieve($params, $defaults);
107 $this->_sid
= $defaults['price_set_id'];
109 // if text, retrieve price
110 if ($defaults['html_type'] == 'Text') {
111 $isActive = $defaults['is_active'];
112 $valueParams = array('price_field_id' => $this->_fid
);
114 CRM_Price_BAO_PriceFieldValue
::retrieve($valueParams, $defaults);
116 // fix the display of the monetary value, CRM-4038
117 $defaults['price'] = CRM_Utils_Money
::format($defaults['amount'], NULL, '%a');
118 $defaults['is_active'] = $isActive;
121 if (!empty($defaults['active_on'])) {
122 list($defaults['active_on'],
123 $defaults['active_on_time']
124 ) = CRM_Utils_Date
::setDateDefaults($defaults['active_on'], 'activityDateTime');
127 if (!empty($defaults['expire_on'])) {
128 list($defaults['expire_on'],
129 $defaults['expire_on_time']
130 ) = CRM_Utils_Date
::setDateDefaults($defaults['expire_on'], 'activityDateTime');
134 $defaults['is_active'] = 1;
135 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
136 $defaults['option_status[' . $i . ']'] = 1;
137 $defaults['option_weight[' . $i . ']'] = $i;
141 if ($this->_action
& CRM_Core_Action
::ADD
) {
142 $fieldValues = array('price_set_id' => $this->_sid
);
143 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
144 $defaults['options_per_line'] = 1;
145 $defaults['is_display_amounts'] = 1;
147 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
148 $eventComponentId = NULL;
149 if (array_key_exists('CiviEvent', $enabledComponents)) {
150 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
153 if (isset($this->_sid
) && $this->_action
== CRM_Core_Action
::ADD
) {
154 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id');
155 $defaults['financial_type_id'] = $financialTypeId;
156 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
157 $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
164 * Build the form object.
166 public function buildQuickForm() {
167 // lets trim all the whitespace
168 $this->applyFilter('__ALL__', 'trim');
170 // add a hidden field to remember the price set id
171 // this get around the browser tab issue
172 $this->add('hidden', 'sid', $this->_sid
);
173 $this->add('hidden', 'fid', $this->_fid
);
176 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
179 $javascript = 'onchange="option_html_type(this.form)";';
181 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
183 // Text box for Participant Count for a field
186 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
187 foreach ($financialType as $finTypeId => $type) {
188 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()
189 && !CRM_Core_Permission
::check('add contributions of type ' . $type)
191 unset($financialType[$finTypeId]);
194 if (count($financialType)) {
195 $this->assign('financialType', $financialType);
197 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
198 $eventComponentId = $memberComponentId = NULL;
199 if (array_key_exists('CiviEvent', $enabledComponents)) {
200 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
202 if (array_key_exists('CiviMember', $enabledComponents)) {
203 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
206 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
208 $this->add('select', 'financial_type_id',
209 ts('Financial Type'),
210 array(' ' => ts('- select -')) +
$financialType
213 $this->assign('useForMember', FALSE);
214 if (in_array($eventComponentId, $this->_extendComponentId
)) {
215 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
217 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
219 $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
220 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
222 $this->assign('useForEvent', TRUE);
225 if (in_array($memberComponentId, $this->_extendComponentId
)) {
226 $this->_useForMember
= 1;
227 $this->assign('useForMember', $this->_useForMember
);
229 $this->assign('useForEvent', FALSE);
232 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
233 $htmlTypes, TRUE, $javascript
236 // price (for text inputs)
237 $this->add('text', 'price', ts('Price'));
238 $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
239 $this->addRule('price', ts('must be a monetary value'), 'money');
241 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
242 $this->freeze('html_type');
245 // form fields of Custom Option rows
246 $_showHide = new CRM_Core_ShowHideBlocks('', '');
248 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
250 //the show hide blocks
251 $showBlocks = 'optionField_' . $i;
253 $_showHide->addHide($showBlocks);
254 if ($i == self
::NUM_OPTION
) {
255 $_showHide->addHide('additionalOption');
259 $_showHide->addShow($showBlocks);
262 $attributes['label']['size'] = 25;
263 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
266 $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
267 $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
272 'option_financial_type_id[' . $i . ']',
273 ts('Financial Type'),
274 array('' => ts('- select -')) +
$financialType
276 if (in_array($eventComponentId, $this->_extendComponentId
)) {
278 $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
279 $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
282 $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
283 $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
286 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
288 elseif (in_array($memberComponentId, $this->_extendComponentId
)) {
289 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
290 $js = array('onchange' => "calculateRowValues( $i );");
292 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
293 array('' => ' ') +
$membershipTypes, FALSE, $js
295 $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array
::value('membership_num_terms', $attributes));
299 $this->add('text', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
302 $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
304 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
306 //for checkbox handling of default option
307 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
309 //default option selection
310 $this->addGroup($defaultOption, 'default_option');
311 $_showHide->addToTemplate();
313 // is_display_amounts
314 $this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
317 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE);
318 $this->addRule('weight', ts('is a numeric field'), 'numeric');
320 // checkbox / radio options per line
321 $this->add('text', 'options_per_line', ts('Options Per Line'));
322 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
324 // help post, mask, attributes, javascript ?
325 $this->add('textarea', 'help_post', ts('Field Help'),
326 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
330 $date_options = array(
331 'format' => 'dmY His',
332 'minYear' => date('Y') - 1,
333 'maxYear' => date('Y') +
5,
334 'addEmptyOption' => TRUE,
336 $this->addDateTime('active_on', ts('Active On'), FALSE, array('formatType' => 'activityDateTime'));
339 $this->addDateTime('expire_on', ts('Expire On'), FALSE, array('formatType' => 'activityDateTime'));
342 $this->add('checkbox', 'is_required', ts('Required?'));
345 $this->add('checkbox', 'is_active', ts('Active?'));
348 $this->addButtons(array(
351 'name' => ts('Save'),
356 'name' => ts('Save and New'),
361 'name' => ts('Cancel'),
365 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
367 // add a form rule to check default value
368 $this->addFormRule(array('CRM_Price_Form_Field', 'formRule'), $this);
370 // if view mode pls freeze it with the done button.
371 if ($this->_action
& CRM_Core_Action
::VIEW
) {
373 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
);
374 $this->addElement('button',
377 array('onclick' => "location.href='$url'")
383 * Global validation rules for the form.
385 * @param array $fields
386 * Posted values of the form.
389 * @param CRM_Core_Form $form
392 * if errors then list of errors to be posted back to the form,
395 public static function formRule($fields, $files, $form) {
397 // all option fields are of type "money"
400 /** Check the option values entered
401 * Appropriate values are required for the selected datatype
402 * Incomplete row checking is also required.
404 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
405 $fields['html_type'] == 'Text' && $fields['price'] == NULL
407 $errors['price'] = ts('Price is a required field');
410 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
411 $fields['html_type'] == 'Text' && $fields['financial_type_id'] == ''
413 $errors['financial_type_id'] = ts('Financial Type is a required field');
416 //avoid the same price field label in Within PriceSet
417 $priceFieldLabel = new CRM_Price_DAO_PriceField();
418 $priceFieldLabel->label
= $fields['label'];
419 $priceFieldLabel->price_set_id
= $form->_sid
;
422 if ($priceFieldLabel->find(TRUE) && $form->_fid
!= $priceFieldLabel->id
) {
427 $errors['label'] = ts('Name already exists in Database.');
430 if ((is_numeric(CRM_Utils_Array
::value('count', $fields)) &&
431 CRM_Utils_Array
::value('count', $fields) == 0
433 (CRM_Utils_Array
::value('html_type', $fields) == 'Text')
435 $errors['count'] = ts('Participant Count must be greater than zero.');
438 if ($form->_action
& CRM_Core_Action
::ADD
) {
439 if ($fields['html_type'] != 'Text') {
441 $_flagOption = $_rowError = 0;
443 $_showHide = new CRM_Core_ShowHideBlocks('', '');
445 for ($index = 1; $index <= self
::NUM_OPTION
; $index++
) {
447 $noLabel = $noAmount = $noWeight = 1;
448 if (!empty($fields['option_label'][$index])) {
450 $duplicateIndex = CRM_Utils_Array
::key($fields['option_label'][$index],
451 $fields['option_label']
454 if ((!($duplicateIndex === FALSE)) &&
455 (!($duplicateIndex == $index))
457 $errors["option_label[{$index}]"] = ts('Duplicate label value');
461 if ($form->_useForMember
) {
462 if (!empty($fields['membership_type_id'][$index])) {
463 $memTypesIDS[] = $fields['membership_type_id'][$index];
467 // allow for 0 value.
468 if (!empty($fields['option_amount'][$index]) ||
469 strlen($fields['option_amount'][$index]) > 0
474 if (!empty($fields['option_weight'][$index])) {
476 $duplicateIndex = CRM_Utils_Array
::key($fields['option_weight'][$index],
477 $fields['option_weight']
480 if ((!($duplicateIndex === FALSE)) &&
481 (!($duplicateIndex == $index))
483 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
487 if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
488 $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
490 if ($noLabel && !$noAmount) {
491 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
495 if (!$noLabel && $noAmount) {
496 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
500 if ($noLabel && $noAmount) {
504 elseif (!empty($fields['option_max_value'][$index]) &&
505 !empty($fields['option_count'][$index]) &&
506 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
508 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
512 $showBlocks = 'optionField_' . $index;
514 $_showHide->addShow($showBlocks);
518 if (!empty($_emptyRow)) {
519 $_showHide->addHide($showBlocks);
522 $_showHide->addShow($showBlocks);
524 if ($index == self
::NUM_OPTION
) {
525 $hideBlock = 'additionalOption';
526 $_showHide->addHide($hideBlock);
529 $_flagOption = $_emptyRow = 0;
532 if (!empty($memTypesIDS)) {
533 // check for checkboxes allowing user to select multiple memberships from same membership organization
534 if ($fields['html_type'] == 'CheckBox') {
535 $foundDuplicate = FALSE;
537 foreach ($memTypesIDS as $key => $val) {
538 $org = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization($val);
539 if (in_array($org[$val], $orgIds)) {
540 $foundDuplicate = TRUE;
543 $orgIds[$val] = $org[$val];
546 if ($foundDuplicate) {
547 $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity.');
551 // CRM-10390 - Only one price field in a set can include auto-renew membership options
552 $foundAutorenew = FALSE;
553 foreach ($memTypesIDS as $key => $val) {
554 // see if any price field option values in this price field are for memberships with autorenew
555 $memTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($val);
556 if (!empty($memTypeDetails['auto_renew'])) {
557 $foundAutorenew = TRUE;
562 if ($foundAutorenew) {
563 // if so, check for other fields in this price set which also have auto-renew membership options
564 $otherFieldAutorenew = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($form->_sid
);
565 if ($otherFieldAutorenew) {
566 $errors['_qf_default'] = ts('You can include auto-renew membership choices for only one price field in a price set. Another field in this set already contains one or more auto-renew membership options.');
570 $_showHide->addToTemplate();
572 if ($countemptyrows == 15) {
573 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
577 elseif (!empty($fields['max_value']) &&
578 !empty($fields['count']) &&
579 ($fields['count'] > $fields['max_value'])
581 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
584 // do not process if no option rows were submitted
585 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
589 if (empty($fields['option_name'])) {
590 $fields['option_amount'] = array();
593 if (empty($fields['option_label'])) {
594 $fields['option_label'] = array();
598 return empty($errors) ?
TRUE : $errors;
604 public function postProcess() {
605 // store the submitted values in an array
606 $params = $this->controller
->exportValues('Field');
608 $params['is_display_amounts'] = CRM_Utils_Array
::value('is_display_amounts', $params, FALSE);
609 $params['is_required'] = CRM_Utils_Array
::value('is_required', $params, FALSE);
610 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
611 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
612 if (isset($params['active_on'])) {
613 $params['active_on'] = CRM_Utils_Date
::processDate($params['active_on'],
614 CRM_Utils_Array
::value('active_on_time', $params),
618 if (isset($params['expire_on'])) {
619 $params['expire_on'] = CRM_Utils_Date
::processDate($params['expire_on'],
620 CRM_Utils_Array
::value('expire_on_time', $params),
624 $params['visibility_id'] = CRM_Utils_Array
::value('visibility_id', $params, FALSE);
625 $params['count'] = CRM_Utils_Array
::value('count', $params, FALSE);
627 // need the FKEY - price set id
628 $params['price_set_id'] = $this->_sid
;
630 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
631 $fieldValues = array('price_set_id' => $this->_sid
);
634 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'weight', 'id');
636 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
639 // make value <=> name consistency.
640 if (isset($params['option_name'])) {
641 $params['option_value'] = $params['option_name'];
643 $params['is_enter_qty'] = CRM_Utils_Array
::value('is_enter_qty', $params, FALSE);
645 if ($params['html_type'] == 'Text') {
646 // if html type is Text, force is_enter_qty on
647 $params['is_enter_qty'] = 1;
648 // modify params values as per the option group and option
650 $params['option_amount'] = array(1 => $params['price']);
651 $params['option_label'] = array(1 => $params['label']);
652 $params['option_count'] = array(1 => $params['count']);
653 $params['option_max_value'] = array(1 => CRM_Utils_Array
::value('max_value', $params));
654 //$params['option_description'] = array( 1 => $params['description'] );
655 $params['option_weight'] = array(1 => $params['weight']);
656 $params['option_financial_type_id'] = array(1 => $params['financial_type_id']);
660 $params['id'] = $this->_fid
;
663 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array
::value('membership_num_terms', $params, 1) : NULL;
665 $priceField = CRM_Price_BAO_PriceField
::create($params);
667 if (!is_a($priceField, 'CRM_Core_Error')) {
668 CRM_Core_Session
::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label
)), ts('Saved'), 'success');
670 $buttonName = $this->controller
->getButtonName();
671 $session = CRM_Core_Session
::singleton();
672 if ($buttonName == $this->getButtonName('next', 'new')) {
673 CRM_Core_Session
::setStatus(ts(' You can add another price set field.'), '', 'info');
674 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid
));
677 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));