| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Event_Cart_Form_Cart |
| 5 | */ |
| 6 | class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { |
| 7 | public $cart; |
| 8 | |
| 9 | public $_action; |
| 10 | public $contact; |
| 11 | public $event_cart_id = NULL; |
| 12 | public $_mode; |
| 13 | public $participants; |
| 14 | |
| 15 | public function preProcess() { |
| 16 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); |
| 17 | $this->_mode = 'live'; |
| 18 | $this->loadCart(); |
| 19 | |
| 20 | $this->checkWaitingList(); |
| 21 | |
| 22 | $this->assignBillingType(); |
| 23 | |
| 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; |
| 27 | } |
| 28 | $this->description = ts("Online Registration for %1", array(1 => implode(", ", $event_titles))); |
| 29 | if (!isset($this->discounts)) { |
| 30 | $this->discounts = array(); |
| 31 | } |
| 32 | } |
| 33 | |
| 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(); |
| 37 | } |
| 38 | else { |
| 39 | $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->event_cart_id); |
| 40 | } |
| 41 | $this->cart->load_associations(); |
| 42 | $this->stub_out_and_inherit(); |
| 43 | } |
| 44 | |
| 45 | public function stub_out_and_inherit() { |
| 46 | $transaction = new CRM_Core_Transaction(); |
| 47 | |
| 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()), |
| 54 | ); |
| 55 | $participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params); |
| 56 | $participant->save(); |
| 57 | $event_in_cart->add_participant($participant); |
| 58 | } |
| 59 | $event_in_cart->save(); |
| 60 | } |
| 61 | $transaction->commit(); |
| 62 | } |
| 63 | |
| 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) { |
| 68 | continue; |
| 69 | } |
| 70 | foreach ($event_in_cart->participants as $participant) { |
| 71 | $participant->must_wait = ($empty_seats <= 0); |
| 72 | $empty_seats--; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param int $event_id |
| 79 | * |
| 80 | * @return bool|int|null|string |
| 81 | */ |
| 82 | public function checkEventCapacity($event_id) { |
| 83 | $empty_seats = CRM_Event_BAO_Participant::eventFull($event_id, TRUE); |
| 84 | if (is_numeric($empty_seats)) { |
| 85 | return $empty_seats; |
| 86 | } |
| 87 | if (is_string($empty_seats)) { |
| 88 | return 0; |
| 89 | } |
| 90 | else { |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return bool |
| 97 | */ |
| 98 | public static function is_administrator() { |
| 99 | global $user; |
| 100 | return CRM_Core_Permission::check('administer CiviCRM'); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return mixed |
| 105 | */ |
| 106 | public function getContactID() { |
| 107 | //XXX when do we query 'cid' ? |
| 108 | $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 109 | |
| 110 | //check if this is a checksum authentication |
| 111 | $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); |
| 112 | if ($userChecksum) { |
| 113 | //check for anonymous user. |
| 114 | $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum); |
| 115 | if ($validUser) { |
| 116 | return $tempID; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // check if the user is registered and we have a contact ID |
| 121 | $session = CRM_Core_Session::singleton(); |
| 122 | return $session->get('userID'); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param $fields |
| 127 | * |
| 128 | * @return mixed|null |
| 129 | */ |
| 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); |
| 136 | } |
| 137 | else { |
| 138 | return NULL; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param int $registeringContactID |
| 144 | * @param array $fields |
| 145 | * |
| 146 | * @return int|mixed|null |
| 147 | */ |
| 148 | public static function find_or_create_contact($registeringContactID = NULL, $fields = array()) { |
| 149 | $contact_id = self::find_contact($fields); |
| 150 | |
| 151 | if ($contact_id) { |
| 152 | return $contact_id; |
| 153 | } |
| 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), |
| 159 | ); |
| 160 | $no_fields = array(); |
| 161 | $contact_id = CRM_Contact_BAO_Contact::createProfileContact($contact_params, $no_fields, NULL); |
| 162 | if (!$contact_id) { |
| 163 | CRM_Core_Error::displaySessionError("Could not create or match a contact with that email address. Please contact the webmaster."); |
| 164 | } |
| 165 | return $contact_id; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param string $page_name |
| 170 | * |
| 171 | * @return mixed |
| 172 | */ |
| 173 | public function getValuesForPage($page_name) { |
| 174 | $container = $this->controller->container(); |
| 175 | return $container['values'][$page_name]; |
| 176 | } |
| 177 | |
| 178 | } |