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
35 * form to process actions on the field aspect of Price
37 class CRM_Price_Form_Field
extends CRM_Core_Form
{
40 * Constants for number of options for data types of multiple option.
42 const NUM_OPTION
= 15;
45 * The custom set id saved to the session for an update.
52 * The field id, used when editing the field
59 * The extended component Id.
63 protected $_extendComponentId;
66 * Variable is set if price set is used for membership.
68 protected $_useForMember;
71 * Set variables up before form is built.
73 public function preProcess() {
75 $this->_sid
= CRM_Utils_Request
::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
76 $this->_fid
= CRM_Utils_Request
::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
77 $url = CRM_Utils_System
::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
78 $breadCrumb = array(array('title' => ts('Price Set Fields'), 'url' => $url));
80 $this->_extendComponentId
= array();
81 $extendComponentId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'extends', 'id');
82 if ($extendComponentId) {
83 $this->_extendComponentId
= explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $extendComponentId);
86 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
88 $this->setPageTitle(ts('Price Field'));
92 * Set default values for the form.
95 * array of default values
97 public function setDefaultValues() {
99 // is it an edit operation ?
100 if (isset($this->_fid
)) {
101 $params = array('id' => $this->_fid
);
102 $this->assign('fid', $this->_fid
);
103 CRM_Price_BAO_PriceField
::retrieve($params, $defaults);
104 $this->_sid
= $defaults['price_set_id'];
106 // if text, retrieve price
107 if ($defaults['html_type'] == 'Text') {
108 $isActive = $defaults['is_active'];
109 $valueParams = array('price_field_id' => $this->_fid
);
111 CRM_Price_BAO_PriceFieldValue
::retrieve($valueParams, $defaults);
113 // fix the display of the monetary value, CRM-4038
114 $defaults['price'] = CRM_Utils_Money
::format($defaults['amount'], NULL, '%a');
115 $defaults['is_active'] = $isActive;
118 if (!empty($defaults['active_on'])) {
119 list($defaults['active_on'],
120 $defaults['active_on_time']
121 ) = CRM_Utils_Date
::setDateDefaults($defaults['active_on'], 'activityDateTime');
124 if (!empty($defaults['expire_on'])) {
125 list($defaults['expire_on'],
126 $defaults['expire_on_time']
127 ) = CRM_Utils_Date
::setDateDefaults($defaults['expire_on'], 'activityDateTime');
131 $defaults['is_active'] = 1;
132 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
133 $defaults['option_status[' . $i . ']'] = 1;
134 $defaults['option_weight[' . $i . ']'] = $i;
138 if ($this->_action
& CRM_Core_Action
::ADD
) {
139 $fieldValues = array('price_set_id' => $this->_sid
);
140 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Price_DAO_PriceField', $fieldValues);
141 $defaults['options_per_line'] = 1;
142 $defaults['is_display_amounts'] = 1;
144 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
145 $eventComponentId = NULL;
146 if (array_key_exists('CiviEvent', $enabledComponents)) {
147 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
150 if (isset($this->_sid
) && $this->_action
== CRM_Core_Action
::ADD
) {
151 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $this->_sid
, 'financial_type_id');
152 $defaults['financial_type_id'] = $financialTypeId;
153 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
154 $defaults['option_financial_type_id[' . $i . ']'] = $financialTypeId;
161 * Build the form object.
163 public function buildQuickForm() {
164 // lets trim all the whitespace
165 $this->applyFilter('__ALL__', 'trim');
167 // add a hidden field to remember the price set id
168 // this get around the browser tab issue
169 $this->add('hidden', 'sid', $this->_sid
);
170 $this->add('hidden', 'fid', $this->_fid
);
173 $this->add('text', 'label', ts('Field Label'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
176 $javascript = 'onchange="option_html_type(this.form)";';
178 $htmlTypes = CRM_Price_BAO_PriceField
::htmlTypes();
180 // Text box for Participant Count for a field
183 $financialType = CRM_Financial_BAO_FinancialType
::getIncomeFinancialType();
184 foreach ($financialType as $finTypeId => $type) {
185 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus()
186 && !CRM_Core_Permission
::check('add contributions of type ' . $type)
188 unset($financialType[$finTypeId]);
191 if (count($financialType)) {
192 $this->assign('financialType', $financialType);
194 $enabledComponents = CRM_Core_Component
::getEnabledComponents();
195 $eventComponentId = $memberComponentId = NULL;
196 if (array_key_exists('CiviEvent', $enabledComponents)) {
197 $eventComponentId = CRM_Core_Component
::getComponentID('CiviEvent');
199 if (array_key_exists('CiviMember', $enabledComponents)) {
200 $memberComponentId = CRM_Core_Component
::getComponentID('CiviMember');
203 $attributes = CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceFieldValue');
205 $this->add('select', 'financial_type_id',
206 ts('Financial Type'),
207 array(' ' => ts('- select -')) +
$financialType
210 $this->assign('useForMember', FALSE);
211 if (in_array($eventComponentId, $this->_extendComponentId
)) {
212 $this->add('text', 'count', ts('Participant Count'), $attributes['count']);
214 $this->addRule('count', ts('Participant Count should be a positive number'), 'positiveInteger');
216 $this->add('text', 'max_value', ts('Max Participants'), $attributes['max_value']);
217 $this->addRule('max_value', ts('Please enter a valid Max Participants.'), 'positiveInteger');
219 $this->assign('useForEvent', TRUE);
222 if (in_array($memberComponentId, $this->_extendComponentId
)) {
223 $this->_useForMember
= 1;
224 $this->assign('useForMember', $this->_useForMember
);
226 $this->assign('useForEvent', FALSE);
229 $sel = $this->add('select', 'html_type', ts('Input Field Type'),
230 $htmlTypes, TRUE, $javascript
233 // price (for text inputs)
234 $this->add('text', 'price', ts('Price'));
235 $this->registerRule('price', 'callback', 'money', 'CRM_Utils_Rule');
236 $this->addRule('price', ts('must be a monetary value'), 'money');
238 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
239 $this->freeze('html_type');
242 // form fields of Custom Option rows
243 $_showHide = new CRM_Core_ShowHideBlocks('', '');
245 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
247 //the show hide blocks
248 $showBlocks = 'optionField_' . $i;
250 $_showHide->addHide($showBlocks);
251 if ($i == self
::NUM_OPTION
) {
252 $_showHide->addHide('additionalOption');
256 $_showHide->addShow($showBlocks);
259 $attributes['label']['size'] = 25;
260 $this->add('text', 'option_label[' . $i . ']', ts('Label'), $attributes['label']);
263 $this->add('text', 'option_amount[' . $i . ']', ts('Amount'), $attributes['amount']);
264 $this->addRule('option_amount[' . $i . ']', ts('Please enter a valid amount for this field.'), 'money');
269 'option_financial_type_id[' . $i . ']',
270 ts('Financial Type'),
271 array('' => ts('- select -')) +
$financialType
273 if (in_array($eventComponentId, $this->_extendComponentId
)) {
275 $this->add('text', 'option_count[' . $i . ']', ts('Participant Count'), $attributes['count']);
276 $this->addRule('option_count[' . $i . ']', ts('Please enter a valid Participants Count.'), 'positiveInteger');
279 $this->add('text', 'option_max_value[' . $i . ']', ts('Max Participants'), $attributes['max_value']);
280 $this->addRule('option_max_value[' . $i . ']', ts('Please enter a valid Max Participants.'), 'positiveInteger');
283 //$this->add('textArea', 'option_description['.$i.']', ts('Description'), array('rows' => 1, 'cols' => 40 ));
285 elseif (in_array($memberComponentId, $this->_extendComponentId
)) {
286 $membershipTypes = CRM_Member_PseudoConstant
::membershipType();
287 $js = array('onchange' => "calculateRowValues( $i );");
289 $this->add('select', 'membership_type_id[' . $i . ']', ts('Membership Type'),
290 array('' => ' ') +
$membershipTypes, FALSE, $js
292 $this->add('text', 'membership_num_terms[' . $i . ']', ts('Number of Terms'), CRM_Utils_Array
::value('membership_num_terms', $attributes));
296 $this->add('text', 'option_weight[' . $i . ']', ts('Order'), $attributes['weight']);
299 $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?'));
301 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
303 //for checkbox handling of default option
304 $this->add('checkbox', "default_checkbox_option[$i]", NULL);
306 //default option selection
307 $this->addGroup($defaultOption, 'default_option');
308 $_showHide->addToTemplate();
310 // is_display_amounts
311 $this->add('checkbox', 'is_display_amounts', ts('Display Amount?'));
314 $this->add('text', 'weight', ts('Order'), CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'weight'), TRUE);
315 $this->addRule('weight', ts('is a numeric field'), 'numeric');
317 // checkbox / radio options per line
318 $this->add('text', 'options_per_line', ts('Options Per Line'));
319 $this->addRule('options_per_line', ts('must be a numeric value'), 'numeric');
321 // help post, mask, attributes, javascript ?
322 $this->add('textarea', 'help_post', ts('Field Help'),
323 CRM_Core_DAO
::getAttribute('CRM_Price_DAO_PriceField', 'help_post')
327 $date_options = array(
328 'format' => 'dmY His',
329 'minYear' => date('Y') - 1,
330 'maxYear' => date('Y') +
5,
331 'addEmptyOption' => TRUE,
333 $this->addDateTime('active_on', ts('Active On'), FALSE, array('formatType' => 'activityDateTime'));
336 $this->addDateTime('expire_on', ts('Expire On'), FALSE, array('formatType' => 'activityDateTime'));
339 $this->add('checkbox', 'is_required', ts('Required?'));
342 $this->add('checkbox', 'is_active', ts('Active?'));
345 $this->addButtons(array(
348 'name' => ts('Save'),
353 'name' => ts('Save and New'),
358 'name' => ts('Cancel'),
362 $this->add('select', 'visibility_id', ts('Visibility'), CRM_Core_PseudoConstant
::visibility());
364 // add a form rule to check default value
365 $this->addFormRule(array('CRM_Price_Form_Field', 'formRule'), $this);
367 // if view mode pls freeze it with the done button.
368 if ($this->_action
& CRM_Core_Action
::VIEW
) {
370 $url = CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
);
371 $this->addElement('button',
374 array('onclick' => "location.href='$url'")
380 * Global validation rules for the form.
382 * @param array $fields
383 * Posted values of the form.
386 * @param CRM_Core_Form $form
389 * if errors then list of errors to be posted back to the form,
392 public static function formRule($fields, $files, $form) {
394 // all option fields are of type "money"
397 /** Check the option values entered
398 * Appropriate values are required for the selected datatype
399 * Incomplete row checking is also required.
401 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
402 $fields['html_type'] == 'Text' && $fields['price'] == NULL
404 $errors['price'] = ts('Price is a required field');
407 if (($form->_action
& CRM_Core_Action
::ADD ||
$form->_action
& CRM_Core_Action
::UPDATE
) &&
408 $fields['html_type'] == 'Text' && $fields['financial_type_id'] == ''
410 $errors['financial_type_id'] = ts('Financial Type is a required field');
413 //avoid the same price field label in Within PriceSet
414 $priceFieldLabel = new CRM_Price_DAO_PriceField();
415 $priceFieldLabel->label
= $fields['label'];
416 $priceFieldLabel->price_set_id
= $form->_sid
;
419 if ($priceFieldLabel->find(TRUE) && $form->_fid
!= $priceFieldLabel->id
) {
424 $errors['label'] = ts('Name already exists in Database.');
427 if ((is_numeric(CRM_Utils_Array
::value('count', $fields)) &&
428 CRM_Utils_Array
::value('count', $fields) == 0
430 (CRM_Utils_Array
::value('html_type', $fields) == 'Text')
432 $errors['count'] = ts('Participant Count must be greater than zero.');
435 if ($form->_action
& CRM_Core_Action
::ADD
) {
436 if ($fields['html_type'] != 'Text') {
438 $_flagOption = $_rowError = 0;
440 $_showHide = new CRM_Core_ShowHideBlocks('', '');
442 for ($index = 1; $index <= self
::NUM_OPTION
; $index++
) {
444 $noLabel = $noAmount = $noWeight = 1;
445 if (!empty($fields['option_label'][$index])) {
447 $duplicateIndex = CRM_Utils_Array
::key($fields['option_label'][$index],
448 $fields['option_label']
451 if ((!($duplicateIndex === FALSE)) &&
452 (!($duplicateIndex == $index))
454 $errors["option_label[{$index}]"] = ts('Duplicate label value');
458 if ($form->_useForMember
) {
459 if (!empty($fields['membership_type_id'][$index])) {
460 $memTypesIDS[] = $fields['membership_type_id'][$index];
464 // allow for 0 value.
465 if (!empty($fields['option_amount'][$index]) ||
466 strlen($fields['option_amount'][$index]) > 0
471 if (!empty($fields['option_weight'][$index])) {
473 $duplicateIndex = CRM_Utils_Array
::key($fields['option_weight'][$index],
474 $fields['option_weight']
477 if ((!($duplicateIndex === FALSE)) &&
478 (!($duplicateIndex == $index))
480 $errors["option_weight[{$index}]"] = ts('Duplicate weight value');
484 if (!$noLabel && !$noAmount && !empty($fields['option_financial_type_id']) && $fields['option_financial_type_id'][$index] == '' && $fields['html_type'] != 'Text') {
485 $errors["option_financial_type_id[{$index}]"] = ts('Financial Type is a Required field.');
487 if ($noLabel && !$noAmount) {
488 $errors["option_label[{$index}]"] = ts('Label cannot be empty.');
492 if (!$noLabel && $noAmount) {
493 $errors["option_amount[{$index}]"] = ts('Amount cannot be empty.');
497 if ($noLabel && $noAmount) {
501 elseif (!empty($fields['option_max_value'][$index]) &&
502 !empty($fields['option_count'][$index]) &&
503 ($fields['option_count'][$index] > $fields['option_max_value'][$index])
505 $errors["option_max_value[{$index}]"] = ts('Participant count can not be greater than max participants.');
509 $showBlocks = 'optionField_' . $index;
511 $_showHide->addShow($showBlocks);
515 if (!empty($_emptyRow)) {
516 $_showHide->addHide($showBlocks);
519 $_showHide->addShow($showBlocks);
521 if ($index == self
::NUM_OPTION
) {
522 $hideBlock = 'additionalOption';
523 $_showHide->addHide($hideBlock);
526 $_flagOption = $_emptyRow = 0;
529 if (!empty($memTypesIDS)) {
530 // check for checkboxes allowing user to select multiple memberships from same membership organization
531 if ($fields['html_type'] == 'CheckBox') {
532 $foundDuplicate = FALSE;
534 foreach ($memTypesIDS as $key => $val) {
535 $org = CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization($val);
536 if (in_array($org[$val], $orgIds)) {
537 $foundDuplicate = TRUE;
540 $orgIds[$val] = $org[$val];
543 if ($foundDuplicate) {
544 $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.');
548 // CRM-10390 - Only one price field in a set can include auto-renew membership options
549 $foundAutorenew = FALSE;
550 foreach ($memTypesIDS as $key => $val) {
551 // see if any price field option values in this price field are for memberships with autorenew
552 $memTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($val);
553 if (!empty($memTypeDetails['auto_renew'])) {
554 $foundAutorenew = TRUE;
559 if ($foundAutorenew) {
560 // if so, check for other fields in this price set which also have auto-renew membership options
561 $otherFieldAutorenew = CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($form->_sid
);
562 if ($otherFieldAutorenew) {
563 $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.');
567 $_showHide->addToTemplate();
569 if ($countemptyrows == 15) {
570 $errors['option_label[1]'] = $errors['option_amount[1]'] = ts('Label and value cannot be empty.');
574 elseif (!empty($fields['max_value']) &&
575 !empty($fields['count']) &&
576 ($fields['count'] > $fields['max_value'])
578 $errors['max_value'] = ts('Participant count can not be greater than max participants.');
581 // do not process if no option rows were submitted
582 if (empty($fields['option_amount']) && empty($fields['option_label'])) {
586 if (empty($fields['option_name'])) {
587 $fields['option_amount'] = array();
590 if (empty($fields['option_label'])) {
591 $fields['option_label'] = array();
595 return empty($errors) ?
TRUE : $errors;
601 public function postProcess() {
602 // store the submitted values in an array
603 $params = $this->controller
->exportValues('Field');
605 $params['is_display_amounts'] = CRM_Utils_Array
::value('is_display_amounts', $params, FALSE);
606 $params['is_required'] = CRM_Utils_Array
::value('is_required', $params, FALSE);
607 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
608 $params['financial_type_id'] = CRM_Utils_Array
::value('financial_type_id', $params, FALSE);
609 if (isset($params['active_on'])) {
610 $params['active_on'] = CRM_Utils_Date
::processDate($params['active_on'],
611 CRM_Utils_Array
::value('active_on_time', $params),
615 if (isset($params['expire_on'])) {
616 $params['expire_on'] = CRM_Utils_Date
::processDate($params['expire_on'],
617 CRM_Utils_Array
::value('expire_on_time', $params),
621 $params['visibility_id'] = CRM_Utils_Array
::value('visibility_id', $params, FALSE);
622 $params['count'] = CRM_Utils_Array
::value('count', $params, FALSE);
624 // need the FKEY - price set id
625 $params['price_set_id'] = $this->_sid
;
627 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::ADD
)) {
628 $fieldValues = array('price_set_id' => $this->_sid
);
631 $oldWeight = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid
, 'weight', 'id');
633 $params['weight'] = CRM_Utils_Weight
::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
636 // make value <=> name consistency.
637 if (isset($params['option_name'])) {
638 $params['option_value'] = $params['option_name'];
640 $params['is_enter_qty'] = CRM_Utils_Array
::value('is_enter_qty', $params, FALSE);
642 if ($params['html_type'] == 'Text') {
643 // if html type is Text, force is_enter_qty on
644 $params['is_enter_qty'] = 1;
645 // modify params values as per the option group and option
647 $params['option_amount'] = array(1 => $params['price']);
648 $params['option_label'] = array(1 => $params['label']);
649 $params['option_count'] = array(1 => $params['count']);
650 $params['option_max_value'] = array(1 => CRM_Utils_Array
::value('max_value', $params));
651 //$params['option_description'] = array( 1 => $params['description'] );
652 $params['option_weight'] = array(1 => $params['weight']);
653 $params['option_financial_type_id'] = array(1 => $params['financial_type_id']);
657 $params['id'] = $this->_fid
;
660 $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array
::value('membership_num_terms', $params, 1) : NULL;
662 $priceField = CRM_Price_BAO_PriceField
::create($params);
664 if (!is_a($priceField, 'CRM_Core_Error')) {
665 CRM_Core_Session
::setStatus(ts('Price Field \'%1\' has been saved.', array(1 => $priceField->label
)), ts('Saved'), 'success');
667 $buttonName = $this->controller
->getButtonName();
668 $session = CRM_Core_Session
::singleton();
669 if ($buttonName == $this->getButtonName('next', 'new')) {
670 CRM_Core_Session
::setStatus(ts(' You can add another price set field.'), '', 'info');
671 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid
));
674 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid
));