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();
 }
 /**
  * @param $StartTime Academic_time Start time.
  * @param $EndTime Academic_time End time.
  * @pre @a $StartTime < @a $EndTime
  * @return bool Success state:
  *	- TRUE (Success).
  *	- FALSE (Failure).
  */
 function Retrieve($StartTime, $EndTime)
 {
     // Validate input
     if ($StartTime >= $EndTime) {
         return FALSE;
     }
     $success = TRUE;
     // Day information
     if ($this->IsEnabled('day-info')) {
         $this->mDayInformation = array();
         // Initialise the day info array
         $current_time = $StartTime->Midnight();
         $current_index = 0;
         while ($current_time->Timestamp() < $EndTime->Timestamp()) {
             $this->mDayInformation[$current_time->Timestamp()] = array('index' => $current_index, 'date' => $current_time, 'special_headings' => array());
             // Onto the next day
             $current_time = $current_time->Adjust('1day');
             ++$current_index;
         }
     }
     // Event occurrences
     if ($this->IsEnabled('occurrences-all')) {
         $occurrence_query = new EventOccurrenceQuery();
         $filter = $this->mOccurrenceFilter;
         if (NULL === $filter) {
             $filter = new EventOccurrenceFilter();
         }
         $filter->SetRange($StartTime->Timestamp(), $EndTime->Timestamp());
         $fields = array('ref_id' => 'event_occurrences.event_occurrence_id', 'name' => 'events.event_name', 'start' => 'UNIX_TIMESTAMP(event_occurrences.event_occurrence_start_time)', 'end' => 'UNIX_TIMESTAMP(event_occurrences.event_occurrence_end_time)', 'system_update_ts' => 'UNIX_TIMESTAMP(events.event_timestamp)', 'blurb' => 'events.event_blurb', 'shortloc' => $occurrence_query->ExpressionLocationName(), 'type' => 'event_types.event_type_name', 'state' => 'event_occurrences.event_occurrence_state', 'organisation' => 'organisations.organisation_name', 'organisation_directory' => 'organisations.organisation_directory_entry_name');
         $this->mOccurrences = $filter->GenerateOccurrences($fields);
     }
     return $success;
 }
 function events($EventId = FALSE, $OccurrenceId = FALSE)
 {
     show_404();
     if (FALSE !== $EventId && !is_numeric($EventId)) {
         show_404();
     }
     if (FALSE !== $OccurrenceId && !is_numeric($OccurrenceId)) {
         show_404();
     }
     if (!CheckPermissions('vip+pr')) {
         return;
     }
     $this->load->model('calendar/events_model');
     $this->load->model('calendar/recurrence_model');
     $this->load->helper('text');
     if (FALSE === $EventId) {
         // Quick thingy to add atandard english calendar rules to current
         // organisation. Note they aren't created with occurrences.
         if (FALSE) {
             $rules = $this->RuleCollectionStdEngland();
             foreach ($rules as $info) {
                 $name = $info[0];
                 $rule = $info[1];
                 /// @todo Update for new recurrence system
                 $rule_id = $this->recurrence_model->AddRule($rule);
                 if (FALSE === $rule_id) {
                     $this->messages->AddMessage('warning', 'RRule named ' . $name . ' could not be added');
                 } else {
                     $new_event = array('name' => $name, 'recurrence_rule_id' => $rule_id);
                     try {
                         $result = $this->events_model->EventCreate($new_event);
                         $this->messages->AddMessage('success', 'added ' . $name);
                     } catch (Exception $e) {
                         $this->messages->AddMessage('error', 'while creating event ' . $name . ': ' . $e->getMessage());
                     }
                 }
             }
         }
         $fields = array('event_id' => 'events.event_id', 'name' => 'events.event_name', 'description' => 'events.event_description');
         $results = $this->events_model->EventsGet($fields, FALSE, TRUE);
         $events = array();
         if (FALSE !== $results) {
             foreach ($results as $result) {
                 if (!array_key_exists($result['event_id'], $events)) {
                     $events[$result['event_id']] = $result;
                 }
             }
         }
         $op = '<OL>';
         foreach ($events as $event) {
             if (array_key_exists('event_recurrence_rule', $event) && NULL != $event['event_recurrence_rule']) {
                 $event['description'] = $event['event_recurrence_rule']->ToString() . ' - ' . $event['description'];
             }
             $op .= '<LI><A HREF="' . vip_url('calendar/events/' . $event['event_id']) . '">' . $event['name'] . '</A> - ' . $event['description'] . '</LI>';
         }
         $op .= '</OL>';
         $this->main_frame->SetTitle('Events');
         $this->main_frame->SetContent(new SimpleView($op));
     } else {
         $special_condition = 'events.event_id=' . $EventId;
         if (FALSE === $OccurrenceId) {
             $filter = new EventOccurrenceFilter();
             $filter->DisableSource('subscribed');
             $filter->SetSpecialCondition($special_condition);
             $fields = array('occurrence_id' => 'event_occurrences.event_occurrence_id', 'start' => 'event_occurrences.event_occurrence_start_time', 'end' => 'event_occurrences.event_occurrence_end_time', 'status' => $filter->ExpressionPublicState());
             $results = $filter->GenerateOccurrences($fields);
             $occurrences = array();
             foreach ($results as $result) {
                 if (!array_key_exists($result['occurrence_id'], $occurrences)) {
                     $occurrences[$result['occurrence_id']] = $result;
                 }
             }
             $op = '<H4>Existing occurrences</H4>';
             $op .= '<OL>';
             foreach ($occurrences as $occurrence) {
                 $operations = array();
                 if ($occurrence['status'] === 'draft') {
                     $operations[] = 'publish';
                     $operations[] = 'trash';
                 }
                 if ($occurrence['status'] === 'movedraft') {
                     $operations[] = 'publishmove';
                     $operations[] = 'restoremove';
                     $operations[] = 'cancelmove';
                 }
                 if ($occurrence['status'] === 'trashed') {
                     $operations[] = 'untrash';
                 }
                 if ($occurrence['status'] === 'published') {
                     $operations[] = 'cancel';
                     $operations[] = 'postpone';
                 }
                 if ($occurrence['status'] === 'cancelled') {
                     $operations[] = 'uncancel';
                     $operations[] = 'postpone';
                 }
                 $links = array();
                 foreach ($operations as $operation) {
                     $links[] = '<A HREF="' . vip_url('calendar/occop/' . $operation . '/' . $EventId . '/' . $occurrence['occurrence_id']) . '">' . $operation . '</A>';
                 }
                 $op .= '<LI>' . $occurrence['status'] . ' <A HREF="' . vip_url('calendar/events/' . $EventId . '/' . $occurrence['occurrence_id']) . '">' . $occurrence['start'] . ' -> ' . $occurrence['end'] . '</A> ' . ' (' . implode(', ', $links) . ') </LI>';
             }
             $op .= '</OL>';
             // Get event information
             $events = $this->events_model->EventsGet(array('events.*'), $EventId, TRUE);
             // If theres a recurrence rule, use it to generate the next two
             // years occurrences
             if (isset($events[0]['event_recurrence_rule'])) {
                 $op .= '<H4>Generated occurrences in next 5 years</H4>';
                 $op .= '<P><em>' . $events[0]['event_recurrence_rule']->ToString() . '</em></P>';
                 $occurrences_calculated = array_keys($events[0]['event_recurrence_rule']->FindTimes(time(), strtotime('+5years')));
                 foreach ($occurrences_calculated as $key => $timestamp) {
                     $occurrences_calculated[$key] = date(DATE_RFC822, $timestamp);
                 }
                 $op .= '<pre>' . ascii_to_entities(var_export($occurrences_calculated, true)) . '</pre>';
             }
             $op .= '<H4>Event information</H4>';
             $op .= '<pre>' . ascii_to_entities(var_export($events, true)) . '</pre>';
             $op .= '<H4>RSVP list</H4>';
             $rsvps = $this->events_model->GetEventRsvp($EventId);
             $op .= '<pre>' . ascii_to_entities(var_export($rsvps, true)) . '</pre>';
             $this->main_frame->SetTitle('Events');
             $this->main_frame->SetContent(new SimpleView($op));
         } else {
             $special_condition .= ' AND event_occurrences.event_occurrence_id=' . $OccurrenceId;
             $filter = new EventOccurrenceFilter();
             $filter->DisableSource('subscribed');
             $filter->SetRange(strtotime('-1year'), strtotime('+1year'));
             $filter->SetSpecialCondition($special_condition);
             $result = $filter->GenerateOccurrences(array('occurrence_id' => 'event_occurrences.event_occurrence_id', 'start' => 'event_occurrences.event_occurrence_start_time', 'end' => 'event_occurrences.event_occurrence_end_time', 'active_id' => 'event_occurrences.event_occurrence_active_occurrence_id', 'rescheduled_start' => 'active_occurrence.event_occurrence_start_time', 'rescheduled_end' => 'active_occurrence.event_occurrence_end_time', 'status' => $filter->ExpressionPublicState(), 'cancelled' => $filter->ExpressionPublicCancelled(), 'postponed' => $filter->ExpressionPublicPostponed(), 'rescheduled' => $filter->ExpressionPublicRescheduled(), 'ts' => 'event_occurrences.event_occurrence_last_modified'));
             $rsvps = $this->events_model->GetOccurrenceRsvp($OccurrenceId);
             $op = '<pre>' . ascii_to_entities(var_export($result, true)) . '</pre><pre>' . ascii_to_entities(var_export($rsvps, true)) . '</pre>';
             $this->main_frame->SetContent(new SimpleView($op));
         }
     }
     // Load the main frame
     $this->main_frame->Load();
 }