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 Aug 7, 2020
1 parent a79b9e5 commit aa83d32
Show file tree
Hide file tree
Showing 18 changed files with 532 additions and 761 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;
}

}
39 changes: 23 additions & 16 deletions ext/eventcart/CRM/Event/Cart/BAO/EventInCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,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 +168,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 +179,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;
}
/** @var \CRM_Event_BAO_Participant $participant */
foreach ($this->participants as $participant) {
$participant->load_associations();
$this->add_participant($participant);
}
}

Expand Down Expand Up @@ -285,7 +298,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 +328,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);
}

}
120 changes: 40 additions & 80 deletions ext/eventcart/CRM/Event/Cart/BAO/MerParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,32 @@ class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant {
public $cart = NULL;

/**
* XXX.
* @param null $participant
* @param array $participant
*/
public function __construct($participant = NULL) {
public function __construct($participant = []) {
parent::__construct();
$a = (array) $participant;
$this->copyValues($a);
$this->copyValues($participant);

$this->email = $a['email'] ?? NULL;
$this->email = $participant['email'] ?? NULL;
}

/**
* @param array $params
* @param array $participantParams
* You MUST pass in event_id and cart_id
*
* @return CRM_Event_Cart_BAO_MerParticipant
* @throws Exception
*/
public static function create(&$params) {
$participantParams = [
'id' => $params['id'] ?? NULL,
'role_id' => self::get_attendee_role_id(),
'status_id' => self::get_pending_in_cart_status_id(),
'contact_id' => $params['contact_id'],
'event_id' => $params['event_id'],
'cart_id' => $params['cart_id'],
];
$participant = CRM_Event_BAO_Participant::create($participantParams);

if (is_a($participant, 'CRM_Core_Error')) {
throw new CRM_Core_Exception(ts('There was an error creating a cart participant'));
public static function create(&$participantParams) {
if (empty($participantParams['event_id'] || empty($participantParams['cart_id']))) {
throw new CRM_Core_Exception('MerParticipant create: Missing required params: event_id/cart_id');
}
$participantParams['contact_id'] = $participantParams['contact_id'] ?? CRM_Event_Cart_Form_Cart::find_or_create_contact();
$participantParams['role_id'] = $participantParams['role_id'] ?? CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'role_id', 'Attendee');
$participantParams['status_id'] = $participantParams['status_id'] ?? CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Pending in cart');

$mer_participant = new CRM_Event_Cart_BAO_MerParticipant($participant);
return $mer_participant;
}

/**
* @return mixed
*/
public static function get_attendee_role_id() {
$roles = CRM_Event_PseudoConstant::participantRole(NULL, "v.label='Attendee'");
$role_names = array_keys($roles);
return end($role_names);
}

/**
* @return mixed
*/
public static function get_pending_in_cart_status_id() {
$status_types = CRM_Event_PseudoConstant::participantStatus(NULL, "name='Pending in cart'");
$status_names = array_keys($status_types);
return end($status_names);
$participant = reset(civicrm_api3('Participant', 'create', $participantParams)['values']);
return new CRM_Event_Cart_BAO_MerParticipant($participant);
}

/**
Expand All @@ -93,7 +67,14 @@ public static function find_all_by_cart_id($event_cart_id) {
if ($event_cart_id == NULL) {
return NULL;
}
return self::find_all_by_params(['cart_id' => $event_cart_id]);
$participants = \Civi\Api4\Participant::get()
->addWhere('cart_id', '=', $event_cart_id)
->execute();
$result = [];
foreach ($participants as $participant) {
$result[] = new CRM_Event_Cart_BAO_MerParticipant($participant);
}
return $result;
}

/**
Expand All @@ -106,39 +87,37 @@ public static function find_all_by_event_and_cart_id($event_id, $event_cart_id)
if ($event_cart_id == NULL) {
return NULL;
}
return self::find_all_by_params(['event_id' => $event_id, 'cart_id' => $event_cart_id]);
}

/**
* @param array $params
*
* @return array
*/
public static function find_all_by_params($params) {
$participant = new CRM_Event_BAO_Participant();
$participant->copyValues($params);
$participants = \Civi\Api4\Participant::get()
->addWhere('event_id', '=', $event_id)
->addWhere('cart_id', '=', $event_cart_id)
->execute();
$result = [];
if ($participant->find()) {
while ($participant->fetch()) {
$result[] = new CRM_Event_Cart_BAO_MerParticipant(clone($participant));
}
foreach ($participants as $participant) {
$result[] = new CRM_Event_Cart_BAO_MerParticipant($participant);
}
return $result;
}

/**
* @param int $id
*
* @return mixed
* @return \CRM_Event_Cart_BAO_MerParticipant
*/
public static function get_by_id($id) {
$results = self::find_all_by_params(['id' => $id]);
return array_pop($results);
$participant = \Civi\Api4\Participant::get()
->addWhere('id', '=', $id)
->execute()
->first();
return new CRM_Event_Cart_BAO_MerParticipant($participant);
}

public function load_associations() {
$contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id);
$this->email = $contact_details[1];
$email = \Civi\Api4\Email::get()
->addWhere('contact_id', '=', $this->contact_id)
->addOrderBy('is_primary', 'DESC')
->execute()
->first();
$this->email = $email['email'] ?? NULL;
}

/**
Expand All @@ -153,25 +132,6 @@ public function get_participant_index() {
return $index + 1;
}

/**
* @param $contact
*
* @return null
*/
public static function billing_address_from_contact($contact) {
foreach ($contact->address as $loc) {
if ($loc['is_billing']) {
return $loc;
}
}
foreach ($contact->address as $loc) {
if ($loc['is_primary']) {
return $loc;
}
}
return NULL;
}

/**
* @return CRM_Event_Cart_Form_MerParticipant
*/
Expand Down
Loading

0 comments on commit aa83d32

Please sign in to comment.