function src_event_edit()
 {
     if (!CheckPermissions($this->mPermission)) {
         return;
     }
     // Get data from uri resolution.
     $source_id = $this->mData['SourceId'];
     $event_id = $this->mData['EventId'];
     $occurrence_specified = array_key_exists('OccurrenceId', $this->mData);
     if ($occurrence_specified) {
         $occurrence_id = $this->mData['OccurrenceId'];
     } else {
         $occurrence_id = NULL;
     }
     // Validate the source
     if (!$this->_GetSource()) {
         return;
     }
     $this->load->library('calendar_view_edit_simple');
     $this->pages_model->SetPageCode('calendar_event_edit');
     // Get the redirect url tail
     $args = func_get_args();
     $tail = implode('/', $args);
     $get_action = '';
     if (isset($_GET['action'])) {
         $get_action = $_GET['action'];
     }
     // Fetch the specified event
     $calendar_data = new CalendarData();
     $this->mMainSource->FetchEvent($calendar_data, $source_id, $event_id);
     $events = $calendar_data->GetEvents();
     if (array_key_exists(0, $events)) {
         $event = $events[0];
         // Find the occurrence
         $found_occurrence = NULL;
         if (NULL !== $occurrence_id) {
             foreach ($event->Occurrences as $key => $occurrence) {
                 if ($occurrence->SourceOccurrenceId == $occurrence_id) {
                     $found_occurrence =& $event->Occurrences[$key];
                     break;
                 }
             }
             if (NULL === $found_occurrence) {
                 $this->messages->AddMessage('warning', 'The event occurrence with id ' . $occurrence_id . ' does not belong to the event with id ' . $event_id . '.');
                 redirect($this->mPaths->EventInfo($event) . '/' . $tail);
                 return;
             }
         }
         if (NULL === $occurrence_id) {
             // default to the only occurrence if there is only one.
             if (count($event->Occurrences) == 1) {
                 $found_occurrence =& $event->Occurrences[0];
                 $occurrence_id = $found_occurrence->SourceOccurrenceId;
             }
         }
         // Get the buttons from post data
         $prefix = 'eved';
         $return_button = isset($_POST[$prefix . '_return']);
         if (false !== $event && $event->ReadOnly) {
             $return_button = TRUE;
             $this->messages->AddMessage('error', 'You do not have permission to make changes to this event.');
         }
         if ($return_button) {
             // REDIRECT
             if ($occurrence_specified) {
                 $path = $this->mPaths->OccurrenceInfo($found_occurrence);
             } else {
                 $path = $this->mPaths->EventInfo($event);
             }
             return redirect($path . '/' . $tail);
         }
         $input_summary = $this->input->post($prefix . '_summary');
         $errors = array();
         $process_input = false;
         // Read the recurrence data
         if (isset($_POST[$prefix . '_recur_simple']) and isset($_POST[$prefix . '_start']) and isset($_POST[$prefix . '_duration']) and isset($_POST[$prefix . '_inex'])) {
             $rset_arr = $_POST[$prefix . '_recur_simple'];
             $rset = Calendar_view_edit_simple::validate_recurrence_set_data(!(isset($_POST[$prefix . '_allday']) && $_POST[$prefix . '_allday']), $_POST[$prefix . '_start'], $_POST[$prefix . '_duration'], $_POST[$prefix . '_recur_simple'], $_POST[$prefix . '_inex'], $errors);
         }
         // Fill it in if none supplied
         /// @todo Fix that the new rset doesn't have the same rrule ids, so theres a deletion/insertion instead of update.
         if (!isset($rset_arr)) {
             $rset = $event->GetRecurrenceSet();
             if ((isset($_POST['evview_delete']) || 'delete' == $get_action) && NULL !== $found_occurrence) {
                 $inex_date = array($found_occurrence->StartTime->Format('Ymd') => array(NULL => NULL));
                 $rset->RemoveRDates($inex_date);
                 $rset->AddExDates($inex_date);
                 $process_input = true;
                 $_POST[$prefix . '_save'] = true;
                 $input_valid = true;
             } elseif (isset($_POST['evview_delete_all']) || 'delete_all' == $get_action) {
                 $rset->ClearRecurrence();
                 $process_input = true;
                 $_POST[$prefix . '_save'] = true;
                 $input_valid = true;
             } elseif ((isset($_POST['evview_restore']) || 'restore' == $get_action) && NULL !== $found_occurrence) {
                 $inex_date = array($found_occurrence->StartTime->Format('Ymd') => array(NULL => NULL));
                 $rset->RemoveExDates($inex_date);
                 $rset->AddRDates($inex_date);
                 $process_input = true;
                 $_POST[$prefix . '_save'] = true;
                 $input_valid = true;
             }
             $rset_arr = Calendar_view_edit_simple::transform_recur_for_view($rset, $errors);
         }
         // Always fill in the inex info again, ignoring input from form.
         $inex_arr = Calendar_view_edit_simple::transform_inex_for_view($rset, $errors);
         list($start, $end) = $rset->GetStartEnd();
         $categories = $this->mSource->GetAllCategories();
         $input = array('name' => $event->Name, 'description' => $event->Description, 'location_name' => $event->LocationDescription, 'category' => 0, 'time_associated' => $event->TimeAssociated);
         if (isset($categories[$event->Category])) {
             $input['category'] = $categories[$event->Category]['id'];
         }
         $input_summary = $this->input->post($prefix . '_summary');
         $confirm_list = NULL;
         if (false !== $input_summary) {
             $input_valid = true;
             $process_input = true;
             // Get more post data
             $input['name'] = $input_summary;
             if (strlen($input['name']) <= 3 or strlen($input['name']) >= 256) {
                 $input_valid = false;
                 $this->messages->AddMessage('error', 'Event summary is too long or too short.');
             }
             $input_description = $this->input->post($prefix . '_description');
             if (false !== $input_description) {
                 $input['description'] = $input_description;
                 if (strlen($input['name']) > 65535) {
                     $input_valid = false;
                     $this->messages->AddMessage('error', 'Event description is too long.');
                 }
             }
             $input_category = $this->input->post($prefix . '_category');
             if (false !== $input_category) {
                 $input['category'] = $input_category;
             }
             $input_location = $this->input->post($prefix . '_location');
             if (false !== $input_location) {
                 $input['location_name'] = $input_location;
                 if (strlen($input['location_name']) > 50) {
                     $input_valid = false;
                     $this->messages->AddMessage('error', 'Event location is too long.');
                 }
             }
             $input['time_associated'] = $this->input->post($prefix . '_allday') === false;
         }
         if ($process_input) {
             // at this point $start and $end are still plain timestamps
             $input['recur'] = $rset;
             if ($input_valid && empty($errors)) {
                 if (isset($_POST[$prefix . '_save'])) {
                     $confirm_list = $this->mMainSource->GetEventRecurChanges($event, $rset);
                     if (isset($confirm_list['draft']) && !$this->mMainSource->IsSupported('publish')) {
                         unset($confirm_list['draft']);
                     }
                     if (empty($confirm_list)) {
                         $_POST[$prefix . '_confirm']['confirm_btn'] = 'Confirm';
                     }
                 }
                 if (isset($_POST[$prefix . '_confirm']['confirm_btn'])) {
                     if (NULL === $confirm_list) {
                         $confirm_list = $this->mMainSource->GetEventRecurChanges($event, $rset);
                     }
                     // Make the change
                     $messages = $this->mMainSource->AmmendEvent($event, $input);
                     $this->messages->AddMessages($messages);
                     if (!array_key_exists('error', $messages) || empty($messages['error'])) {
                         // Success
                         $this->messages->AddMessage('success', 'Event updated');
                         // Publish the specified occurrences.
                         $publish_occurrences = array();
                         foreach (array('create', 'draft') as $namespace) {
                             if (isset($_POST[$prefix . '_confirm'][$namespace . '_publish'])) {
                                 foreach ($_POST[$prefix . '_confirm'][$namespace . '_publish'] as $day => $dummy) {
                                     if (isset($confirm_list[$namespace][$day])) {
                                         $publish_occurrences[] = $confirm_list[$namespace][$day]['start_time'];
                                     }
                                 }
                             }
                         }
                         if (!empty($publish_occurrences)) {
                             $published = $this->mSource->PublishOccurrences($event, $publish_occurrences);
                             $desired = count($publish_occurrences);
                             if ($published < $desired) {
                                 $message_type = 'warning';
                             } else {
                                 $message_type = 'success';
                             }
                             $this->messages->Addmessage($message_type, "{$published} out of {$desired} occurrences were published.");
                         }
                         // changes have been made including possible deletion.
                         // if the redirection results in event not found, we
                         // want it to redirect straight back to the tail.
                         $_SESSION['calendar']['modded'][$event_id] = 1;
                         // REDIRECT
                         if ($occurrence_specified) {
                             $path = $this->mPaths->OccurrenceInfo($found_occurrence);
                         } else {
                             $path = $this->mPaths->EventInfo($event);
                         }
                         return redirect($path . '/' . $tail);
                     }
                 }
             }
         }
         // Ready output data
         $start = new Academic_time($start);
         $end = new Academic_time($end);
         $eventinfo = array('summary' => $input['name'], 'description' => $input['description'], 'location' => $input['location_name'], 'category' => $input['category'], 'allday' => !$input['time_associated'], 'start' => array('monthday' => $start->DayOfMonth(), 'month' => $start->Month(), 'year' => $start->Year(), 'time' => $start->Hour() . ':' . $start->Minute(), 'yearday' => $start->DayOfYear(), 'day' => $start->DayOfWeek(), 'monthweek' => (int) (($start->DayOfMonth() + 6) / 7)), 'duration' => Calendar_view_edit_simple::calculate_duration($start, $end));
         if ($this->events_model->IsVip()) {
             $help_xhtml = $this->pages_model->GetPropertyWikitext('help_vip');
         } else {
             $help_xhtml = $this->pages_model->GetPropertyWikitext('help_personal');
         }
         $data = array('SimpleRecur' => $rset_arr, 'InExDates' => $inex_arr, 'FailRedirect' => '/' . $tail, 'Path' => $this->mPaths, 'EventCategories' => $categories, 'FormPrefix' => $prefix, 'EventInfo' => $eventinfo, 'Help' => $help_xhtml, 'CanPublish' => $this->mMainSource->IsSupported('publish'), 'Create' => false, 'SuccessRedirect' => false);
         if (is_array($confirm_list)) {
             $data['Confirms'] = $confirm_list;
             if (isset($_POST[$prefix . '_confirm'])) {
                 $data['Confirm'] = $_POST[$prefix . '_confirm'];
             } else {
                 $data['Confirm'] = NULL;
             }
         }
         foreach ($errors as $error) {
             $this->messages->AddMessage('error', $error['text']);
         }
         $this->SetupTabs('', $start);
         $this->main_frame->SetTitleParameters(array('source' => $this->mSource->GetSourceName(), 'event' => $event->Name));
         $this->main_frame->IncludeCss('stylesheets/calendar.css');
         $this->main_frame->IncludeJs('javascript/simple_ajax.js');
         $this->main_frame->IncludeJs('javascript/css_classes.js');
         $this->main_frame->IncludeJs('javascript/calendar_edit.js');
         $this->main_frame->SetContent(new FramesView('calendar/event_edit', $data));
     } else {
         // Event not in list
         $this->ErrorNotAccessible($tail);
     }
     $this->main_frame->Load();
 }
 function SetData($data)
 {
     if (isset($data['prefix'])) {
         $prefix = $data['prefix'];
         if (isset($data[$prefix . '_recur_simple']) and isset($data[$prefix . '_start']) and isset($data[$prefix . '_duration'])) {
             $this->mRset = Calendar_view_edit_simple::validate_recurrence_set_data(!(isset($data[$prefix . '_allday']) && $data[$prefix . '_allday']), $data[$prefix . '_start'], $data[$prefix . '_duration'], $data[$prefix . '_recur_simple'], $data[$prefix . '_inex'], $this->mErrors);
             $this->mResults = $this->mRset->Resolve(strtotime('-1year'), strtotime('1year'));
         } else {
             $this->mErrors[] = array('field' => '', 'text' => 'No simple recurrence data.');
         }
     } else {
         $this->mErrors[] = array('field' => '', 'text' => 'No GET data.');
     }
 }