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