Merge pull request #1679 from davecivicrm/CRM-13428
[civicrm-core.git] / CRM / Event / Cart / Form / Cart.php
CommitLineData
6a488035
TO
1<?php
2class CRM_Event_Cart_Form_Cart extends CRM_Core_Form {
3 public $cart;
4
5 public $_action;
6 public $contact;
7 public $event_cart_id = NULL;
8 public $_mode;
9 public $participants;
10
11 public function preProcess() {
12 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
13 $this->_mode = 'live';
14 $this->loadCart();
15
16 $this->checkWaitingList();
17
b2b0530a 18 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
6a488035
TO
19 $this->_bltID = array_search('Billing', $locationTypes);
20 $this->assign('bltID', $this->_bltID);
21
22 $event_titles = array();
23 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
24 $event_titles[] = $event_in_cart->event->title;
25 }
26 $this->description = ts("Online Registration for %1", array(1 => implode(", ", $event_titles)));
27 if (!isset($this->discounts)) {
28 $this->discounts = array();
29 }
30 }
31
32 function loadCart() {
33 if ($this->event_cart_id == NULL) {
34 $this->cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session();
35 }
36 else {
37 $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id);
38 }
39 $this->cart->load_associations();
40 $this->stub_out_and_inherit();
41 }
42
43 function stub_out_and_inherit() {
44 $transaction = new CRM_Core_Transaction();
45
46 foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) {
47 if (empty($event_in_cart->participants)) {
48 $participant_params = array(
49 'cart_id' => $this->cart->id,
50 'event_id' => $event_in_cart->event_id,
51 'contact_id' => self::find_or_create_contact($this->getContactID()),
52 );
53 $participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params);
54 $participant->save();
55 $event_in_cart->add_participant($participant);
56 }
57 $event_in_cart->save();
58 }
59 $transaction->commit();
60 }
61
62 function checkWaitingList() {
63 foreach ($this->cart->events_in_carts as $event_in_cart) {
64 $empty_seats = $this->checkEventCapacity($event_in_cart->event_id);
65 if ($empty_seats === NULL) {
66 continue;
67 }
68 foreach ($event_in_cart->participants as $participant) {
69 $participant->must_wait = ($empty_seats <= 0);
70 $empty_seats--;
71 }
72 }
73 }
74
75 function checkEventCapacity($event_id) {
76 $empty_seats = CRM_Event_BAO_Participant::eventFull($event_id, TRUE);
77 if (is_numeric($empty_seats)) {
78 return $empty_seats;
79 }
80 if (is_string($empty_seats)) {
81 return 0;
82 }
83 else {
84 return NULL;
85 }
86 }
87
88 static function is_administrator() {
89 global $user;
90 return CRM_Core_Permission::check('administer CiviCRM');
91 }
92
93 function getContactID() {
94 //XXX when do we query 'cid' ?
95 $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
96
97 //check if this is a checksum authentication
98 $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this);
99 if ($userChecksum) {
100 //check for anonymous user.
101 $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum);
102 if ($validUser) {
103 return $tempID;
104 }
105 }
106
107 // check if the user is registered and we have a contact ID
108 $session = CRM_Core_Session::singleton();
109 return $session->get('userID');
110 }
111
112 static function find_contact($fields) {
113 $dedupe_params = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
114 $dedupe_params['check_permission'] = FALSE;
115 $ids = CRM_Dedupe_Finder::dupesByParams($dedupe_params, 'Individual');
116 if (is_array($ids)) {
117 return array_pop($ids);
118 }
119 else return NULL;
120 }
121
122 static function find_or_create_contact($registeringContactID = NULL, $fields = array(
123 )) {
124 $contact_id = self::find_contact($fields);
125
126 if ($contact_id) {
127 return $contact_id;
128 }
129 $contact_params = array(
130 'email-Primary' => CRM_Utils_Array::value('email', $fields, NULL),
131 'first_name' => CRM_Utils_Array::value('first_name', $fields, NULL),
132 'last_name' => CRM_Utils_Array::value('last_name', $fields, NULL),
133 'is_deleted' => CRM_Utils_Array::value('is_deleted', $fields, TRUE),
134 );
135 $no_fields = array();
136 $contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL);
137 if (!$contact_id) {
138 CRM_Core_Error::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
139 }
140 return $contact_id;
141 }
142
143 function getValuesForPage($page_name) {
144 $container = $this->controller->container();
145 return $container['values'][$page_name];
146 }
147}
148