Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | class CRM_Event_BAO_Event extends CRM_Event_DAO_Event { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Takes a bunch of params that are needed to match certain criteria and | |
46 | * retrieves the relevant objects. Typically the valid params are only | |
47 | * contact_id. We'll tweak this function to be more full featured over a period | |
48 | * of time. This is the inverse function of create. It also stores all the retrieved | |
49 | * values in the default array | |
50 | * | |
51 | * @param array $params (reference ) an assoc array of name/value pairs | |
52 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
53 | * | |
54 | * @return object CRM_Event_BAO_ManageEvent object | |
55 | * @access public | |
56 | * @static | |
57 | */ | |
58 | static function retrieve(&$params, &$defaults) { | |
59 | $event = new CRM_Event_DAO_Event(); | |
60 | $event->copyValues($params); | |
61 | if ($event->find(TRUE)) { | |
62 | CRM_Core_DAO::storeValues($event, $defaults); | |
63 | return $event; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | /** | |
69 | * update the is_active flag in the db | |
70 | * | |
71 | * @param int $id id of the database record | |
72 | * @param boolean $is_active value we want to set the is_active field | |
73 | * | |
74 | * @return Object DAO object on sucess, null otherwise | |
75 | * @static | |
76 | */ | |
77 | static function setIsActive($id, $is_active) { | |
78 | return CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $id, 'is_active', $is_active); | |
79 | } | |
80 | ||
81 | /** | |
82 | * function to add the event | |
83 | * | |
84 | * @param array $params reference array contains the values submitted by the form | |
85 | * | |
86 | * @access public | |
87 | * @static | |
88 | * | |
89 | * @return object | |
90 | */ | |
91 | static function add(&$params) { | |
92 | CRM_Utils_System::flushCache(); | |
93 | ||
94 | if (CRM_Utils_Array::value('id', $params)) { | |
95 | CRM_Utils_Hook::pre('edit', 'Event', $params['id'], $params); | |
96 | } | |
97 | else { | |
98 | CRM_Utils_Hook::pre('create', 'Event', NULL, $params); | |
99 | } | |
100 | ||
101 | $event = new CRM_Event_DAO_Event(); | |
102 | ||
103 | $event->copyValues($params); | |
104 | $result = $event->save(); | |
105 | ||
106 | if (CRM_Utils_Array::value('id', $params)) { | |
107 | CRM_Utils_Hook::post('edit', 'Event', $event->id, $event); | |
108 | } | |
109 | else { | |
110 | CRM_Utils_Hook::post('create', 'Event', $event->id, $event); | |
111 | } | |
112 | ||
113 | return $result; | |
114 | } | |
115 | ||
116 | /** | |
117 | * function to create the event | |
118 | * | |
119 | * @param array $params reference array contains the values submitted by the form | |
120 | * | |
121 | * @access public | |
122 | * @static | |
123 | * | |
124 | */ | |
125 | public static function create(&$params) { | |
126 | $transaction = new CRM_Core_Transaction(); | |
127 | if (empty($params['is_template'])) { | |
128 | $params['is_template'] = 0; | |
129 | } | |
130 | // check if new event, if so set the created_id (if not set) | |
131 | // and always set created_date to now | |
132 | if (empty($params['id'])) { | |
133 | if (empty($params['created_id'])) { | |
134 | $session = CRM_Core_Session::singleton(); | |
135 | $params['created_id'] = $session->get('userID'); | |
136 | } | |
137 | $params['created_date'] = date('YmdHis'); | |
138 | } | |
139 | ||
140 | $event = self::add($params); | |
141 | ||
142 | if (is_a($event, 'CRM_Core_Error')) { | |
143 | CRM_Core_DAO::transaction('ROLLBACK'); | |
144 | return $event; | |
145 | } | |
146 | ||
147 | $session = CRM_Core_Session::singleton(); | |
148 | $contactId = $session->get('userID'); | |
149 | if (!$contactId) { | |
150 | $contactId = CRM_Utils_Array::value('contact_id', $params); | |
151 | } | |
152 | ||
153 | // Log the information on successful add/edit of Event | |
154 | $logParams = array( | |
155 | 'entity_table' => 'civicrm_event', | |
156 | 'entity_id' => $event->id, | |
157 | 'modified_id' => $contactId, | |
158 | 'modified_date' => date('Ymd'), | |
159 | ); | |
160 | ||
161 | CRM_Core_BAO_Log::add($logParams); | |
162 | ||
163 | if (CRM_Utils_Array::value('custom', $params) && | |
164 | is_array($params['custom']) | |
165 | ) { | |
166 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_event', $event->id); | |
167 | } | |
168 | ||
169 | $transaction->commit(); | |
170 | ||
171 | return $event; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Function to delete the event | |
176 | * | |
177 | * @param int $id event id | |
178 | * | |
179 | * @access public | |
180 | * @static | |
181 | * | |
182 | */ | |
183 | static function del($id) { | |
184 | if (!$id) { | |
185 | return NULL; | |
186 | } | |
187 | ||
188 | CRM_Utils_Hook::pre('delete', 'Event', $id, CRM_Core_DAO::$_nullArray); | |
189 | ||
190 | $extends = array('event'); | |
191 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); | |
192 | foreach ($groupTree as $values) { | |
193 | $query = "DELETE FROM " . $values['table_name'] . " WHERE entity_id = " . $id; | |
194 | ||
195 | $params = array(1 => array($values['table_name'], 'string'), | |
196 | 2 => array($id, 'integer'), | |
197 | ); | |
198 | ||
199 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
200 | } | |
201 | ||
202 | // price set cleanup, CRM-5527 | |
203 | CRM_Price_BAO_Set::removeFrom('civicrm_event', $id); | |
204 | ||
205 | $event = new CRM_Event_DAO_Event(); | |
206 | $event->id = $id; | |
207 | ||
208 | if ($event->find(TRUE)) { | |
209 | $locBlockId = $event->loc_block_id; | |
210 | $result = $event->delete(); | |
211 | ||
212 | if (!is_null($locBlockId)) { | |
213 | self::deleteEventLocBlock($locBlockId, $id); | |
214 | } | |
215 | ||
216 | CRM_Utils_Hook::post('delete', 'Event', $id, $event); | |
217 | return $result; | |
218 | } | |
219 | ||
220 | return NULL; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Function to delete the location block associated with an event, | |
225 | * if not being used by any other event. | |
226 | * | |
227 | * @param int $loc_block_id location block id to be deleted | |
228 | * @param int $eventid event id with which loc block is associated | |
229 | * | |
230 | * @access public | |
231 | * @static | |
232 | * | |
233 | */ | |
234 | static function deleteEventLocBlock($locBlockId, $eventId = NULL) { | |
235 | $query = "SELECT count(ce.id) FROM civicrm_event ce WHERE ce.loc_block_id = $locBlockId"; | |
236 | ||
237 | if ($eventId) { | |
238 | $query .= " AND ce.id != $eventId;"; | |
239 | } | |
240 | ||
241 | $locCount = CRM_Core_DAO::singleValueQuery($query); | |
242 | ||
243 | if ($locCount == 0) { | |
244 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); | |
245 | } | |
246 | } | |
247 | ||
248 | /** | |
249 | * Function to get current/future Events | |
250 | * | |
251 | * @param $all boolean true if events all are required else returns current and future events | |
252 | * @param $id int id of a specific event to return | |
253 | * @param $isActive boolean true if you need only active events | |
254 | * @param $checkPermission boolean true if you need to check permission else false | |
255 | * | |
256 | * @static | |
257 | */ | |
258 | static function getEvents($all = FALSE, | |
259 | $id = FALSE, | |
260 | $isActive = TRUE, | |
261 | $checkPermission = TRUE | |
262 | ) { | |
263 | $query = " | |
264 | SELECT `id`, `title`, `start_date` | |
265 | FROM `civicrm_event` | |
266 | WHERE ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0 )"; | |
267 | ||
268 | if ($id) { | |
269 | $query .= " AND `id` = {$id}"; | |
270 | } | |
271 | elseif (!$all) { | |
272 | $endDate = date('YmdHis'); | |
273 | $query .= " AND ( `end_date` >= {$endDate} OR end_date IS NULL )"; | |
274 | } | |
275 | if ($isActive) { | |
276 | $query .= " AND civicrm_event.is_active = 1"; | |
277 | } | |
278 | ||
279 | $query .= " ORDER BY title asc"; | |
280 | $events = array(); | |
281 | ||
282 | $dao = CRM_Core_DAO::executeQuery($query); | |
283 | while ($dao->fetch()) { | |
284 | if ((!$checkPermission || | |
285 | CRM_Event_BAO_Event::checkPermission($dao->id) | |
286 | ) && | |
287 | $dao->title | |
288 | ) { | |
289 | $events[$dao->id] = $dao->title . ' - ' . CRM_Utils_Date::customFormat($dao->start_date); | |
290 | } | |
291 | } | |
292 | ||
293 | return $events; | |
294 | } | |
295 | ||
296 | /** | |
297 | * Function to get events Summary | |
298 | * | |
299 | * @static | |
300 | * | |
301 | * @return array Array of event summary values | |
302 | */ | |
303 | static function getEventSummary() { | |
304 | $eventSummary = $eventIds = array(); | |
305 | $config = CRM_Core_Config::singleton(); | |
306 | ||
307 | // get permission and include them here | |
308 | // does not scale, but rearranging code for now | |
309 | // FIXME in a future release | |
310 | $permissions = CRM_Event_BAO_Event::checkPermission(); | |
311 | $validEventIDs = ''; | |
312 | if (empty($permissions[CRM_Core_Permission::VIEW])) { | |
313 | $eventSummary['total_events'] = 0; | |
314 | return $eventSummary; | |
315 | } | |
316 | else { | |
317 | $validEventIDs = " AND civicrm_event.id IN ( " . implode(',', array_values($permissions[CRM_Core_Permission::VIEW])) . " ) "; | |
318 | } | |
319 | ||
320 | // We're fetching recent and upcoming events (where start date is 7 days ago OR later) | |
321 | $query = " | |
322 | SELECT count(id) as total_events | |
323 | FROM civicrm_event | |
324 | WHERE civicrm_event.is_active = 1 AND | |
325 | ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND | |
326 | civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day ) | |
327 | $validEventIDs"; | |
328 | ||
329 | $dao = CRM_Core_DAO::executeQuery($query); | |
330 | ||
331 | if ($dao->fetch()) { | |
332 | $eventSummary['total_events'] = $dao->total_events; | |
333 | } | |
334 | ||
335 | if (empty($eventSummary) || | |
336 | $dao->total_events == 0 | |
337 | ) { | |
338 | return $eventSummary; | |
339 | } | |
340 | ||
341 | //get the participant status type values. | |
342 | $query = 'SELECT id, name, class FROM civicrm_participant_status_type'; | |
343 | $status = CRM_Core_DAO::executeQuery($query); | |
344 | $statusValues = array(); | |
345 | while ($status->fetch()) { | |
346 | $statusValues[$status->id]['id'] = $status->id; | |
347 | $statusValues[$status->id]['name'] = $status->name; | |
348 | $statusValues[$status->id]['class'] = $status->class; | |
349 | } | |
350 | ||
351 | // Get the Id of Option Group for Event Types | |
352 | $optionGroupDAO = new CRM_Core_DAO_OptionGroup(); | |
353 | $optionGroupDAO->name = 'event_type'; | |
354 | $optionGroupId = NULL; | |
355 | if ($optionGroupDAO->find(TRUE)) { | |
356 | $optionGroupId = $optionGroupDAO->id; | |
357 | } | |
358 | ||
359 | $query = " | |
360 | SELECT civicrm_event.id as id, civicrm_event.title as event_title, civicrm_event.is_public as is_public, | |
361 | civicrm_event.max_participants as max_participants, civicrm_event.start_date as start_date, | |
362 | civicrm_event.end_date as end_date, civicrm_event.is_online_registration, civicrm_event.is_monetary, civicrm_event.is_show_location,civicrm_event.is_map as is_map, civicrm_option_value.label as event_type, civicrm_tell_friend.is_active as is_friend_active, | |
363 | civicrm_event.slot_label_id, | |
364 | civicrm_event.summary as summary, | |
365 | civicrm_pcp_block.id as is_pcp_enabled | |
366 | FROM civicrm_event | |
367 | LEFT JOIN civicrm_option_value ON ( | |
368 | civicrm_event.event_type_id = civicrm_option_value.value AND | |
369 | civicrm_option_value.option_group_id = %1 ) | |
370 | LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_event.id AND civicrm_tell_friend.entity_table = 'civicrm_event' ) | |
371 | LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_event.id AND civicrm_pcp_block.entity_table = 'civicrm_event') | |
372 | WHERE civicrm_event.is_active = 1 AND | |
373 | ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND | |
374 | civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day ) | |
375 | $validEventIDs | |
376 | GROUP BY civicrm_event.id | |
377 | ORDER BY civicrm_event.start_date ASC | |
378 | LIMIT 0, 10 | |
379 | "; | |
380 | $eventParticipant = array(); | |
381 | ||
382 | $properties = array( | |
383 | 'eventTitle' => 'event_title', 'isPublic' => 'is_public', | |
384 | 'maxParticipants' => 'max_participants', 'startDate' => 'start_date', | |
385 | 'endDate' => 'end_date', 'eventType' => 'event_type', | |
386 | 'isMap' => 'is_map', 'participants' => 'participants', | |
387 | 'notCountedDueToRole' => 'notCountedDueToRole', | |
388 | 'notCountedDueToStatus' => 'notCountedDueToStatus', | |
389 | 'notCountedParticipants' => 'notCountedParticipants', | |
390 | ); | |
391 | ||
392 | $params = array(1 => array($optionGroupId, 'Integer')); | |
7431d9cc | 393 | $mappingID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'civicrm_event', 'id', 'entity_value'); |
6a488035 TO |
394 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
395 | while ($dao->fetch()) { | |
396 | foreach ($properties as $property => $name) { | |
397 | $set = NULL; | |
398 | switch ($name) { | |
399 | case 'is_public': | |
400 | if ($dao->$name) { | |
401 | $set = 'Yes'; | |
402 | } | |
403 | else { | |
404 | $set = 'No'; | |
405 | } | |
406 | $eventSummary['events'][$dao->id][$property] = $set; | |
407 | break; | |
408 | ||
409 | case 'is_map': | |
410 | if ($dao->$name && $config->mapAPIKey) { | |
411 | $values = array(); | |
412 | $ids = array(); | |
413 | $params = array('entity_id' => $dao->id, 'entity_table' => 'civicrm_event'); | |
414 | $values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE); | |
415 | if (is_numeric(CRM_Utils_Array::value('geo_code_1', $values['location']['address'][1])) || | |
416 | ($config->mapGeoCoding && | |
417 | !empty($values['location']['address'][1]['city']) && | |
418 | !empty($values['location']['address'][1]['state_province_id']) | |
419 | ) | |
420 | ) { | |
421 | $set = CRM_Utils_System::url('civicrm/contact/map/event', "reset=1&eid={$dao->id}"); | |
422 | } | |
423 | } | |
424 | ||
425 | $eventSummary['events'][$dao->id][$property] = $set; | |
426 | if (in_array($dao->id, $permissions[CRM_Core_Permission::EDIT])) { | |
7431d9cc | 427 | $eventSummary['events'][$dao->id]['configure'] = CRM_Utils_System::url('civicrm/admin/event', "action=update&id=$dao->id&reset=1"); |
6a488035 TO |
428 | } |
429 | break; | |
430 | ||
431 | case 'end_date': | |
432 | case 'start_date': | |
433 | $eventSummary['events'][$dao->id][$property] = CRM_Utils_Date::customFormat($dao->$name, | |
434 | NULL, array('d') | |
435 | ); | |
436 | break; | |
437 | ||
438 | case 'participants': | |
439 | case 'notCountedDueToRole': | |
440 | case 'notCountedDueToStatus': | |
441 | case 'notCountedParticipants': | |
442 | $set = NULL; | |
443 | $propertyCnt = 0; | |
444 | if ($name == 'participants') { | |
445 | $propertyCnt = self::getParticipantCount($dao->id); | |
446 | if ($propertyCnt) { | |
447 | $set = CRM_Utils_System::url('civicrm/event/search', | |
448 | "reset=1&force=1&event=$dao->id&status=true&role=true" | |
449 | ); | |
450 | } | |
451 | } | |
452 | elseif ($name == 'notCountedParticipants') { | |
453 | $propertyCnt = self::getParticipantCount($dao->id, TRUE, FALSE, TRUE, FALSE); | |
454 | if ($propertyCnt) { | |
455 | // FIXME : selector fail to search w/ OR operator. | |
456 | // $set = CRM_Utils_System::url( 'civicrm/event/search', | |
457 | // "reset=1&force=1&event=$dao->id&status=false&role=false" ); | |
458 | } | |
459 | } | |
460 | elseif ($name == 'notCountedDueToStatus') { | |
461 | $propertyCnt = self::getParticipantCount($dao->id, TRUE, FALSE, FALSE, FALSE); | |
462 | if ($propertyCnt) { | |
463 | $set = CRM_Utils_System::url('civicrm/event/search', | |
464 | "reset=1&force=1&event=$dao->id&status=false" | |
465 | ); | |
466 | } | |
467 | } | |
468 | else { | |
469 | $propertyCnt = self::getParticipantCount($dao->id, FALSE, FALSE, TRUE, FALSE); | |
470 | if ($propertyCnt) { | |
471 | $set = CRM_Utils_System::url('civicrm/event/search', | |
472 | "reset=1&force=1&event=$dao->id&role=false" | |
473 | ); | |
474 | } | |
475 | } | |
476 | ||
477 | $eventSummary['events'][$dao->id][$property] = $propertyCnt; | |
478 | $eventSummary['events'][$dao->id][$name . '_url'] = $set; | |
479 | break; | |
480 | ||
481 | default: | |
482 | $eventSummary['events'][$dao->id][$property] = $dao->$name; | |
483 | break; | |
484 | } | |
485 | } | |
486 | ||
487 | // prepare the area for per-status participant counts | |
488 | $statusClasses = array('Positive', 'Pending', 'Waiting', 'Negative'); | |
489 | $eventSummary['events'][$dao->id]['statuses'] = array_fill_keys($statusClasses, array()); | |
490 | ||
491 | $eventSummary['events'][$dao->id]['friend'] = $dao->is_friend_active; | |
492 | $eventSummary['events'][$dao->id]['is_monetary'] = $dao->is_monetary; | |
493 | $eventSummary['events'][$dao->id]['is_online_registration'] = $dao->is_online_registration; | |
494 | $eventSummary['events'][$dao->id]['is_show_location'] = $dao->is_show_location; | |
495 | $eventSummary['events'][$dao->id]['is_subevent'] = $dao->slot_label_id; | |
496 | $eventSummary['events'][$dao->id]['is_pcp_enabled'] = $dao->is_pcp_enabled; | |
7431d9cc | 497 | $eventSummary['events'][$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mappingID); |
eea16664 | 498 | |
6a488035 TO |
499 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(); |
500 | foreach ($statusValues as $statusId => $statusValue) { | |
501 | if (!array_key_exists($statusId, $statusTypes)) { | |
502 | continue; | |
503 | } | |
504 | $class = $statusValue['class']; | |
505 | $statusCount = self::eventTotalSeats($dao->id, "( participant.status_id = {$statusId} )"); | |
506 | if ($statusCount) { | |
507 | $urlString = "reset=1&force=1&event={$dao->id}&status=$statusId"; | |
508 | $statusInfo = array('url' => CRM_Utils_System::url('civicrm/event/search', $urlString), | |
509 | 'name' => $statusValue['name'], | |
510 | 'count' => $statusCount, | |
511 | ); | |
512 | $eventSummary['events'][$dao->id]['statuses'][$class][] = $statusInfo; | |
513 | } | |
514 | } | |
515 | } | |
516 | ||
517 | $countedRoles = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1'); | |
518 | $nonCountedRoles = CRM_Event_PseudoConstant::participantRole(NULL, '( filter = 0 OR filter IS NULL )'); | |
519 | $countedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
520 | $nonCountedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, '( is_counted = 0 OR is_counted IS NULL )'); | |
521 | ||
522 | $countedStatusANDRoles = array_merge($countedStatus, $countedRoles); | |
523 | $nonCountedStatusANDRoles = array_merge($nonCountedStatus, $nonCountedRoles); | |
524 | ||
525 | $eventSummary['nonCountedRoles'] = implode('/', array_values($nonCountedRoles)); | |
526 | $eventSummary['nonCountedStatus'] = implode('/', array_values($nonCountedStatus)); | |
527 | $eventSummary['countedStatusANDRoles'] = implode('/', array_values($countedStatusANDRoles)); | |
528 | $eventSummary['nonCountedStatusANDRoles'] = implode('/', array_values($nonCountedStatusANDRoles)); | |
529 | ||
530 | return $eventSummary; | |
531 | } | |
532 | ||
533 | /** | |
534 | * Function to get participant count | |
535 | * | |
536 | * @param boolean $considerStatus consider status for participant count. | |
537 | * @param boolean $status consider counted participant. | |
538 | * @param boolean $considerRole consider role for participant count. | |
539 | * @param boolean $role consider counted( is filter role) participant. | |
540 | * @param array $eventIds consider participants from given events. | |
541 | * @param boolean $countWithStatus retrieve participant count w/ each participant status. | |
542 | * | |
543 | * @access public | |
544 | * | |
545 | * @return array array with count of participants for each event based on status/role | |
546 | */ | |
547 | public static function getParticipantCount($eventId, | |
548 | $considerStatus = TRUE, | |
549 | $status = TRUE, | |
550 | $considerRole = TRUE, | |
551 | $role = TRUE | |
552 | ) { | |
553 | ||
554 | // consider both role and status for counted participants, CRM-4924. | |
555 | $operator = " AND "; | |
556 | // not counted participant. | |
557 | if ($considerStatus && $considerRole && !$status && !$role) { | |
558 | $operator = " OR "; | |
559 | } | |
560 | $clause = array(); | |
561 | if ($considerStatus) { | |
562 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
563 | $statusClause = 'NOT IN'; | |
564 | if ($status) { | |
565 | $statusClause = 'IN'; | |
566 | } | |
567 | $status = implode(',', array_keys($statusTypes)); | |
568 | if (empty($status)) { | |
569 | $status = 0; | |
570 | } | |
571 | $clause[] = "participant.status_id {$statusClause} ( {$status} ) "; | |
572 | } | |
573 | ||
574 | if ($considerRole) { | |
575 | $roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1'); | |
576 | $roleClause = 'NOT IN'; | |
577 | if ($role) { | |
578 | $roleClause = 'IN'; | |
579 | } | |
580 | $roles = implode(',', array_keys($roleTypes)); | |
581 | if (empty($roles)) { | |
582 | $roles = 0; | |
583 | } | |
584 | $clause[] = "participant.role_id {$roleClause} ( $roles )"; | |
585 | } | |
586 | ||
587 | $sqlClause = ''; | |
588 | if (!empty($clause)) { | |
589 | $sqlClause = ' ( ' . implode($operator, $clause) . ' )'; | |
590 | } | |
591 | ||
592 | return self::eventTotalSeats($eventId, $sqlClause); | |
593 | } | |
594 | ||
595 | /** | |
596 | * function to get the information to map a event | |
597 | * | |
598 | * @param array $ids the list of ids for which we want map info | |
599 | * | |
600 | * @return null|string title of the event | |
601 | * @static | |
602 | * @access public | |
603 | */ | |
604 | static function &getMapInfo(&$id) { | |
605 | ||
606 | $sql = " | |
607 | SELECT | |
608 | civicrm_event.id AS event_id, | |
609 | civicrm_event.title AS display_name, | |
610 | civicrm_address.street_address AS street_address, | |
611 | civicrm_address.city AS city, | |
612 | civicrm_address.postal_code AS postal_code, | |
613 | civicrm_address.postal_code_suffix AS postal_code_suffix, | |
614 | civicrm_address.geo_code_1 AS latitude, | |
615 | civicrm_address.geo_code_2 AS longitude, | |
616 | civicrm_state_province.abbreviation AS state, | |
617 | civicrm_country.name AS country, | |
618 | civicrm_location_type.name AS location_type | |
619 | FROM | |
620 | civicrm_event | |
621 | LEFT JOIN civicrm_loc_block ON ( civicrm_event.loc_block_id = civicrm_loc_block.id ) | |
622 | LEFT JOIN civicrm_address ON ( civicrm_loc_block.address_id = civicrm_address.id ) | |
623 | LEFT JOIN civicrm_state_province ON ( civicrm_address.state_province_id = civicrm_state_province.id ) | |
624 | LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id | |
625 | LEFT JOIN civicrm_location_type ON ( civicrm_location_type.id = civicrm_address.location_type_id ) | |
626 | WHERE civicrm_address.geo_code_1 IS NOT NULL | |
627 | AND civicrm_address.geo_code_2 IS NOT NULL | |
628 | AND civicrm_event.id = " . CRM_Utils_Type::escape($id, 'Integer'); | |
629 | ||
630 | $dao = new CRM_Core_DAO(); | |
631 | $dao->query($sql); | |
632 | ||
633 | $locations = array(); | |
634 | ||
635 | $config = CRM_Core_Config::singleton(); | |
636 | ||
637 | while ($dao->fetch()) { | |
638 | ||
639 | $location = array(); | |
640 | $location['displayName'] = addslashes($dao->display_name); | |
641 | $location['lat'] = $dao->latitude; | |
642 | $location['marker_class'] = 'Event'; | |
643 | $location['lng'] = $dao->longitude; | |
644 | ||
645 | $params = array('entity_id' => $id, 'entity_table' => 'civicrm_event'); | |
646 | $addressValues = CRM_Core_BAO_Location::getValues($params, TRUE); | |
647 | $location['address'] = str_replace(array("\r", "\n"), '', addslashes(nl2br($addressValues['address'][1]['display_text']))); | |
648 | ||
649 | $location['url'] = CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $dao->event_id); | |
650 | $location['location_type'] = $dao->location_type; | |
651 | $eventImage = '<img src="' . $config->resourceBase . 'i/contact_org.gif" alt="Organization " height="20" width="15" />'; | |
652 | $location['image'] = $eventImage; | |
653 | $location['displayAddress'] = str_replace('<br />', ', ', $location['address']); | |
654 | $locations[] = $location; | |
655 | } | |
656 | return $locations; | |
657 | } | |
658 | ||
659 | /** | |
660 | * function to get the complete information for one or more events | |
661 | * | |
662 | * @param date $start get events with start date >= this date | |
663 | * @param integer $type get events on the a specific event type (by event_type_id) | |
664 | * @param integer $eventId return a single event - by event id | |
665 | * @param date $end also get events with end date >= this date | |
666 | * | |
667 | * @return array $all array of all the events that are searched | |
668 | * @static | |
669 | * @access public | |
670 | */ | |
671 | static function &getCompleteInfo($start = NULL, $type = NULL, $eventId = NULL, $end = NULL) { | |
672 | $dateCondition = ''; | |
673 | // if start and end date are NOT passed, return all events with start_date OR end_date >= today CRM-5133 | |
674 | if ($start) { | |
675 | // get events with start_date >= requested start | |
676 | $startDate = CRM_Utils_Type::escape($start, 'Date'); | |
677 | $dateCondition .= " AND ( civicrm_event.start_date >= {$startDate} )"; | |
678 | } | |
679 | ||
680 | if ($end) { | |
681 | // also get events with end_date <= requested end | |
682 | $endDate = CRM_Utils_Type::escape($end, 'Date'); | |
683 | $dateCondition .= " AND ( civicrm_event.end_date <= '{$endDate}' ) "; | |
684 | } | |
685 | ||
686 | // CRM-9421 and CRM-8620 Default mode for ical/rss feeds. No start or end filter passed. Need to exclude old events with only start date | |
687 | // and not exclude events in progress (start <= today and end >= today). DGG | |
688 | if (empty($start) && empty($end)) { | |
689 | // get events with end date >= today, not sure of this logic | |
690 | // but keeping this for backward compatibility as per issue CRM-5133 | |
691 | $today = date("Y-m-d G:i:s"); | |
692 | $dateCondition .= " AND ( civicrm_event.end_date >= '{$today}' OR civicrm_event.start_date >= '{$today}' ) "; | |
693 | } | |
694 | ||
695 | if ($type) { | |
696 | $typeCondition = " AND civicrm_event.event_type_id = " . CRM_Utils_Type::escape($type, 'Integer'); | |
697 | } | |
698 | ||
699 | // Get the Id of Option Group for Event Types | |
700 | $optionGroupDAO = new CRM_Core_DAO_OptionGroup(); | |
701 | $optionGroupDAO->name = 'event_type'; | |
702 | $optionGroupId = NULL; | |
703 | if ($optionGroupDAO->find(TRUE)) { | |
704 | $optionGroupId = $optionGroupDAO->id; | |
705 | } | |
706 | ||
707 | $query = " | |
708 | SELECT | |
709 | civicrm_event.id as event_id, | |
710 | civicrm_email.email as email, | |
711 | civicrm_event.title as title, | |
712 | civicrm_event.summary as summary, | |
713 | civicrm_event.start_date as start, | |
714 | civicrm_event.end_date as end, | |
715 | civicrm_event.description as description, | |
716 | civicrm_event.is_show_location as is_show_location, | |
717 | civicrm_event.is_online_registration as is_online_registration, | |
718 | civicrm_event.registration_link_text as registration_link_text, | |
719 | civicrm_event.registration_start_date as registration_start_date, | |
720 | civicrm_event.registration_end_date as registration_end_date, | |
721 | civicrm_option_value.label as event_type, | |
722 | civicrm_address.name as address_name, | |
723 | civicrm_address.street_address as street_address, | |
724 | civicrm_address.supplemental_address_1 as supplemental_address_1, | |
725 | civicrm_address.supplemental_address_2 as supplemental_address_2, | |
726 | civicrm_address.city as city, | |
727 | civicrm_address.postal_code as postal_code, | |
728 | civicrm_address.postal_code_suffix as postal_code_suffix, | |
729 | civicrm_state_province.abbreviation as state, | |
730 | civicrm_country.name AS country | |
731 | FROM civicrm_event | |
732 | LEFT JOIN civicrm_loc_block ON civicrm_event.loc_block_id = civicrm_loc_block.id | |
733 | LEFT JOIN civicrm_address ON civicrm_loc_block.address_id = civicrm_address.id | |
734 | LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id | |
735 | LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id | |
736 | LEFT JOIN civicrm_email ON civicrm_loc_block.email_id = civicrm_email.id | |
737 | LEFT JOIN civicrm_option_value ON ( | |
738 | civicrm_event.event_type_id = civicrm_option_value.value AND | |
739 | civicrm_option_value.option_group_id = %1 ) | |
740 | WHERE civicrm_event.is_active = 1 | |
741 | AND civicrm_event.is_public = 1 | |
742 | AND (is_template = 0 OR is_template IS NULL) | |
743 | {$dateCondition}"; | |
744 | ||
745 | if (isset($typeCondition)) { | |
746 | $query .= $typeCondition; | |
747 | } | |
748 | ||
749 | if (isset($eventId)) { | |
750 | $query .= " AND civicrm_event.id =$eventId "; | |
751 | } | |
752 | $query .= " ORDER BY civicrm_event.start_date ASC"; | |
753 | ||
754 | ||
755 | $params = array(1 => array($optionGroupId, 'Integer')); | |
756 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
757 | $all = array(); | |
758 | $config = CRM_Core_Config::singleton(); | |
759 | ||
760 | $baseURL = parse_url($config->userFrameworkBaseURL); | |
761 | $url = "@" . $baseURL['host']; | |
762 | if (CRM_Utils_Array::value('path', $baseURL)) { | |
763 | $url .= substr($baseURL['path'], 0, -1); | |
764 | } | |
765 | ||
766 | // check 'view event info' permission | |
767 | $permissions = CRM_Core_Permission::event(CRM_Core_Permission::VIEW); | |
768 | ||
769 | // check if we're in shopping cart mode for events | |
770 | $enable_cart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, | |
771 | 'enable_cart' | |
772 | ); | |
773 | if ($enable_cart) {} | |
774 | while ($dao->fetch()) { | |
775 | if (in_array($dao->event_id, $permissions)) { | |
776 | $info = array(); | |
777 | $info['uid'] = "CiviCRM_EventID_{$dao->event_id}_" . md5($config->userFrameworkBaseURL) . $url; | |
778 | ||
779 | $info['title'] = $dao->title; | |
780 | $info['event_id'] = $dao->event_id; | |
781 | $info['summary'] = $dao->summary; | |
782 | $info['description'] = $dao->description; | |
783 | $info['start_date'] = $dao->start; | |
784 | $info['end_date'] = $dao->end; | |
785 | $info['contact_email'] = $dao->email; | |
786 | $info['event_type'] = $dao->event_type; | |
787 | $info['is_show_location'] = $dao->is_show_location; | |
788 | $info['is_online_registration'] = $dao->is_online_registration; | |
789 | $info['registration_link_text'] = $dao->registration_link_text; | |
790 | $info['registration_start_date'] = $dao->registration_start_date; | |
791 | $info['registration_end_date'] = $dao->registration_end_date; | |
792 | ||
793 | $address = ''; | |
794 | ||
795 | $addrFields = array( | |
796 | 'address_name' => $dao->address_name, | |
797 | 'street_address' => $dao->street_address, | |
798 | 'supplemental_address_1' => $dao->supplemental_address_1, | |
799 | 'supplemental_address_2' => $dao->supplemental_address_2, | |
800 | 'city' => $dao->city, | |
801 | 'state_province' => $dao->state, | |
802 | 'postal_code' => $dao->postal_code, | |
803 | 'postal_code_suffix' => $dao->postal_code_suffix, | |
804 | 'country' => $dao->country, | |
805 | 'county' => NULL, | |
806 | ); | |
807 | ||
808 | CRM_Utils_String::append($address, ', ', | |
809 | CRM_Utils_Address::format($addrFields) | |
810 | ); | |
811 | $info['location'] = $address; | |
812 | $info['url'] = CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE); | |
813 | ||
814 | if ($enable_cart) { | |
815 | $reg = CRM_Event_Cart_BAO_EventInCart::get_registration_link($dao->event_id); | |
816 | $info['registration_link'] = CRM_Utils_System::url($reg['path'], $reg['query'], TRUE); | |
817 | $info['registration_link_text'] = $reg['label']; | |
818 | } | |
819 | ||
820 | $all[] = $info; | |
821 | } | |
822 | } | |
823 | ||
824 | return $all; | |
825 | } | |
826 | ||
827 | /** | |
828 | * This function is to make a copy of a Event, including | |
829 | * all the fields in the event Wizard | |
830 | * | |
831 | * @param int $id the event id to copy | |
832 | * obj $newEvent object of CRM_Event_DAO_Event | |
833 | * boolean $afterCreate call to copy after the create function | |
834 | * @return void | |
835 | * @access public | |
836 | */ | |
837 | static function copy($id, $newEvent = NULL, $afterCreate = FALSE) { | |
838 | ||
839 | $defaults = $eventValues = array(); | |
840 | ||
841 | //get the require event values. | |
842 | $eventParams = array('id' => $id); | |
843 | $returnProperties = array('loc_block_id', 'is_show_location', 'default_fee_id', 'default_discount_fee_id', 'is_template'); | |
844 | ||
845 | CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $eventParams, $eventValues, $returnProperties); | |
846 | ||
847 | // since the location is sharable, lets use the same loc_block_id. | |
848 | $locBlockId = CRM_Utils_Array::value('loc_block_id', $eventValues); | |
849 | ||
850 | $fieldsFix = ($afterCreate) ? array( ) : array('prefix' => array('title' => ts('Copy of') . ' ')); | |
851 | if (!CRM_Utils_Array::value('is_show_location', $eventValues)) { | |
852 | $fieldsFix['prefix']['is_show_location'] = 0; | |
853 | } | |
854 | ||
855 | if ($newEvent && is_a($newEvent, 'CRM_Event_DAO_Event')) { | |
856 | $copyEvent = $newEvent; | |
857 | } | |
858 | ||
859 | if (!isset($copyEvent)) { | |
860 | $copyEvent = &CRM_Core_DAO::copyGeneric('CRM_Event_DAO_Event', | |
861 | array('id' => $id), | |
862 | array( | |
863 | 'loc_block_id' => | |
864 | ($locBlockId) ? $locBlockId : NULL, | |
865 | ), | |
866 | $fieldsFix | |
867 | ); | |
868 | } | |
c6914066 | 869 | CRM_Price_BAO_Set::copyPriceSet('civicrm_event', $id, $copyEvent->id); |
6a488035 TO |
870 | $copyUF = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', |
871 | array( | |
872 | 'entity_id' => $id, | |
873 | 'entity_table' => 'civicrm_event', | |
874 | ), | |
875 | array('entity_id' => $copyEvent->id) | |
876 | ); | |
877 | ||
878 | $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', | |
879 | array( | |
880 | 'entity_id' => $id, | |
881 | 'entity_table' => 'civicrm_event', | |
882 | ), | |
883 | array('entity_id' => $copyEvent->id) | |
884 | ); | |
885 | ||
886 | $copyPCP = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', | |
887 | array( | |
888 | 'entity_id' => $id, | |
889 | 'entity_table' => 'civicrm_event', | |
890 | ), | |
891 | array('entity_id' => $copyEvent->id), | |
892 | array('replace' => array('target_entity_id' => $copyEvent->id)) | |
893 | ); | |
894 | ||
895 | if ($eventValues['is_template']) { | |
896 | $field = 'event_template'; | |
897 | } | |
898 | else { | |
899 | $field = 'civicrm_event'; | |
900 | } | |
901 | $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); | |
902 | $oldData = array('entity_value' => $id, 'mapping_id' => $mappingId); | |
903 | if ($copyEvent->is_template == 1) { | |
904 | $field = 'event_template'; | |
905 | } | |
906 | else { | |
907 | $field = 'civicrm_event'; | |
908 | } | |
909 | $copyMappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', $field, 'id', 'entity_value'); | |
910 | $newData = array('entity_value' => $copyEvent->id, 'mapping_id' => $copyMappingId); | |
911 | $copyReminder = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule', | |
912 | $oldData, | |
913 | $newData | |
914 | ); | |
915 | ||
916 | if (!$afterCreate) { | |
917 | //copy custom data | |
918 | $extends = array('event'); | |
919 | $groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends); | |
920 | if ($groupTree) { | |
921 | foreach ($groupTree as $groupID => $group) { | |
922 | $table[$groupTree[$groupID]['table_name']] = array('entity_id'); | |
923 | foreach ($group['fields'] as $fieldID => $field) { | |
924 | $table[$groupTree[$groupID]['table_name']][] = $groupTree[$groupID]['fields'][$fieldID]['column_name']; | |
925 | } | |
926 | } | |
927 | ||
928 | foreach ($table as $tableName => $tableColumns) { | |
929 | $insert = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $tableColumns) . ') '; | |
930 | $tableColumns[0] = $copyEvent->id; | |
931 | $select = 'SELECT ' . implode(', ', $tableColumns); | |
932 | $from = ' FROM ' . $tableName; | |
933 | $where = " WHERE {$tableName}.entity_id = {$id}"; | |
934 | $query = $insert . $select . $from . $where; | |
935 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
936 | } | |
937 | } | |
938 | } | |
939 | $copyEvent->save(); | |
940 | ||
941 | CRM_Utils_System::flushCache(); | |
942 | if (!$afterCreate) { | |
943 | CRM_Utils_Hook::copy('Event', $copyEvent); | |
944 | } | |
945 | return $copyEvent; | |
946 | } | |
947 | ||
948 | /** | |
949 | * This is sometimes called in a loop (during event search) | |
950 | * hence we cache the values to prevent repeated calls to the db | |
951 | */ | |
952 | static function isMonetary($id) { | |
953 | static $isMonetary = array(); | |
954 | if (!array_key_exists($id, $isMonetary)) { | |
955 | $isMonetary[$id] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', | |
956 | $id, | |
957 | 'is_monetary' | |
958 | ); | |
959 | } | |
960 | return $isMonetary[$id]; | |
961 | } | |
962 | ||
963 | /** | |
964 | * This is sometimes called in a loop (during event search) | |
965 | * hence we cache the values to prevent repeated calls to the db | |
966 | */ | |
967 | static function usesPriceSet($id) { | |
968 | static $usesPriceSet = array(); | |
969 | if (!array_key_exists($id, $usesPriceSet)) { | |
970 | $usesPriceSet[$id] = CRM_Price_BAO_Set::getFor('civicrm_event', $id); | |
971 | } | |
972 | return $usesPriceSet[$id]; | |
973 | } | |
974 | ||
975 | /** | |
976 | * Process that send e-mails | |
977 | * | |
978 | * @return void | |
979 | * @access public | |
980 | */ | |
981 | static function sendMail($contactID, &$values, $participantId, $isTest = FALSE, $returnMessageText = FALSE) { | |
982 | ||
983 | $template = CRM_Core_Smarty::singleton(); | |
984 | $gIds = array( | |
985 | 'custom_pre_id' => $values['custom_pre_id'], | |
986 | 'custom_post_id' => $values['custom_post_id'], | |
987 | ); | |
988 | ||
989 | //get the params submitted by participant. | |
990 | $participantParams = CRM_Utils_Array::value($participantId, $values['params'], array()); | |
991 | ||
992 | if (!$returnMessageText) { | |
993 | //send notification email if field values are set (CRM-1941) | |
994 | foreach ($gIds as $key => $gIdValues) { | |
995 | if ($gIdValues) { | |
996 | if (!is_array($gIdValues)) { | |
997 | $gIdValues = array( $gIdValues ); | |
998 | } | |
999 | ||
1000 | foreach ($gIdValues as $gId) { | |
1001 | $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify'); | |
1002 | if ($email) { | |
1003 | //get values of corresponding profile fields for notification | |
1004 | list($profileValues) = self::buildCustomDisplay($gId, | |
1005 | NULL, | |
1006 | $contactID, | |
1007 | $template, | |
1008 | $participantId, | |
1009 | $isTest, | |
1010 | TRUE, | |
1011 | $participantParams | |
1012 | ); | |
1013 | list($profileValues) = $profileValues; | |
1014 | $val = array( | |
1015 | 'id' => $gId, | |
1016 | 'values' => $profileValues, | |
1017 | 'email' => $email, | |
1018 | ); | |
1019 | CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val); | |
1020 | } | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | if ($values['event']['is_email_confirm'] || $returnMessageText) { | |
1027 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); | |
1028 | ||
1029 | //send email only when email is present | |
1030 | if (isset($email) || $returnMessageText) { | |
1031 | $preProfileID = CRM_Utils_Array::value('custom_pre_id', $values); | |
1032 | $postProfileID = CRM_Utils_Array::value('custom_post_id', $values); | |
1033 | ||
1034 | if (CRM_Utils_Array::value('additionalParticipant', $values['params'])) { | |
1035 | $preProfileID = CRM_Utils_Array::value('additional_custom_pre_id', $values, $preProfileID ); | |
1036 | $postProfileID = CRM_Utils_Array::value('additional_custom_post_id', $values, $postProfileID ); | |
1037 | } | |
1038 | ||
1039 | self::buildCustomDisplay($preProfileID, | |
1040 | 'customPre', | |
1041 | $contactID, | |
1042 | $template, | |
1043 | $participantId, | |
1044 | $isTest, | |
1045 | NULL, | |
1046 | $participantParams | |
1047 | ); | |
1048 | ||
1049 | self::buildCustomDisplay($postProfileID, | |
1050 | 'customPost', | |
1051 | $contactID, | |
1052 | $template, | |
1053 | $participantId, | |
1054 | $isTest, | |
1055 | NULL, | |
1056 | $participantParams | |
1057 | ); | |
1058 | ||
1059 | $sessions = CRM_Event_Cart_BAO_Conference::get_participant_sessions($participantId); | |
1060 | ||
1061 | $tplParams = array_merge($values, $participantParams, array( | |
1062 | 'email' => $email, | |
1063 | 'confirm_email_text' => CRM_Utils_Array::value('confirm_email_text', $values['event']), | |
1064 | 'isShowLocation' => CRM_Utils_Array::value('is_show_location', $values['event']), | |
1065 | 'contributeMode' => NULL, | |
1066 | 'participantID' => $participantId, | |
1067 | 'conference_sessions' => $sessions, | |
1068 | )); | |
1069 | ||
1070 | $sendTemplateParams = array( | |
1071 | 'groupName' => 'msg_tpl_workflow_event', | |
1072 | 'valueName' => 'event_online_receipt', | |
1073 | 'contactId' => $contactID, | |
1074 | 'isTest' => $isTest, | |
1075 | 'tplParams' => $tplParams, | |
1076 | 'PDFFilename' => 'eventReceipt.pdf', | |
1077 | ); | |
1078 | ||
1079 | // address required during receipt processing (pdf and email receipt) | |
1080 | if ($displayAddress = CRM_Utils_Array::value('address', $values)) { | |
1081 | $sendTemplateParams['tplParams']['address'] = $displayAddress; | |
1082 | $sendTemplateParams['tplParams']['contributeMode'] = NULL; | |
1083 | } | |
1084 | ||
1085 | // set lineItem details | |
1086 | if ($lineItem = CRM_Utils_Array::value('lineItem', $values)) { | |
1087 | // check if additional prticipant, if so filter only to relevant ones | |
1088 | // CRM-9902 | |
1089 | if (CRM_Utils_Array::value('additionalParticipant', $values['params'])) { | |
1090 | $ownLineItems = array( ); | |
1091 | foreach ( $lineItem as $liKey => $liValue ) { | |
1092 | $firstElement = array_pop( $liValue ); | |
1093 | if ( $firstElement['entity_id'] == $participantId ) { | |
1094 | $ownLineItems[0] = $lineItem[$liKey]; | |
1095 | break; | |
1096 | } | |
1097 | } | |
1098 | if ( ! empty( $ownLineItems ) ) { | |
1099 | $sendTemplateParams['tplParams']['lineItem'] = $ownLineItems; | |
1100 | } | |
1101 | } | |
1102 | else { | |
1103 | $sendTemplateParams['tplParams']['lineItem'] = $lineItem; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | if ($returnMessageText) { | |
1108 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams); | |
1109 | return array( | |
1110 | 'subject' => $subject, | |
1111 | 'body' => $message, | |
1112 | 'to' => $displayName, | |
1113 | 'html' => $html, | |
1114 | ); | |
1115 | } | |
1116 | else { | |
1117 | $sendTemplateParams['from'] = CRM_Utils_Array::value('confirm_from_name', $values['event']) . " <{$values['event']['confirm_from_email']}>"; | |
1118 | $sendTemplateParams['toName'] = $displayName; | |
1119 | $sendTemplateParams['toEmail'] = $email; | |
1120 | $sendTemplateParams['autoSubmitted'] = TRUE; | |
1121 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_confirm', | |
1122 | $values['event'] | |
1123 | ); | |
1124 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_confirm', | |
1125 | $values['event'] | |
1126 | ); | |
1127 | CRM_Core_BAO_MessageTemplates::sendTemplate($sendTemplateParams); | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | /** | |
1134 | * Function to add the custom fields OR array of participant's | |
1135 | * profile info | |
1136 | * | |
1137 | * @return None | |
1138 | * @access public | |
1139 | */ | |
1140 | static function buildCustomDisplay($id, | |
1141 | $name, | |
1142 | $cid, | |
1143 | &$template, | |
1144 | $participantId, | |
1145 | $isTest, | |
1146 | $isCustomProfile = FALSE, | |
1147 | $participantParams = array() | |
1148 | ) { | |
1149 | if (!$id) { | |
1150 | return array(NULL, NULL); | |
1151 | } | |
1152 | ||
1153 | if (!is_array($id)) { | |
1154 | $id = CRM_Utils_Type::escape($id, 'Positive'); | |
1155 | $profileIds = array($id); | |
1156 | } | |
1157 | else { | |
1158 | $profileIds = $id; | |
1159 | } | |
1160 | ||
1161 | foreach ($profileIds as $gid) { | |
1162 | if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) { | |
1163 | $values = array(); | |
1164 | $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, | |
1165 | NULL, NULL, FALSE, NULL, | |
1166 | FALSE, NULL, CRM_Core_Permission::CREATE, | |
1167 | 'field_name', TRUE | |
1168 | ); | |
1169 | ||
1170 | //this condition is added, since same contact can have multiple event registrations.. | |
1171 | $params = array(array('participant_id', '=', $participantId, 0, 0)); | |
1172 | ||
1173 | //add participant id | |
1174 | $fields['participant_id'] = array( | |
1175 | 'name' => 'participant_id', | |
1176 | 'title' => 'Participant Id', | |
1177 | ); | |
1178 | //check whether its a text drive | |
1179 | if ($isTest) { | |
1180 | $params[] = array('participant_test', '=', 1, 0, 0); | |
1181 | } | |
1182 | ||
1183 | //display campaign on thankyou page. | |
1184 | if (array_key_exists('participant_campaign_id', $fields)) { | |
1185 | if ($participantId) { | |
1186 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
1187 | $participantId, | |
1188 | 'campaign_id' | |
1189 | ); | |
1190 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); | |
1191 | $values[$fields['participant_campaign_id']['title']] = CRM_Utils_Array::value($campaignId, | |
1192 | $campaigns | |
1193 | ); | |
1194 | } | |
1195 | unset($fields['participant_campaign_id']); | |
1196 | } | |
1197 | ||
1198 | $groupTitle = NULL; | |
1199 | foreach ($fields as $k => $v) { | |
1200 | if (!$groupTitle) { | |
7431d9cc | 1201 | $groupTitle = $v['groupTitle']; |
6a488035 TO |
1202 | } |
1203 | // suppress all file fields from display | |
1204 | if ( | |
1205 | CRM_Utils_Array::value('data_type', $v, '') == 'File' || | |
1206 | CRM_Utils_Array::value('name', $v, '') == 'image_URL' || | |
1207 | CRM_Utils_Array::value('field_type', $v) == 'Formatting' | |
1208 | ) { | |
1209 | unset($fields[$k]); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | if ($groupTitle) { | |
1214 | $groupTitles[] = $groupTitle; | |
1215 | } | |
1216 | //display profile groups those are subscribed by participant. | |
1217 | if (($groups = CRM_Utils_Array::value('group', $participantParams)) && | |
1218 | is_array($groups) | |
1219 | ) { | |
1220 | $grpIds = array(); | |
1221 | foreach ($groups as $grpId => $isSelected) { | |
1222 | if ($isSelected) { | |
1223 | $grpIds[] = $grpId; | |
1224 | } | |
1225 | } | |
1226 | if (!empty($grpIds)) { | |
1227 | //get the group titles. | |
1228 | $grpTitles = array(); | |
1229 | $query = 'SELECT title FROM civicrm_group where id IN ( ' . implode(',', $grpIds) . ' )'; | |
1230 | $grp = CRM_Core_DAO::executeQuery($query); | |
1231 | while ($grp->fetch()) { | |
1232 | $grpTitles[] = $grp->title; | |
1233 | } | |
1234 | if (!empty($grpTitles) && | |
1235 | CRM_Utils_Array::value('title', CRM_Utils_Array::value('group', $fields)) | |
1236 | ) { | |
1237 | $values[$fields['group']['title']] = implode(', ', $grpTitles); | |
1238 | } | |
1239 | unset($fields['group']); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params); | |
1244 | ||
1245 | if (isset($fields['participant_status_id']['title']) && | |
1246 | isset($values[$fields['participant_status_id']['title']]) && | |
1247 | is_numeric($values[$fields['participant_status_id']['title']]) | |
1248 | ) { | |
1249 | $status = array(); | |
1250 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1251 | $values[$fields['participant_status_id']['title']] = $status[$values[$fields['participant_status_id']['title']]]; | |
1252 | } | |
1253 | ||
1254 | if (isset($fields['participant_role_id']['title']) && | |
1255 | isset($values[$fields['participant_role_id']['title']]) && | |
1256 | is_numeric($values[$fields['participant_role_id']['title']]) | |
1257 | ) { | |
1258 | $roles = array(); | |
1259 | $roles = CRM_Event_PseudoConstant::participantRole(); | |
1260 | $values[$fields['participant_role_id']['title']] = $roles[$values[$fields['participant_role_id']['title']]]; | |
1261 | } | |
1262 | ||
1263 | if (isset($fields['participant_register_date']['title']) && | |
1264 | isset($values[$fields['participant_register_date']['title']]) | |
1265 | ) { | |
1266 | $values[$fields['participant_register_date']['title']] = CRM_Utils_Date::customFormat($values[$fields['participant_register_date']['title']]); | |
1267 | } | |
1268 | ||
1269 | //handle fee_level for price set | |
1270 | if (isset($fields['participant_fee_level']['title']) && | |
1271 | isset($values[$fields['participant_fee_level']['title']]) | |
1272 | ) { | |
1273 | $feeLevel = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1274 | $values[$fields['participant_fee_level']['title']] | |
1275 | ); | |
1276 | foreach ($feeLevel as $key => $value) { | |
1277 | if (!$value) { | |
1278 | unset($feeLevel[$key]); | |
1279 | } | |
1280 | } | |
7431d9cc | 1281 | $values[$fields['participant_fee_level']['title']] = implode(',', $feeLevel); |
6a488035 TO |
1282 | } |
1283 | ||
1284 | unset($values[$fields['participant_id']['title']]); | |
1285 | ||
1286 | $val[] = $values; | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | if (count($val)) { | |
1291 | $template->assign($name, $val); | |
1292 | } | |
1293 | ||
1294 | if (count($groupTitles)) { | |
1295 | $template->assign($name . '_grouptitle', $groupTitles); | |
1296 | } | |
1297 | ||
1298 | //return if we only require array of participant's info. | |
1299 | if ($isCustomProfile) { | |
1300 | if (count($val)) { | |
1301 | return array($val, $groupTitles); | |
1302 | } | |
1303 | else { | |
1304 | return NULL; | |
1305 | } | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | /** | |
1310 | * Function to build the array for display the profile fields | |
1311 | * | |
1312 | * @param array $params key value. | |
1313 | * @param int $gid profile Id | |
1314 | * @param array $groupTitle Profile Group Title. | |
1315 | * @param array $values formatted array of key value | |
1316 | * | |
1317 | * @return None | |
1318 | * @access public | |
1319 | * @static | |
1320 | */ | |
1321 | static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$profileFields = array()) { | |
1322 | if ($gid) { | |
1323 | $session = CRM_Core_Session::singleton(); | |
1324 | $contactID = $session->get('userID'); | |
1325 | if ($contactID) { | |
1326 | if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $contactID)) { | |
1327 | $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW); | |
1328 | } | |
1329 | } | |
1330 | else { | |
1331 | $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::ADD); | |
1332 | } | |
1333 | ||
1334 | foreach ($fields as $v) { | |
1335 | if (CRM_Utils_Array::value('groupTitle', $v)) { | |
7431d9cc | 1336 | $groupTitle['groupTitle'] = $v['groupTitle']; |
6a488035 TO |
1337 | break; |
1338 | } | |
1339 | } | |
1340 | $customVal = ''; | |
e7e657f0 | 1341 | $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); |
6a488035 TO |
1342 | //start of code to set the default values |
1343 | foreach ($fields as $name => $field) { | |
9e7eeb0f | 1344 | $skip = FALSE; |
6a488035 TO |
1345 | // skip fields that should not be displayed separately |
1346 | if ($field['skipDisplay']) { | |
1347 | continue; | |
1348 | } | |
1349 | ||
1350 | $index = $field['title']; | |
1351 | if ($name === 'organization_name') { | |
1352 | $values[$index] = $params[$name]; | |
1353 | } | |
1354 | ||
1355 | if ('state_province' == substr($name, 0, 14)) { | |
1356 | if ($params[$name]) { | |
1357 | $values[$index] = CRM_Core_PseudoConstant::stateProvince($params[$name]); | |
1358 | } | |
1359 | else { | |
1360 | $values[$index] = ''; | |
1361 | } | |
1362 | } | |
1363 | elseif ('date' == substr($name, -4)) { | |
1364 | $values[$index] = $params[$name]; | |
1365 | } | |
1366 | elseif ('country' == substr($name, 0, 7)) { | |
1367 | if ($params[$name]) { | |
1368 | $values[$index] = CRM_Core_PseudoConstant::country($params[$name]); | |
1369 | } | |
1370 | else { | |
1371 | $values[$index] = ''; | |
1372 | } | |
1373 | } | |
1374 | elseif ('county' == substr($name, 0, 6)) { | |
1375 | if ($params[$name]) { | |
1376 | $values[$index] = CRM_Core_PseudoConstant::county($params[$name]); | |
1377 | } | |
1378 | else { | |
1379 | $values[$index] = ''; | |
1380 | } | |
1381 | } | |
1382 | elseif ('gender' == substr($name, 0, 6)) { | |
26cf88b5 | 1383 | $gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 TO |
1384 | $values[$index] = $gender[$params[$name]]; |
1385 | } | |
1386 | elseif ('individual_prefix' == substr($name, 0, 17)) { | |
e6c4755b | 1387 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 TO |
1388 | $values[$index] = $prefix[$params[$name]]; |
1389 | } | |
1390 | elseif ('individual_suffix' == substr($name, 0, 17)) { | |
e6c4755b | 1391 | $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 TO |
1392 | $values[$index] = $suffix[$params[$name]]; |
1393 | } | |
1394 | elseif (in_array($name, array( | |
1395 | 'addressee', 'email_greeting', 'postal_greeting'))) { | |
1396 | $filterCondition = array('greeting_type' => $name); | |
1397 | $greeting = CRM_Core_PseudoConstant::greeting($filterCondition); | |
1398 | $values[$index] = $greeting[$params[$name]]; | |
1399 | } | |
1400 | elseif ($name === 'preferred_communication_method') { | |
e7e657f0 | 1401 | $communicationFields = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); |
6a488035 TO |
1402 | $compref = array(); |
1403 | $pref = $params[$name]; | |
1404 | if (is_array($pref)) { | |
1405 | foreach ($pref as $k => $v) { | |
1406 | if ($v) { | |
1407 | $compref[] = $communicationFields[$k]; | |
1408 | } | |
1409 | } | |
1410 | } | |
1411 | $values[$index] = implode(',', $compref); | |
1412 | } | |
1413 | elseif ($name == 'contact_sub_type') { | |
1414 | $values[$index] = implode(', ', $params[$name]); | |
1415 | } | |
1416 | elseif ($name == 'group') { | |
1417 | $groups = CRM_Contact_BAO_GroupContact::getGroupList(); | |
1418 | $title = array(); | |
1419 | foreach ($params[$name] as $gId => $dontCare) { | |
1420 | if ($dontCare) { | |
1421 | $title[] = $groups[$gId]; | |
1422 | } | |
1423 | } | |
1424 | $values[$index] = implode(', ', $title); | |
1425 | } | |
1426 | elseif ($name == 'tag') { | |
1427 | $entityTags = $params[$name]; | |
cd43c5e3 | 1428 | $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); |
6a488035 TO |
1429 | $title = array(); |
1430 | if (is_array($entityTags)) { | |
1431 | foreach ($entityTags as $tagId => $dontCare) { | |
1432 | $title[] = $allTags[$tagId]; | |
1433 | } | |
1434 | } | |
1435 | $values[$index] = implode(', ', $title); | |
1436 | } | |
1437 | elseif ('participant_role_id' == $name OR | |
1438 | 'participant_role' == $name | |
1439 | ) { | |
1440 | $roles = CRM_Event_PseudoConstant::participantRole(); | |
1441 | $values[$index] = $roles[$params[$name]]; | |
1442 | } | |
1443 | elseif ('participant_status_id' == $name OR | |
1444 | 'participant_status' == $name | |
1445 | ) { | |
1446 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1447 | $values[$index] = $status[$params[$name]]; | |
1448 | } | |
1449 | elseif (substr($name, -11) == 'campaign_id') { | |
1450 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($params[$name]); | |
1451 | $values[$index] = CRM_Utils_Array::value($params[$name], $campaigns); | |
1452 | } | |
1453 | elseif (strpos($name, '-') !== FALSE) { | |
1454 | list($fieldName, $id) = CRM_Utils_System::explode('-', $name, 2); | |
1455 | $detailName = str_replace(' ', '_', $name); | |
1456 | if (in_array($fieldName, array( | |
1457 | 'state_province', 'country', 'county'))) { | |
1458 | $values[$index] = $params[$detailName]; | |
1459 | $idx = $detailName . '_id'; | |
1460 | $values[$index] = $params[$idx]; | |
1461 | } | |
1462 | elseif ($fieldName == 'im') { | |
1463 | $providerName = NULL; | |
1464 | if ($providerId = $detailName . '-provider_id') { | |
1465 | $providerName = CRM_Utils_Array::value($params[$providerId], $imProviders); | |
1466 | } | |
1467 | if ($providerName) { | |
1468 | $values[$index] = $params[$detailName] . " (" . $providerName . ")"; | |
1469 | } | |
1470 | else { | |
1471 | $values[$index] = $params[$detailName]; | |
1472 | } | |
1473 | } | |
1474 | elseif ($fieldName == 'phone') { | |
1475 | $phoneExtField = str_replace('phone', 'phone_ext', $detailName); | |
1476 | if (isset($params[$phoneExtField])) { | |
1477 | $values[$index] = $params[$detailName] . " (" . $params[$phoneExtField] . ")"; | |
1478 | } | |
1479 | else { | |
1480 | $values[$index] = $params[$detailName]; | |
1481 | } | |
1482 | } | |
1483 | else { | |
1484 | $values[$index] = $params[$detailName]; | |
1485 | } | |
1486 | } | |
1487 | else { | |
1488 | if (substr($name, 0, 7) === 'do_not_' or substr($name, 0, 3) === 'is_') { | |
1489 | if ($params[$name]) { | |
1490 | $values[$index] = '[ x ]'; | |
1491 | } | |
1492 | } | |
1493 | else { | |
1494 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
1495 | $query = " | |
1496 | SELECT html_type, data_type | |
1497 | FROM civicrm_custom_field | |
1498 | WHERE id = $cfID | |
1499 | "; | |
1500 | $dao = CRM_Core_DAO::executeQuery($query); | |
1501 | $dao->fetch(); | |
1502 | $htmlType = $dao->html_type; | |
1503 | $dataType = $dao->data_type; | |
1504 | ||
1505 | if ($htmlType == 'File') { | |
1506 | //$fileURL = CRM_Core_BAO_CustomField::getFileURL( $contactID, $cfID ); | |
1507 | //$params[$index] = $values[$index] = $fileURL['file_url']; | |
1508 | $values[$index] = $params[$index]; | |
1509 | } | |
1510 | else { | |
1511 | if ($dao->data_type == 'Int' || | |
1512 | $dao->data_type == 'Boolean' | |
1513 | ) { | |
1514 | $customVal = (int )($params[$name]); | |
1515 | } | |
1516 | elseif ($dao->data_type == 'Float') { | |
1517 | $customVal = (float )($params[$name]); | |
1518 | } | |
1519 | elseif ($dao->data_type == 'Date') { | |
9e7eeb0f | 1520 | $customVal = $displayValue = $params[$name]; |
1521 | if (!empty($params[$name . '_time'])) { | |
1522 | $customVal = $displayValue = $params[$name] . ' ' . $params[$name . '_time']; | |
6a488035 | 1523 | } |
9e7eeb0f | 1524 | $skip = TRUE; |
6a488035 TO |
1525 | } |
1526 | else { | |
1527 | $customVal = $params[$name]; | |
1528 | } | |
1529 | //take the custom field options | |
1530 | $returnProperties = array($name => 1); | |
1531 | $query = new CRM_Contact_BAO_Query($params, $returnProperties, $fields); | |
1532 | $options = &$query->_options; | |
9e7eeb0f | 1533 | if (!$skip) { |
1534 | $displayValue = CRM_Core_BAO_CustomField::getDisplayValue($customVal, $cfID, $options); | |
1535 | } | |
6a488035 TO |
1536 | //Hack since we dont have function to check empty. |
1537 | //FIXME in 2.3 using crmIsEmptyArray() | |
1538 | $customValue = TRUE; | |
1539 | if (is_array($customVal) && is_array($displayValue)) { | |
1540 | $customValue = array_diff($customVal, $displayValue); | |
1541 | } | |
1542 | //use difference of arrays | |
1543 | if (empty($customValue) || !$customValue) { | |
1544 | $values[$index] = ''; | |
1545 | } | |
1546 | else { | |
1547 | $values[$index] = $displayValue; | |
1548 | } | |
1549 | } | |
1550 | } | |
1551 | elseif ($name == 'home_URL' && | |
1552 | !empty($params[$name]) | |
1553 | ) { | |
1554 | $url = CRM_Utils_System::fixURL($params[$name]); | |
1555 | $values[$index] = "<a href=\"$url\">{$params[$name]}</a>"; | |
1556 | } | |
1557 | elseif (in_array($name, array( | |
1558 | 'birth_date', 'deceased_date', 'participant_register_date'))) { | |
1559 | $values[$index] = CRM_Utils_Date::customFormat(CRM_Utils_Date::format($params[$name])); | |
1560 | } | |
1561 | else { | |
1562 | $values[$index] = $params[$name]; | |
1563 | } | |
1564 | } | |
1565 | } | |
1566 | $profileFields[$name] = $field; | |
1567 | } | |
1568 | } | |
1569 | } | |
1570 | ||
1571 | /** | |
1572 | * Function to build the array for Additional participant's information array of priamry and additional Ids | |
1573 | * | |
1574 | *@param int $participantId id of Primary participant | |
1575 | *@param array $values key/value event info | |
1576 | *@param int $contactId contact id of Primary participant | |
1577 | *@param boolean $isTest whether test or live transaction | |
1578 | *@param boolean $isIdsArray to return an array of Ids | |
1579 | * | |
1580 | *@return array $customProfile array of Additional participant's info OR array of Ids. | |
1581 | *@access public | |
1582 | */ | |
1583 | static function buildCustomProfile($participantId, | |
1584 | $values, | |
1585 | $contactId = NULL, | |
1586 | $isTest = FALSE, | |
1587 | $isIdsArray = FALSE, | |
1588 | $skipCancel = TRUE | |
1589 | ) { | |
1590 | ||
1591 | $customProfile = $additionalIDs = array(); | |
1592 | if (!$participantId) { | |
1593 | CRM_Core_Error::fatal(ts('Cannot find participant ID')); | |
1594 | } | |
1595 | ||
1596 | //set Ids of Primary Participant also. | |
1597 | if ($isIdsArray && $contactId) { | |
1598 | $additionalIDs[$participantId] = $contactId; | |
1599 | } | |
1600 | ||
1601 | //hack to skip cancelled participants, CRM-4320 | |
1602 | $where = "participant.registered_by_id={$participantId}"; | |
1603 | if ($skipCancel) { | |
1604 | $cancelStatusId = 0; | |
1605 | $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"); | |
1606 | $cancelStatusId = array_search('Cancelled', $negativeStatuses); | |
1607 | $where .= " AND participant.status_id != {$cancelStatusId}"; | |
1608 | } | |
1609 | $query = " | |
1610 | SELECT participant.id, participant.contact_id | |
1611 | FROM civicrm_participant participant | |
1612 | WHERE {$where}"; | |
1613 | ||
1614 | $dao = CRM_Core_DAO::executeQuery($query); | |
1615 | while ($dao->fetch()) { | |
1616 | $additionalIDs[$dao->id] = $dao->contact_id; | |
1617 | } | |
1618 | ||
1619 | //return if only array is required. | |
1620 | if ($isIdsArray && $contactId) { | |
1621 | return $additionalIDs; | |
1622 | } | |
1623 | ||
1624 | $preProfileID = CRM_Utils_Array::value('additional_custom_pre_id', $values); | |
1625 | $postProfileID = CRM_Utils_Array::value('additional_custom_post_id', $values); | |
1626 | //else build array of Additional participant's information. | |
1627 | if (count($additionalIDs)) { | |
1628 | if ($preProfileID || $postProfileID) { | |
1629 | $template = CRM_Core_Smarty::singleton(); | |
1630 | ||
1631 | $isCustomProfile = TRUE; | |
1632 | $i = 1; | |
1633 | $title = $groupTitles = array(); | |
1634 | foreach ($additionalIDs as $pId => $cId) { | |
1635 | //get the params submitted by participant. | |
1636 | $participantParams = CRM_Utils_Array::value($pId, $values['params'], array()); | |
1637 | ||
1638 | list($profilePre, $groupTitles) = self::buildCustomDisplay($preProfileID, | |
1639 | 'additionalCustomPre', | |
1640 | $cId, | |
1641 | $template, | |
1642 | $pId, | |
1643 | $isTest, | |
1644 | $isCustomProfile, | |
1645 | $participantParams | |
1646 | ); | |
1647 | ||
1648 | if ($profilePre) { | |
1649 | $profile = $profilePre; | |
1650 | // $customProfile[$i] = array_merge( $groupTitles, $customProfile[$i] ); | |
1651 | if ($i === 1) { | |
1652 | $title = $groupTitles; | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | list($profilePost, $groupTitles) = self::buildCustomDisplay($postProfileID, | |
1657 | 'additionalCustomPost', | |
1658 | $cId, | |
1659 | $template, | |
1660 | $pId, | |
1661 | $isTest, | |
1662 | $isCustomProfile, | |
1663 | $participantParams | |
1664 | ); | |
1665 | ||
1666 | if ($profilePost) { | |
1667 | if (isset($profilePre)) { | |
1668 | $profile = array_merge($profilePre, $profilePost); | |
1669 | if ($i === 1) { | |
1670 | $title = array_merge($title, $groupTitles); | |
1671 | } | |
1672 | } | |
1673 | else { | |
1674 | $profile = $profilePost; | |
1675 | if ($i === 1) { | |
1676 | $title = $groupTitles; | |
1677 | } | |
1678 | } | |
1679 | } | |
1680 | $profiles[] = $profile; | |
1681 | $i++; | |
1682 | } | |
1683 | $customProfile['title'] = $title; | |
1684 | $customProfile['profile'] = $profiles; | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | return $customProfile; | |
1689 | } | |
1690 | ||
1691 | /* Function to retrieve all events those having location block set. | |
1692 | * | |
1693 | * @return array $events array of all events. | |
1694 | */ | |
1695 | static function getLocationEvents() { | |
1696 | $events = array(); | |
1697 | ||
1698 | $query = " | |
1699 | SELECT CONCAT_WS(' :: ' , ca.name, ca.street_address, ca.city, sp.name) title, ce.loc_block_id | |
1700 | FROM civicrm_event ce | |
1701 | INNER JOIN civicrm_loc_block lb ON ce.loc_block_id = lb.id | |
1702 | INNER JOIN civicrm_address ca ON lb.address_id = ca.id | |
1703 | LEFT JOIN civicrm_state_province sp ON ca.state_province_id = sp.id | |
1704 | ORDER BY sp.name, ca.city, ca.street_address ASC | |
1705 | "; | |
1706 | ||
1707 | $dao = CRM_Core_DAO::executeQuery($query); | |
1708 | while ($dao->fetch()) { | |
1709 | $events[$dao->loc_block_id] = $dao->title; | |
1710 | } | |
1711 | ||
1712 | return $events; | |
1713 | } | |
1714 | ||
1715 | static function countEventsUsingLocBlockId($locBlockId) { | |
1716 | if (!$locBlockId) { | |
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | $locBlockId = CRM_Utils_Type::escape($locBlockId, 'Integer'); | |
1721 | ||
1722 | $query = " | |
1723 | SELECT count(*) FROM civicrm_event ce | |
1724 | WHERE ce.loc_block_id = $locBlockId"; | |
1725 | ||
1726 | return CRM_Core_DAO::singleValueQuery($query); | |
1727 | } | |
1728 | ||
1729 | static function validRegistrationRequest($values, $contactID) { | |
1730 | // check that the user has permission to register for this event | |
1731 | $hasPermission = CRM_Core_Permission::event(CRM_Core_Permission::EDIT, | |
1732 | $contactID | |
1733 | ); | |
1734 | ||
1735 | return $hasPermission && self::validRegistrationDate($values); | |
1736 | } | |
1737 | ||
1738 | static function validRegistrationDate(&$values) { | |
1739 | // make sure that we are between registration start date and registration end date | |
1740 | $startDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_start_date', $values)); | |
1741 | $endDate = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('registration_end_date', $values)); | |
1742 | $eventEnd = CRM_Utils_Date::unixTime(CRM_Utils_Array::value('end_date', $values)); | |
1743 | $now = time(); | |
1744 | $validDate = TRUE; | |
1745 | if ($startDate && $startDate >= $now) { | |
1746 | $validDate = FALSE; | |
1747 | } | |
1748 | if ($endDate && $endDate < $now) { | |
1749 | $validDate = FALSE; | |
1750 | } | |
1751 | if ($eventEnd && $eventEnd < $now) { | |
1752 | $validDate = FALSE; | |
1753 | } | |
1754 | ||
1755 | return $validDate; | |
1756 | } | |
1757 | ||
1758 | /* Function to Show - Hide the Registration Link. | |
1759 | * | |
1760 | * @param array $values key/value event info | |
1761 | * @return boolean true if allow registration otherwise false | |
1762 | * @access public | |
1763 | */ | |
1764 | static function showHideRegistrationLink($values) { | |
1765 | ||
1766 | $session = CRM_Core_Session::singleton(); | |
1767 | $contactID = $session->get('userID'); | |
1768 | $alreadyRegistered = FALSE; | |
1769 | ||
1770 | if ($contactID) { | |
1771 | $params = array('contact_id' => $contactID); | |
1772 | ||
1773 | if ($eventId = CRM_Utils_Array::value('id', $values['event'])) { | |
1774 | $params['event_id'] = $eventId; | |
1775 | } | |
1776 | if ($roleId = CRM_Utils_Array::value('default_role_id', $values['event'])) { | |
1777 | $params['role_id'] = $roleId; | |
1778 | } | |
1779 | $alreadyRegistered = self::checkRegistration($params); | |
1780 | } | |
1781 | ||
1782 | if (CRM_Utils_Array::value('allow_same_participant_emails', $values['event']) || | |
1783 | !$alreadyRegistered | |
1784 | ) { | |
1785 | return TRUE; | |
1786 | } | |
1787 | return FALSE; | |
1788 | } | |
1789 | ||
1790 | /* Function to check if given contact is already registered. | |
1791 | * | |
1792 | * @param array $params key/value participant info | |
1793 | * @return boolean $alreadyRegistered true/false | |
1794 | * @access public | |
1795 | */ | |
1796 | static function checkRegistration($params) { | |
1797 | $alreadyRegistered = FALSE; | |
1798 | if (!CRM_Utils_Array::value('contact_id', $params)) { | |
1799 | return $alreadyRegistered; | |
1800 | } | |
1801 | ||
1802 | $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1'); | |
1803 | ||
1804 | $participant = new CRM_Event_DAO_Participant(); | |
1805 | $participant->copyValues($params); | |
1806 | ||
1807 | $participant->is_test = CRM_Utils_Array::value('is_test', $params, 0); | |
1808 | $participant->selectAdd(); | |
1809 | $participant->selectAdd('status_id'); | |
1810 | if ($participant->find(TRUE) && array_key_exists($participant->status_id, $statusTypes)) { | |
1811 | $alreadyRegistered = TRUE; | |
1812 | } | |
1813 | ||
1814 | return $alreadyRegistered; | |
1815 | } | |
1816 | ||
1817 | /** | |
1818 | * make sure that the user has permission to access this event | |
1819 | * | |
1820 | * @param int $id the id of the event | |
1821 | * @param int $name the name or title of the event | |
1822 | * | |
1823 | * @return string the permission that the user has (or null) | |
1824 | * @access public | |
1825 | * @static | |
1826 | */ | |
1827 | static function checkPermission($eventId = NULL, $type = CRM_Core_Permission::VIEW) { | |
1828 | static $permissions = NULL; | |
1829 | ||
1830 | if (empty($permissions)) { | |
1831 | $allEvents = CRM_Event_PseudoConstant::event(NULL, TRUE); | |
1832 | $createdEvents = array(); | |
1833 | ||
1834 | $session = CRM_Core_Session::singleton(); | |
1835 | if ($userID = $session->get('userID')) { | |
1836 | $createdEvents = array_keys(CRM_Event_PseudoConstant::event(NULL, TRUE, "created_id={$userID}")); | |
1837 | } | |
1838 | ||
1839 | // Note: for a multisite setup, a user with edit all events, can edit all events | |
1840 | // including those from other sites | |
1841 | if (CRM_Core_Permission::check('edit all events')) { | |
1842 | $permissions[CRM_Core_Permission::EDIT] = array_keys($allEvents); | |
1843 | } | |
1844 | else { | |
1845 | $permissions[CRM_Core_Permission::EDIT] = &CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_event', $allEvents, $createdEvents); | |
1846 | } | |
1847 | ||
1848 | if (CRM_Core_Permission::check('edit all events')) { | |
1849 | $permissions[CRM_Core_Permission::VIEW] = array_keys($allEvents); | |
1850 | } | |
1851 | else { | |
1852 | if (CRM_Core_Permission::check('access CiviEvent') && | |
1853 | CRM_Core_Permission::check('view event participants') | |
1854 | ) { | |
1855 | // use case: allow "view all events" but NOT "edit all events" | |
1856 | // so for a normal site allow users with these two permissions to view all events AND | |
1857 | // at the same time also allow any hook to override if needed. | |
1858 | $createdEvents = array_keys($allEvents); | |
1859 | } | |
1860 | $permissions[CRM_Core_Permission::VIEW] = &CRM_ACL_API::group(CRM_Core_Permission::VIEW, NULL, 'civicrm_event', $allEvents, $createdEvents); | |
1861 | } | |
1862 | ||
1863 | $permissions[CRM_Core_Permission::DELETE] = array(); | |
1864 | if (CRM_Core_Permission::check('delete in CiviEvent')) { | |
1865 | // Note: we want to restrict the scope of delete permission to | |
1866 | // events that are editable/viewable (usecase multisite). | |
1867 | // We can remove array_intersect once we have ACL support for delete functionality. | |
1868 | $permissions[CRM_Core_Permission::DELETE] = array_intersect($permissions[CRM_Core_Permission::EDIT], | |
1869 | $permissions[CRM_Core_Permission::VIEW] | |
1870 | ); | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | if ($eventId) { | |
1875 | return in_array($eventId, $permissions[$type]) ? TRUE : FALSE; | |
1876 | } | |
1877 | ||
1878 | return $permissions; | |
1879 | } | |
1880 | ||
1881 | /** | |
1882 | * Build From Email as the combination of all the email ids of the logged in user, | |
1883 | * the domain email id and the email id configured for the event | |
1884 | * | |
1885 | * @param int $eventId the id of the event | |
1886 | * | |
1887 | * @return array an array of email ids | |
1888 | * @access public | |
1889 | * @static | |
1890 | */ | |
1891 | static function getFromEmailIds($eventId = NULL) { | |
1892 | $fromEmailValues['from_email_id'] = CRM_Core_BAO_Email::getFromEmail(); | |
1893 | ||
1894 | if ($eventId) { | |
1895 | // add the email id configured for the event | |
1896 | $params = array('id' => $eventId); | |
1897 | $returnProperties = array('confirm_from_name', 'confirm_from_email', 'cc_confirm', 'bcc_confirm'); | |
1898 | $eventEmail = array(); | |
1899 | ||
1900 | CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $params, $eventEmail, $returnProperties); | |
1901 | if (CRM_Utils_Array::value('confirm_from_name', $eventEmail) | |
1902 | && CRM_Utils_Array::value('confirm_from_email', $eventEmail) | |
1903 | ) { | |
1904 | $eventEmailId = "{$eventEmail['confirm_from_name']} <{$eventEmail['confirm_from_email']}>"; | |
1905 | ||
1906 | $fromEmailValues['from_email_id'][$eventEmailId] = htmlspecialchars($eventEmailId); | |
1907 | $fromEmailId = array('cc' => CRM_Utils_Array::value('cc_confirm', $eventEmail), | |
1908 | 'bcc' => CRM_Utils_Array::value('bcc_confirm', $eventEmail), | |
1909 | ); | |
1910 | $fromEmailValues = array_merge($fromEmailValues, $fromEmailId); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | return $fromEmailValues; | |
1915 | } | |
1916 | ||
1917 | /** | |
1918 | * Function to calculate event total seats occupied. | |
1919 | * | |
1920 | * @param int $eventId event id. | |
1921 | * @param sting $extraWhereClause extra filter on participants. | |
1922 | * | |
1923 | * @return int event total seats w/ given criteria. | |
1924 | * @access public | |
1925 | * @static | |
1926 | */ | |
1927 | static function eventTotalSeats($eventId, $extraWhereClause = NULL) { | |
1928 | if (empty($eventId)) { | |
1929 | return 0; | |
1930 | } | |
1931 | ||
1932 | $extraWhereClause = trim($extraWhereClause); | |
1933 | if (!empty($extraWhereClause)) { | |
1934 | $extraWhereClause = " AND ( {$extraWhereClause} )"; | |
1935 | } | |
1936 | ||
1937 | //event seats calculation : | |
1938 | //1. consider event seat as a single when participant does not have line item. | |
1939 | //2. consider event seat as a single when participant has line items but does not | |
1940 | // have count for corresponding price field value ( ie price field value does not carry any seat ) | |
1941 | //3. consider event seat as a sum of all seats from line items in case price field value carries count. | |
1942 | ||
1943 | $query = " | |
1944 | SELECT IF ( SUM( value.count*lineItem.qty ), | |
1945 | SUM( value.count*lineItem.qty ) + | |
1946 | COUNT( DISTINCT participant.id ) - | |
1947 | COUNT( DISTINCT IF ( value.count, participant.id, NULL ) ), | |
1948 | COUNT( DISTINCT participant.id ) ) | |
1949 | FROM civicrm_participant participant | |
1950 | INNER JOIN civicrm_contact contact ON ( contact.id = participant.contact_id AND contact.is_deleted = 0 ) | |
1951 | INNER JOIN civicrm_event event ON ( event.id = participant.event_id ) | |
1952 | LEFT JOIN civicrm_line_item lineItem ON ( lineItem.entity_id = participant.id | |
1953 | AND lineItem.entity_table = 'civicrm_participant' ) | |
1954 | LEFT JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_value_id AND value.count ) | |
1955 | WHERE ( participant.event_id = %1 ) | |
1956 | {$extraWhereClause} | |
1957 | GROUP BY participant.event_id"; | |
1958 | ||
1959 | return (int)CRM_Core_DAO::singleValueQuery($query, array(1 => array($eventId, 'Positive'))); | |
1960 | } | |
1961 | ||
1962 | /* | |
1963 | * Retrieve event template default values to be set | |
1964 | * as default values for current new event. | |
1965 | * | |
1966 | * @params int $templateId event template id. | |
1967 | * | |
1968 | * @return $defaults an array of custom data defaults. | |
1969 | */ | |
1970 | public function getTemplateDefaultValues($templateId) { | |
1971 | $defaults = array(); | |
1972 | if (!$templateId) { | |
1973 | return $defaults; | |
1974 | } | |
1975 | ||
1976 | $templateParams = array('id' => $templateId); | |
1977 | CRM_Event_BAO_Event::retrieve($templateParams, $defaults); | |
1978 | $fieldsToExclude = array( | |
1979 | 'id', | |
1980 | 'default_fee_id', | |
1981 | 'default_discount_fee_id', | |
1982 | 'created_date', | |
1983 | 'created_id', | |
1984 | 'is_template', | |
1985 | 'template_title', | |
1986 | ); | |
1987 | $defaults = array_diff_key($defaults, array_flip($fieldsToExclude)); | |
1988 | return $defaults; | |
1989 | } | |
1990 | ||
1991 | static function get_sub_events($event_id) { | |
1992 | $params = array('parent_event_id' => $event_id); | |
1993 | $defaults = array(); | |
1994 | return CRM_Event_BAO_Event::retrieve($params, $defaults); | |
1995 | } | |
49f68a22 DL |
1996 | |
1997 | /* | |
1998 | * Update the Campaign Id of all the participants of the given event | |
1999 | * | |
2000 | * @params int $eventID event id. | |
2001 | * @params int $eventCampaignID campaign id of that event | |
2002 | * | |
2003 | */ | |
2004 | static function updateParticipantCampaignID($eventID, $eventCampaignID) { | |
2005 | $params = array(); | |
2006 | $params[1] = array($eventID, 'Integer'); | |
2007 | ||
2008 | if(empty($eventCampaignID)) { | |
2009 | $query = "UPDATE civicrm_participant SET campaign_id = NULL WHERE event_id = %1"; | |
2010 | } | |
2011 | else { | |
2012 | $query = "UPDATE civicrm_participant SET campaign_id = %2 WHERE event_id = %1"; | |
2013 | $params[2] = array($eventCampaignID, 'Integer'); | |
2014 | } | |
2015 | CRM_Core_DAO::executeQuery($query, $params); | |
2016 | } | |
6a488035 TO |
2017 | } |
2018 |