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 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
23 $this->_bltID
= array_search('Billing', $locationTypes);
24 $this->assign('bltID', $this->_bltID
);
26 $event_titles = array();
27 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
28 $event_titles[] = $event_in_cart->event
->title
;
30 $this->description
= ts("Online Registration for %1", array(1 => implode(", ", $event_titles)));
31 if (!isset($this->discounts
)) {
32 $this->discounts
= array();
37 if ($this->event_cart_id
== NULL) {
38 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_or_create_for_current_session();
41 $this->cart
= CRM_Event_Cart_BAO_Cart
::find_by_id($this->event_cart_id
);
43 $this->cart
->load_associations();
44 $this->stub_out_and_inherit();
47 function stub_out_and_inherit() {
48 $transaction = new CRM_Core_Transaction();
50 foreach ($this->cart
->get_main_events_in_carts() as $event_in_cart) {
51 if (empty($event_in_cart->participants
)) {
52 $participant_params = array(
53 'cart_id' => $this->cart
->id
,
54 'event_id' => $event_in_cart->event_id
,
55 'contact_id' => self
::find_or_create_contact($this->getContactID()),
57 $participant = CRM_Event_Cart_BAO_MerParticipant
::create($participant_params);
59 $event_in_cart->add_participant($participant);
61 $event_in_cart->save();
63 $transaction->commit();
66 function checkWaitingList() {
67 foreach ($this->cart
->events_in_carts
as $event_in_cart) {
68 $empty_seats = $this->checkEventCapacity($event_in_cart->event_id
);
69 if ($empty_seats === NULL) {
72 foreach ($event_in_cart->participants
as $participant) {
73 $participant->must_wait
= ($empty_seats <= 0);
82 * @return bool|int|null|string
84 function checkEventCapacity($event_id) {
85 $empty_seats = CRM_Event_BAO_Participant
::eventFull($event_id, TRUE);
86 if (is_numeric($empty_seats)) {
89 if (is_string($empty_seats)) {
100 static function is_administrator() {
102 return CRM_Core_Permission
::check('administer CiviCRM');
108 function getContactID() {
109 //XXX when do we query 'cid' ?
110 $tempID = CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
112 //check if this is a checksum authentication
113 $userChecksum = CRM_Utils_Request
::retrieve('cs', 'String', $this);
115 //check for anonymous user.
116 $validUser = CRM_Contact_BAO_Contact_Utils
::validChecksum($tempID, $userChecksum);
122 // check if the user is registered and we have a contact ID
123 $session = CRM_Core_Session
::singleton();
124 return $session->get('userID');
132 static function find_contact($fields) {
133 $dedupe_params = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
134 $dedupe_params['check_permission'] = FALSE;
135 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupe_params, 'Individual');
136 if (is_array($ids)) {
137 return array_pop($ids);
143 * @param null $registeringContactID
144 * @param array $fields
146 * @return int|mixed|null
148 static function find_or_create_contact($registeringContactID = NULL, $fields = array(
150 $contact_id = self
::find_contact($fields);
155 $contact_params = array(
156 'email-Primary' => CRM_Utils_Array
::value('email', $fields, NULL),
157 'first_name' => CRM_Utils_Array
::value('first_name', $fields, NULL),
158 'last_name' => CRM_Utils_Array
::value('last_name', $fields, NULL),
159 'is_deleted' => CRM_Utils_Array
::value('is_deleted', $fields, TRUE),
161 $no_fields = array();
162 $contact_id = CRM_Contact_BAO_Contact
::createProfileContact($contact_params, $no_fields, NULL);
164 CRM_Core_Error
::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster.");
174 function getValuesForPage($page_name) {
175 $container = $this->controller
->container();
176 return $container['values'][$page_name];