First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,170 @@
<?php
/**
* Class CRM_Event_Cart_Form_Cart
*/
class CRM_Event_Cart_Form_Cart extends CRM_Core_Form {
public $cart;
public $_action;
public $contact;
public $event_cart_id = NULL;
public $_mode;
public $participants;
public function preProcess() {
$this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
$this->_mode = 'live';
$this->loadCart();
$this->checkWaitingList();
$this->assignBillingType();
$event_titles = array();
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
$event_titles[] = $event_in_cart->event->title;
}
$this->description = ts("Online Registration for %1", array(1 => implode(", ", $event_titles)));
if (!isset($this->discounts)) {
$this->discounts = array();
}
}
public function loadCart() {
if ($this->event_cart_id == NULL) {
$this->cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
}
else {
$this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
}
$this->cart->load_associations();
$this->stub_out_and_inherit();
}
public function stub_out_and_inherit() {
$transaction = new CRM_Core_Transaction();
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
if (empty($event_in_cart->participants)) {
$participant_params = array(
'cart_id' => $this->cart->id,
'event_id' => $event_in_cart->event_id,
'contact_id' => self::find_or_create_contact($this->getContactID()),
);
$participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params);
$participant->save();
$event_in_cart->add_participant($participant);
}
$event_in_cart->save();
}
$transaction->commit();
}
public function checkWaitingList() {
foreach ($this->cart->events_in_carts as $event_in_cart) {
$empty_seats = $this->checkEventCapacity($event_in_cart->event_id);
if ($empty_seats === NULL) {
continue;
}
foreach ($event_in_cart->participants as $participant) {
$participant->must_wait = ($empty_seats <= 0);
$empty_seats--;
}
}
}
/**
* @param int $event_id
*
* @return bool|int|null|string
*/
public function checkEventCapacity($event_id) {
$empty_seats = CRM_Event_BAO_Participant::eventFull($event_id, TRUE);
if (is_numeric($empty_seats)) {
return $empty_seats;
}
if (is_string($empty_seats)) {
return 0;
}
else {
return NULL;
}
}
/**
* @return bool
*/
public static function is_administrator() {
global $user;
return CRM_Core_Permission::check('administer CiviCRM');
}
/**
* @return mixed
*/
public function getContactID() {
//XXX when do we query 'cid' ?
$tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
//check if this is a checksum authentication
$userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
if ($userChecksum) {
//check for anonymous user.
$validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
if ($validUser) {
return $tempID;
}
}
// check if the user is registered and we have a contact ID
$session = CRM_Core_Session::singleton();
return $session->get('userID');
}
/**
* @param $fields
*
* @return mixed|null
*/
public static function find_contact($fields) {
return CRM_Contact_BAO_Contact::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', array(), FALSE);
}
/**
* @param int $registeringContactID
* @param array $fields
*
* @return int|mixed|null
*/
public static function find_or_create_contact($registeringContactID = NULL, $fields = array()) {
$contact_id = self::find_contact($fields);
if ($contact_id) {
return $contact_id;
}
$contact_params = array(
'email-Primary' => CRM_Utils_Array::value('email', $fields, NULL),
'first_name' => CRM_Utils_Array::value('first_name', $fields, NULL),
'last_name' => CRM_Utils_Array::value('last_name', $fields, NULL),
'is_deleted' => CRM_Utils_Array::value('is_deleted', $fields, TRUE),
);
$no_fields = array();
$contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL);
if (!$contact_id) {
CRM_Core_Error::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
}
return $contact_id;
}
/**
* @param string $page_name
*
* @return mixed
*/
public function getValuesForPage($page_name) {
$container = $this->controller->container();
return $container['values'][$page_name];
}
}

View file

