Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Event Calendar selecting upcoming events from every calendar on a site


Reply


6 Posts   764 Views

Avatar
Andrew Houle

Community Member, 133 Posts

9 April 2010 at 6:05am

I know I can use code like this to specify an individual calendar

function NewEvents() {
   return DataObject::get_by_id("Cal","814")->upcomingEvents(4, "ShowEventOnHome = 1", "ShowOnHome = 1");
}

But is it possible to run something like below in order to grab upcoming events/announcements that are set to show on home for every calendar throughout a site?

function NewEvents() {
   return DataObject::get("Cal")->upcomingEvents(4, "ShowEventOnHome = 1", "ShowOnHome = 1");
}

Thanks in advance for any advice in the right direction.

Andrew

Avatar
UncleCheese

Forum Moderator, 4096 Posts

9 April 2010 at 8:05am

Edited: 09/04/2010 8:05am

Sure, but it's a bit ugly..

pubclic function NewEvents()
{
$set = new DataObjectSet();
foreach(DataObject::get("Cal") as $cal) {
$set->merge($cal->UpcomingEvents(4, "ShowEventOnHome = 1", "ShowOnHome = 1"));
}
$arr = $set->toArray();
CalendarUtil::date_sort($arr);
return new DataObjectSet($arr);
}

Avatar
Andrew Houle

Community Member, 133 Posts

9 April 2010 at 8:11am

Awesome, thanks for the help!

Avatar
Andrew Houle

Community Member, 133 Posts

13 April 2010 at 1:18am

I just noticed that this script grabs a limit of 4 events from every calendar. So, if I had 10 calendars, and all of them had upcoming events, it would get a set of 40 events instead of just 4. Any ideas how to set the limit for a total of only 4 events?

Thanks,
Andy

Avatar
UncleCheese

Forum Moderator, 4096 Posts

13 April 2010 at 2:46am

I think after I posted that code, I realized I had left that out. I meant to edit the message, but I must have gotten distracted.

public function NewEvents($max = 5)
{
$set = new DataObjectSet();
foreach(DataObject::get("Cal") as $cal) {
$set->merge($cal->UpcomingEvents(4, "ShowEventOnHome = 1", "ShowOnHome = 1"));
}
$arr = $set->toArray();
CalendarUtil::date_sort($arr);
if($set->Count() > $max) {
   $arr = array_slice($arr, 0, $max);
}
return new DataObjectSet($arr);
}

Avatar
Andrew Houle

Community Member, 133 Posts

13 April 2010 at 3:19am

Perfect, thanks!

Andrew