private function _fillTipsFeed($category_id = null, $limit = 20)
 {
     $this->load->library('academic_calendar');
     $channel =& $this->main_frame->Channel();
     $channel->SetImage('http://' . $_SERVER['HTTP_HOST'] . '/images/crosswords/xw.png', 64, 64);
     $channel->SetEditor('crosswords@theyorker.co.uk (crosswords editor)');
     $channel->SetWebmaster('webmaster@theyorker.co.uk (webmaster)');
     $tips = $this->crosswords_model->GetTips($category_id, null, null, true);
     $url = 'http://' . $_SERVER['HTTP_HOST'];
     foreach ($tips as &$tip) {
         $item =& $channel->NewItem();
         $item->SetPublicationDate($tip['publication']);
         $pub = new Academic_time($tip['publication']);
         $pub_str = $pub->Format('D ') . $pub->AcademicTermNameUnique() . ' week ' . $pub->AcademicWeek();
         $title = '(' . $tip['category_name'] . ') ' . $pub_str;
         $item->SetTitle($title);
         $item->AddCategory($tip['category_name']);
         $item->SetLink($url . '/crosswords/' . (int) $tip['crossword_id'] . '#tip' . (int) $tip['id']);
         $item->SetDescription($tip['content_xhtml']);
     }
 }
 function ajax_termdates()
 {
     OutputModes('ajax');
     if (!CheckPermissions('public')) {
         return;
     }
     $error = false;
     $years = array();
     if (isset($_GET['years'])) {
         $year_ranges = split(',', $_GET['years']);
         foreach ($year_ranges as $year_range) {
             if ($year_range === '') {
                 continue;
             }
             $ends = split('-', $year_range);
             if (count($ends) > 2) {
                 $this->main_frame->Error(array('class' => 'error', 'text' => 'Invalid year range: ' . $year_range));
                 $error = true;
             } else {
                 if (count($ends) == 1) {
                     $ends = array($year_range, $year_range);
                 }
                 $range_error = false;
                 foreach ($ends as &$end) {
                     if (is_numeric($end) && $end >= 1970 && $end < 2037) {
                         $end = (int) $end;
                     } else {
                         $this->main_frame->Error(array('class' => 'error', 'text' => 'Invalid year: ' . $end));
                         $range_error = true;
                     }
                 }
                 if (!$range_error) {
                     for ($year = $ends[0]; $year <= $ends[1]; ++$year) {
                         $years[] = $year;
                     }
                 } else {
                     $error = true;
                 }
             }
         }
     }
     if (!$error) {
         $root = array('_tag' => 'calendar', 'termdates' => array());
         foreach ($years as $year) {
             $acyear = array('_tag' => 'academicyear', '_attr' => array('year' => $year));
             for ($term = 0; $term < 6; ++$term) {
                 $start_ts = Academic_time::StartOfAcademicTerm($year, $term);
                 $monday_ts = Academic_time::MondayWeek1OfAcademicTerm($year, $term);
                 $start = new Academic_time($start_ts);
                 $days = Academic_time::LengthOfAcademicTerm($year, $term);
                 $end = $start->Adjust($days . 'days');
                 $monday = new Academic_time($monday_ts);
                 $acterm = array('_tag' => 'term', '_attr' => array('id' => "term_{$year}_{$term}", 'term' => $term), 'type' => $start->AcademicTermTypeName(), 'name' => $start->AcademicTermName(), 'unique' => $start->AcademicTermNameUnique(), 'start' => $start->Format('Y-m-d'), 'end' => $end->Format('Y-m-d'), 'days' => $days, 'mondayweek1' => $monday->Format('Y-m-d'), 'weeks' => $start->AcademicTermWeeks());
                 $acyear[] = $acterm;
             }
             $root['termdates'][] = $acyear;
         }
         $this->main_frame->SetXml($root);
     }
     $this->main_frame->Load();
 }
    protected function _Load()
    {
        $value = $this->value;
        if (null === $value) {
            $value = Academic_time::NewToday();
        } else {
            $value = new Academic_time($value);
        }
        ?>
<div	class="input_date_display"<?php 
        ?>
	onclick="<?php 
        echo xml_escape('return input_date_click("' . $this->name . '");');
        ?>
"<?php 
        ?>
	><?php 
        if ($this->date) {
            ?>
<span class="day" id="<?php 
            echo $this->id . '__day';
            ?>
"><?php 
            echo $value->Format('l');
            ?>
</span> <?php 
            ?>
week <span class="week" id="<?php 
            echo $this->id . '__wk';
            ?>
"><?php 
            echo $value->AcademicWeek();
            ?>
</span> <?php 
            ?>
of <span class="term" id="<?php 
            echo $this->id . '__term';
            ?>
"><?php 
            echo ucfirst($value->AcademicTermNameUnique());
            echo ' ' . $value->StartOfTerm()->Year();
            ?>
</span> <?php 
        }
        if ($this->time) {
            ?>
at <span class="hour" id="<?php 
            echo $this->id . '__hr';
            ?>
"><?php 
            echo $value->Hour();
            ?>
</span><?php 
            ?>
:<span class="minute" id="<?php 
            echo $this->id . '__min';
            ?>
"><?php 
            echo $value->Minute();
            ?>
</span> <?php 
        }
        ?>
</div><?php 
        $days = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
        ?>
<div	class="input_date_selector"<?php 
        ?>
	id="<?php 
        echo $this->id . '__selector';
        ?>
"<?php 
        ?>
	><?php 
        // Init script
        ?>
<script type="text/javascript"><?php 
        echo xml_escape('onLoadFunctions.push(function() {' . 'input_date_init(' . js_literalise($this->name) . ');' . '});', false);
        ?>
</script><?php 
        ?>
<div><?php 
        // Day of the week
        ?>
<select	id="<?php 
        echo $this->id . '__day_select';
        ?>
"<?php 
        ?>
	name="<?php 
        echo $this->name . '[day]';
        ?>
"<?php 
        ?>
	onchange="<?php 
        echo xml_escape('return input_date_day_changed("' . $this->name . '");');
        ?>
"<?php 
        ?>
><?php 
        foreach ($days as $val => $day) {
            ?>
<option value="<?php 
            echo $val;
            ?>
"<?php 
            if ($val == $value->DayOfWeek(1)) {
                ?>
 selected="selected"<?php 
            }
            ?>
><?php 
            echo $day;
            ?>
</option><?php 
        }
        ?>
</select><?php 
        // Week of the term
        ?>
<span>week</span><?php 
        ?>
<select	id="<?php 
        echo $this->id . '__wk_select';
        ?>
"<?php 
        ?>
	name="<?php 
        echo $this->name . '[wk]';
        ?>
"<?php 
        ?>
	onchange="<?php 
        echo xml_escape('return input_date_day_changed("' . $this->name . '");');
        ?>
"<?php 
        ?>
><?php 
        $weeks = $value->AcademicTermWeeks();
        for ($wk = 1; $wk <= $weeks; ++$wk) {
            ?>
<option value="<?php 
            echo $wk;
            ?>
"<?php 
            if ($wk == $value->AcademicWeek()) {
                ?>
 selected="selected"<?php 
            }
            ?>
><?php 
            echo $wk;
            ?>
</option><?php 
        }
        ?>
</select><?php 
        // Term
        ?>
<span>of</span><?php 
        ?>
<select	id="<?php 
        echo $this->id . '__term_select';
        ?>
"<?php 
        ?>
	name="<?php 
        echo $this->name . '[term]';
        ?>
"<?php 
        ?>
	onchange="<?php 
        echo xml_escape('return input_date_term_changed("' . $this->name . '");');
        ?>
"<?php 
        ?>
><?php 
        $sel_year = $value->AcademicYear();
        $sel_term = $value->AcademicTerm();
        $year = $sel_year;
        $term = 0;
        ?>
<option value="earlier">(more)</option><?php 
        for ($i = 0; $i < 6; ++$i) {
            $cur = new Academic_time(Academic_time::StartOfAcademicTerm($year, $term));
            ?>
<option value="<?php 
            echo "{$year}-{$term}";
            ?>
"<?php 
            if ($term == $sel_term && $year == $sel_year) {
                ?>
 selected="selected"<?php 
            }
            ?>
><?php 
            echo xml_escape(ucfirst($cur->AcademicTermNameUnique()) . ' ' . $cur->Year());
            ?>
</option><?php 
            ++$term;
            if ($term == 6) {
                $term = 0;
                ++$year;
            }
        }
        ?>
<option value="later">(more)</option><?php 
        ?>
</select><?php 
        // Time of day
        if ($this->time) {
            ?>
<span>at</span><?php 
            ?>
<select	id="<?php 
            echo $this->id . '__hr_select';
            ?>
"<?php 
            ?>
	name="<?php 
            echo $this->name . '[hr]';
            ?>
"<?php 
            ?>
	onchange="<?php 
            echo xml_escape('return input_date_time_changed("' . $this->name . '");');
            ?>
"<?php 
            ?>
><?php 
            for ($hr = 0; $hr < 24; ++$hr) {
                ?>
<option value="<?php 
                echo $hr;
                ?>
"<?php 
                if ($hr == $value->Hour()) {
                    ?>
 selected="selected"<?php 
                }
                ?>
><?php 
                echo sprintf('%02d', $hr);
                ?>
</option><?php 
            }
            ?>
</select><?php 
            ?>
<span>:</span><?php 
            ?>
<select	id="<?php 
            echo $this->id . '__min_select';
            ?>
"<?php 
            ?>
	name="<?php 
            echo $this->name . '[min]';
            ?>
"<?php 
            ?>
	onchange="<?php 
            echo xml_escape('return input_date_time_changed("' . $this->name . '");');
            ?>
"<?php 
            ?>
><?php 
            $minute = $value->Minute();
            $minute_interval = 5;
            for ($min = 0; $min < 60; $min += $minute_interval) {
                ?>
<option value="<?php 
                echo $min;
                ?>
"<?php 
                if ($min <= $minute && $min + $minute_interval > $minute) {
                    ?>
 selected="selected"<?php 
                }
                ?>
><?php 
                echo sprintf('%02d', $min);
                ?>
</option><?php 
            }
            ?>
</select><?php 
        }
        // Close button
        ?>
<input	type="button" value="x"<?php 
        ?>
	onclick="<?php 
        echo xml_escape('return input_selector_click("' . $this->name . '__selector");');
        ?>
"<?php 
        ?>
	/><?php 
        ?>
</div><?php 
        ?>
<div><?php 
        ?>
<table class="recur-cal cal-text"><?php 
        // Days along the top
        ?>
<tr><?php 
        ?>
<th /><?php 
        foreach ($days as $day) {
            ?>
<th><?php 
            echo xml_escape($day);
            ?>
</th><?php 
        }
        ?>
</tr><?php 
        $cur = $value->MondayWeek1OfTerm();
        $sel = $value->Midnight()->Timestamp();
        $today = Academic_time::NewToday()->Timestamp();
        $last_month = 0;
        $term = $cur->AcademicTerm();
        for ($wk = 1; $cur->AcademicTerm() == $term; ++$wk) {
            ?>
<tr id="<?php 
            echo $this->id . '__wk_' . $wk;
            ?>
"><?php 
            ?>
<th><?php 
            echo $wk;
            ?>
</th><?php 
            for ($dy = 0; $dy < 7; ++$dy) {
                $month = $cur->Month();
                $ts = $cur->Timestamp();
                $classes = array();
                if ($ts < $today) {
                    $classes[] = "pa";
                }
                if ($month % 2 == 0) {
                    $classes[] = "ev";
                }
                if ($ts == $today) {
                    $classes[] = "tod";
                }
                if ($ts == $sel) {
                    $classes[] = "sel";
                }
                if ($dy >= 5) {
                    $classes[] = "we";
                }
                ?>
<td	class="<?php 
                echo join(' ', $classes);
                ?>
"<?php 
                ?>
	id="<?php 
                echo $this->id . '__' . $cur->AcademicWeek() . '_' . $cur->Format('D');
                ?>
"<?php 
                ?>
	onclick="<?php 
                echo xml_escape('return input_date_change(' . js_literalise($this->name) . ',' . js_literalise($wk) . ',' . js_literalise($dy) . ');');
                ?>
"<?php 
                ?>
	><?php 
                if ($month != $last_month) {
                    echo xml_escape($cur->Format('M')) . '&nbsp;';
                    $last_month = $month;
                }
                echo xml_escape($cur->Format('j'));
                ?>
</td><?php 
                $cur = $cur->Adjust('+1day');
            }
            ?>
</tr><?php 
        }
        ?>
</table><?php 
        ?>
</div><?php 
        ?>
</div><?php 
    }
 function calendar($organisation, $DateRange = NULL, $Filter = NULL)
 {
     $this->load->model('subcontrollers/calendar_subcontroller');
     if (!CheckPermissions('public')) {
         return;
     }
     // Set up the filter
     $data = $this->organisations->_GetOrgData($organisation);
     if (!empty($data)) {
         $this->_SetupOrganisationFrame($organisation);
         $this->main_frame->SetPage('calendar', 'directory');
         $this->calendar_subcontroller->SetRangePageCode('directory_calendar');
         // restrict to this organisation
         $this->calendar_subcontroller->UseStreams(array((int) $data['organisation']['id'] => array('subscribed' => isset($data['organisation']['subscription']) && $data['organisation']['subscription']['calendar'], 'name' => $data['organisation']['name'], 'short_name' => $organisation)));
         // disable cancelled events
         $sources =& $this->calendar_subcontroller->GetSources();
         $sources->DisableGroup('inactive');
         $args = func_get_args();
         array_shift($args);
         $this->calendar_subcontroller->_map($args);
     } else {
         $this->load->library('custom_pages');
         $this->main_frame->SetContent(new CustomPageView('directory_notfound', 'error'));
         $this->main_frame->Load();
     }
     return;
     $this->pages_model->SetPageCode('directory_calendar');
     $data = $this->organisations->_GetOrgData($organisation);
     if (!empty($data)) {
         $this->_SetupOrganisationFrame($organisation);
         $this->load->library('my_calendar');
         $this->load->library('calendar_source_yorker');
         $this->my_calendar->SetUrlPrefix('/directory/' . $organisation . '/calendar/');
         //$this->My_calendar->SetAgenda(vip_url('calendar/agenda').'/');
         $yorker_source = new CalendarSourceYorker(0);
         // Only those events of the organisation
         $yorker_source->DisableGroup('subscribed');
         $yorker_source->DisableGroup('owned');
         $yorker_source->DisableGroup('private');
         $yorker_source->EnableGroup('active');
         $yorker_source->DisableGroup('inactive');
         $yorker_source->EnableGroup('hide');
         $yorker_source->EnableGroup('show');
         $yorker_source->EnableGroup('rsvp');
         $yorker_source->IncludeStream((int) $data['organisation']['id'], TRUE);
         $now = new Academic_time(time());
         $this->my_calendar->SetTabs(FALSE);
         $this->my_calendar->SetDefaultRange($now->AcademicYear() . '-' . $now->AcademicTermNameUnique());
         $this->my_calendar->SetPath('edit', site_url('calendar/event'));
         $calendar_view = $this->my_calendar->GetMyCalendar($yorker_source, $DateRange, $Filter);
         if (FALSE) {
             $this->load->model('calendar/events_model');
             $this->load->library('view_calendar_select_week');
             $this->load->library('view_calendar_list');
             $this->load->library('date_uri');
             $use_default_range = FALSE;
             if (empty($DateRange)) {
                 // $DateRange Empty
                 $use_default_range = TRUE;
             } else {
                 $uri_result = $this->date_uri->ReadUri($DateRange);
                 if ($uri_result['valid']) {
                     // valid
                     $start_time = $uri_result['start'];
                     $end_time = $uri_result['end'];
                     $format = $uri_result['format'];
                     $range_description = $uri_result['description'];
                 } else {
                     // invalid
                     $this->main_frame->AddMessage('error', 'Unrecognised date range: "' . $DateRange . '"');
                     $use_default_range = TRUE;
                 }
             }
             if ($use_default_range) {
                 // Default to this week
                 $start_time = Academic_time::NewToday();
                 $start_time = $start_time->BackToMonday();
                 $end_time = $start_time->Adjust('1week');
                 $format = 'ac';
                 //$range_description = 'from today for 1 week';
                 $range_description = 'this week';
             }
             // Use the start time, end time, and format to set up views
             //$weeks_start = $start_time->Adjust('-2week')->BackToMonday();
             $weeks_start = $this->academic_calendar->AcademicDayOfTerm($start_time->AcademicYear(), $start_time->AcademicTerm(), 1, 0, 0, 0);
             /*if ($weeks_start->Timestamp() < $monday->Timestamp()) {
             			$weeks_start = $monday;
             		}*/
             /*$weeks_end = $end_time->Adjust('5week')->BackToMonday();
             		if ($weeks_end->Timestamp() < $monday->Timestamp()) {
             			$weeks_end = $monday->Adjust('5week');
             		}*/
             // Set up the week select view
             $week_select = new ViewCalendarSelectWeek();
             $week_select->SetUriBase('directory/' . $organisation . '/calendar/');
             $week_select->SetUriFormat($format);
             //$week_select->SetRange($weeks_start, $weeks_end);
             $week_select->SetAcademicTerm($weeks_start->AcademicYear(), $weeks_start->AcademicTerm());
             $week_select->SetSelectedWeek($start_time, $end_time);
             $week_select->Retrieve();
             $occurrence_filter = new EventOccurrenceFilter();
             $occurrence_filter->EnableSource('all');
             $occurrence_filter->SetSpecialCondition('organisations.organisation_directory_entry_name = ' . $this->db->escape($organisation));
             // Set up the events list
             $events_list = new ViewCalendarList();
             $events_list->SetUriBase('directory/' . $organisation . '/calendar/');
             $events_list->SetUriFormat($format);
             $events_list->SetRange($start_time, $end_time);
             $events_list->SetOccurrenceFilter($occurrence_filter);
             $events_list->Retrieve();
             // Set up the directory events view to contain the week select and
             // events list
             $directory_events = new FramesFrame('directory/directory_view_events', $data);
             $directory_events->SetContent($week_select, 'week_select');
             $directory_events->SetContent($events_list, 'events_list');
             $directory_events->SetData('date_range_description', $range_description);
         }
         // Set up the directory frame to use the messages frame
         $this->main_frame->SetPage('calendar', 'directory');
         $this->frame_directory->SetOrganisation($data['organisation']);
         $this->frame_directory->SetContent($calendar_view);
         // Set up the public frame to use the directory frame
         $this->main_frame->SetTitleParameters(array('organisation' => $data['organisation']['name']));
         $this->main_frame->SetContent($this->frame_directory);
     } else {
         $this->load->library('custom_pages');
         $this->main_frame->SetContent(new CustomPageView('directory_notfound', 'error'));
     }
     // Load the public frame view
     $this->main_frame->Load();
 }
    ?>
