commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Event / Cart / BAO / MerParticipant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
28 /**
29 * Class CRM_Event_Cart_BAO_MerParticipant
30 */
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
36 /**
37 * XXX.
38 * @param null $participant
39 */
40 public 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
48 /**
49 * @param array $params
50 *
51 * @return CRM_Event_Cart_BAO_MerParticipant
52 * @throws Exception
53 */
54 public static function create(&$params) {
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
78 /**
79 * @return mixed
80 */
81 public 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
87 /**
88 * @return mixed
89 */
90 public 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
96 /**
97 * @param int $event_cart_id
98 *
99 * @return array|null
100 */
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
108 /**
109 * @param int $event_id
110 * @param int $event_cart_id
111 *
112 * @return array|null
113 */
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
121 /**
122 * @param array $params
123 *
124 * @return array
125 */
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
138 /**
139 * @param int $id
140 *
141 * @return mixed
142 */
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 public function load_associations() {
149 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id);
150 $this->email = $contact_details[1];
151 }
152
153 /**
154 * @return int
155 */
156 public 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
165 /**
166 * @param $contact
167 *
168 * @return null
169 */
170 public 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
184 /**
185 * @return CRM_Event_Cart_Form_MerParticipant
186 */
187 public function get_form() {
188 return new CRM_Event_Cart_Form_MerParticipant($this);
189 }
190
191 }