@ -0,0 +1,148 @@
<?php
/**
* Class CRM_Event_Cart_Form_Checkout_ConferenceEvents
*/
class CRM_Event_Cart_Form_Checkout_ConferenceEvents extends CRM_Event_Cart_Form_Cart {
public $conference_event = NULL;
public $events_by_slot = array();
public $main_participant = NULL;
public $contact_id = NULL;
public function preProcess() {
parent::preProcess();
$matches = array();
preg_match("/.*_(\d+)_(\d+)/", $this->getAttribute('name'), $matches);
$event_id = $matches[1];
$participant_id = $matches[2];
$event_in_cart = $this->cart->get_event_in_cart_by_event_id($event_id);
$this->conference_event = $event_in_cart->event;
$this->main_participant = $event_in_cart->get_participant_by_id($participant_id);
$this->contact_id = $this->main_participant->contact_id;
$this->main_participant->display_name = CRM_Contact_BAO_Contact::displayName($this->contact_id);
$events = new CRM_Event_BAO_Event();
$query = <<<EOS
SELECT
civicrm_event.*,
slot.label AS slot_label
FROM
civicrm_event
JOIN
civicrm_option_value slot ON civicrm_event.slot_label_id = slot.value
JOIN
civicrm_option_group og ON slot.option_group_id = og.id
WHERE
parent_event_id = {$this->conference_event->id}
AND civicrm_event.is_active = 1
AND COALESCE(civicrm_event.is_template, 0) = 0
AND og.name = 'conference_slot'
ORDER BY
slot.weight, start_date
EOS;
$events->query($query);
while ($events->fetch()) {
if (!array_key_exists($events->slot_label, $this->events_by_slot)) {
$this->events_by_slot[$events->slot_label] = array();
}
$this->events_by_slot[$events->slot_label][] = clone($events);
}
}
public function buildQuickForm() {
//drupal_add_css(drupal_get_path('module', 'jquery_ui') . '/jquery.ui/themes/base/jquery-ui.css');
//variable_set('jquery_update_compression_type', 'none');
//jquery_ui_add('ui.dialog');
$slot_index = -1;
$slot_fields = array();
$session_options = array();
$defaults = array();
$previous_event_choices = $this->cart->get_subparticipants($this->main_participant);
foreach ($this->events_by_slot as $slot_name => $events) {
$slot_index++;
$slot_buttons = array();
$group_name = "slot_$slot_index";
foreach ($events as $event) {
$seats_available = $this->checkEventCapacity($event->id);
$event_is_full = ($seats_available === NULL) ? FALSE : ($seats_available < 1);
$radio = $this->createElement('radio', NULL, NULL, $event->title, $event->id);
$slot_buttons[] = $radio;
$event_description = ($event_is_full ? $event->event_full_text . "<p>" : '') . $event->description;
$session_options[$radio->getAttribute('id')] = array(
'session_title' => $event->title,
'session_description' => $event_description,
'session_full' => $event_is_full,
'event_id' => $event->id,
);
foreach ($previous_event_choices as $choice) {
if ($choice->event_id == $event->id) {
$defaults[$group_name] = $event->id;
}
}
}
$this->addGroup($slot_buttons, $group_name, $slot_name);
$slot_fields[$slot_name] = $group_name;
if (!isset($defaults[$group_name])) {
$defaults[$group_name] = $events[0]->id;
}
}
$this->setDefaults($defaults);
$this->assign('mer_participant', $this->main_participant);
$this->assign('events_by_slot', $this->events_by_slot);
$this->assign('slot_fields', $slot_fields);
$this->assign('session_options', json_encode($session_options));
$buttons = array();
$buttons[] = array(
'name' => ts('Go Back'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
'type' => 'back',
);
$buttons[] = array(
'isDefault' => TRUE,
'name' => ts('Continue'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'type' => 'next',
);
$this->addButtons($buttons);
}
public function postProcess() {
$params = $this->controller->exportValues($this->_name);
$main_event_in_cart = $this->cart->get_event_in_cart_by_event_id($this->conference_event->id);
foreach ($this->cart->events_in_carts as $event_in_cart) {
if ($event_in_cart->event->parent_event_id == $this->conference_event->id) {
$event_in_cart->remove_participant_by_contact_id($this->contact_id);
if (empty($event_in_cart->participants)) {
$this->cart->remove_event_in_cart($event_in_cart->id);
}
}
}
$slot_index = -1;
foreach ($this->events_by_slot as $slot_name => $events) {
$slot_index++;
$field_name = "slot_$slot_index";
$session_event_id = CRM_Utils_Array::value($field_name, $params, NULL);
if (!$session_event_id) {
continue;
}
$event_in_cart = $this->cart->add_event($session_event_id);
$values = array();
CRM_Core_DAO::storeValues($this->main_participant, $values);
$values['id'] = NULL;
$values['event_id'] = $event_in_cart->event_id;
$participant = CRM_Event_Cart_BAO_MerParticipant::create($values);
$participant->save();
$event_in_cart->add_participant($participant);
}
$this->cart->save();
}
}

View file

@ -0,0 +1,305 @@
<?php
/**
* Class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices
*/
class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_Form_Cart {
public $price_fields_for_event;
public $_values = NULL;
/**
* Pre process function.
*/
public function preProcess() {
parent::preProcess();
$this->cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
if (!isset($this->cid) || $this->cid > 0) {
//TODO users with permission can default to another contact
$this->cid = self::getContactID();
}
}
/**
* Build quick form.
*/
public function buildQuickForm() {
$this->price_fields_for_event = array();
foreach ($this->cart->get_main_event_participants() as $participant) {
$form = new CRM_Event_Cart_Form_MerParticipant($participant);
$form->appendQuickForm($this);
}
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
$this->price_fields_for_event[$event_in_cart->event_id] = $this->build_price_options($event_in_cart->event);
}
//If events in cart have discounts the textbox for discount code will be displayed at the top, as long as this
//form name is added to cividiscount
$this->assign('events_in_carts', $this->cart->get_main_events_in_carts());
$this->assign('price_fields_for_event', $this->price_fields_for_event);
$this->addButtons(
array(
array(
'type' => 'upload',
'name' => ts('Continue'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE,
),
)
);
if ($this->cid) {
$params = array('id' => $this->cid);
$contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
$contact_values = array();
CRM_Core_DAO::storeValues($contact, $contact_values);
$this->assign('contact', $contact_values);
}
}
/**
* Get the primary emil for the contact.
*
* @param CRM_Contact_BAO_Contact $contact
*
* @return string
*/
public static function primary_email_from_contact($contact) {
foreach ($contact->email as $email) {
if ($email['is_primary']) {
return $email['email'];
}
}
return NULL;
}
/**
* Build price options.
*
* @param CRM_Event_BAO_Event $event
*
* @return array
*/
public function build_price_options($event) {
$price_fields_for_event = array();
$base_field_name = "event_{$event->id}_amount";
$price_set_id = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $event->id);
//CRM-14492 display admin fields only if user is admin
$adminFieldVisible = FALSE;
if (CRM_Core_Permission::check('administer CiviCRM')) {
$adminFieldVisible = TRUE;
}
if ($price_set_id) {
$price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE, TRUE);
$price_set = $price_sets[$price_set_id];
$index = -1;
foreach ($price_set['fields'] as $field) {
$index++;
if (CRM_Utils_Array::value('visibility', $field) == 'public' ||
(CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE)) {
$field_name = "event_{$event->id}_price_{$field['id']}";
if (!empty($field['options'])) {
CRM_Price_BAO_PriceField::addQuickFormElement($this, $field_name, $field['id'], FALSE);
$price_fields_for_event[] = $field_name;
}
}
}
}
return $price_fields_for_event;
}
/**
* Validate values.
*
* @return bool
*/
public function validate() {
parent::validate();
if ($this->_errors) {
return FALSE;
}
$this->cart->load_associations();
$fields = $this->_submitValues;
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
$price_set_id = CRM_Event_BAO_Event::usesPriceSet($event_in_cart->event_id);
if ($price_set_id) {
$priceField = new CRM_Price_DAO_PriceField();
$priceField->price_set_id = $price_set_id;
$priceField->find();
$check = array();
while ($priceField->fetch()) {
if (!empty($fields["event_{$event_in_cart->event_id}_price_{$priceField->id}"])) {
$check[] = $priceField->id;
}
}
//XXX
if (empty($check)) {
$this->_errors['_qf_default'] = ts("Select at least one option from Price Levels.");
}
$lineItem = array();
if (is_array($this->_values['fee']['fields'])) {
CRM_Price_BAO_PriceSet::processAmount($this->_values['fee']['fields'], $fields, $lineItem);
//XXX total...
if ($fields['amount'] < 0) {
$this->_errors['_qf_default'] = ts("Price Levels can not be less than zero. Please select the options accordingly");
}
}
}
foreach ($event_in_cart->participants as $mer_participant) {
$participant_fields = $fields['event'][$event_in_cart->event_id]['participant'][$mer_participant->id];
//TODO what to do when profile responses differ for the same contact?
$contact_id = self::find_contact($participant_fields);
if ($contact_id) {
$participant = new CRM_Event_BAO_Participant();
$participant->event_id = $event_in_cart->event_id;
$participant->contact_id = $contact_id;
$statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
$participant->find();
while ($participant->fetch()) {
if (array_key_exists($participant->status_id, $statusTypes)) {
$form = $mer_participant->get_form();
$this->_errors[$form->html_field_name('email')] = ts("The participant %1 is already registered for %2 (%3).", array(
1 => $participant_fields['email'],
2 => $event_in_cart->event->title,
3 => $event_in_cart->event->start_date,
));
}
}
}
}
}
return empty($this->_errors);
}
/**
* Set default values.
*
* @return array
*/
public function setDefaultValues() {
$this->loadCart();
$defaults = array();
foreach ($this->cart->get_main_event_participants() as $participant) {
$form = $participant->get_form();
if (empty($participant->email)
&& !CRM_Event_Cart_Form_Cart::is_administrator()
&& ($participant->get_participant_index() == 1)
&& ($this->cid != 0)
) {
$defaults = array();
$params = array('id' => $this->cid);
$contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
$participant->contact_id = $this->cid;
$participant->save();
$participant->email = self::primary_email_from_contact($contact);
}
elseif ($this->cid == 0
&& $participant->contact_id == self::getContactID()
) {
$participant->email = NULL;
$participant->contact_id = self::find_or_create_contact($this->getContactID());
}
$defaults += $form->setDefaultValues();
//Set price defaults if any
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
$event_id = $event_in_cart->event_id;
$price_set_id = CRM_Event_BAO_Event::usesPriceSet($event_in_cart->event_id);
if ($price_set_id) {
$price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE, TRUE);
$price_set = $price_sets[$price_set_id];
foreach ($price_set['fields'] as $field) {
$options = CRM_Utils_Array::value('options', $field);
if (!is_array($options)) {
continue;
}
$field_name = "event_{$event_id}_price_{$field['id']}";
foreach ($options as $value) {
if ($value['is_default']) {
if ($field['html_type'] == 'Checkbox') {
$defaults[$field_name] = 1;
}
else {
$defaults[$field_name] = $value['id'];
}
}
}
}
}
}
}
return $defaults;
}
/**
* Post process function.
*/
public function postProcess() {
if (!array_key_exists('event', $this->_submitValues)) {
return;
}
// XXX de facto primary key
$email_to_contact_id = array();
foreach ($this->_submitValues['event'] as $event_id => $participants) {
foreach ($participants['participant'] as $participant_id => $fields) {
if (array_key_exists($fields['email'], $email_to_contact_id)) {
$contact_id = $email_to_contact_id[$fields['email']];
}
else {
$contact_id = self::find_or_create_contact($this->getContactID(), $fields);
$email_to_contact_id[$fields['email']] = $contact_id;
}
$participant = $this->cart->get_event_in_cart_by_event_id($event_id)->get_participant_by_id($participant_id);
if ($participant->contact_id && $contact_id != $participant->contact_id) {
$defaults = array();
$params = array('id' => $participant->contact_id);
$temporary_contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
foreach ($this->cart->get_subparticipants($participant) as $subparticipant) {
$subparticipant->contact_id = $contact_id;
$subparticipant->save();
}
$participant->contact_id = $contact_id;
$participant->save();
if ($temporary_contact->is_deleted) {
// ARGH a permissions check prevents us from using skipUndelete,
// so we potentially leave records pointing to this contact for now
// CRM_Contact_BAO_Contact::deleteContact($temporary_contact->id);
$temporary_contact->delete();
}
}
//TODO security check that participant ids are already in this cart
$participant_params = array(
'id' => $participant_id,
'cart_id' => $this->cart->id,
'event_id' => $event_id,
'contact_id' => $contact_id,
//'registered_by_id' => $this->cart->user_id,
'email' => $fields['email'],
);
$participant = new CRM_Event_Cart_BAO_MerParticipant($participant_params);
$participant->save();
$this->cart->add_participant_to_cart($participant);
if (array_key_exists('field', $this->_submitValues) && array_key_exists($participant_id, $this->_submitValues['field'])) {
$custom_fields = array_merge($participant->get_form()->get_participant_custom_data_fields());
CRM_Contact_BAO_Contact::createProfileContact($this->_submitValues['field'][$participant_id], $custom_fields, $contact_id);
}
}
}
$this->cart->save();
}
}

