(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Event / Cart / BAO / MerParticipant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Event_Cart_BAO_MerParticipant
14 */
15 class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant {
16 public $email = NULL;
17 public $contribution_id = NULL;
18 public $cart = NULL;
19
20 /**
21 * XXX.
22 * @param null $participant
23 */
24 public function __construct($participant = NULL) {
25 parent::__construct();
26 $a = (array) $participant;
27 $this->copyValues($a);
28
29 $this->email = CRM_Utils_Array::value('email', $participant);
30 }
31
32 /**
33 * @param array $params
34 *
35 * @return CRM_Event_Cart_BAO_MerParticipant
36 * @throws Exception
37 */
38 public static function create(&$params) {
39 $participantParams = [
40 'id' => CRM_Utils_Array::value('id', $params),
41 'role_id' => self::get_attendee_role_id(),
42 'status_id' => self::get_pending_in_cart_status_id(),
43 'contact_id' => $params['contact_id'],
44 'event_id' => $params['event_id'],
45 'cart_id' => $params['cart_id'],
46 //XXX
47 //'registered_by_id' =>
48 //'discount_amount' =>
49 //'fee_level' => $params['fee_level'],
50 ];
51 $participant = CRM_Event_BAO_Participant::create($participantParams);
52
53 if (is_a($participant, 'CRM_Core_Error')) {
54 CRM_Core_Error::fatal(ts('There was an error creating a cart participant'));
55 }
56
57 $mer_participant = new CRM_Event_Cart_BAO_MerParticipant($participant);
58
59 return $mer_participant;
60 }
61
62 /**
63 * @return mixed
64 */
65 public static function get_attendee_role_id() {
66 $roles = CRM_Event_PseudoConstant::participantRole(NULL, "v.label='Attendee'");
67 $role_names = array_keys($roles);
68 return end($role_names);
69 }
70
71 /**
72 * @return mixed
73 */
74 public static function get_pending_in_cart_status_id() {
75 $status_types = CRM_Event_PseudoConstant::participantStatus(NULL, "name='Pending in cart'");
76 $status_names = array_keys($status_types);
77 return end($status_names);
78 }
79
80 /**
81 * @param int $event_cart_id
82 *
83 * @return array|null
84 */
85 public static function find_all_by_cart_id($event_cart_id) {
86 if ($event_cart_id == NULL) {
87 return NULL;
88 }
89 return self::find_all_by_params(['cart_id' => $event_cart_id]);
90 }
91
92 /**
93 * @param int $event_id
94 * @param int $event_cart_id
95 *
96 * @return array|null
97 */
98 public static function find_all_by_event_and_cart_id($event_id, $event_cart_id) {
99 if ($event_cart_id == NULL) {
100 return NULL;
101 }
102 return self::find_all_by_params(['event_id' => $event_id, 'cart_id' => $event_cart_id]);
103 }
104
105 /**
106 * @param array $params
107 *
108 * @return array
109 */
110 public static function find_all_by_params($params) {
111 $participant = new CRM_Event_BAO_Participant();
112 $participant->copyValues($params);
113 $result = [];
114 if ($participant->find()) {
115 while ($participant->fetch()) {
116 $result[] = new CRM_Event_Cart_BAO_MerParticipant(clone($participant));
117 }
118 }
119 return $result;
120 }
121
122 /**
123 * @param int $id
124 *
125 * @return mixed
126 */
127 public static function get_by_id($id) {
128 $results = self::find_all_by_params(['id' => $id]);
129 return array_pop($results);
130 }
131
132 public function load_associations() {
133 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id);
134 $this->email = $contact_details[1];
135 }
136
137 /**
138 * @return int
139 */
140 public function get_participant_index() {
141 if (!$this->cart) {
142 $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->cart_id);
143 $this->cart->load_associations();
144 }
145 $index = $this->cart->get_participant_index_from_id($this->id);
146 return $index + 1;
147 }
148
149 /**
150 * @param $contact
151 *
152 * @return null
153 */
154 public static function billing_address_from_contact($contact) {
155 foreach ($contact->address as $loc) {
156 if ($loc['is_billing']) {
157 return $loc;
158 }
159 }
160 foreach ($contact->address as $loc) {
161 if ($loc['is_primary']) {
162 return $loc;
163 }
164 }
165 return NULL;
166 }
167
168 /**
169 * @return CRM_Event_Cart_Form_MerParticipant
170 */
171 public function get_form() {
172 return new CRM_Event_Cart_Form_MerParticipant($this);
173 }
174
175 }