From 41eb7beee0fc16d5c67aeec295a269b5f0e144a2 Mon Sep 17 00:00:00 2001 From: Uncle Cheese Date: Wed, 20 Feb 2013 11:10:28 -0500 Subject: [PATCH] ENHANCEMENT: New calendar caching --- MOVED_TO_GITHUB.txt | 1 - _config.php | 0 _config/calendar.yml | 2 + code/CachedCalendarBuildTask.php | 15 +++++ code/CachedCalendarEntry.php | 87 +++++++++++++++++++++++++++++ code/CachedCalendarTask.php | 58 +++++++++++++++++++ code/Calendar.php | 81 +++++++++++++++++---------- code/CalendarWidget.php | 7 +-- code/RecursionReader.php | 2 + composer.json | 26 --------- images/loader.gif | Bin javascript/calendar.js | 0 javascript/calendar_widget_init.js | 73 +++++++++++------------- javascript/lang/calendar_en.js | 0 templates/.gitignore | 0 templates/Includes/EventList.ss | 0 templates/Includes/MonthJumper.ss | 0 templates/Includes/QuickNav.ss | 0 test.txt | 0 19 files changed, 253 insertions(+), 99 deletions(-) delete mode 100755 MOVED_TO_GITHUB.txt mode change 100644 => 100755 _config.php create mode 100755 _config/calendar.yml create mode 100755 code/CachedCalendarBuildTask.php create mode 100755 code/CachedCalendarEntry.php create mode 100755 code/CachedCalendarTask.php delete mode 100644 composer.json mode change 100644 => 100755 images/loader.gif mode change 100644 => 100755 javascript/calendar.js mode change 100644 => 100755 javascript/lang/calendar_en.js mode change 100644 => 100755 templates/.gitignore mode change 100644 => 100755 templates/Includes/EventList.ss mode change 100644 => 100755 templates/Includes/MonthJumper.ss mode change 100644 => 100755 templates/Includes/QuickNav.ss delete mode 100644 test.txt diff --git a/MOVED_TO_GITHUB.txt b/MOVED_TO_GITHUB.txt deleted file mode 100755 index 9eccd6a..0000000 --- a/MOVED_TO_GITHUB.txt +++ /dev/null @@ -1 +0,0 @@ -git://github.com/unclecheese/EventCalendar.git diff --git a/_config.php b/_config.php old mode 100644 new mode 100755 diff --git a/_config/calendar.yml b/_config/calendar.yml new file mode 100755 index 0000000..ea03b87 --- /dev/null +++ b/_config/calendar.yml @@ -0,0 +1,2 @@ +CachedCalendarTask: + cache_future_years: 2 \ No newline at end of file diff --git a/code/CachedCalendarBuildTask.php b/code/CachedCalendarBuildTask.php new file mode 100755 index 0000000..ac3ec87 --- /dev/null +++ b/code/CachedCalendarBuildTask.php @@ -0,0 +1,15 @@ +process(); + } + +} \ No newline at end of file diff --git a/code/CachedCalendarEntry.php b/code/CachedCalendarEntry.php new file mode 100755 index 0000000..8235fdb --- /dev/null +++ b/code/CachedCalendarEntry.php @@ -0,0 +1,87 @@ + 'Date', + 'StartTime' => 'Time', + 'EndDate' => 'Date', + 'EndTime' => 'Time', + 'AllDay' => 'Boolean', + 'Announcement' => 'Boolean', + 'DateRange' => 'HTMLText', + 'TimeRange' => 'HTMLText', + 'ICSLink' => 'Varchar(100)', + 'Title' => 'Varchar(255)', + 'Content' => 'HTMLText' + ); + + + static $has_one = array ( + 'CachedCalendar' => 'Calendar', + 'Calendar' => 'Calendar', + 'Event' => 'CalendarEvent' + ); + + + + static $default_sort = "StartDate ASC, StartTime ASC"; + + + + + + public static function create_from_datetime(CalendarDateTime $dt, Calendar $calendar) { + $cached = CachedCalendarEntry::create(); + $cached->hydrate($dt, $calendar); + return $cached; + } + + + + public static function create_from_announcement(CalendarAnnouncement $dt, Calendar $calendar) { + $cached = CachedCalendarEntry::create(); + $cached->hydrate($dt, $calendar); + $cached->CalendarID = $dt->CalendarID; + $cached->Announcement = 1; + return $cached; + } + + + + + public function OtherDates() { + if($this->Announcement) { + return false; + } + + return CachedCalendarEntry::get() + ->where("EventID = {$this->EventID}") + ->where("StartDate != '{$this->StartDate}'") + ->limit($this->CachedCalendar()->DefaultEventDisplay); + } + + + + public function hydrate(CalendarDateTime $dt, Calendar $calendar) { + $this->CachedCalendarID = $calendar->ID; + foreach($dt->db() as $field => $type) { + $this->$field = $dt->$field; + } + foreach($dt->has_one() as $field => $type) { + $this->{$field."ID"} = $dt->{$field."ID"}; + } + $this->DateRange = $dt->DateRange(); + $this->TimeRange = $dt->TimeRange(); + $this->ICSLink = $dt->ICSLink(); + $this->Title = $dt->getTitle(); + $this->Content = $dt->getContent(); + return $this; + } + + + +} \ No newline at end of file diff --git a/code/CachedCalendarTask.php b/code/CachedCalendarTask.php new file mode 100755 index 0000000..975e210 --- /dev/null +++ b/code/CachedCalendarTask.php @@ -0,0 +1,58 @@ +config()->cache_future_years; + foreach(Calendar::get() as $calendar) { + echo "