View file

@ -0,0 +1,836 @@
<?php
/**
* Class CRM_Event_Cart_Form_Checkout_Payment
*/
class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart {
public $all_participants;
public $financial_type_id;
public $description;
public $line_items;
public $_fields = array();
public $_paymentProcessor;
public $total;
public $sub_total;
public $payment_required = TRUE;
public $payer_contact_id;
public $is_pay_later = FALSE;
public $pay_later_receipt;
/**
* Register a participant.
*
* @param array $params
* @param CRM_Event_BAO_Participant $participant
* @param CRM_Event_BAO_Event $event
*
* @return mixed
*/
public function registerParticipant($params, &$participant, $event) {
$transaction = new CRM_Core_Transaction();
// handle register date CRM-4320
$registerDate = date('YmdHis');
$participantParams = array(
'id' => $participant->id,
'event_id' => $event->id,
'register_date' => $registerDate,
'source' => CRM_Utils_Array::value('participant_source', $params, $this->description),
//'fee_level' => $participant->fee_level,
'is_pay_later' => $this->is_pay_later,
'fee_amount' => CRM_Utils_Array::value('amount', $params, 0),
//XXX why is this a ref to participant and not contact?:
//'registered_by_id' => $this->payer_contact_id,
'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
);
if ($participant->must_wait) {
$participant_status = 'On waitlist';
}
elseif (CRM_Utils_Array::value('is_pay_later', $params, FALSE)) {
$participant_status = 'Pending from pay later';
}
else {
$participant_status = 'Registered';
}
$participant_statuses = CRM_Event_PseudoConstant::participantStatus();
$participantParams['status_id'] = array_search($participant_status, $participant_statuses);
$participant_status_label = CRM_Utils_Array::value($participantParams['status_id'], CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'));
$participantParams['participant_status'] = $participant_status_label;
$this->assign('isOnWaitlist', $participant->must_wait);
if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
$participantParams['is_test'] = 1;
}
else {
$participantParams['is_test'] = 0;
}
if (self::is_administrator()) {
if (!empty($params['note'])) {
$note_params = array(
'participant_id' => $participant->id,
'contact_id' => self::getContactID(),
'note' => $params['note'],
);
CRM_Event_BAO_Participant::update_note($note_params);
}
}
$participant->copyValues($participantParams);
$participant->save();
if (!empty($params['contributionID'])) {
$payment_params = array(
'participant_id' => $participant->id,
'contribution_id' => $params['contributionID'],
);
$ids = array();
CRM_Event_BAO_ParticipantPayment::create($payment_params, $ids);
}
$transaction->commit();
$event_values = array();
CRM_Core_DAO::storeValues($event, $event_values);
$location = array();
if (CRM_Utils_Array::value('is_show_location', $event_values) == 1) {
$locationParams = array(
'entity_id' => $participant->event_id,
'entity_table' => 'civicrm_event',
);
$location = CRM_Core_BAO_Location::getValues($locationParams, TRUE);
CRM_Core_BAO_Address::fixAddress($location['address'][1]);
}
list($pre_id, $post_id) = CRM_Event_Cart_Form_MerParticipant::get_profile_groups($participant->event_id);
$payer_values = array(
'email' => '',
'name' => '',
);
if ($this->payer_contact_id) {
$payer_contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
$payer_values = array(
'email' => $payer_contact_details[1],
'name' => $payer_contact_details[0],
);
}
$values = array(
'params' => array($participant->id => $participantParams),
'event' => $event_values,
'location' => $location,
'custom_pre_id' => $pre_id,
'custom_post_id' => $post_id,
'payer' => $payer_values,
);
CRM_Event_BAO_Event::sendMail($participant->contact_id, $values, $participant->id);
return $participant;
}
/**
* Build payment fields.
*/
public function buildPaymentFields() {
$payment_processor_id = NULL;
$can_pay_later = TRUE;
$pay_later_text = "";
$this->pay_later_receipt = "";
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) {
$payment_processor_id = $event_in_cart->event->payment_processor;
$this->financial_type_id = $event_in_cart->event->financial_type_id;
}
else {
if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) {
CRM_Core_Error::statusBounce(ts('When registering for multiple events all events must use the same payment processor. '));
}
}
if (!$event_in_cart->event->is_pay_later) {
$can_pay_later = FALSE;
}
else {
//XXX
$pay_later_text = $event_in_cart->event->pay_later_text;
$this->pay_later_receipt = $event_in_cart->event->pay_later_receipt;
}
}
if ($payment_processor_id == NULL) {
CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).'));
}
$this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, $this->_mode);
$this->assign('paymentProcessor', $this->_paymentProcessor);
CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, FALSE);
if ($can_pay_later || self::is_administrator()) {
$this->addElement('checkbox', 'is_pay_later',
$pay_later_text
);
$this->addElement('checkbox', 'payment_completed',
ts('Payment Completed')
);
$this->assign('pay_later_instructions', $this->pay_later_receipt);
}
}
/**
* Build QuickForm.
*/
public function buildQuickForm() {
$this->line_items = array();
$this->sub_total = 0;
$this->_price_values = $this->getValuesForPage('ParticipantsAndPrices');
// iterate over each event in cart
foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
$this->process_event_line_item($event_in_cart);
foreach ($this->cart->get_events_in_carts_by_main_event_id($event_in_cart->event_id) as $subevent) {
$this->process_event_line_item($subevent, 'subevent');
}
}
$this->total = $this->sub_total;
$this->payment_required = ($this->total > 0);
$this->assign('payment_required', $this->payment_required);
$this->assign('line_items', $this->line_items);
$this->assign('sub_total', $this->sub_total);
$this->assign('total', $this->total);
$buttons = array();
$buttons[] = array(
'name' => ts('Go Back'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
'type' => 'back',
);
$buttons[] = array(
'isDefault' => TRUE,
'name' => ts('Complete Transaction'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'type' => 'next',
);
if ($this->total) {
$this->add('text', 'billing_contact_email', 'Billing Email', '', TRUE);
$this->assign('collect_billing_email', TRUE);
}
if (self::is_administrator()) {
$this->add('textarea', 'note', 'Note');
$this->add('text', 'source', 'Source', array('size' => 80));
$instruments = array();
CRM_Core_OptionGroup::getAssoc('payment_instrument', $instruments, TRUE);
$options = array();
foreach ($instruments as $type) {
$options[] = $this->createElement('radio', NULL, '', $type['label'], $type['value']);
}
$this->addGroup($options, 'payment_type', ts("Alternative Payment Type"));
$this->add('text', 'check_number', ts('Check No.'), array('size' => 20));
$this->addElement('checkbox', 'is_pending', ts('Create a pending registration'));
$this->assign('administrator', TRUE);
}
$this->addButtons($buttons);
$this->addFormRule(array('CRM_Event_Cart_Form_Checkout_Payment', 'formRule'), $this);
if ($this->payment_required) {
$this->buildPaymentFields();
}
}
/**
* Process line item for event.
*
* @param bool $event_in_cart
* @param string $class
*/
public function process_event_line_item(&$event_in_cart, $class = NULL) {
$cost = 0;
$price_set_id = CRM_Price_BAO_PriceSet::getFor("civicrm_event", $event_in_cart->event_id);
$amount_level = NULL;
if ($price_set_id) {
$event_price_values = array();
foreach ($this->_price_values as $key => $value) {
if (preg_match("/event_{$event_in_cart->event_id}_(price.*)/", $key, $matches)) {
$event_price_values[$matches[1]] = $value;
}
}
$price_sets = CRM_Price_BAO_PriceSet::getSetDetail($price_set_id, TRUE);
$price_set = $price_sets[$price_set_id];
$price_set_amount = array();
CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount);
$discountCode = $this->_price_values['discountcode'];
if (!empty($discountCode)) {
$ret = $this->apply_discount($discountCode, $price_set_amount, $cost, $event_in_cart->event_id);
if ($ret == FALSE) {
$cost = $event_price_values['amount'];
}
}
else {
$cost = $event_price_values['amount'];
}
// @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
// function to get correct amount level consistently. Remove setting of the amount level in
// CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
// to cover all variants.
$amount_level = $event_price_values['amount_level'];
$price_details[$price_set_id] = $price_set_amount;
}
// iterate over each participant in event
foreach ($event_in_cart->participants as & $participant) {
$participant->cost = $cost;
$participant->fee_level = $amount_level;
$participant->price_details = $price_details;
}
$this->add_line_item($event_in_cart, $class);
}
/**
* Add line item.
*
* @param CRM_Event_BAO_Event $event_in_cart
* @param string $class
*/
public function add_line_item($event_in_cart, $class = NULL) {
$amount = 0;
$cost = 0;
$not_waiting_participants = array();
foreach ($event_in_cart->not_waiting_participants() as $participant) {
$amount += $participant->cost;
$cost = max($cost, $participant->cost);
$not_waiting_participants[] = array(
'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
);
}
$waiting_participants = array();
foreach ($event_in_cart->waiting_participants() as $participant) {
$waiting_participants[] = array(
'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
);
}
$this->line_items[] = array(
'amount' => $amount,
'cost' => $cost,
'event' => $event_in_cart->event,
'participants' => $not_waiting_participants,
'num_participants' => count($not_waiting_participants),
'num_waiting_participants' => count($waiting_participants),
'waiting_participants' => $waiting_participants,
'class' => $class,
);
$this->sub_total += $amount;
}
/**
* Get default from address.
*
* @return mixed
*/
public function getDefaultFrom() {
$values = CRM_Core_OptionGroup::values('from_email_address');
return $values[1];
}
/**
* Send email receipt.
*
* @param array $events_in_cart
* @param array $params
*/
public function emailReceipt($events_in_cart, $params) {
$contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->payer_contact_id);
$state_province = new CRM_Core_DAO_StateProvince();
$state_province->id = $params["billing_state_province_id-{$this->_bltID}"];
$state_province->find();
$state_province->fetch();
$country = new CRM_Core_DAO_Country();
$country->id = $params["billing_country_id-{$this->_bltID}"];
$country->find();
$country->fetch();
foreach ($this->line_items as & $line_item) {
$location_params = array('entity_id' => $line_item['event']->id, 'entity_table' => 'civicrm_event');
$line_item['location'] = CRM_Core_BAO_Location::getValues($location_params, TRUE);
CRM_Core_BAO_Address::fixAddress($line_item['location']['address'][1]);
}
$send_template_params = array(
'table' => 'civicrm_msg_template',
'contactId' => $this->payer_contact_id,
'from' => $this->getDefaultFrom(),
'groupName' => 'msg_tpl_workflow_event',
'isTest' => FALSE,
'toEmail' => $contact_details[1],
'toName' => $contact_details[0],
'tplParams' => array(
'billing_name' => "{$params['billing_first_name']} {$params['billing_last_name']}",
'billing_city' => $params["billing_city-{$this->_bltID}"],
'billing_country' => $country->name,
'billing_postal_code' => $params["billing_postal_code-{$this->_bltID}"],
'billing_state' => $state_province->abbreviation,
'billing_street_address' => $params["billing_street_address-{$this->_bltID}"],
'credit_card_exp_date' => $params['credit_card_exp_date'],
'credit_card_type' => $params['credit_card_type'],
'credit_card_number' => "************" . substr($params['credit_card_number'], -4, 4),
// XXX cart->get_discounts
'discounts' => $this->discounts,
'email' => $contact_details[1],
'events_in_cart' => $events_in_cart,
'line_items' => $this->line_items,
'name' => $contact_details[0],
'transaction_id' => $params['trxn_id'],
'transaction_date' => $params['trxn_date'],
'is_pay_later' => $this->is_pay_later,
'pay_later_receipt' => $this->pay_later_receipt,
),
'valueName' => 'event_registration_receipt',
'PDFFilename' => ts('confirmation') . '.pdf',
);
$template_params_to_copy = array(
'billing_name',
'billing_city',
'billing_country',
'billing_postal_code',
'billing_state',
'billing_street_address',
'credit_card_exp_date',
'credit_card_type',
'credit_card_number',
);
foreach ($template_params_to_copy as $template_param_to_copy) {
$this->set($template_param_to_copy, $send_template_params['tplParams'][$template_param_to_copy]);
}
CRM_Core_BAO_MessageTemplate::sendTemplate($send_template_params);
}
/**
* Apply form rules.
*
* @param array $fields
* @param array $files
* @param CRM_Core_Form $self
*
* @return array|bool
*/
public static function formRule($fields, $files, $self) {
$errors = array();
if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) {
CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
// validate payment instrument values (e.g. credit card number)
CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, NULL);
}
return empty($errors) ? TRUE : $errors;
}
/**
* Validate form.
*
* @todo this should surely go! Test & remove.
* @return bool
*/
public function validate() {
if ($this->is_pay_later) {
$this->_fields['credit_card_number']['is_required'] = FALSE;
$this->_fields['cvv2']['is_required'] = FALSE;
$this->_fields['credit_card_exp_date']['is_required'] = FALSE;
$this->_fields['credit_card_type']['is_required'] = FALSE;
}
return parent::validate();
}
/**
* Pre-process form.
*/
public function preProcess() {
$params = $this->_submitValues;
$this->is_pay_later = CRM_Utils_Array::value('is_pay_later', $params, FALSE) && !CRM_Utils_Array::value('payment_completed', $params);
parent::preProcess();
}
/**
* Post process form.
*/
public function postProcess() {
$transaction = new CRM_Core_Transaction();
$trxnDetails = NULL;
$params = $this->_submitValues;
$main_participants = $this->cart->get_main_event_participants();
foreach ($main_participants as $participant) {
$defaults = array();
$ids = array('contact_id' => $participant->contact_id);
$contact = CRM_Contact_BAO_Contact::retrieve($ids, $defaults);
$contact->is_deleted = 0;
$contact->save();
}
$trxn_prefix = 'VR';
if (array_key_exists('billing_contact_email', $params)) {
$this->payer_contact_id = self::find_or_create_contact($this->getContactID(), array(
'email' => $params['billing_contact_email'],
'first_name' => $params['billing_first_name'],
'last_name' => $params['billing_last_name'],
'is_deleted' => FALSE,
));
$ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
$this->payer_contact_id,
'contact_type'
);
$billing_fields = array(
"billing_first_name" => 1,
"billing_middle_name" => 1,
"billing_last_name" => 1,
"billing_street_address-{$this->_bltID}" => 1,
"billing_city-{$this->_bltID}" => 1,
"billing_state_province_id-{$this->_bltID}" => 1,
"billing_postal_code-{$this->_bltID}" => 1,
"billing_country_id-{$this->_bltID}" => 1,
"address_name-{$this->_bltID}" => 1,
"email-{$this->_bltID}" => 1,
);
$params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params);
$params["email-{$this->_bltID}"] = $params['billing_contact_email'];
CRM_Contact_BAO_Contact::createProfileContact(
$params,
$billing_fields,
$this->payer_contact_id,
NULL,
NULL,
$ctype,
TRUE
);
}
$params['now'] = date('YmdHis');
$params['invoiceID'] = md5(uniqid(rand(), TRUE));
$params['amount'] = $this->total;
$params['financial_type_id'] = $this->financial_type_id;
if ($this->payment_required && empty($params['is_pay_later'])) {
$trxnDetails = $this->make_payment($params);
$params['trxn_id'] = $trxnDetails['trxn_id'];
$params['trxn_date'] = $trxnDetails['trxn_date'];
$params['currencyID'] = $trxnDetails['currency'];
}
$this->cart->completed = TRUE;
$this->cart->save();
$this->set('last_event_cart_id', $this->cart->id);
$contribution_statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$params['payment_instrument_id'] = NULL;
if (!empty($params['is_pay_later'])) {
$params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Check', 'name');
$trxn_prefix = 'CK';
}
else {
$params['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', 'Credit Card', 'name');
}
if ($this->is_pay_later && empty($params['payment_completed'])) {
$params['contribution_status_id'] = array_search('Pending', $contribution_statuses);
}
else {
$params['contribution_status_id'] = array_search('Completed', $contribution_statuses);
$params['participant_status'] = 'Registered';
$params['is_pay_later'] = 0;
}
if ($trxnDetails == NULL) {
$params['trxn_id'] = $trxn_prefix . strftime("%Y%m%d%H%M%S");
$params['trxn_date'] = $params['now'];
}
if ($this->payment_required) {
$this->emailReceipt($this->cart->events_in_carts, $params);
}
// n.b. we need to process the subparticipants before main event
// participants so that session attendance can be included in the email
$main_participants = $this->cart->get_main_event_participants();
$this->all_participants = array();
foreach ($main_participants as $main_participant) {
$this->all_participants = array_merge($this->all_participants, $this->cart->get_subparticipants($main_participant));
}
$this->all_participants = array_merge($this->all_participants, $main_participants);
$this->sub_trxn_index = 0;
foreach ($this->all_participants as $mer_participant) {
$event_in_cart = $this->cart->get_event_in_cart_by_event_id($mer_participant->event_id);
$this->sub_trxn_index += 1;
unset($params['contributionID']);
if ($mer_participant->must_wait) {
$this->registerParticipant($params, $mer_participant, $event_in_cart->event);
}
else {
$params['amount'] = $mer_participant->cost - $mer_participant->discount_amount;
if ($event_in_cart->event->financial_type_id && $mer_participant->cost) {
$params['financial_type_id'] = $event_in_cart->event->financial_type_id;
$params['participant_contact_id'] = $mer_participant->contact_id;
$contribution = $this->record_contribution($mer_participant, $params, $event_in_cart->event);
// Record civicrm_line_item
CRM_Price_BAO_LineItem::processPriceSet($mer_participant->id, $mer_participant->price_details, $contribution, $entity_table = 'civicrm_participant');
}
$this->registerParticipant($params, $mer_participant, $event_in_cart->event);
}
}
$this->trxn_id = $params['trxn_id'];
$this->trxn_date = $params['trxn_date'];
$this->saveDataToSession();
$transaction->commit();
}
/**
* Make payment.
*
* @param array $params
*
* @return array
* @throws Exception
*/
public function make_payment(&$params) {
$config = CRM_Core_Config::singleton();
if (isset($params["billing_state_province_id-{$this->_bltID}"]) && $params["billing_state_province_id-{$this->_bltID}"]) {
$params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
}
if (isset($params["billing_country_id-{$this->_bltID}"]) && $params["billing_country_id-{$this->_bltID}"]) {
$params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
}
$params['ip_address'] = CRM_Utils_System::ipAddress();
$params['currencyID'] = $config->defaultCurrency;
$payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE);
$params['month'] = $params['credit_card_exp_date']['M'];
$params['year'] = $params['credit_card_exp_date']['Y'];
try {
$result = $payment->doPayment($params);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
CRM_Core_Error::displaySessionError($result);
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/cart_checkout', "_qf_Payment_display=1&qfKey={$this->controller->_key}", TRUE, NULL, FALSE));
}
$trxnDetails = array(
'trxn_id' => $result['trxn_id'],
'trxn_date' => $result['now'],
'currency' => CRM_Utils_Array::value('currencyID', $result),
);
return $trxnDetails;
}
/**
* Record contribution.
*
* @param CRM_Event_BAO_Participant $mer_participant
* @param array $params
* @param CRM_Event_BAO_Event $event
*
* @return object
* @throws Exception
*/
public function record_contribution(&$mer_participant, &$params, $event) {
if (self::is_administrator() && !empty($params['payment_type'])) {
$params['payment_instrument_id'] = $params['payment_type'];
}
if ($this->payer_contact_id) {
$payer = $this->payer_contact_id;
}
elseif (self::getContactID()) {
$payer = self::getContactID();
}
else {
$payer = $params['participant_contact_id'];
}
$contribParams = array(
'contact_id' => $payer,
'financial_type_id' => $params['financial_type_id'],
'receive_date' => $params['now'],
'total_amount' => $params['amount'],
'amount_level' => $mer_participant->fee_level,
'net_amount' => $params['amount'],
'invoice_id' => "{$params['invoiceID']}-{$this->sub_trxn_index}",
'trxn_id' => "{$params['trxn_id']}-{$this->sub_trxn_index}",
'currency' => CRM_Utils_Array::value('currencyID', $params),
'source' => $event->title,
'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
'contribution_status_id' => $params['contribution_status_id'],
'payment_instrument_id' => $params['payment_instrument_id'],
'check_number' => CRM_Utils_Array::value('check_number', $params),
'skipLineItem' => 1,
);
if (is_array($this->_paymentProcessor)) {
$contribParams['payment_processor'] = $this->_paymentProcessor['id'];
}
$contribution = &CRM_Contribute_BAO_Contribution::add($contribParams);
if (is_a($contribution, 'CRM_Core_Error')) {
CRM_Core_Error::fatal(ts("There was an error creating a contribution record for your event. Please report this error to the webmaster. Details: %1", array(1 => $contribution->getMessages($contribution))));
}
$mer_participant->contribution_id = $contribution->id;
$params['contributionID'] = $contribution->id;
return $contribution;
}
/**
* Save data to session.
*/
public function saveDataToSession() {
$session_line_items = array();
foreach ($this->line_items as $line_item) {
$session_line_item = array();
$session_line_item['amount'] = $line_item['amount'];
$session_line_item['cost'] = $line_item['cost'];
$session_line_item['event_id'] = $line_item['event']->id;
$session_line_items[] = $session_line_item;
}
$this->set('line_items', $session_line_items);
$this->set('payment_required', $this->payment_required);
$this->set('is_pay_later', $this->is_pay_later);
$this->set('pay_later_receipt', $this->pay_later_receipt);
$this->set('trxn_id', $this->trxn_id);
$this->set('trxn_date', $this->trxn_date);
$this->set('total', $this->total);
}
/**
* Set form default values.
*
* @return array
*/
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
$config = CRM_Core_Config::singleton();
$default_country = new CRM_Core_DAO_Country();
$default_country->iso_code = $config->defaultContactCountry();
$default_country->find(TRUE);
$defaults["billing_country_id-{$this->_bltID}"] = $default_country->id;
if (self::getContactID() && !self::is_administrator()) {
$params = array('id' => self::getContactID());
$contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
foreach ($contact->email as $email) {
if ($email['is_billing']) {
$defaults["billing_contact_email"] = $email['email'];
}
}
if (empty($defaults['billing_contact_email'])) {
foreach ($contact->email as $email) {
if ($email['is_primary']) {
$defaults["billing_contact_email"] = $email['email'];
}
}
}
$defaults["billing_first_name"] = $contact->first_name;
$defaults["billing_middle_name"] = $contact->middle_name;
$defaults["billing_last_name"] = $contact->last_name;
$billing_address = CRM_Event_Cart_BAO_MerParticipant::billing_address_from_contact($contact);
if ($billing_address != NULL) {
$defaults["billing_street_address-{$this->_bltID}"] = $billing_address['street_address'];
$defaults["billing_city-{$this->_bltID}"] = $billing_address['city'];
$defaults["billing_postal_code-{$this->_bltID}"] = $billing_address['postal_code'];
$defaults["billing_state_province_id-{$this->_bltID}"] = $billing_address['state_province_id'];
$defaults["billing_country_id-{$this->_bltID}"] = $billing_address['country_id'];
}
}
$defaults["source"] = $this->description;
return $defaults;
}
/**
* Apply discount.
*
* @param string $discountCode
* @param array $price_set_amount
* @param float $cost
* @param int $event_id
*
* @return bool
*/
protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) {
//need better way to determine if cividiscount installed
$autoDiscount = array();
$sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'";
$dao = CRM_Core_DAO::executeQuery($sql, '');
while ($dao->fetch()) {
if ($dao->is_active != '1') {
return FALSE;
}
}
$discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids();
$discounts = _cividiscount_get_discounts();
$stat = FALSE;
foreach ($discounts as $key => $discountValue) {
if ($key == $discountCode) {
$events = CRM_Utils_Array::value('events', $discountValue);
$evt_ids = implode(",", $events);
if ($evt_ids == "0" || strpos($evt_ids, $event_id)) {
$event_match = TRUE;
}
//check priceset is_active
if ($discountValue['active_on'] != NULL) {
$today = date('Y-m-d');
$diff1 = date_diff(date_create($today), date_create($discountValue['active_on']));
if ($diff1->days > 0) {
$active1 = TRUE;
}
}
else {
$active1 = TRUE;
}
if ($discountValue['expire_on'] != NULL) {
$diff2 = date_diff(date_create($today), date_create($discountValue['expire_on']));
if ($diff2->days > 0) {
$active2 = TRUE;
}
}
else {
$active2 = TRUE;
}
}
if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == TRUE && $event_match == TRUE) {
foreach ($price_set_amount as $key => $price) {
if (array_search($price['price_field_value_id'], $discounted_priceset_ids) != NULL) {
$discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, "USD");
$price_set_amount[$key]['line_total'] = $discounted[0];
$cost += $discounted[0];
$price_set_amount[$key]['label'] = $discounted[1];
}
else {
$cost += $price['line_total'];
}
}
$stat = TRUE;
}
}
return $stat;
}
}

