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,344 @@
<?php
/**
* Class CRM_Event_Cart_BAO_Cart
*/
class CRM_Event_Cart_BAO_Cart extends CRM_Event_Cart_DAO_Cart {
public $associations_loaded = FALSE;
/* event_in_cart_id => $event_in_cart */
public $events_in_carts = array();
/**
* @param array $params
*
* @return CRM_Event_Cart_BAO_Cart
*/
public static function add(&$params) {
$cart = new CRM_Event_Cart_BAO_Cart();
$cart->copyValues($params);
$result = $cart->save();
return $result;
}
/**
* @param int $event_id
*
* @return mixed
*/
public function add_event($event_id) {
$this->load_associations();
$event_in_cart = $this->get_event_in_cart_by_event_id($event_id);
if ($event_in_cart) {
return $event_in_cart;
}
$params = array(
'event_id' => $event_id,
'event_cart_id' => $this->id,
);
$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
*/
public function add_participant_to_cart($participant) {
$event_in_cart = $this->get_event_in_cart_by_event_id($participant->event_id);
if (!$event_in_cart) {
$event_in_cart = $this->add_event($participant->event_id);
}
$event_in_cart->add_participant($participant);
$event_in_cart->save();
}
/**
* @param array $params
*
* @return CRM_Event_Cart_BAO_Cart
* @throws Exception
*/
public static function create($params) {
$transaction = new CRM_Core_Transaction();
$cart = self::add($params);
if (is_a($cart, 'CRM_Core_Error')) {
$transaction->rollback();
CRM_Core_Error::fatal(ts('There was an error creating an event cart'));
}
$transaction->commit();
return $cart;
}
/**
* @param int $id
*
* @return bool|CRM_Event_Cart_BAO_Cart
*/
public static function find_by_id($id) {
return self::find_by_params(array('id' => $id));
}
/**
* @param array $params
*
* @return bool|CRM_Event_Cart_BAO_Cart
*/
public static function find_by_params($params) {
$cart = new CRM_Event_Cart_BAO_Cart();
$cart->copyValues($params);
if ($cart->find(TRUE)) {
return $cart;
}
else {
return FALSE;
}
}
/**
* @return self|bool|CRM_Event_Cart_BAO_Cart
*/
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');
$cart = FALSE;
if (!is_null($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;
$cart->save();
}
}
}
}
if ($cart === FALSE) {
if (is_null($userID)) {
$cart = self::create(array());
}
else {
$cart = self::find_uncompleted_by_user_id($userID);
if ($cart === FALSE) {
$cart = self::create(array('user_id' => $userID));
}
}
$session->set('event_cart_id', $cart->id);
}
return $cart;
}
/**
* @param int $id
*
* @return bool|CRM_Event_Cart_BAO_Cart
*/
public static function find_uncompleted_by_id($id) {
return self::find_by_params(array('id' => $id, 'completed' => 0));
}
/**
* @param int $user_id
*
* @return bool|CRM_Event_Cart_BAO_Cart
*/
public static function find_uncompleted_by_user_id($user_id) {
return self::find_by_params(array('user_id' => $user_id, 'completed' => 0));
}
/**
* @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 = array();
foreach ($this->events_in_carts as $event_in_cart) {
if (!$event_in_cart->is_child_event()) {
$all[] = $event_in_cart;
}
}
return $all;
}
/**
* @param int $main_conference_event_id
*
* @return array
*/
public function get_events_in_carts_by_main_event_id($main_conference_event_id) {
$all = array();
if (!$main_conference_event_id) {
return $all;
}
foreach ($this->events_in_carts as $event_in_cart) {
if ($event_in_cart->event->parent_event_id == $main_conference_event_id) {
$all[] = $event_in_cart;
}
}
usort($all, "CRM_Event_Cart_BAO_Cart::compare_event_dates");
return $all;
}
/**
* @param $event_in_cart_1
* @param $event_in_cart_2
*
* @return int
*/
public static function compare_event_dates($event_in_cart_1, $event_in_cart_2) {
$date_1 = CRM_Utils_Date::unixTime($event_in_cart_1->event->start_date);
$date_2 = CRM_Utils_Date::unixTime($event_in_cart_2->event->start_date);
if ($date_1 == $date_2) {
return 0;
}
return ($date_1 < $date_2) ? -1 : 1;
}
/**
* @param $main_participant
*
* @return array
*/
public function get_subparticipants($main_participant) {
$subparticipants = array();
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) {
if ($participant->contact_id == $main_participant->contact_id) {
$subparticipants[] = $participant;
continue;
}
}
}
}
return $subparticipants;
}
/**
* @param int $event_id
*
* @return mixed
*/
public function get_event_in_cart_by_event_id($event_id) {
return CRM_Utils_Array::value($event_id, $this->events_in_carts);
}
/**
* @param int $event_in_cart_id
*
* @return null
*/
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;
}
}
return NULL;
}
/**
* @return array
*/
public function get_main_event_participants() {
$participants = array();
foreach ($this->get_main_events_in_carts() as $event_in_cart) {
$participants = array_merge($participants, $event_in_cart->participants);
}
return $participants;
}
public function load_associations() {
if ($this->associations_loaded) {
return;
}
$this->associations_loaded = TRUE;
$this->events_in_carts = CRM_Event_Cart_BAO_EventInCart::find_all_by_event_cart_id($this->id);
foreach ($this->events_in_carts as $event_in_cart) {
$event_in_cart->load_associations($this);
}
$this->save();
}
/**
* @param int $event_in_cart_id
*
* @return bool|CRM_Event_Cart_BAO_EventInCart
*/
public function remove_event_in_cart($event_in_cart_id) {
$event_in_cart = CRM_Event_Cart_BAO_EventInCart::find_by_id($event_in_cart_id);
if ($event_in_cart) {
$sessions_to_remove = $this->get_events_in_carts_by_main_event_id($event_in_cart->event_id);
foreach ($sessions_to_remove as $session) {
$this->remove_event_in_cart($session->id);
}
unset($this->events_in_carts[$event_in_cart->event_id]);
$event_in_cart->delete();
}
return $event_in_cart;
}
/**
* @param int $participant_id
*
* @return int
*/
public function get_participant_index_from_id($participant_id) {
foreach ($this->events_in_carts as $event_in_cart) {
$index = 0;
foreach ($event_in_cart->participants as $participant) {
if ($participant->id == $participant_id) {
return $index;
}
$index++;
}
}
return -1;
}
/**
* @param array $params
* @param $values
*
* @return mixed
* @throws Exception
*/
public static function retrieve(&$params, &$values) {
$cart = self::find_by_params($params);
if ($cart === FALSE) {
CRM_Core_Error::fatal(ts('Could not find cart matching %1', array(1 => var_export($params, TRUE))));
}
CRM_Core_DAO::storeValues($cart, $values);
return $values;
}
/**
* @param int $from_cart_id
*/
public function adopt_participants($from_cart_id) {
$params = array(
1 => array($this->id, 'Integer'),
2 => array($from_cart_id, 'Integer'),
);
$sql = "UPDATE civicrm_participant SET cart_id='%1' WHERE cart_id='%2'";
CRM_Core_DAO::executeQuery($sql, $params);
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* Class CRM_Event_Cart_BAO_Conference
*/
class CRM_Event_Cart_BAO_Conference {
/**
* XXX assumes we don't allow a contact to register for the same conference more than once
* XXX flattens the object tree for convenient templating
* @param int $main_event_participant_id
*
* @return array|null
*/
public static function get_participant_sessions($main_event_participant_id) {
$sql = <<<EOS
SELECT sub_event.* FROM civicrm_participant main_participant
JOIN civicrm_event sub_event ON sub_event.parent_event_id = main_participant.event_id
JOIN civicrm_participant sub_participant ON sub_participant.event_id = sub_event.id
LEFT JOIN
civicrm_option_value slot ON sub_event.slot_label_id = slot.value
LEFT JOIN
civicrm_option_group og ON slot.option_group_id = og.id
WHERE
main_participant.id = %1
AND sub_participant.contact_id = main_participant.contact_id
AND og.name = 'conference_slot'
ORDER BY
slot.weight,
sub_event.start_date
EOS;
$sql_args = array(1 => array($main_event_participant_id, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($sql, $sql_args);
$smarty_sessions = array();
while ($dao->fetch()) {
$smarty_sessions[] = get_object_vars($dao);
}
if (empty($smarty_sessions)) {
return NULL;
}
return $smarty_sessions;
}
}

View file

@ -0,0 +1,329 @@
<?php
/**
* Class CRM_Event_Cart_BAO_EventInCart
*/
class CRM_Event_Cart_BAO_EventInCart extends CRM_Event_Cart_DAO_EventInCart implements ArrayAccess {
public $assocations_loaded = FALSE;
public $event;
public $event_cart;
public $location = NULL;
public $participants = array();
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Add participant to cart.
*
* @param $participant
*/
public function add_participant($participant) {
$this->participants[$participant->id] = $participant;
}
/**
* @param array $params
*
* @return object $this|CRM_Event_Cart_BAO_EventInCart
* @throws Exception
*/
public static function create(&$params) {
$transaction = new CRM_Core_Transaction();
$event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
$event_in_cart->copyValues($params);
$event_in_cart = $event_in_cart->save();
if (is_a($event_in_cart, 'CRM_Core_Error')) {
$transaction->rollback();
CRM_Core_Error::fatal(ts('There was an error creating an event_in_cart'));
}
$transaction->commit();
return $event_in_cart;
}
/**
* @param bool $useWhere
*
* @return mixed|void
*/
public function delete($useWhere = FALSE) {
$this->load_associations();
$contacts_to_delete = array();
foreach ($this->participants as $participant) {
$defaults = array();
$params = array('id' => $participant->contact_id);
$temporary_contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults);
if ($temporary_contact->is_deleted) {
$contacts_to_delete[$temporary_contact->id] = 1;
}
$participant->delete();
}
foreach (array_keys($contacts_to_delete) as $contact_id) {
CRM_Contact_BAO_Contact::deleteContact($contact_id);
}
return parent::delete();
}
/**
* @param int $event_cart_id
*
* @return array
*/
public static function find_all_by_event_cart_id($event_cart_id) {
return self::find_all_by_params(array('event_cart_id' => $event_cart_id));
}
/**
* @param array $params
*
* @return array
*/
public static function find_all_by_params($params) {
$event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
$event_in_cart->copyValues($params);
$result = array();
if ($event_in_cart->find()) {
while ($event_in_cart->fetch()) {
$result[$event_in_cart->event_id] = clone($event_in_cart);
}
}
return $result;
}
/**
* @param int $id
*
* @return bool|CRM_Event_Cart_BAO_EventInCart
*/
public static function find_by_id($id) {
return self::find_by_params(array('id' => $id));
}
/**
* @param array $params
*
* @return bool|CRM_Event_Cart_BAO_EventInCart
*/
public static function find_by_params($params) {
$event_in_cart = new CRM_Event_Cart_BAO_EventInCart();
$event_in_cart->copyValues($params);
if ($event_in_cart->find(TRUE)) {
return $event_in_cart;
}
else {
return FALSE;
}
}
/**
* @param int $contact_id
*/
public function remove_participant_by_contact_id($contact_id) {
$to_remove = array();
foreach ($this->participants as $participant) {
if ($participant->contact_id == $contact_id) {
$to_remove[$participant->id] = 1;
$participant->delete();
}
}
$this->participants = array_diff_key($this->participants, $to_remove);
}
/**
* @param int $participant_id
*
* @return mixed
*/
public function get_participant_by_id($participant_id) {
return $this->participants[$participant_id];
}
/**
* @param int $participant_id
*/
public function remove_participant_by_id($participant_id) {
$this->get_participant_by_id($participant_id)->delete();
unset($this->participants[$participant_id]);
}
/**
* @param $participant
*
* @return mixed
*/
public static function part_key($participant) {
return $participant->id;
}
/**
* @param null $event_cart
*/
public function load_associations($event_cart = NULL) {
if ($this->assocations_loaded) {
return;
}
$this->assocations_loaded = TRUE;
$params = array('id' => $this->event_id);
$defaults = array();
$this->event = CRM_Event_BAO_Event::retrieve($params, $defaults);
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) {
$participant->load_associations();
$this->add_participant($participant);
}
}
public function load_location() {
if ($this->location == NULL) {
$location_params = array('entity_id' => $this->event_id, 'entity_table' => 'civicrm_event');
$this->location = CRM_Core_BAO_Location::getValues($location_params, TRUE);
}
}
/**
* @return array
*/
public function not_waiting_participants() {
$result = array();
foreach ($this->participants as $participant) {
if (!$participant->must_wait) {
$result[] = $participant;
}
}
return $result;
}
/**
* @return int
*/
public function num_not_waiting_participants() {
return count($this->not_waiting_participants());
}
/**
* @return int
*/
public function num_waiting_participants() {
return count($this->waiting_participants());
}
/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset) {
return array_key_exists(array_merge($this->fields(), array('main_conference_event_id')), $offset);
}
/**
* @param mixed $offset
*
* @return int
*/
public function offsetGet($offset) {
if ($offset == 'event') {
return $this->event->toArray();
}
if ($offset == 'id') {
return $this->id;
}
if ($offset == 'main_conference_event_id') {
return $this->main_conference_event_id;
}
$fields = &$this->fields();
return $fields[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value) {
}
/**
* @param mixed $offset
*/
public function offsetUnset($offset) {
}
/**
* @return array
*/
public function waiting_participants() {
$result = array();
foreach ($this->participants as $participant) {
if ($participant->must_wait) {
$result[] = $participant;
}
}
return $result;
}
/**
* @param int $event_id
*
* @return array
*/
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) {
return array(
'label' => ts("Remove from Cart"),
'path' => 'civicrm/event/remove_from_cart',
'query' => "reset=1&id={$event_id}",
);
}
else {
return array(
'label' => ts("Add to Cart"),
'path' => 'civicrm/event/add_to_cart',
'query' => "reset=1&id={$event_id}",
);
}
}
/**
* @return bool
*/
public function is_parent_event() {
return (NULL !== (CRM_Event_BAO_Event::get_sub_events($this->event_id)));
}
/**
* @param int $parent_event_id
*
* @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;
}
}
}

View file

@ -0,0 +1,191 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Class CRM_Event_Cart_BAO_MerParticipant
*/
class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant {
public $email = NULL;
public $contribution_id = NULL;
public $cart = NULL;
/**
* XXX.
* @param null $participant
*/
public function __construct($participant = NULL) {
parent::__construct();
$a = (array) $participant;
$this->copyValues($a);
$this->email = CRM_Utils_Array::value('email', $participant);
}
/**
* @param array $params
*
* @return CRM_Event_Cart_BAO_MerParticipant
* @throws Exception
*/
public static function create(&$params) {
$participantParams = array(
'id' => CRM_Utils_Array::value('id', $params),
'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'],
//XXX
//'registered_by_id' =>
//'discount_amount' =>
//'fee_level' => $params['fee_level'],
);
$participant = CRM_Event_BAO_Participant::create($participantParams);
if (is_a($participant, 'CRM_Core_Error')) {
CRM_Core_Error::fatal(ts('There was an error creating a cart participant'));
}
$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);
}
/**
* @param int $event_cart_id
*
* @return array|null
*/
public static function find_all_by_cart_id($event_cart_id) {
if ($event_cart_id == NULL) {
return NULL;
}
return self::find_all_by_params(array('cart_id' => $event_cart_id));
}
/**
* @param int $event_id
* @param int $event_cart_id
*
* @return array|null
*/
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(array('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);
$result = array();
if ($participant->find()) {
while ($participant->fetch()) {
$result[] = new CRM_Event_Cart_BAO_MerParticipant(clone($participant));
}
}
return $result;
}
/**
* @param int $id
*
* @return mixed
*/
public static function get_by_id($id) {
$results = self::find_all_by_params(array('id' => $id));
return array_pop($results);
}
public function load_associations() {
$contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id);
$this->email = $contact_details[1];
}
/**
* @return int
*/
public function get_participant_index() {
if (!$this->cart) {
$this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->cart_id);
$this->cart->load_associations();
}
$index = $this->cart->get_participant_index_from_id($this->id);
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
*/
public function get_form() {
return new CRM_Event_Cart_Form_MerParticipant($this);
}
}