Caching calendar '$calendar->Title'

\n"; + foreach($calendar->getAllCalendars() as $c) { + foreach($c->AllChildren() as $event) { + // All the dates of regular events + if($event->Recursion) { + echo "

Creating recurring events for '$event->Title'

\n"; + $i = 0; + $dt = $event->DateTimes()->first(); + if(!$dt) continue; + if($dt->EndDate) { + $end_date = sfDate::getInstance($dt->EndDate); + } + else { + $end_date = sfDate::getInstance()->addYear($future_years); + } + $start_date = sfDate::getInstance($dt->StartDate); + $recursion = $event->getRecursionReader(); + while($start_date->get() <= $end_date->get()) { + if($recursion->recursionHappensOn($start_date->get())) { + $dt->StartDate = $start_date->format('Y-m-d'); + $cached = CachedCalendarEntry::create_from_datetime($dt, $calendar); + $cached->EndDate = $cached->StartDate; + $cached->write(); + } + $start_date->addDay(); + $i++; + } + echo "

$i events created.

\n"; + } + else { + foreach($event->DateTimes() as $dt) { + echo "

Adding dates for event '$event->Title'

\n"; + $cached = CachedCalendarEntry::create_from_datetime($dt, $calendar); + $cached->write(); + } + } + // Announcements + } + foreach($c->Announcements() as $a) { + echo "

Adding announcement $a->Title

