Merge pull request #2288 from eileenmcnaughton/CRM-14043
[civicrm-core.git] / CRM / Event / Cart / BAO / MerParticipant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant {
29 public $email = NULL;
30 public $contribution_id = NULL;
31 public $cart = NULL;
32
33 //XXX
34 function __construct($participant = NULL) {
35 parent::__construct();
36 $a = (array)$participant;
37 $this->copyValues($a);
38
39 $this->email = CRM_Utils_Array::value('email', $participant);
40 }
41
42 public static function &create($params) {
43 $participantParams = array(
44 'id' => CRM_Utils_Array::value('id', $params),
45 'role_id' => self::get_attendee_role_id(),
46 'status_id' => self::get_pending_in_cart_status_id(),
47 'contact_id' => $params['contact_id'],
48 'event_id' => $params['event_id'],
49 'cart_id' => $params['cart_id'],
50 //XXX
51 //'registered_by_id' =>
52 //'discount_amount' =>
53 //'fee_level' => $params['fee_level'],
54 );
55 $participant = CRM_Event_BAO_Participant::create($participantParams);
56
57 if (is_a($participant, 'CRM_Core_Error')) {
58 CRM_Core_Error::fatal(ts('There was an error creating a cart participant'));
59 }
60
61 $mer_participant = new CRM_Event_Cart_BAO_MerParticipant($participant);
62
63 return $mer_participant;
64 }
65
66 static function get_attendee_role_id() {
67 $roles = CRM_Event_PseudoConstant::participantRole(NULL, "v.label='Attendee'");
68 $role_names = array_keys($roles);
69 return end($role_names);
70 }
71
72 static function get_pending_in_cart_status_id() {
73 $status_types = CRM_Event_PseudoConstant::participantStatus(NULL, "name='Pending in cart'");
74 $status_names = array_keys($status_types);
75 return end($status_names);
76 }
77
78 public static function find_all_by_cart_id($event_cart_id) {
79 if ($event_cart_id == NULL) {
80 return NULL;
81 }
82 return self::find_all_by_params(array('cart_id' => $event_cart_id));
83 }
84
85 public static function find_all_by_event_and_cart_id($event_id, $event_cart_id) {
86 if ($event_cart_id == NULL) {
87 return NULL;
88 }
89 return self::find_all_by_params(array('event_id' => $event_id, 'cart_id' => $event_cart_id));
90 }
91
92 public static function find_all_by_params($params) {
93 $participant = new CRM_Event_BAO_Participant();
94 $participant->copyValues($params);
95 $result = array();
96 if ($participant->find()) {
97 while ($participant->fetch()) {
98 $result[] = new CRM_Event_Cart_BAO_MerParticipant(clone($participant));
99 }
100 }
101 return $result;
102 }
103
104 public static function get_by_id($id) {
105 $results = self::find_all_by_params(array('id' => $id));
106 return array_pop($results);
107 }
108
109 function load_associations() {
110 $contact_details = CRM_Contact_BAO_Contact::getContactDetails($this->contact_id);
111 $this->email = $contact_details[1];
112 }
113
114 function get_participant_index() {
115 if (!$this->cart) {
116 $this->cart = CRM_Event_Cart_BAO_Cart::find_by_id($this->cart_id);
117 $this->cart->load_associations();
118 }
119 $index = $this->cart->get_participant_index_from_id($this->id);
120 return $index + 1;
121 }
122
123 static function billing_address_from_contact($contact) {
124 foreach ($contact->address as $loc) {
125 if ($loc['is_billing']) {
126 return $loc;
127 }
128 }
129 foreach ($contact->address as $loc) {
130 if ($loc['is_primary']) {
131 return $loc;
132 }
133 }
134 return NULL;
135 }
136
137 function get_form() {
138 return new CRM_Event_Cart_Form_MerParticipant($this);
139 }
140 }
141