Commit 80ed6fda authored by bgm's avatar bgm Committed by Stéphane Lussier

Fixes various issues with default selections

parent ee9f7b8b
......@@ -24,20 +24,17 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
$form->removeElement('email-5');
}
// Rename the "Contribute" button
$buttons = $form->getElement('buttons');
$buttons->_elements[0]->_attributes['value'] = E::ts('Add to Cart');
// Settings for the JS code
$settings = [
'total_amount' => $eventpack->total_amount,
];
self::addCartButtons($form);
self::setDefaults($form);
self::addCartButtons($form, $eventpack);
self::setDefaultsEvents($form, $eventpack);
Civi::resources()
->addScriptFile('eventpack', 'js/eventpack.js')
->addStyleFile('eventpack', 'css/eventpack.css')
->addSetting(['eventpack' => $settings])
->addScript('CRM.eventpackCustomizeContributionForm();');
}
......@@ -45,7 +42,7 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
/**
* Helper function for the quantity field.
*/
static private function addCartButtons(&$form) {
static private function addCartButtons(&$form, $eventpack) {
$contribution_page_id = $form->get('id');
// FIXME: create BAO helper function?
......@@ -57,14 +54,49 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
return;
}
$options = [];
$default_option = null;
$has_value = false;
// Build the options for the Quantity field.
$options = [
0 => 0,
];
$pv = civicrm_api3('PriceFieldValue', 'get', [
'price_field_id' => $eventpack->price_field_id,
])['values'];
foreach ($pv as $key => $val) {
if ($val['is_active'] && $val['visibility_id'] == 1) {
$options[$key] = $val['label'];
if ($default_option == null) {
$default_option = $key;
}
}
}
// If the user already selected options, select it by default
$selections = CRM_Boxoffice_BAO_Boxoffice::getAllSelections();
for ($i = 1; $i <= 10; $i++) {
$options[$i] = $i;
foreach ($selections as $event_id => $sels) {
if (!empty($sels[$eventpack->price_field_id])) {
$default_option = $sels[$eventpack->price_field_id];
$has_value = true;
}
}
$form->setDefaults([
'quantity' => $default_option,
]);
$form->addElement('select', 'quantity', E::ts('Quantity'), $options, TRUE);
// Rename the "Contribute" button
$buttons = $form->getElement('buttons');
$buttons->_elements[0]->_attributes['value'] = ($has_value ? E::ts('Update Cart') : E::ts('Add to Cart'));
// FIXME: move to eventpack
CRM_Core_Region::instance('form-body')->add([
'template' => 'CRM/Boxoffice/Contribute/Form/Contribution/Boxoffice.tpl',
]);
......@@ -73,7 +105,7 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
/**
* Helper function to set default selections.
*/
static private function setDefaults(&$form) {
static private function setDefaultsEvents(&$form, $eventpack) {
$defaults = [];
$contribution_page_id = $form->get('id');
......@@ -101,19 +133,28 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
'sequential' => 1,
])['values'];
foreach ($selections as $event_id => $sels) {
// Fetch the event title
// and match them to the price value options of this form.
$event_title = civicrm_api3('Event', 'getsingle', [
'id' => $event_id,
])['title'];
// Special-case, if the user must select X items, and there happens to be
// exactly X items, then pre-select all of them, since that's the only option.
if (count($pv_values) == $eventpack->min_events) {
foreach ($pv_values as $k1 => $v1) {
if ($v1['label'] == $event_title) {
$pfid = $eventpack->price_field_id;
if (!empty($sels[$pfid])) {
$defaults['price_' . $price_field_id][$v1['id']] = 1;
$defaults['price_' . $price_field_id][$v1['id']] = 1;
}
}
else {
foreach ($selections as $event_id => $sels) {
// Fetch the event title
// and match them to the price value options of this form.
$event_title = civicrm_api3('Event', 'getsingle', [
'id' => $event_id,
])['title'];
foreach ($pv_values as $k1 => $v1) {
if ($v1['label'] == $event_title) {
$pfid = $eventpack->price_field_id;
if (!empty($sels[$pfid])) {
$defaults['price_' . $price_field_id][$v1['id']] = 1;
}
}
}
}
......@@ -165,6 +206,26 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
return;
}
// Before we add the events, clear the previous selection
// This makes it easier to remove items that were unselected by the user.
CRM_Boxoffice_BAO_Boxoffice::removePriceFieldSelection($eventpack->price_field_id);
// This will be useful later
$cart_url = CRM_Utils_System::url('civicrm/event/view_cart');
$pack_title = civicrm_api3('ContributionPage', 'getsingle', [
'id' => $contribution_page_id,
])['title'];
// User selected qty=0, so remove the eventpack
if (!$fields['quantity']) {
CRM_Utils_System::setUFMessage(ts("%1 has been removed from your cart. <a href='%2'>View your cart.</a>", [
1 => $pack_title,
2 => $cart_url,
]));
CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']);
}
// Fetch the price fields from the form, so that we can see which were selected
$form_price_fields = self::getPriceSetFields($fields['priceSetId'], $fields);
......@@ -193,11 +254,8 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
return;
}
// Before we add the events, clear the previous selection
// This makes it easier to remove items that were unselected by the user.
CRM_Boxoffice_BAO_Boxoffice::removePriceFieldSelection($eventpack->price_field_id);
// Add the selected events to the cart
// This loops through the checkboxes on the contribution page.
foreach ($form_price_fields as $pf_id) {
foreach ($fields[$pf_id] as $pvo => $discard) {
// Fetch the Price Field Value Option (pvo) label, which we will match to an existing active event.
......@@ -214,39 +272,22 @@ class CRM_Eventpack_Contribute_Form_Contribution_Main {
throw new Exception("Could not find the event: " . $pv['label']);
}
// At this point, we only know the event_id and the quantity,
// and the eventpack config for this contribution tells us which
// price_field to use. We then use the 'count' (number of participants)
// to fetch the associated price_value_option.
// It should be the same for all events, because of fixed-pricing.
// eventpacks have a price_field_id associated with the contribution page
// and the "quantity" field is generated from the price field option IDs.
$quantity = $fields['quantity'];
$price_field_id = $eventpack->price_field_id;
$value = CRM_Core_DAO::singleValueQuery('select id from civicrm_price_field_value where price_field_id = %1 and count = %2', [
1 => [$price_field_id, 'Positive'],
2 => [$fields['quantity'], 'Positive'],
]);
if (!$value) {
throw new Exception("Could not find a civicrm_price_field_value for price_field_id $price_field_id and count {$fields['quantity']}");
}
self::addToCart($event_id, $price_field_id, $value);
self::addToCart($event_id, $price_field_id, $quantity);
}
}
if ($count_selected && empty($errors)) {
$url = CRM_Utils_System::url('civicrm/event/view_cart');
$pack_title = civicrm_api3('ContributionPage', 'getsingle', [
'id' => $contribution_page_id,
])['title'];
// FIXME: detect if adding or modifying
CRM_Utils_System::setUFMessage(ts("%1 has been added to your cart. <a href='%2'>View your cart.</a>", [
1 => $pack_title,
2 => $url,
2 => $cart_url,
]));
// CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/view_cart', "reset=1"));
CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']);
}
}
......
......@@ -16,6 +16,9 @@
var $label = $(this).parent();
$label.html($label.find('span'));
});
$('.crm-boxoffice-addtocart-section').prependTo('.crm-contribution-main-form-block');
$('#crm-submit-buttons').appendTo('.crm-boxoffice-addtocart-button-wrapper');
};
})(CRM.$, CRM._, CRM.ts('boxoffice'));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment