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   710 Views

Avatar
Andrew Houle

9 April 2010 at 6:05am Community Member, 132 Posts

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

9 April 2010 at 8:05am (Last edited: 9 April 2010 8:05am), 4085 Posts

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

9 April 2010 at 8:11am Community Member, 132 Posts

Awesome, thanks for the help!

Avatar
Andrew Houle

13 April 2010 at 1:18am Community Member, 132 Posts

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

13 April 2010 at 2:46am 4085 Posts

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

13 April 2010 at 3:19am Community Member, 132 Posts

Perfect, thanks!

Andrew