-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathEvent.php
2441 lines (2217 loc) · 85.2 KB
/
Event.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Event_BAO_Event extends CRM_Event_DAO_Event {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Fetch object based on array of properties.
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
* @param array $defaults
* (reference ) an assoc array to hold the flattened values.
*
* @return CRM_Event_DAO_Event
*/
public static function retrieve(&$params, &$defaults) {
$event = new CRM_Event_DAO_Event();
$event->copyValues($params);
if ($event->find(TRUE)) {
CRM_Core_DAO::storeValues($event, $defaults);
return $event;
}
return NULL;
}
/**
* Update the is_active flag in the db.
*
* @param int $id
* Id of the database record.
* @param bool $is_active
* Value we want to set the is_active field.
*
* @return bool
* true if we found and updated the object, else false
*/
public static function setIsActive($id, $is_active) {
return CRM_Core_DAO::setFieldValue('CRM_Event_DAO_Event', $id, 'is_active', $is_active);
}
/**
* Add the event.
*
* @param array $params
* Reference array contains the values submitted by the form.
*
* @return CRM_Event_DAO_Event
*/
public static function add(&$params) {
$financialTypeId = NULL;
if (!empty($params['id'])) {
CRM_Utils_Hook::pre('edit', 'Event', $params['id'], $params);
if (empty($params['skipFinancialType'])) {
$financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['id'], 'financial_type_id');
}
}
else {
CRM_Utils_Hook::pre('create', 'Event', NULL, $params);
}
$event = new CRM_Event_DAO_Event();
$event->copyValues($params);
$result = $event->save();
if (!empty($params['id'])) {
CRM_Utils_Hook::post('edit', 'Event', $event->id, $event);
}
else {
CRM_Utils_Hook::post('create', 'Event', $event->id, $event);
}
if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) {
CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_event', $params['financial_type_id']);
}
return $result;
}
/**
* Create the event.
*
* @param array $params
* Reference array contains the values submitted by the form.
*
* @return object
*/
public static function create(&$params) {
$transaction = new CRM_Core_Transaction();
if (empty($params['is_template'])) {
$params['is_template'] = 0;
}
// check if new event, if so set the created_id (if not set)
// and always set created_date to now
if (empty($params['id'])) {
if (empty($params['created_id'])) {
$session = CRM_Core_Session::singleton();
$params['created_id'] = $session->get('userID');
}
$params['created_date'] = date('YmdHis');
// Clone from template
if (!empty($params['template_id'])) {
$copy = self::copy($params['template_id']);
$params['id'] = $copy->id;
unset($params['template_id']);
}
}
$event = self::add($params);
CRM_Price_BAO_PriceSet::setPriceSets($params, $event, 'event');
if (is_a($event, 'CRM_Core_Error')) {
CRM_Core_DAO::transaction('ROLLBACK');
return $event;
}
$contactId = CRM_Core_Session::getLoggedInContactID();
if (!$contactId) {
$contactId = $params['contact_id'] ?? NULL;
}
// Log the information on successful add/edit of Event
$logParams = [
'entity_table' => 'civicrm_event',
'entity_id' => $event->id,
'modified_id' => $contactId,
'modified_date' => date('Ymd'),
];
CRM_Core_BAO_Log::add($logParams);
if (!empty($params['custom']) &&
is_array($params['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_event', $event->id);
}
$transaction->commit();
return $event;
}
/**
* Delete the event.
*
* @param int $id
* Event id.
*
* @return mixed|null
*/
public static function del($id) {
if (!$id) {
return NULL;
}
CRM_Utils_Hook::pre('delete', 'Event', $id);
$extends = ['event'];
$groupTree = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, NULL, $extends);
foreach ($groupTree as $values) {
$query = "DELETE FROM %1 WHERE entity_id = %2";
CRM_Core_DAO::executeQuery($query, [
1 => [$values['table_name'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES],
2 => [$id, 'Integer'],
]);
}
// Clean up references to profiles used by the event (CRM-20935)
$ufJoinParams = [
'module' => 'CiviEvent',
'entity_table' => 'civicrm_event',
'entity_id' => $id,
];
CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
$ufJoinParams = [
'module' => 'CiviEvent_Additional',
'entity_table' => 'civicrm_event',
'entity_id' => $id,
];
CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
// price set cleanup, CRM-5527
CRM_Price_BAO_PriceSet::removeFrom('civicrm_event', $id);
$event = new CRM_Event_DAO_Event();
$event->id = $id;
if ($event->find(TRUE)) {
$locBlockId = $event->loc_block_id;
$result = $event->delete();
if (!is_null($locBlockId)) {
self::deleteEventLocBlock($locBlockId, $id);
}
CRM_Utils_Hook::post('delete', 'Event', $id, $event);
return $result;
}
return NULL;
}
/**
* Delete the location block associated with an event.
*
* Function checks that it is not being used by any other event.
*
* @param int $locBlockId
* Location block id to be deleted.
* @param int $eventId
* Event with which loc block is associated.
*/
public static function deleteEventLocBlock($locBlockId, $eventId = NULL) {
$query = "SELECT count(ce.id) FROM civicrm_event ce WHERE ce.loc_block_id = $locBlockId";
if ($eventId) {
$query .= " AND ce.id != $eventId;";
}
$locCount = CRM_Core_DAO::singleValueQuery($query);
if ($locCount == 0) {
CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
}
}
/**
* Get current/future Events.
*
* @param int $all
* 0 returns current and future events.
* 1 if events all are required
* 2 returns events since 3 months ago
* @param int|array $id single int event id or array of multiple event ids to return
* @param bool $isActive
* true if you need only active events.
* @param bool $checkPermission
* true if you need to check permission else false.
* @param bool $titleOnly
* true if you need only title not appended with start date
*
* @return array
*/
public static function getEvents(
$all = 0,
$id = NULL,
$isActive = TRUE,
$checkPermission = TRUE,
$titleOnly = FALSE
) {
$query = "
SELECT `id`, `title`, `start_date`
FROM `civicrm_event`
WHERE ( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0 )";
if (!empty($id)) {
$op = is_array($id) ? 'IN' : '=';
$where = CRM_Contact_BAO_Query::buildClause('id', $op, $id);
$query .= " AND {$where}";
}
elseif ($all == 0) {
// find only events ending in the future
$endDate = date('YmdHis');
$query .= "
AND ( `end_date` >= {$endDate} OR
(
( end_date IS NULL OR end_date = '' ) AND start_date >= {$endDate}
)
)";
}
elseif ($all == 2) {
// find only events starting in the last 3 months
$startDate = date('YmdHis', strtotime('3 months ago'));
$query .= " AND ( `start_date` >= {$startDate} OR start_date IS NULL )";
}
if ($isActive) {
$query .= " AND civicrm_event.is_active = 1";
}
$query .= " ORDER BY title asc";
$events = [];
$dao = CRM_Core_DAO::executeQuery($query);
while ($dao->fetch()) {
if ((!$checkPermission ||
CRM_Event_BAO_Event::checkPermission($dao->id)
) &&
$dao->title
) {
$events[$dao->id] = $dao->title;
if (!$titleOnly) {
$events[$dao->id] .= ' - ' . CRM_Utils_Date::customFormat($dao->start_date);
}
}
}
return $events;
}
/**
* Get events Summary.
*
* @return array
* Array of event summary values
*
* @throws \CiviCRM_API3_Exception
*/
public static function getEventSummary() {
$eventSummary = $eventIds = [];
$config = CRM_Core_Config::singleton();
// get permission and include them here
// does not scale, but rearranging code for now
// FIXME in a future release
$permissions = self::getAllPermissions();
$validEventIDs = '';
if (empty($permissions[CRM_Core_Permission::VIEW])) {
$eventSummary['total_events'] = 0;
return $eventSummary;
}
else {
$validEventIDs = " AND civicrm_event.id IN ( " . implode(',', array_values($permissions[CRM_Core_Permission::VIEW])) . " ) ";
}
// We're fetching recent and upcoming events (where start date is 7 days ago OR later)
$query = "
SELECT count(id) as total_events
FROM civicrm_event
WHERE civicrm_event.is_active = 1 AND
( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND
civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day )
$validEventIDs";
$dao = CRM_Core_DAO::executeQuery($query);
if ($dao->fetch()) {
$eventSummary['total_events'] = $dao->total_events;
}
if (empty($eventSummary) ||
$dao->total_events == 0
) {
return $eventSummary;
}
//get the participant status type values.
$cpstObject = new CRM_Event_DAO_ParticipantStatusType();
$cpst = $cpstObject->getTableName();
$query = "SELECT id, name, label, class FROM $cpst";
$status = CRM_Core_DAO::executeQuery($query);
$statusValues = [];
while ($status->fetch()) {
$statusValues[$status->id]['id'] = $status->id;
$statusValues[$status->id]['name'] = $status->name;
$statusValues[$status->id]['label'] = $status->label;
$statusValues[$status->id]['class'] = $status->class;
}
// Get the Id of Option Group for Event Types
$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = NULL;
if ($optionGroupDAO->find(TRUE)) {
$optionGroupId = $optionGroupDAO->id;
}
// Get the event summary display preferences
$show_max_events = Civi::settings()->get('show_events');
// show all events if show_events is set to a negative value
if (isset($show_max_events) && $show_max_events >= 0) {
$event_summary_limit = "LIMIT 0, $show_max_events";
}
else {
$event_summary_limit = "";
}
$query = "
SELECT civicrm_event.id as id, civicrm_event.title as event_title, civicrm_event.is_public as is_public,
civicrm_event.max_participants as max_participants, civicrm_event.start_date as start_date,
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,
civicrm_event.slot_label_id,
civicrm_event.summary as summary,
civicrm_pcp_block.id as is_pcp_enabled,
civicrm_recurring_entity.parent_id as is_repeating_event
FROM civicrm_event
LEFT JOIN civicrm_option_value ON (
civicrm_event.event_type_id = civicrm_option_value.value AND
civicrm_option_value.option_group_id = %1 )
LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_event.id AND civicrm_tell_friend.entity_table = 'civicrm_event' )
LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_event.id AND civicrm_pcp_block.entity_table = 'civicrm_event')
LEFT JOIN civicrm_recurring_entity ON ( civicrm_event.id = civicrm_recurring_entity.entity_id AND civicrm_recurring_entity.entity_table = 'civicrm_event' )
WHERE civicrm_event.is_active = 1 AND
( civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0) AND
civicrm_event.start_date >= DATE_SUB( NOW(), INTERVAL 7 day )
$validEventIDs
ORDER BY civicrm_event.start_date ASC
$event_summary_limit
";
$eventParticipant = [];
$properties = [
'id' => 'id',
'eventTitle' => 'event_title',
'isPublic' => 'is_public',
'maxParticipants' => 'max_participants',
'startDate' => 'start_date',
'endDate' => 'end_date',
'eventType' => 'event_type',
'isMap' => 'is_map',
'participants' => 'participants',
'notCountedDueToRole' => 'notCountedDueToRole',
'notCountedDueToStatus' => 'notCountedDueToStatus',
'notCountedParticipants' => 'notCountedParticipants',
];
$params = [1 => [$optionGroupId, 'Integer']];
$mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
]));
$dao = CRM_Core_DAO::executeQuery($query, $params);
while ($dao->fetch()) {
foreach ($properties as $property => $name) {
$set = NULL;
switch ($name) {
case 'is_public':
if ($dao->$name) {
$set = 'Yes';
}
else {
$set = 'No';
}
$eventSummary['events'][$dao->id][$property] = $set;
break;
case 'is_map':
if ($dao->$name && $config->mapAPIKey) {
$values = [];
$ids = [];
$params = ['entity_id' => $dao->id, 'entity_table' => 'civicrm_event'];
$values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
if (is_numeric(CRM_Utils_Array::value('geo_code_1', $values['location']['address'][1])) ||
(
!empty($values['location']['address'][1]['city']) &&
!empty($values['location']['address'][1]['state_province_id'])
)
) {
$set = CRM_Utils_System::url('civicrm/contact/map/event', "reset=1&eid={$dao->id}");
}
}
$eventSummary['events'][$dao->id][$property] = $set;
if (is_array($permissions[CRM_Core_Permission::EDIT])
&& in_array($dao->id, $permissions[CRM_Core_Permission::EDIT])) {
$eventSummary['events'][$dao->id]['configure'] = CRM_Utils_System::url('civicrm/admin/event', "action=update&id=$dao->id&reset=1");
}
break;
case 'end_date':
case 'start_date':
$eventSummary['events'][$dao->id][$property] = CRM_Utils_Date::customFormat($dao->$name,
NULL, ['d']
);
break;
case 'participants':
case 'notCountedDueToRole':
case 'notCountedDueToStatus':
case 'notCountedParticipants':
$set = NULL;
$propertyCnt = 0;
if ($name == 'participants') {
$propertyCnt = self::getParticipantCount($dao->id);
if ($propertyCnt) {
$set = CRM_Utils_System::url('civicrm/event/search',
"reset=1&force=1&event=$dao->id&status=true&role=true"
);
}
}
elseif ($name == 'notCountedParticipants') {
$propertyCnt = self::getParticipantCount($dao->id, TRUE, FALSE, TRUE, FALSE);
if ($propertyCnt) {
// FIXME : selector fail to search w/ OR operator.
// $set = CRM_Utils_System::url( 'civicrm/event/search',
// "reset=1&force=1&event=$dao->id&status=false&role=false" );
}
}
elseif ($name == 'notCountedDueToStatus') {
$propertyCnt = self::getParticipantCount($dao->id, TRUE, FALSE, FALSE, FALSE);
if ($propertyCnt) {
$set = CRM_Utils_System::url('civicrm/event/search',
"reset=1&force=1&event=$dao->id&status=false"
);
}
}
else {
$propertyCnt = self::getParticipantCount($dao->id, FALSE, FALSE, TRUE, FALSE);
if ($propertyCnt) {
$set = CRM_Utils_System::url('civicrm/event/search',
"reset=1&force=1&event=$dao->id&role=false"
);
}
}
$eventSummary['events'][$dao->id][$property] = $propertyCnt;
$eventSummary['events'][$dao->id][$name . '_url'] = $set;
break;
default:
$eventSummary['events'][$dao->id][$property] = $dao->$name;
break;
}
}
// prepare the area for per-status participant counts
$statusClasses = ['Positive', 'Pending', 'Waiting', 'Negative'];
$eventSummary['events'][$dao->id]['statuses'] = array_fill_keys($statusClasses, []);
$eventSummary['events'][$dao->id]['friend'] = $dao->is_friend_active;
$eventSummary['events'][$dao->id]['is_monetary'] = $dao->is_monetary;
$eventSummary['events'][$dao->id]['is_online_registration'] = $dao->is_online_registration;
$eventSummary['events'][$dao->id]['is_show_location'] = $dao->is_show_location;
$eventSummary['events'][$dao->id]['is_subevent'] = $dao->slot_label_id;
$eventSummary['events'][$dao->id]['is_pcp_enabled'] = $dao->is_pcp_enabled;
$eventSummary['events'][$dao->id]['reminder'] = CRM_Core_BAO_ActionSchedule::isConfigured($dao->id, $mapping->getId());
$eventSummary['events'][$dao->id]['is_repeating_event'] = $dao->is_repeating_event;
$statusTypes = CRM_Event_PseudoConstant::participantStatus();
foreach ($statusValues as $statusId => $statusValue) {
if (!array_key_exists($statusId, $statusTypes)) {
continue;
}
$class = $statusValue['class'];
$statusCount = self::eventTotalSeats($dao->id, "( participant.status_id = {$statusId} )");
if ($statusCount) {
$urlString = "reset=1&force=1&event={$dao->id}&status=$statusId";
$statusInfo = [
'url' => CRM_Utils_System::url('civicrm/event/search', $urlString),
'name' => $statusValue['name'],
'label' => $statusValue['label'],
'count' => $statusCount,
];
$eventSummary['events'][$dao->id]['statuses'][$class][] = $statusInfo;
}
}
}
$countedRoles = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1');
$nonCountedRoles = CRM_Event_PseudoConstant::participantRole(NULL, '( filter = 0 OR filter IS NULL )');
$countedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
$nonCountedStatus = CRM_Event_PseudoConstant::participantStatus(NULL, '( is_counted = 0 OR is_counted IS NULL )');
$countedStatusANDRoles = array_merge($countedStatus, $countedRoles);
$nonCountedStatusANDRoles = array_merge($nonCountedStatus, $nonCountedRoles);
$eventSummary['nonCountedRoles'] = implode('/', array_values($nonCountedRoles));
$eventSummary['nonCountedStatus'] = implode('/', array_values($nonCountedStatus));
$eventSummary['countedStatusANDRoles'] = implode('/', array_values($countedStatusANDRoles));
$eventSummary['nonCountedStatusANDRoles'] = implode('/', array_values($nonCountedStatusANDRoles));
return $eventSummary;
}
/**
* Get participant count.
*
* @param int $eventId
* @param bool $considerStatus consider status for participant count.
* Consider status for participant count.
* @param bool $status counted participant.
* Consider counted participant.
* @param bool $considerRole consider role for participant count.
* Consider role for participant count.
* @param bool $role consider counted( is filter role) participant.
* Consider counted( is filter role) participant.
*
* @return array
* array with count of participants for each event based on status/role
*/
public static function getParticipantCount(
$eventId,
$considerStatus = TRUE,
$status = TRUE,
$considerRole = TRUE,
$role = TRUE
) {
// consider both role and status for counted participants, CRM-4924.
$operator = " AND ";
// not counted participant.
if ($considerStatus && $considerRole && !$status && !$role) {
$operator = " OR ";
}
$clause = [];
if ($considerStatus) {
$statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
$statusClause = 'NOT IN';
if ($status) {
$statusClause = 'IN';
}
$status = implode(',', array_keys($statusTypes));
if (empty($status)) {
$status = 0;
}
$clause[] = "participant.status_id {$statusClause} ( {$status} ) ";
}
if ($considerRole) {
$roleTypes = CRM_Event_PseudoConstant::participantRole(NULL, 'filter = 1');
$roleClause = 'NOT IN';
if ($role) {
$roleClause = 'IN';
}
if (!empty($roleTypes)) {
$escapedRoles = [];
foreach (array_keys($roleTypes) as $roleType) {
$escapedRoles[] = CRM_Utils_Type::escape($roleType, 'String');
}
$clause[] = "participant.role_id {$roleClause} ( '" . implode("', '", $escapedRoles) . "' ) ";
}
}
$sqlClause = '';
if (!empty($clause)) {
$sqlClause = ' ( ' . implode($operator, $clause) . ' )';
}
return self::eventTotalSeats($eventId, $sqlClause);
}
/**
* Get the information to map a event.
*
* @param int $id
* For which we want map info.
*
* @return null|string
* title of the event
*/
public static function &getMapInfo(&$id) {
$sql = "
SELECT
civicrm_event.id AS event_id,
civicrm_event.title AS display_name,
civicrm_address.street_address AS street_address,
civicrm_address.city AS city,
civicrm_address.postal_code AS postal_code,
civicrm_address.postal_code_suffix AS postal_code_suffix,
civicrm_address.geo_code_1 AS latitude,
civicrm_address.geo_code_2 AS longitude,
civicrm_state_province.abbreviation AS state,
civicrm_country.name AS country,
civicrm_location_type.name AS location_type
FROM
civicrm_event
LEFT JOIN civicrm_loc_block ON ( civicrm_event.loc_block_id = civicrm_loc_block.id )
LEFT JOIN civicrm_address ON ( civicrm_loc_block.address_id = civicrm_address.id )
LEFT JOIN civicrm_state_province ON ( civicrm_address.state_province_id = civicrm_state_province.id )
LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
LEFT JOIN civicrm_location_type ON ( civicrm_location_type.id = civicrm_address.location_type_id )
WHERE civicrm_address.geo_code_1 IS NOT NULL
AND civicrm_address.geo_code_2 IS NOT NULL
AND civicrm_event.id = " . CRM_Utils_Type::escape($id, 'Integer');
$dao = new CRM_Core_DAO();
$dao->query($sql);
$locations = [];
$config = CRM_Core_Config::singleton();
while ($dao->fetch()) {
$location = [];
$location['displayName'] = addslashes($dao->display_name);
$location['lat'] = $dao->latitude;
$location['marker_class'] = 'Event';
$location['lng'] = $dao->longitude;
$params = ['entity_id' => $id, 'entity_table' => 'civicrm_event'];
$addressValues = CRM_Core_BAO_Location::getValues($params, TRUE);
$location['address'] = str_replace([
"\r",
"\n",
], '', addslashes(nl2br($addressValues['address'][1]['display_text'])));
$location['url'] = CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $dao->event_id);
$location['location_type'] = $dao->location_type;
$eventImage = '<img src="' . $config->resourceBase . 'i/contact_org.gif" alt="Organization " height="20" width="15" />';
$location['image'] = $eventImage;
$location['displayAddress'] = str_replace('<br />', ', ', $location['address']);
$locations[] = $location;
}
return $locations;
}
/**
* Get the complete information for one or more events.
*
* @param date $start
* Get events with start date >= this date.
* @param int $type Get events on the a specific event type (by event_type_id).
* Get events on the a specific event type (by event_type_id).
* @param int $eventId Return a single event - by event id.
* Return a single event - by event id.
* @param date $end
* Also get events with end date >= this date.
* @param bool $onlyPublic Include public events only, default TRUE.
* Include public events only, default TRUE.
*
* @return array
* array of all the events that are searched
*/
public static function getCompleteInfo(
$start = NULL,
$type = NULL,
$eventId = NULL,
$end = NULL,
$onlyPublic = TRUE
) {
$publicCondition = NULL;
if ($onlyPublic) {
$publicCondition = " AND civicrm_event.is_public = 1";
}
$dateCondition = '';
// if start and end date are NOT passed, return all events with start_date OR end_date >= today CRM-5133
if ($start) {
// get events with start_date >= requested start
$startDate = CRM_Utils_Type::escape($start, 'Date');
$dateCondition .= " AND ( civicrm_event.start_date >= {$startDate} )";
}
if ($end) {
// also get events with end_date <= requested end
$endDate = CRM_Utils_Type::escape($end, 'Date');
$dateCondition .= " AND ( civicrm_event.end_date <= '{$endDate}' ) ";
}
// CRM-9421 and CRM-8620 Default mode for ical/rss feeds. No start or end filter passed.
// Need to exclude old events with only start date
// and not exclude events in progress (start <= today and end >= today). DGG
if (empty($start) && empty($end)) {
// get events with end date >= today, not sure of this logic
// but keeping this for backward compatibility as per issue CRM-5133
$today = date("Y-m-d G:i:s");
$dateCondition .= " AND ( civicrm_event.end_date >= '{$today}' OR civicrm_event.start_date >= '{$today}' ) ";
}
if ($type) {
$typeCondition = " AND civicrm_event.event_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
}
// Get the Id of Option Group for Event Types
$optionGroupDAO = new CRM_Core_DAO_OptionGroup();
$optionGroupDAO->name = 'event_type';
$optionGroupId = NULL;
if ($optionGroupDAO->find(TRUE)) {
$optionGroupId = $optionGroupDAO->id;
}
$query = "
SELECT
civicrm_event.id as event_id,
civicrm_email.email as email,
civicrm_event.title as title,
civicrm_event.summary as summary,
civicrm_event.start_date as start,
civicrm_event.end_date as end,
civicrm_event.description as description,
civicrm_event.is_show_location as is_show_location,
civicrm_event.is_online_registration as is_online_registration,
civicrm_event.registration_link_text as registration_link_text,
civicrm_event.registration_start_date as registration_start_date,
civicrm_event.registration_end_date as registration_end_date,
civicrm_option_value.label as event_type,
civicrm_address.name as address_name,
civicrm_address.street_address as street_address,
civicrm_address.supplemental_address_1 as supplemental_address_1,
civicrm_address.supplemental_address_2 as supplemental_address_2,
civicrm_address.supplemental_address_3 as supplemental_address_3,
civicrm_address.city as city,
civicrm_address.postal_code as postal_code,
civicrm_address.postal_code_suffix as postal_code_suffix,
civicrm_state_province.abbreviation as state,
civicrm_country.name AS country
FROM civicrm_event
LEFT JOIN civicrm_loc_block ON civicrm_event.loc_block_id = civicrm_loc_block.id
LEFT JOIN civicrm_address ON civicrm_loc_block.address_id = civicrm_address.id
LEFT JOIN civicrm_state_province ON civicrm_address.state_province_id = civicrm_state_province.id
LEFT JOIN civicrm_country ON civicrm_address.country_id = civicrm_country.id
LEFT JOIN civicrm_email ON civicrm_loc_block.email_id = civicrm_email.id
LEFT JOIN civicrm_option_value ON (
civicrm_event.event_type_id = civicrm_option_value.value AND
civicrm_option_value.option_group_id = %1 )
WHERE civicrm_event.is_active = 1
AND (is_template = 0 OR is_template IS NULL)
{$publicCondition}
{$dateCondition}";
if (isset($typeCondition)) {
$query .= $typeCondition;
}
if (isset($eventId)) {
$query .= " AND civicrm_event.id =$eventId ";
}
$query .= " ORDER BY civicrm_event.start_date ASC";
$params = [1 => [$optionGroupId, 'Integer']];
$dao = CRM_Core_DAO::executeQuery($query, $params);
$all = [];
$config = CRM_Core_Config::singleton();
$baseURL = parse_url($config->userFrameworkBaseURL);
$url = "@" . $baseURL['host'];
if (!empty($baseURL['path'])) {
$url .= substr($baseURL['path'], 0, -1);
}
// check 'view event info' permission
//@todo - per CRM-14626 we have resolved that 'view event info' means 'view ALL event info'
if (CRM_Core_Permission::check('view event info')) {
$permissions = TRUE;
}
else {
$permissions = CRM_Core_Permission::event(CRM_Core_Permission::VIEW);
}
while ($dao->fetch()) {
if (!empty($permissions) && ($permissions === TRUE || in_array($dao->event_id, $permissions))) {
$info = [];
$info['uid'] = "CiviCRM_EventID_{$dao->event_id}_" . md5($config->userFrameworkBaseURL) . $url;
$info['title'] = $dao->title;
$info['event_id'] = $dao->event_id;
$info['summary'] = $dao->summary;
$info['description'] = $dao->description;
$info['start_date'] = $dao->start;
$info['end_date'] = $dao->end;
$info['contact_email'] = $dao->email;
$info['event_type'] = $dao->event_type;
$info['is_show_location'] = $dao->is_show_location;
$info['is_online_registration'] = $dao->is_online_registration;
$info['registration_link_text'] = $dao->registration_link_text;
$info['registration_start_date'] = $dao->registration_start_date;
$info['registration_end_date'] = $dao->registration_end_date;
$address = '';
$addrFields = [
'address_name' => $dao->address_name,
'street_address' => $dao->street_address,
'supplemental_address_1' => $dao->supplemental_address_1,
'supplemental_address_2' => $dao->supplemental_address_2,
'supplemental_address_3' => $dao->supplemental_address_3,
'city' => $dao->city,
'state_province' => $dao->state,
'postal_code' => $dao->postal_code,
'postal_code_suffix' => $dao->postal_code_suffix,
'country' => $dao->country,
'county' => NULL,
];
CRM_Utils_String::append($address, ', ',
CRM_Utils_Address::format($addrFields)
);
$info['location'] = $address;
$info['url'] = CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $dao->event_id, TRUE, NULL, FALSE);
// @todo Move to eventcart extension
// check if we're in shopping cart mode for events
if ((bool) Civi::settings()->get('enable_cart')) {
$reg = CRM_Event_Cart_BAO_EventInCart::get_registration_link($dao->event_id);
$info['registration_link'] = CRM_Utils_System::url($reg['path'], $reg['query'], TRUE);
$info['registration_link_text'] = $reg['label'];
}
$all[] = $info;
}
}
return $all;
}
/**
* Make a copy of a Event.
*
* Include all the fields in the event Wizard.
*
* @param int $id
* The event id to copy.
* @param array $params
*
* @return CRM_Event_DAO_Event
* @throws \CRM_Core_Exception
*/
public static function copy($id, $params = []) {
$session = CRM_Core_Session::singleton();
$eventValues = [];
//get the required event values.
$eventParams = ['id' => $id];
$returnProperties = [
'loc_block_id',
'is_show_location',
'default_fee_id',
'default_discount_fee_id',
'is_template',
];
CRM_Core_DAO::commonRetrieve('CRM_Event_DAO_Event', $eventParams, $eventValues, $returnProperties);
$fieldsFix = [
'prefix' => [
'title' => ts('Copy of') . ' ',
],
'replace' => [
'created_id' => $session->get('userID'),
'created_date' => date('YmdHis'),
],
];
if (empty($eventValues['is_show_location'])) {
$fieldsFix['prefix']['is_show_location'] = 0;
}
$blockCopyOfCustomValue = (!empty($params['custom']));
/** @var \CRM_Event_DAO_Event $copyEvent */
$copyEvent = CRM_Core_DAO::copyGeneric('CRM_Event_DAO_Event',
['id' => $id],
// since the location is sharable, lets use the same loc_block_id.
['loc_block_id' => CRM_Utils_Array::value('loc_block_id', $eventValues)] + $params,
$fieldsFix,
NULL,
$blockCopyOfCustomValue
);
CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_event', $id, $copyEvent->id);
CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin',
[
'entity_id' => $id,
'entity_table' => 'civicrm_event',
],
['entity_id' => $copyEvent->id]
);
CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend',
[
'entity_id' => $id,
'entity_table' => 'civicrm_event',
],
['entity_id' => $copyEvent->id]
);
CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock',
[
'entity_id' => $id,
'entity_table' => 'civicrm_event',
],
['entity_id' => $copyEvent->id],
['replace' => ['target_entity_id' => $copyEvent->id]]
);
$oldMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
'id' => ($eventValues['is_template'] ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID),
]));
$copyMapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
'id' => ($copyEvent->is_template == 1 ? CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID : CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID),
]));
CRM_Core_DAO::copyGeneric('CRM_Core_DAO_ActionSchedule',
['entity_value' => $id, 'mapping_id' => $oldMapping->getId()],
['entity_value' => $copyEvent->id, 'mapping_id' => $copyMapping->getId()]
);
$copyEvent->save();
if ($blockCopyOfCustomValue) {
foreach ($params['custom'] as &$values) {
foreach ($values as &$value) {
// Ensure we don't copy over the template's id if it is passed in
// This is a bit hacky but it's unclear what the