<div class="crosswordShare"><?php 
    ?>
<a href="http://www.facebook.com/share.php?u=http://<?php 
    echo $ShareUrl;
    ?>
" target="_blank" class="fb_share_button" onclick="return fbs_click()">Share</a><?php 
    ?>
</div><?php 
}
// Rest of top box
?>
<h2><?php 
if ($Crossword['publication'] !== null) {
    $pub = new Academic_time($Crossword['publication']);
    echo $pub->Format('D ') . $pub->AcademicTermNameUnique() . ' week ' . $pub->AcademicWeek();
} else {
    ?>
unscheduled<?php 
}
?>
</h2><?php 
if (!empty($Links)) {
    ?>
<ul><?php 
    // Main links
    foreach ($Links as $label => $url) {
        ?>
<li><a href="<?php 
        echo xml_escape($url);
        ?>
    echo site_url('office/crosswords/crossword/' . $crossword['id'] . '/preview?cellsize=3');
    ?>
" /><?php 
    ?>
</a><?php 
    ?>
</div><?php 
    // Publishing date
    ?>
<div class="publish_date"><?php 
    if (null === $crossword['publication']) {
        ?>
<a>schedule</a><?php 
    } else {
        $pub = new Academic_time($crossword['publication']);
        echo $pub->Format('D') . ' ' . $pub->AcademicTermNameUnique() . ' ' . $pub->AcademicWeek() . $pub->Format(' (jS M Y)') . ' at ' . $pub->Format('H:i');
    }
    ?>
</div><?php 
    // Authors
    ?>
<div class="authors"><?php 
    echo xml_escape(join(', ', $crossword['author_fullnames']));
    ?>
</div><?php 
    // Progress bar
    ?>
<div class="completeness"><?php 
    ?>
<div class="bar" style="width: <?php 
    echo (int) $crossword['completeness'] . '%';