View file

@ -0,0 +1,83 @@
<?php
/**
* Class CRM_Event_Cart_Form_Checkout_ThankYou
*/
class CRM_Event_Cart_Form_Checkout_ThankYou extends CRM_Event_Cart_Form_Cart {
public $line_items = NULL;
public $sub_total = 0;
public function buildLineItems() {
foreach ($this->cart->events_in_carts as $event_in_cart) {
$event_in_cart->load_location();
}
$line_items = $this->get('line_items');
foreach ($line_items as $line_item) {
$event_in_cart = $this->cart->get_event_in_cart_by_event_id($line_item['event_id']);
$not_waiting_participants = array();
foreach ($event_in_cart->not_waiting_participants() as $participant) {
$not_waiting_participants[] = array(
'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
);
}
$waiting_participants = array();
foreach ($event_in_cart->waiting_participants() as $participant) {
$waiting_participants[] = array(
'display_name' => CRM_Contact_BAO_Contact::displayName($participant->contact_id),
);
}
$line_item['event'] = $event_in_cart->event;
$line_item['num_participants'] = count($not_waiting_participants);
$line_item['participants'] = $not_waiting_participants;
$line_item['num_waiting_participants'] = count($waiting_participants);
$line_item['waiting_participants'] = $waiting_participants;
$line_item['location'] = $event_in_cart->location;
$line_item['class'] = $event_in_cart->event->parent_event_id ? 'subevent' : NULL;
$this->sub_total += $line_item['amount'];
$this->line_items[] = $line_item;
}
$this->assign('line_items', $this->line_items);
}
public function buildQuickForm() {
$defaults = array();
$ids = array();
$template_params_to_copy = array(
'billing_name',
'billing_city',
'billing_country',
'billing_postal_code',
'billing_state',
'billing_street_address',
'credit_card_exp_date',
'credit_card_type',
'credit_card_number',
);
foreach ($template_params_to_copy as $template_param_to_copy) {
$this->assign($template_param_to_copy, $this->get($template_param_to_copy));
}
$this->buildLineItems();
$this->assign('discounts', $this->get('discounts'));
$this->assign('events_in_carts', $this->cart->events_in_carts);
$this->assign('transaction_id', $this->get('trxn_id'));
$this->assign('transaction_date', $this->get('trxn_date'));
$this->assign('payment_required', $this->get('payment_required'));
$this->assign('is_pay_later', $this->get('is_pay_later'));
$this->assign('pay_later_receipt', $this->get('pay_later_receipt'));
$this->assign('sub_total', $this->sub_total);
$this->assign('total', $this->get('total'));
// XXX Configure yourself
//$this->assign( 'site_name', "" );
//$this->assign( 'site_contact', "" );
}
public function preProcess() {
$this->event_cart_id = $this->get('last_event_cart_id');
$this->loadCart();
//$this->loadParticipants( );
}
}

