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