unclecheese / silverstripe-event-calendar

The Event Calendar module for SilverStripe
GNU General Public License v2.0
31 stars 78 forks source link

RecentEvents sorting ignored #127

Open TheBnl opened 7 years ago

TheBnl commented 7 years ago

In the Calendar'sRecentEvents method the sorting is ignored after the getEventList method is called.

public function RecentEvents($limit = null, $filter = null)  {
    $start_date = sfDate::getInstance();
    $end_date = sfDate::getInstance();
    $l = ($limit === null) ? "9999" : $limit;
    $events = $this->getEventList(
        $start_date->subtractMonth($this->DefaultFutureMonths)->date(),
        $end_date->yesterday()->date(), 
        $filter,
        $l
    );
    $events->sort('StartDate DESC'); // the sorting stays ASC as set in the getEventList method
    return $events->limit($limit);
}

Removing the sort from the getEventList method solves the issue, but i'm not sure if that is desired. The $default_sort of the CalendarDateTime keeps it in good order though.

unclecheese commented 7 years ago

I think it's because the list is immutable. Should be $events = $events->sort(...);

chromos33 commented 6 years ago

isn't the extra sort and limit not redundant as far as I see it getEventList either in it's main Path or in the getCachedEventList already sorts by StartDate and limits by $l/$limit.

chromos33 commented 6 years ago

Think I found out what's wrong If you set a Limit of 1 it first sorts all Events by StartDate ASC then limits 1 and then the return events get sorted StartDate DESC and then limited by once more i think getEventList needs another Paramter for DESC/ASC direction.

chromos33 commented 6 years ago

Changed the Functions to this and now works as intended

public function getCachedEventList($start, $end, $filter = null, $limit = null, $sort = "ASC") {
        return CachedCalendarEntry::get()
            ->filter(array(
                "CachedCalendarID" => $this->ID
            ))
            ->exclude(array(
                "StartDate:LessThan" => $end,
                "EndDate:GreaterThan" => $start,
            ))
            ->sort(array(
                "StartDate" => $sort,
                "StartTime" => $sort
            ))
            ->limit($limit);

    }

    public function getEventList($start, $end, $filter = null, $limit = null, $announcement_filter = null,$sortorder = "ASC") {
        if(Config::inst()->get("Calendar", "caching_enabled")) {
            return $this->getCachedEventList($start, $end, $filter, $limit,$sortorder);
        }

        $eventList = new ArrayList();

        foreach($this->getAllCalendars() as $calendar) {
            if($events = $calendar->getStandardEvents($start, $end, $filter)) {
                $eventList->merge($events);
            }

            $announcements = DataList::create($this->getAnnouncementClass())
                ->filter(array(
                    "CalendarID" => $calendar->ID,
                    "StartDate:LessThan:Not" => $start,
                    "EndDate:GreaterThan:Not" => $end,
                ));
            if($announcement_filter) {
                $announcements = $announcements->where($announcement_filter);
            }

            if($announcements) {
                foreach($announcements as $announcement) {
                    $eventList->push($announcement);
                }
            }

            if($recurring = $calendar->getRecurringEvents($filter)) {
                $eventList = $calendar->addRecurringEvents($start, $end, $recurring, $eventList);
            }

            if($feedevents = $calendar->getFeedEvents($start,$end)) {
                $eventList->merge($feedevents);
            }
        }

        $eventList = $eventList->sort(array("StartDate" => $sortorder, "StartTime" => $sortorder));
        $eventList = $eventList->limit($limit);

        return $this->EventList_cache = $eventList;
    }
public function RecentEvents($limit = null, $filter = null)  {
        $start_date = sfDate::getInstance();
        $end_date = sfDate::getInstance();
        $l = ($limit === null) ? "9999" : $limit;
        $events = $this->getEventList(
            $start_date->subtractMonth($this->DefaultFutureMonths)->date(),
            $end_date->yesterday()->date(), 
            $filter,
            $l,
            null,
            "DESC"
        );
        return $events;
    }