View file

@ -0,0 +1,137 @@
<?php
/**
* Class CRM_Event_Cart_Form_MerParticipant
*/
class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form {
public $participant = NULL;
/**
* @param null|object $participant
*/
public function __construct($participant) {
parent::__construct();
//XXX
$this->participant = $participant;
}
/**
* @param CRM_Core_Form $form
*/
public function appendQuickForm(&$form) {
$textarea_size = array('size' => 30, 'maxlength' => 60);
$form->add('text', $this->email_field_name(), ts('Email Address'), $textarea_size, TRUE);
list(
$custom_fields_pre,
$custom_fields_post
) = $this->get_participant_custom_data_fields($this->participant->event_id);
foreach ($custom_fields_pre as $key => $field) {
CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, $this->participant->id);
}
foreach ($custom_fields_post as $key => $field) {
CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, $this->participant->id);
}
$custom = CRM_Utils_Array::value('custom', $form->getTemplate()->_tpl_vars, array());
$form->assign('custom', array_merge($custom, array(
$this->html_field_name('customPre') => $custom_fields_pre,
$this->html_field_name('customPost') => $custom_fields_post,
$this->html_field_name('number') => $this->name(),
)));
}
/**
* @param int $event_id
*
* @return array
*/
public static function get_profile_groups($event_id) {
$ufJoinParams = array(
'entity_table' => 'civicrm_event',
'module' => 'CiviEvent',
'entity_id' => $event_id,
);
$group_ids = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
return $group_ids;
}
/**
* @return array
*/
public function get_participant_custom_data_fields() {
list($custom_pre_id, $custom_post_id) = self::get_profile_groups($this->participant->event_id);
$pre_fields = $post_fields = array();
if ($custom_pre_id && CRM_Core_BAO_UFGroup::filterUFGroups($custom_pre_id, $this->participant->contact_id)) {
$pre_fields = CRM_Core_BAO_UFGroup::getFields($custom_pre_id, FALSE, CRM_Core_Action::ADD);
}
if ($custom_post_id && CRM_Core_BAO_UFGroup::filterUFGroups($custom_post_id, $this->participant->contact_id)) {
$post_fields = CRM_Core_BAO_UFGroup::getFields($custom_post_id, FALSE, CRM_Core_Action::ADD);
}
return array($pre_fields, $post_fields);
}
/**
* @return string
*/
public function email_field_name() {
return $this->html_field_name("email");
}
/**
* @param int $event_id
* @param int $participant_id
* @param string $field_name
*
* @return string
*/
public static function full_field_name($event_id, $participant_id, $field_name) {
return "event[$event_id][participant][$participant_id][$field_name]";
}
/**
* @param string $field_name
*
* @return string
*/
public function html_field_name($field_name) {
return self::full_field_name($this->participant->event_id, $this->participant->id, $field_name);
}
/**
* @return string
*/
public function name() {
return "Participant {$this->participant->get_participant_index()}";
}
/**
* XXX poor name.
* @param $participant
*
* @return CRM_Event_Cart_Form_MerParticipant
*/
static public function get_form($participant) {
return new CRM_Event_Cart_Form_MerParticipant($participant);
}
/**
* @return array
*/
public function setDefaultValues() {
$defaults = array(
$this->html_field_name('email') => $this->participant->email,
);
list($custom_fields_pre, $custom_fields_post) = $this->get_participant_custom_data_fields($this->participant->event_id);
$all_fields = $custom_fields_pre + $custom_fields_post;
$flat = array();
CRM_Core_BAO_UFGroup::setProfileDefaults($this->participant->contact_id, $all_fields, $flat);
foreach ($flat as $name => $field) {
$defaults["field[{$this->participant->id}][{$name}]"] = $field;
}
return $defaults;
}
}