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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

Event Calendar selecting upcoming events from every calendar on a site


Go to End


6 Posts   1097 Views

Avatar
Andrew Houle

Community Member, 140 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, 4102 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, 140 Posts

9 April 2010 at 8:11am

Awesome, thanks for the help!

Avatar
Andrew Houle

Community Member, 140 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, 4102 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, 140 Posts

13 April 2010 at 3:19am

Perfect, thanks!

Andrew