3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
55 * The field id, used when editing the field
63 * The extended component Id
68 protected $_extendComponentId;
71 * variable is set if price set is used for membership
74 protected $_useForMember;
77 * Function to set variables up before form is built
84 public function preProcess() {
86 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
87 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
88 $url = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
89 $breadCrumb = array(array('title' => ts('Price Set Fields'),
93 $this->_extendComponentId
= array();
94 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
95 if ($extendComponentId) {
96 $this->_extendComponentId
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
99 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
103 * This function sets the default values for the form. Note that in edit/view mode
104 * the default values are retrieved from the database
108 * @return array array of default values
111 function setDefaultValues() {
113 // is it an edit operation ?
114 if (isset($this->_fid
)) {
115 $params = array('id' => $this->_fid
);
116 $this->assign('fid', $this->_fid
);
117 CRM_Price_BAO_PriceField
::retrieve($params, $defaults);
118 $this->_sid
= $defaults['price_set_id'];
120 // if text, retrieve price
121 if ($defaults['html_type'] == 'Text') {
122 $valueParams = array('price_field_id' => $this->_fid
);
124 CRM_Price_BAO_PriceFieldValue
::retrieve($valueParams, $defaults);
126 // fix the display of the monetary value, CRM-4038
127 $defaults['price'] = CRM_Utils_Money
::format($defaults['amount'], NULL, '%a');
130 if (CRM_Utils_Array
::value('active_on', $defaults)) {
131 list($defaults['active_on'],
132 $defaults['active_on_time']
133 ) = CRM_Utils_Date
::setDateDefaults($defaults['active_on'], 'activityDateTime');
136 if (CRM_Utils_Array
::value('expire_on', $defaults)) {
137 list($defaults['expire_on'],
138 $defaults['expire_on_time']
139 ) = CRM_Utils_Date
::setDateDefaults($defaults['expire_on'], 'activityDateTime');
143 $defaults['is_active'] = 1;
144 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
145 $defaults['option_status[' . $i . ']'] = 1;
146 $defaults['option_weight[' . $i . ']'] = $i;
150 if ($this->_action
& CRM_Core_Action
::ADD
) {
151 $fieldValues = array('price_set_id' => $this->_sid
);
152 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
153 $defaults['options_per_line'] = 1;
154 $defaults['is_display_amounts'] = 1;
156 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
157 $eventComponentId = NULL;
158 if (array_key_exists('CiviEvent',$enabledComponents)) {
159 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
162 if (isset($this->_sid
) && $this->_action
== CRM_Core_Action
::ADD
) {
163 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id');
164 $defaults['financial_type_id'] = $financialTypeId;
165 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
166 $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
173 * Function to actually build the form
180 public function buildQuickForm() {
181 // lets trim all the whitespace
182 $this->applyFilter('__ALL__', 'trim');
184 // add a hidden field to remember the price set id
185 // this get around the browser tab issue
186 $this->add('hidden', 'sid', $this->_sid
);
187 $this->add('hidden', 'fid', $this->_fid
);
190 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
193 $javascript = 'onchange="option_html_type(this.form)";';
195 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
197 // Text box for Participant Count for a field
200 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
201 if (count($financialType)) {
202 $this->assign('financialType', $financialType);
204 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
205 $eventComponentId = $memberComponentId = NULL;
206 if (array_key_exists('CiviEvent',$enabledComponents)) {
207 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
209 if (array_key_exists('CiviMember',$enabledComponents)) {
210 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
213 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
215 $this->add('select', 'financial_type_id',
216 ts('Financial Type'),
217 array(' '=> ts('- select -')) +
$financialType
220 $this->assign('useForMember', FALSE);
221 if (in_array($eventComponentId, $this->_extendComponentId
)) {
222 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
224 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
226 $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
227 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
229 $this->assign('useForEvent', TRUE);
232 if (in_array($memberComponentId, $this->_extendComponentId
)) {
233 $this->_useForMember
= 1;
234 $this->assign('useForMember', $this->_useForMember
);
236 $this->assign('useForEvent', FALSE);
239 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
240 $htmlTypes, TRUE, $javascript
243 // price (for text inputs)
244 $this->add('text', 'price', ts('Price'));
245 $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
246 $this->addRule('price', ts('must be a monetary value'), 'money');
248 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
249 $this->freeze('html_type');
252 // form fields of Custom Option rows
253 $_showHide = new CRM_Core_ShowHideBlocks('', '');
255 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
257 //the show hide blocks
258 $showBlocks = 'optionField_' . $i;
260 $_showHide->addHide($showBlocks);
261 if ($i == self
::NUM_OPTION
) {
262 $_showHide->addHide('additionalOption');
266 $_showHide->addShow($showBlocks);
269 $attributes['label']['size'] = 25;
270 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
273 $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
274 $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
279 'option_financial_type_id['.$i.']',
280 ts('Financial Type'),
281 array('' => ts('- select -')) +
$financialType
283 if (in_array($eventComponentId, $this->_extendComponentId
)) {
285 $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
286 $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
289 $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
290 $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
293 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
295 elseif (in_array($memberComponentId, $this->_extendComponentId
)) {
296 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
297 $js = array('onchange' => "calculateRowValues( $i );");
299 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
301 '' => ' ') +
$membershipTypes, FALSE, $js
303 $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of terms'), CRM_Utils_Array
::value('membership_num_terms', $attributes));
307 $this->add('text', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
310 $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
312 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
314 //for checkbox handling of default option
315 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
317 //default option selection
318 $this->addGroup($defaultOption, 'default_option');
319 $_showHide->addToTemplate();
321 // is_display_amounts
322 $this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
325 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE);
326 $this->addRule('weight', ts('is a numeric field'), 'numeric');
328 // checkbox / radio options per line
329 $this->add('text', 'options_per_line', ts('Options Per Line'));
330 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
332 // help post, mask, attributes, javascript ?
333 $this->add('textarea', 'help_post', ts('Field Help'),
334 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
338 $date_options = array(
339 'format' => 'dmY His',
340 'minYear' => date('Y') - 1,
341 'maxYear' => date('Y') +
5,
342 'addEmptyOption' => TRUE,
344 $this->addDateTime('active_on', ts('Active On'), FALSE, array('formatType' => 'activityDateTime'));
347 $this->addDateTime('expire_on', ts('Expire On'), FALSE, array('formatType' => 'activityDateTime'));
350 $this->add('checkbox', 'is_required', ts('Required?'));
353 $this->add('checkbox', 'is_active', ts('Active?'));
356 $this->addButtons(array(
359 'name' => ts('Save'),
364 'name' => ts('Save and New'),
369 'name' => ts('Cancel'),
374 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
376 // add a form rule to check default value
377 $this->addFormRule(array('CRM_Price_Form_Field', 'formRule'), $this);
379 // if view mode pls freeze it with the done button.
380 if ($this->_action
& CRM_Core_Action
::VIEW
) {
382 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
);
383 $this->addElement('button',
386 array('onclick' => "location.href='$url'")
392 * global validation rules for the form
394 * @param array $fields (referance) posted values of the form
396 * @return array if errors then list of errors to be posted back to the form,
401 static function formRule($fields, $files, $form) {
403 // all option fields are of type "money"
406 /** Check the option values entered
407 * Appropriate values are required for the selected datatype
408 * Incomplete row checking is also required.
410 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
411 $fields['html_type'] == 'Text' && $fields['price'] == NULL
413 $errors['price'] = ts('Price is a required field');
416 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
417 $fields['html_type'] == 'Text' && $fields['financial_type_id'] == '') {
418 $errors['financial_type_id'] = ts('Financial Type is a required field');
421 //avoid the same price field label in Within PriceSet
422 $priceFieldLabel = new CRM_Price_DAO_PriceField();
423 $priceFieldLabel->label
= $fields['label'];
424 $priceFieldLabel->price_set_id
= $form->_sid
;
427 if ($priceFieldLabel->find(TRUE) && $form->_fid
!= $priceFieldLabel->id
) {
432 $errors['label'] = ts('Name already exists in Database.');
435 if ((is_numeric(CRM_Utils_Array
::value('count', $fields)) &&
436 CRM_Utils_Array
::value('count', $fields) == 0
438 (CRM_Utils_Array
::value('html_type', $fields) == 'Text')
440 $errors['count'] = ts('Participant Count must be greater than zero.');
443 if ($form->_action
& CRM_Core_Action
::ADD
) {
444 if ($fields['html_type'] != 'Text') {
446 $_flagOption = $_rowError = 0;
448 $_showHide = new CRM_Core_ShowHideBlocks('', '');
450 for ($index = 1; $index <= self
::NUM_OPTION
; $index++
) {
452 $noLabel = $noAmount = $noWeight = 1;
453 if (!empty($fields['option_label'][$index])) {
455 $duplicateIndex = CRM_Utils_Array
::key($fields['option_label'][$index],
456 $fields['option_label']
459 if ((!($duplicateIndex === FALSE)) &&
460 (!($duplicateIndex == $index))
462 $errors["option_label[{$index}]"] = ts('Duplicate label value');
466 if ($form->_useForMember
) {
467 if (!empty($fields['membership_type_id'][$index])) {
468 $memTypesIDS[] = $fields['membership_type_id'][$index];
472 // allow for 0 value.
473 if (!empty($fields['option_amount'][$index]) ||
474 strlen($fields['option_amount'][$index]) > 0
479 if (!empty($fields['option_weight'][$index])) {
481 $duplicateIndex = CRM_Utils_Array
::key($fields['option_weight'][$index],
482 $fields['option_weight']
485 if ((!($duplicateIndex === FALSE)) &&
486 (!($duplicateIndex == $index))
488 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
492 if (!$noLabel && !$noAmount && CRM_Utils_Array
::value('option_financial_type_id', $fields) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
493 $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
495 if ($noLabel && !$noAmount) {
496 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
500 if (!$noLabel && $noAmount) {
501 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
505 if ($noLabel && $noAmount) {
509 elseif (!empty($fields['option_max_value'][$index]) &&
510 !empty($fields['option_count'][$index]) &&
511 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
513 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
517 $showBlocks = 'optionField_' . $index;
519 $_showHide->addShow($showBlocks);
523 if (!empty($_emptyRow)) {
524 $_showHide->addHide($showBlocks);
527 $_showHide->addShow($showBlocks);
529 if ($index == self
::NUM_OPTION
) {
530 $hideBlock = 'additionalOption';
531 $_showHide->addHide($hideBlock);
534 $_flagOption = $_emptyRow = 0;
537 if (!empty($memTypesIDS)) {
538 // check for checkboxes allowing user to select multiple memberships from same membership organization
539 if ($fields['html_type'] == 'CheckBox') {
540 $foundDuplicate = FALSE;
542 foreach ($memTypesIDS as $key => $val) {
543 $org = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization($val);
544 if (in_array($org[$val], $orgIds)) {
545 $foundDuplicate = TRUE;
548 $orgIds[$val] = $org[$val];
551 if ($foundDuplicate) {
552 $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.');
556 // CRM-10390 - Only one price field in a set can include auto-renew membership options
557 $foundAutorenew = FALSE;
558 foreach ($memTypesIDS as $key => $val) {
559 // see if any price field option values in this price field are for memberships with autorenew
560 $memTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($val);
561 if (CRM_Utils_Array
::value('auto_renew', $memTypeDetails)) {
562 $foundAutorenew = TRUE;
567 if ($foundAutorenew) {
568 // if so, check for other fields in this price set which also have auto-renew membership options
569 $otherFieldAutorenew = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($form->_sid
);
570 if ($otherFieldAutorenew) {
571 $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.');
575 $_showHide->addToTemplate();
577 if ($countemptyrows == 15) {
578 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
582 elseif (!empty($fields['max_value']) &&
583 !empty($fields['count']) &&
584 ($fields['count'] > $fields['max_value'])
586 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
589 // do not process if no option rows were submitted
590 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
594 if (empty($fields['option_name'])) {
595 $fields['option_amount'] = array();
598 if (empty($fields['option_label'])) {
599 $fields['option_label'] = array();
603 return empty($errors) ?
TRUE : $errors;
614 public function postProcess() {
615 // store the submitted values in an array
616 $params = $this->controller
->exportValues('Field');
618 $params['name'] = CRM_Utils_String
::titleToVar($params['label']);
619 $params['is_display_amounts'] = CRM_Utils_Array
::value('is_display_amounts', $params, FALSE);
620 $params['is_required'] = CRM_Utils_Array
::value('is_required', $params, FALSE);
621 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
622 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, false);
623 if (isset($params['active_on'])) {
624 $params['active_on'] = CRM_Utils_Date
::processDate($params['active_on'],
625 CRM_Utils_Array
::value('active_on_time', $params),
629 if (isset($params['expire_on'])) {
630 $params['expire_on'] = CRM_Utils_Date
::processDate($params['expire_on'],
631 CRM_Utils_Array
::value('expire_on_time', $params),
635 $params['visibility_id'] = CRM_Utils_Array
::value('visibility_id', $params, FALSE);
636 $params['count'] = CRM_Utils_Array
::value('count', $params, FALSE);
638 // need the FKEY - price set id
639 $params['price_set_id'] = $this->_sid
;
641 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
642 $fieldValues = array('price_set_id' => $this->_sid
);
645 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'weight', 'id');
647 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
650 // make value <=> name consistency.
651 if (isset($params['option_name'])) {
652 $params['option_value'] = $params['option_name'];
654 $params['is_enter_qty'] = CRM_Utils_Array
::value('is_enter_qty', $params, FALSE);
656 if ($params['html_type'] == 'Text') {
657 // if html type is Text, force is_enter_qty on
658 $params['is_enter_qty'] = 1;
659 // modify params values as per the option group and option
661 $params['option_amount'] = array(1 => $params['price']);
662 $params['option_label'] = array(1 => $params['label']);
663 $params['option_count'] = array(1 => $params['count']);
664 $params['option_max_value'] = array(1 => CRM_Utils_Array
::value('max_value', $params));
665 //$params['option_description'] = array( 1 => $params['description'] );
666 $params['option_weight'] = array(1 => $params['weight']);
667 $params['option_financial_type_id'] = array(1 => $params['financial_type_id']);
668 $params['is_active'] = array(1 => 1);
672 $params['id'] = $this->_fid
;
675 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array
::value('membership_num_terms', $params, 1) : NULL;
677 $priceField = CRM_Price_BAO_PriceField
::create($params);
679 if (!is_a($priceField, 'CRM_Core_Error')) {
680 CRM_Core_Session
::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label
)), ts('Saved'), 'success');
682 $buttonName = $this->controller
->getButtonName();
683 $session = CRM_Core_Session
::singleton();
684 if ($buttonName == $this->getButtonName('next', 'new')) {
685 CRM_Core_Session
::setStatus(ts(' You can add another price set field.'), '', 'info');
686 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid
));
689 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));