Skip to content

Commit

Permalink
Convert event cart to use standard payment forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jun 2, 2021
1 parent d17a1f0 commit 7e14a75
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 745 deletions.
57 changes: 46 additions & 11 deletions ext/eventcart/CRM/Event/Cart/BAO/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class CRM_Event_Cart_BAO_Cart extends CRM_Event_Cart_DAO_Cart {
*/
public $events_in_carts = [];

/**
* The default contact ID to use when creating a participant
* @var int
*/
public $defaultParticipantContactID = NULL;

/**
* @param array $params
*
Expand Down Expand Up @@ -39,21 +45,22 @@ public function add_event($event_id) {
'event_id' => $event_id,
'event_cart_id' => $this->id,
];
/** @var \CRM_Event_Cart_BAO_EventInCart $event_in_cart */
$event_in_cart = CRM_Event_Cart_BAO_EventInCart::create($params);
$event_in_cart->load_associations($this);
$this->events_in_carts[$event_in_cart->event_id] = $event_in_cart;
return $this->events_in_carts[$event_in_cart->event_id];
}

/**
* @param $participant
* @param array $participantParams
*/
public function add_participant_to_cart($participant) {
$event_in_cart = $this->get_event_in_cart_by_event_id($participant->event_id);
public function add_participant_to_cart($participantParams) {
$event_in_cart = $this->get_event_in_cart_by_event_id($participantParams['event_id']);
if (!$event_in_cart) {
$event_in_cart = $this->add_event($participant->event_id);
$event_in_cart = $this->add_event($participantParams['event_id']);
}
$event_in_cart->add_participant($participant);
$event_in_cart->add_participant($participantParams);
$event_in_cart->save();
}

Expand Down Expand Up @@ -109,17 +116,16 @@ public static function find_by_params($params) {
public static function find_or_create_for_current_session() {
$session = CRM_Core_Session::singleton();
$event_cart_id = $session->get('event_cart_id');
$userID = $session->get('userID');
$userID = CRM_Core_Session::getLoggedInContactID();
$cart = FALSE;
if (!is_null($event_cart_id)) {
if (!empty($event_cart_id)) {
$cart = self::find_uncompleted_by_id($event_cart_id);
if ($cart && $userID) {
if (!$cart->user_id) {
$saved_cart = self::find_uncompleted_by_user_id($userID);
if ($saved_cart) {
$cart->adopt_participants($saved_cart->id);
$saved_cart->delete();
$cart->load_associations();
}
else {
$cart->user_id = $userID;
Expand All @@ -140,6 +146,8 @@ public static function find_or_create_for_current_session() {
}
$session->set('event_cart_id', $cart->id);
}
$cart->defaultParticipantContactID = CRM_Utils_Request::retrieveValue('cid', 'Positive', CRM_Core_Session::getLoggedInContactID());
$cart->load_associations();
return $cart;
}

Expand All @@ -165,10 +173,10 @@ public static function find_uncompleted_by_user_id($user_id) {
* @return array
*/
public function get_main_events_in_carts() {
//return CRM_Event_Cart_BAO_EventInCart::find_all_by_params( array('main_conference_event_id'
$all = [];
/** @var \CRM_Event_Cart_BAO_EventInCart $event_in_cart */
foreach ($this->events_in_carts as $event_in_cart) {
if (!$event_in_cart->is_child_event()) {
if (!$event_in_cart->is_child_event($event_in_cart->event_id)) {
$all[] = $event_in_cart;
}
}
Expand Down Expand Up @@ -219,6 +227,7 @@ public static function compare_event_dates($event_in_cart_1, $event_in_cart_2) {
*/
public function get_subparticipants($main_participant) {
$subparticipants = [];
/** @var \CRM_Event_Cart_BAO_EventInCart $event_in_cart */
foreach ($this->events_in_carts as $event_in_cart) {
if ($event_in_cart->is_child_event($main_participant->event_id)) {
foreach ($event_in_cart->participants as $participant) {
Expand Down Expand Up @@ -246,7 +255,7 @@ public function get_event_in_cart_by_event_id($event_id) {
*
* @return null
*/
public function &get_event_in_cart_by_id($event_in_cart_id) {
public function get_event_in_cart_by_id($event_in_cart_id) {
foreach ($this->events_in_carts as $event_in_cart) {
if ($event_in_cart->id == $event_in_cart_id) {
return $event_in_cart;
Expand All @@ -272,6 +281,7 @@ public function load_associations() {
}
$this->associations_loaded = TRUE;
$this->events_in_carts = CRM_Event_Cart_BAO_EventInCart::find_all_by_event_cart_id($this->id);
/** @var \CRM_Event_Cart_BAO_EventInCart $event_in_cart */
foreach ($this->events_in_carts as $event_in_cart) {
$event_in_cart->load_associations($this);
}
Expand Down Expand Up @@ -343,4 +353,29 @@ public function adopt_participants($from_cart_id) {
CRM_Core_DAO::executeQuery($sql, $params);
}

/**
* Get payment processors.
*
* This differs from the option value in that we append description for
* disambiguation.
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public static function getPaymentProcessors(): array {
$results = civicrm_api3('PaymentProcessor', 'get', [
'is_test' => 0,
'return' => ['id', 'name', 'description', 'domain_id'],
]);

$processors = [];
foreach ($results['values'] as $processorID => $details) {
$processors[$processorID] = $details['name'];
if (!empty($details['description'])) {
$processors[$processorID] .= ' : ' . $details['description'];
}
}
return $processors;
}

}
45 changes: 29 additions & 16 deletions ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart impl
public $event;
public $event_cart;
public $location = NULL;

/**
* Array of CRM_Event_Cart_BAO_MerParticipant indexed by participant ID
*
* @var array
*/
public $participants = [];

/**
Expand All @@ -20,10 +26,14 @@ public function __construct() {
/**
* Add participant to cart.
*
* @param $participant
* @param array $participantParams
*/
public function add_participant($participant) {
$this->participants[$participant->id] = $participant;
public function add_participant($participantParams) {
$participantParams['cart_id'] = $participantParams['cart_id'] ?? $this->event_cart_id;
$participantParams['event_id'] = $participantParams['event_id'] ?? $this->event_id;

$merParticipantObject = CRM_Event_Cart_BAO_MerParticipant::create($participantParams);
$this->participants[$merParticipantObject->id] = $merParticipantObject;
}

/**
Expand Down Expand Up @@ -164,7 +174,7 @@ public static function part_key($participant) {
}

/**
* @param null $event_cart
* @param CRM_Event_Cart_BAO_Cart $event_cart
*/
public function load_associations($event_cart = NULL) {
if ($this->assocations_loaded) {
Expand All @@ -175,18 +185,27 @@ public function load_associations($event_cart = NULL) {
$defaults = [];
$this->event = CRM_Event_BAO_Event::retrieve($params, $defaults);

if ($event_cart != NULL) {
if ($event_cart !== NULL) {
$this->event_cart = $event_cart;
$this->event_cart_id = $event_cart->id;
}
else {
$this->event_cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
}

$participants = CRM_Event_Cart_BAO_MerParticipant::find_all_by_event_and_cart_id($this->event_id, $this->event_cart->id);
foreach ($participants as $participant) {
$this->participants = CRM_Event_Cart_BAO_MerParticipant::find_all_by_event_and_cart_id($this->event_id, $this->event_cart->id);
if (empty($this->participants)) {
$participantParams = ['event_id' => $this->event_id, 'cart_id' => $this->event_cart->id];
if ($this->event_cart->defaultParticipantContactID) {
$participantParams['contact_id'] = $this->event_cart->defaultParticipantContactID;
}
$newParticipant = CRM_Event_Cart_BAO_MerParticipant::create($participantParams);
$this->event_cart->defaultParticipantContactID = $newParticipant->contact_id;
$this->participants[$newParticipant->id] = $newParticipant;
}
/** @var \CRM_Event_BAO_Participant $participant */
foreach ($this->participants as $participant) {
$participant->load_associations();
$this->add_participant($participant);
}
}

Expand Down Expand Up @@ -285,7 +304,6 @@ public function waiting_participants() {
*/
public static function get_registration_link($event_id) {
$cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
$cart->load_associations();
$event_in_cart = $cart->get_event_in_cart_by_event_id($event_id);

if ($event_in_cart) {
Expand Down Expand Up @@ -316,13 +334,8 @@ public function is_parent_event() {
*
* @return bool
*/
public function is_child_event($parent_event_id = NULL) {
if ($parent_event_id == NULL) {
return $this->event->parent_event_id;
}
else {
return $this->event->parent_event_id == $parent_event_id;
}
public function is_child_event($parent_event_id) {
return ($this->event->parent_event_id === $parent_event_id);
}

}
Loading

0 comments on commit 7e14a75

Please sign in to comment.