Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
03e04002 | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
03e04002 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
03e04002 | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
0cf587a7 EM |
28 | /** |
29 | * Class CRM_Event_Cart_BAO_MerParticipant | |
30 | */ | |
6a488035 TO |
31 | class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant { |
32 | public $email = NULL; | |
33 | public $contribution_id = NULL; | |
34 | public $cart = NULL; | |
35 | ||
03e04002 | 36 | //XXX |
0cf587a7 EM |
37 | /** |
38 | * @param null $participant | |
39 | */ | |
6a488035 TO |
40 | function __construct($participant = NULL) { |
41 | parent::__construct(); | |
42 | $a = (array)$participant; | |
43 | $this->copyValues($a); | |
44 | ||
45 | $this->email = CRM_Utils_Array::value('email', $participant); | |
46 | } | |
47 | ||
0cf587a7 EM |
48 | /** |
49 | * @param array $params | |
50 | * | |
51 | * @return CRM_Event_Cart_BAO_MerParticipant | |
52 | * @throws Exception | |
53 | */ | |
c99199d8 | 54 | public static function &create($params) { |
6a488035 TO |
55 | $participantParams = array( |
56 | 'id' => CRM_Utils_Array::value('id', $params), | |
57 | 'role_id' => self::get_attendee_role_id(), | |
58 | 'status_id' => self::get_pending_in_cart_status_id(), | |
59 | 'contact_id' => $params['contact_id'], | |
60 | 'event_id' => $params['event_id'], | |
61 | 'cart_id' => $params['cart_id'], | |
62 | //XXX | |
63 | //'registered_by_id' => | |
64 | //'discount_amount' => | |
65 | //'fee_level' => $params['fee_level'], | |
66 | ); | |
67 | $participant = CRM_Event_BAO_Participant::create($participantParams); | |
68 | ||
69 | if (is_a($participant, 'CRM_Core_Error')) { | |
70 | CRM_Core_Error::fatal(ts('There was an error creating a cart participant')); | |
71 | } | |
72 | ||
73 | $mer_participant = new CRM_Event_Cart_BAO_MerParticipant($participant); | |
74 | ||
75 | return $mer_participant; | |
76 | } | |
77 | ||
0cf587a7 EM |
78 | /** |
79 | * @return mixed | |
80 | */ | |
6a488035 TO |
81 | static function get_attendee_role_id() { |
82 | $roles = CRM_Event_PseudoConstant::participantRole(NULL, "v.label='Attendee'"); | |
83 | $role_names = array_keys($roles); | |
84 | return end($role_names); | |
85 | } | |
86 | ||
0cf587a7 EM |
87 | /** |
88 | * @return mixed | |
89 | */ | |
6a488035 TO |
90 | static function get_pending_in_cart_status_id() { |
91 | $status_types = CRM_Event_PseudoConstant::participantStatus(NULL, "name='Pending in cart'"); | |
92 | $status_names = array_keys($status_types); | |
93 | return end($status_names); | |
94 | } | |
95 | ||
0cf587a7 EM |
96 | /** |
97 | * @param $event_cart_id | |
98 | * | |
99 | * @return array|null | |
100 | */ | |
6a488035 TO |
101 | public static function find_all_by_cart_id($event_cart_id) { |
102 | if ($event_cart_id == NULL) { | |
103 | return NULL; | |
104 | } | |
105 | return self::find_all_by_params(array('cart_id' => $event_cart_id)); | |
106 | } | |
107 | ||
0cf587a7 EM |
108 | /** |
109 | * @param $event_id | |
110 | * @param $event_cart_id | |
111 | * | |
112 | * @return array|null | |
113 | */ | |
6a488035 TO |
114 | public static function find_all_by_event_and_cart_id($event_id, $event_cart_id) { |
115 | if ($event_cart_id == NULL) { | |
116 | return NULL; | |
117 | } | |
118 | return self::find_all_by_params(array('event_id' => $event_id, 'cart_id' => $event_cart_id)); | |
119 | } | |
120 | ||
0cf587a7 EM |
121 | /** |
122 | * @param $params | |
123 | * | |
124 | * @return array | |
125 | */ | |
6a488035 TO |
126 | public static function find_all_by_params($params) { |
127 | $participant = new CRM_Event_BAO_Participant(); | |
128 | $participant->copyValues($params); | |
129 | $result = array(); | |
130 | if ($participant->find()) { | |
131 | while ($participant->fetch()) { | |
132 | $result[] = new CRM_Event_Cart_BAO_MerParticipant(clone($participant)); | |
133 | } | |
134 | } | |
135 | return $result; | |
136 | } | |
137 | ||
0cf587a7 EM |
138 | /** |
139 | * @param $id | |
140 | * | |
141 | * @return mixed | |
142 | */ | |
6a488035 TO |
143 | public static function get_by_id($id) { |
144 | $results = self::find_all_by_params(array('id' => $id)); | |
145 | return array_pop($results); | |
146 | } | |
147 | ||
148 | function load_associations() { | |
149 | $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id); | |
150 | $this->email = $contact_details[1]; | |
151 | } | |
152 | ||
0cf587a7 EM |
153 | /** |
154 | * @return int | |
155 | */ | |
6a488035 TO |
156 | function get_participant_index() { |
157 | if (!$this->cart) { | |
158 | $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->cart_id); | |
159 | $this->cart->load_associations(); | |
160 | } | |
161 | $index = $this->cart->get_participant_index_from_id($this->id); | |
162 | return $index + 1; | |
163 | } | |
164 | ||
0cf587a7 EM |
165 | /** |
166 | * @param $contact | |
167 | * | |
168 | * @return null | |
169 | */ | |
6a488035 TO |
170 | static function billing_address_from_contact($contact) { |
171 | foreach ($contact->address as $loc) { | |
172 | if ($loc['is_billing']) { | |
173 | return $loc; | |
174 | } | |
175 | } | |
176 | foreach ($contact->address as $loc) { | |
177 | if ($loc['is_primary']) { | |
178 | return $loc; | |
179 | } | |
180 | } | |
181 | return NULL; | |
182 | } | |
183 | ||
0cf587a7 EM |
184 | /** |
185 | * @return CRM_Event_Cart_Form_MerParticipant | |
186 | */ | |
6a488035 TO |
187 | function get_form() { |
188 | return new CRM_Event_Cart_Form_MerParticipant($this); | |
189 | } | |
190 | } | |
191 |