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