4 * Class CRM_Event_Cart_Form_Cart
6 class CRM_Event_Cart_Form_Cart
extends CRM_Core_Form
{
11 public $event_cart_id = NULL;
15 public function preProcess() {
16 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE);
17 $this->_mode
= 'live';
20 $this->checkWaitingList();
22 $this->assignBillingType();
24 $event_titles = array();
25 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
26 $event_titles[] = $event_in_cart->event
->title
;
28 $this->description
= ts("Online Registration for %1", array(1 => implode(", ", $event_titles)));
29 if (!isset($this->discounts
)) {
30 $this->discounts
= array();
34 public function loadCart() {
35 if ($this->event_cart_id
== NULL) {
36 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_or_create_for_current_session();
39 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
41 $this->cart
->load_associations();
42 $this->stub_out_and_inherit();
45 public function stub_out_and_inherit() {
46 $transaction = new CRM_Core_Transaction();
48 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
49 if (empty($event_in_cart->participants
)) {
50 $participant_params = array(
51 'cart_id' => $this->cart
->id
,
52 'event_id' => $event_in_cart->event_id
,
53 'contact_id' => self
::find_or_create_contact($this->getContactID()),
55 $participant = CRM_Event_Cart_BAO_MerParticipant
::create($participant_params);
57 $event_in_cart->add_participant($participant);
59 $event_in_cart->save();
61 $transaction->commit();
64 public function checkWaitingList() {
65 foreach ($this->cart
->events_in_carts
as $event_in_cart) {
66 $empty_seats = $this->checkEventCapacity($event_in_cart->event_id
);
67 if ($empty_seats === NULL) {
70 foreach ($event_in_cart->participants
as $participant) {
71 $participant->must_wait
= ($empty_seats <= 0);
78 * @param int $event_id
80 * @return bool|int|null|string
82 public function checkEventCapacity($event_id) {
83 $empty_seats = CRM_Event_BAO_Participant
::eventFull($event_id, TRUE);
84 if (is_numeric($empty_seats)) {
87 if (is_string($empty_seats)) {
98 public static function is_administrator() {
100 return CRM_Core_Permission
::check('administer CiviCRM');
106 public function getContactID() {
107 //XXX when do we query 'cid' ?
108 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
110 //check if this is a checksum authentication
111 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
113 //check for anonymous user.
114 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
120 // check if the user is registered and we have a contact ID
121 $session = CRM_Core_Session
::singleton();
122 return $session->get('userID');
130 public static function find_contact($fields) {
131 $dedupe_params = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
132 $dedupe_params['check_permission'] = FALSE;
133 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupe_params, 'Individual');
134 if (is_array($ids)) {
135 return array_pop($ids);
143 * @param int $registeringContactID
144 * @param array $fields
146 * @return int|mixed|null
148 public static function find_or_create_contact($registeringContactID = NULL, $fields = array()) {
149 $contact_id = self
::find_contact($fields);
154 $contact_params = array(
155 'email-Primary' => CRM_Utils_Array
::value('email', $fields, NULL),
156 'first_name' => CRM_Utils_Array
::value('first_name', $fields, NULL),
157 'last_name' => CRM_Utils_Array
::value('last_name', $fields, NULL),
158 'is_deleted' => CRM_Utils_Array
::value('is_deleted', $fields, TRUE),
160 $no_fields = array();
161 $contact_id = CRM_Contact_BAO_Contact
::createProfileContact($contact_params, $no_fields, NULL);
163 CRM_Core_Error
::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
169 * @param string $page_name
173 public function getValuesForPage($page_name) {
174 $container = $this->controller
->container();
175 return $container['values'][$page_name];