Skip to content

Commit

Permalink
ENHANCEMENT: New calendar caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncle Cheese committed Feb 20, 2013
1 parent b045ac7 commit 41eb7be
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 99 deletions.
1 change: 0 additions & 1 deletion MOVED_TO_GITHUB.txt

This file was deleted.

Empty file modified _config.php
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions _config/calendar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CachedCalendarTask:
cache_future_years: 2
15 changes: 15 additions & 0 deletions code/CachedCalendarBuildTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class CachedCalendarBuildTask extends BuildTask {

protected $title = "Cache the Event Calendars";


protected $description = 'Generates a given number of years of events and populates a readonly table with all the event information. Useful when using recurring events or multiple calendars.';


public function run($request) {
CachedCalendarTask::create()->process();
}

}
87 changes: 87 additions & 0 deletions code/CachedCalendarEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php


class CachedCalendarEntry extends DataObject {



static $db = array (
'StartDate' => '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;
}



}
58 changes: 58 additions & 0 deletions code/CachedCalendarTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


class CachedCalendarTask extends HourlyTask {

public function process() {
increase_time_limit_to(300);
DB::query("DELETE FROM CachedCalendarEntry");
$future_years = $this->config()->cache_future_years;
foreach(Calendar::get() as $calendar) {
echo "<h2>Caching calendar '$calendar->Title'</h2>\n";
foreach($calendar->getAllCalendars() as $c) {
foreach($c->AllChildren() as $event) {
// All the dates of regular events
if($event->Recursion) {
echo "<h3>Creating recurring events for '$event->Title'</h3>\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 "<p>$i events created.</p>\n";
}
else {
foreach($event->DateTimes() as $dt) {
echo "<p>Adding dates for event '$event->Title'</p>\n";
$cached = CachedCalendarEntry::create_from_datetime($dt, $calendar);
$cached->write();
}
}
// Announcements
}
foreach($c->Announcements() as $a) {
echo "<p>Adding announcement $a->Title</p>\n";
$cached = CachedCalendarEntry::create_from_announcement($a, $calendar);
$cached->write();
}
}
}
echo "Done!";
}
}
81 changes: 53 additions & 28 deletions code/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;



Expand All @@ -89,6 +94,12 @@ public static function set_jquery_included($bool = true) {
}



public static function enable_caching() {
self::$caching_enabled = true;
}



public function getCMSFields()
{
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 3 additions & 4 deletions code/CalendarWidget.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<?php


Expand Down Expand Up @@ -54,14 +55,12 @@ public function setSelectionEnd($date) {


public function forTemplate() {
if(!Calendar::$jquery_included) {
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
}
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
Requirements::javascript("event_calendar/javascript/calendar_widget.js");
$locale_file = _t('Calendar.DATEJSFILE','calendar_en.js');
Requirements::javascript("event_calendar/javascript/lang/{$locale_file}");
Requirements::javascript("event_calendar/javascript/calendar_widget_init.js");
Requirements::css("event_calendar/css/calendar_widget.css");
return '<div class="calendar-widget" ' . $this->getDataAttributes() . '></div>';
}
}
}
2 changes: 2 additions & 0 deletions code/RecursionReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ public function recursionHappensOn($ts)
}




}
26 changes: 0 additions & 26 deletions composer.json

This file was deleted.

Empty file modified images/loader.gif
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified javascript/calendar.js
100644 → 100755
Empty file.
Loading

0 comments on commit 41eb7be

Please sign in to comment.