silverstripe-event-calendar icon indicating copy to clipboard operation
silverstripe-event-calendar copied to clipboard

UpcomingAnnouncements error

Open ghost opened this issue 10 years ago • 4 comments

The function "UpcomingAnnouncements" was trying to filter the date using DATE(NOW()) which does not work. Replace that with NOW and it works fine.

public function UpcomingAnnouncements($limit = 5, $filter = null) {
    return $this->Announcements()
        ->filter(array(
            'StartDate:GreaterThan'
        // REPLACED THIS CODE
        //  => 'DATE(NOW())'
        // WITH THIS:
            => 'NOW'
        ))
        ->where($filter)
        ->limit($limit);
}

ghost avatar Apr 20 '14 17:04 ghost

Hello, Thanks for this fix! I was having trouble with announcements not honoring the StartDate. I would post an announcement and it would show up no matter what StarDate I gave it in the CMS.

I added the following to make sure the announcement would honor the EndDate as well (in Calendar.php - around line 412):

'EndDate:GreaterThan' => 'NOW',

My function now looks like this:

public function UpcomingAnnouncements($limit = 5, $filter = null) {
        return $this->Announcements()
            ->filter(array(
                'EndDate:GreaterThan' => 'NOW',
                'StartDate:LessThan' => 'NOW'
            ))
            ->where($filter)
            ->limit($limit);
}

birwin79 avatar May 07 '14 18:05 birwin79

The way you have this filter written it will only show those events which have already begun, but have not yet ended. If that is your goal, I would write a separate function for that. It doesn't work for events that only have a start date, or events that have a start date in the future.

ghost avatar May 07 '14 21:05 ghost

Hello, thanks for the feedback. It is interesting, because it seems to be working for my purposes, but I'm sure there is a better way to do it.

My use scenario: I have an announcement that I would like to appear on the front page of my website for the last 5 days of every month. I scheduled this announcement in the Announcement area in the Calendars CMS.

I added a custom function in my Page.php Page_Controller for the flexibililty to allow this announcement to show up on every page.

Page.php

public function MyAnnouncements($numAnnouncements=1) {
    return DataObject::get_one("Calendar")->UpcomingAnnouncements($numAnnouncements); 
}

Page.ss <% loop MyAnnouncements %>End of month message code in here<% end_loop %>

The only way I have been able to get the Announcement to only show between the Start and End dates is by adding the filters to the UpcomingAnnouncements function in Calendar.php. Otherwise, the announcement shows up until I delete it.

I probably should have just added the filters to my function in Page.php. I'm a little new to php, so I may be missing something totally obvious.

Best Regards

birwin79 avatar May 07 '14 22:05 birwin79

For your purposes I'm sure it works fine.

ghost avatar May 08 '14 02:05 ghost