\n"; + $cached = CachedCalendarEntry::create_from_announcement($a, $calendar); + $cached->write(); + } + } + } + echo "Done!"; + } +} \ No newline at end of file diff --git a/code/Calendar.php b/code/Calendar.php index 6700c15..16b1d37 100755 --- a/code/Calendar.php +++ b/code/Calendar.php @@ -76,11 +76,16 @@ class Calendar extends Page { + static $caching_enabled = false; + + + protected $eventClass_cache, $announcementClass_cache, $datetimeClass_cache, $dateToEventRelation_cache, - $announcementToCalendarRelation_cache; + $announcementToCalendarRelation_cache, + $EventList_cache; @@ -89,6 +94,12 @@ public static function set_jquery_included($bool = true) { } + + public static function enable_caching() { + self::$caching_enabled = true; + } + + public function getCMSFields() { @@ -175,7 +186,31 @@ public function getDateToEventRelation() { + + public function getCachedEventList($start, $end, $filter = null, $limit = null) { + return CachedCalendarEntry::get() + ->where(" + ((StartDate <= '$start' AND EndDate >= '$end') OR + (StartDate BETWEEN '$start' AND '$end') OR + (EndDate BETWEEN '$start' AND '$end')) + AND + CachedCalendarID = $this->ID + ") + ->sort(array( + "StartDate" => "ASC", + "StartTime" => "ASC" + )) + ->limit($limit); + + } + + + + public function getEventList($start, $end, $filter = null, $limit = null, $announcement_filter = null) { + if(Config::inst()->get("Calendar","caching_enabled")) { + return $this->getCachedEventList($start, $end, $filter, $limit); + } $eventList = new ArrayList(); foreach($this->getAllCalendars() as $calendar) { if($events = $calendar->getStandardEvents($start, $end, $filter)) { @@ -188,9 +223,10 @@ public function getEventList($start, $end, $filter = null, $limit = null, $annou (StartDate BETWEEN '$start' AND '$end') OR (EndDate BETWEEN '$start' AND '$end') "); - if($announcement_filter) { + if($filter) { $announcements = $announcements->where($announcement_filter); - } + } + if($announcements) { foreach($announcements as $announcement) { $eventList->push($announcement); @@ -208,15 +244,21 @@ public function getEventList($start, $end, $filter = null, $limit = null, $annou $eventList = $eventList->sort(array("StartDate" => "ASC", "StartTime" => "ASC")); $eventList = $eventList->limit($limit); - return $eventList; + return $this->EventList_cache = $eventList; } protected function getStandardEvents($start, $end, $filter = null) { - if(!$children = $this->AllChildren()) return false; - $ids = $children->column('ID'); + $ids = array (); + $r = DB::query("SELECT ID FROM SiteTree_Live WHERE ClassName = 'CalendarEvent' AND ParentID = $this->ID"); + if($r) { + while($rec = $r->nextRecord()) $ids[] = $rec['ID']; + } + + // $children = $this->AllChildren(); + // $ids = $children->column('ID'); $datetime_class = $this->getDateTimeClass(); $relation = $this->getDateToEventRelation(); $event_class = $this->getEventClass(); @@ -342,30 +384,18 @@ public function getAllCalendars() { - public function UpcomingEvents($limit = 5, $filter = null, $announcement_filter = null) { + public function UpcomingEvents($limit = 5, $filter = null) { $all = $this->getEventList( sfDate::getInstance()->date(), sfDate::getInstance()->addMonth($this->DefaultFutureMonths)->date(), $filter, - $limit, - $announcement_filter + $limit ); return $all->limit($limit); } - public function UpcomingAnnouncements($limit = 5, $filter = null) { - return $this->Announcements() - ->filter(array( - 'StartDate:GreaterThan' => 'DATE(NOW())' - )) - ->where($filter) - ->limit($limit); - } - - - public function RecentEvents($limit = null, $filter = null) { $start_date = sfDate::getInstance(); $end_date = sfDate::getInstance(); @@ -471,12 +501,7 @@ public function getView() { public function setDefaultView() { $this->view = "default"; $this->startDate = sfDate::getInstance(); - if($this->DefaultFutureMonths!=null){ - $mCount = $this->DefaultFutureMonths; - }else{ - $mCount = 6; - } - $this->endDate = sfDate::getInstance()->addMonth($mCount); + $this->endDate = sfDate::getInstance()->addMonth(6); } @@ -822,10 +847,10 @@ public function Events() { null, $announcement_filter ); - + $all_events_count = $all->count(); $list = $all->limit($this->EventsPerPage, $this->getOffset()); $next = $this->getOffset()+$this->EventsPerPage; - $this->MoreEvents = ($next < $all->count()); + $this->MoreEvents = ($next < $all_events_count); $this->MoreLink = HTTP::setGetVar("start", $next); return $list; } diff --git a/code/CalendarWidget.php b/code/CalendarWidget.php index 082bae9..09f0e54 100755 --- a/code/CalendarWidget.php +++ b/code/CalendarWidget.php @@ -1,3 +1,4 @@ + getDataAttributes() . '>'; } -} +} \ No newline at end of file diff --git a/code/RecursionReader.php b/code/RecursionReader.php index 2c46e5b..bea77b9 100755 --- a/code/RecursionReader.php +++ b/code/RecursionReader.php @@ -112,4 +112,6 @@ public function recursionHappensOn($ts) } + + } \ No newline at end of file diff --git a/composer.json b/composer.json deleted file mode 100644 index 567ddc2..0000000 --- a/composer.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name":"silverstripe/event-calendar", - "type": "silverstripe-module", - "description": "Event Calendar for the SilverStripe CMS", - "keywords": ["silverstripe", "events"], - "license": "BSD-3-Clause", - "authors":[ - { - "name": "Aaron Carlino", - "homepage": "http://leftandmain.com" - } - ], - "require": - { - "php": ">=5.3.2", - "silverstripe/framework": "3.*", - "silverstripe/cms": "3.*" - }, - "support": { - "issues": "https://github.com/unclecheese/EventCalendar/issues" - }, - "extra": { - "installer-name": "event_calendar" - } - -} diff --git a/images/loader.gif b/images/loader.gif old mode 100644 new mode 100755 diff --git a/javascript/calendar.js b/javascript/calendar.js old mode 100644 new mode 100755 diff --git a/javascript/calendar_widget_init.js b/javascript/calendar_widget_init.js index 2b4fde7..91f54b1 100755 --- a/javascript/calendar_widget_init.js +++ b/javascript/calendar_widget_init.js @@ -2,23 +2,16 @@ $(function() { var calendar_url = $('.calendar-widget').data('url'); -var loaded_months = {}, - finish = 0; +var loaded_months = {}; -function end(calendar) { - if(finish==3) setSelection(calendar); -} - - -function loadMonthJson(month, year, callback) { +function loadMonthJson(month, year) { $.ajax({ url: calendar_url+"monthjson/"+year+month, - async: true, + async: false, dataType: 'json', success: function(data) { json = data; - loaded_months[year+month] = data; - if(callback) callback(); + loaded_months[year+month] = data; } }); } @@ -29,6 +22,7 @@ function applyMonthJson(month, year) { for(date in json) { if(json[date].events.length) { $('[data-date="'+date+'"]').addClass('hasEvent').attr('title', json[date].events.join("\n")); + } } } @@ -70,19 +64,14 @@ $('.calendar-widget').each(function() { var json; m = calendar.pad(month); if(!loaded_months[year+m]) { - loadMonthJson(m, year, function() { - applyMonthJson(m, year); - setSelection(calendar); - }); - } - else { - applyMonthJson(m, year); - setSelection(calendar); + loadMonthJson(m, year); } + json = loaded_months[year+m]; + applyMonthJson(m, year); + setSelection(calendar); }, - onInit: function(calendar) - { + onInit: function(calendar) { previous = calendar.getPrevMonthYear(); next = calendar.getNextMonthYear(); @@ -93,23 +82,26 @@ $('.calendar-widget').each(function() { prev_year = previous[1]; next_year = next[1]; - loadMonthJson(this_month, this_year, function() { - applyMonthJson(this_month, this_year); - finish++; - end(calendar); - }); - - loadMonthJson(prev_month, prev_year, function() { - applyMonthJson(prev_month, prev_year); - finish++; - end(calendar); - }); - - loadMonthJson(next_month, next_year, function() { - applyMonthJson(next_month, next_year); - finish++; - end(calendar); - }); + loadMonthJson( + this_month, + this_year + ); + + loadMonthJson( + prev_month, + prev_year + ); + + loadMonthJson( + next_month, + next_year + ); + applyMonthJson(this_month, this_year); + applyMonthJson(prev_month, prev_year); + applyMonthJson(next_month, next_year); + + setSelection(calendar); + } }; @@ -118,11 +110,12 @@ $('.calendar-widget').each(function() { var parts = $(this).data('start').split('-'); opts.month = Number(parts[1])-1; opts.year = parts[0]; + + } - $(this).CalendarWidget(opts); }) }); -})(jQuery); +})(jQuery); \ No newline at end of file diff --git a/javascript/lang/calendar_en.js b/javascript/lang/calendar_en.js old mode 100644 new mode 100755 diff --git a/templates/.gitignore b/templates/.gitignore old mode 100644 new mode 100755 diff --git a/templates/Includes/EventList.ss b/templates/Includes/EventList.ss old mode 100644 new mode 100755 diff --git a/templates/Includes/MonthJumper.ss b/templates/Includes/MonthJumper.ss old mode 100644 new mode 100755 diff --git a/templates/Includes/QuickNav.ss b/templates/Includes/QuickNav.ss old mode 100644 new mode 100755 diff --git a/test.txt b/test.txt deleted file mode 100644 index e69de29..0000000