/**
  * Date widget
  *
  * @return	string
  */
 public function date_widget($data)
 {
     $view_data = array();
     $multiple_day_occurrences = array();
     $output = '';
     $event_edit_has_occurrences = FALSE;
     if (!empty($data) and $data['edit_occurrence'] === TRUE or ee()->input->get('event_id') and ee()->input->get('ymd')) {
         $occurrence_data = $this->data->fetch_occurrence_data_by_entry_id(ee()->input->get('entry_id'), ee()->input->get('start_date'), ee()->input->get('end_date'), ee()->input->get('start_time'), ee()->input->get('end_time'));
         foreach ($occurrence_data as $k => $v) {
             $view_data[$k] = $v;
         }
         $vars = array('entry_id', 'event_id', 'new_occurrence', 'occurrence_id', 'calendar_id', 'site_id', 'start_date', 'end_date', 'start_time', 'end_time', 'ymd', 'all_day');
         foreach ($vars as $var) {
             if (!isset($view_data[$var])) {
                 $view_data[$var] = ee()->input->get($var);
             }
         }
         if ($data['edit_occurrence'] === FALSE and ee()->input->get('event_id')) {
             $view_data['event_id'] = $view_data['entry_id'];
         }
         if ($view_data['start_time'] == '0000' and $view_data['end_time'] == '2400') {
             $view_data['time_range'] = lang('all_day');
         } else {
             if (!class_exists('Calendar_datetime')) {
                 require_once CALENDAR_PATH . 'calendar.datetime' . EXT;
             }
             $CDT = new Calendar_datetime();
             $CDT->change_time(substr($view_data['start_time'], 0, strlen($view_data['start_time']) - 2), substr($view_data['start_time'], -2));
             $start = $this->data->preference('clock_type') == '12' ? $CDT->format_date_string('h:i a') : $CDT->format_date_string('H:i');
             $CDT->change_time(substr($view_data['end_time'], 0, strlen($view_data['end_time']) - 2), substr($view_data['end_time'], -2));
             $end = $this->data->preference('clock_type') == '12' ? $CDT->format_date_string('h:i a') : $CDT->format_date_string('H:i');
             $view_data['time_range'] = $start . '–' . $end;
         }
         $output .= self::edit_occurrence($view_data);
     } else {
         //this test will show a checkbox on an events page if it has edited occurances
         //the checkbox is an option to delete those occurances if the dates are being changed
         if (isset($data['entry_id'])) {
             $query = ee()->db->query("SELECT entry_id, event_id\n\t\t\t\t\t FROM\texp_calendar_events_occurrences\n\t\t\t\t\t WHERE\tevent_id ='" . ee()->db->escape_str($data['entry_id']) . "'");
             //its only true of the occurance is different than its parent
             if ($query->num_rows() > 0) {
                 foreach ($query->result_array() as $row) {
                     if ($row['entry_id'] != $row['event_id']) {
                         $event_edit_has_occurrences = TRUE;
                     }
                 }
             }
         }
         if (isset($data['calendar_id'])) {
             $view_data['calendar_id'] = $data['calendar_id'];
         } else {
             $entry_id = $this->data->get_event_entry_id_by_channel_entry_id(ee()->input->get('entry_id'));
             $view_data['calendar_id'] = $this->data->get_calendar_id_by_event_entry_id($entry_id);
             $view_data['calendar_id'] = isset($view_data['calendar_id'][$entry_id]) ? $view_data['calendar_id'][$entry_id] : '';
         }
         $output .= self::calendar_select($view_data);
         if ($this->data->calendars_exist() === TRUE) {
             $count = 0;
             //--------------------------------------------
             //	Does this have main rules?
             //--------------------------------------------
             if (!empty($data['rules'])) {
                 foreach ($data['rules'] as $id => $rule) {
                     $count++;
                     if ($count == 1 and $data['start_date'] != $rule['start_date']) {
                         $view_data['rule'] = array('rule_id' => '', 'rule_type' => '+', 'repeat_years' => '', 'repeat_months' => '', 'repeat_days' => '', 'repeat_weeks' => '', 'days_of_week' => '', 'relative_dow' => '', 'days_of_month' => '', 'months_of_year' => '', 'repeat_dates' => '', 'end_by' => '', 'end_after' => '', 'start_date' => $data['start_date'], 'end_date' => $data['end_date'], 'start_time' => $data['start_time'], 'end_time' => $data['end_time'], 'all_day' => $data['all_day'], 'recurs' => $data['recurs']);
                         $view_data['first'] = TRUE;
                         $view_data['rule_id'] = 0;
                         $view_data['rule_number'] = $count;
                         $output .= self::rule($view_data);
                         $count++;
                         if (!isset($rule['recurs'])) {
                             $rule['recurs'] = '';
                         }
                         $view_data = array('start_date' => $rule['start_date'], 'all_day' => $rule['all_day'], 'start_time' => isset($rule['start_time']) ? $rule['start_time'] : '', 'end_date' => $rule['end_date'], 'end_time' => isset($rule['end_time']) ? $rule['end_time'] : '', 'recurs' => $rule['recurs'], 'first' => TRUE, 'rule_id' => 0, 'rule_number' => $count, 'rule' => $rule);
                         $output .= self::rule($view_data);
                     } else {
                         $view_data = array('first' => $count == 1 ? TRUE : FALSE, 'rule_id' => $id, 'rule_number' => $count, 'start_date' => isset($rule['start_date']) ? $rule['start_date'] : '', 'all_day' => isset($rule['all_day']) ? $rule['all_day'] : '', 'start_time' => isset($rule['start_time']) ? $rule['start_time'] : '', 'end_date' => isset($rule['end_date']) ? $rule['end_date'] : '', 'end_time' => isset($rule['end_time']) ? $rule['end_time'] : '', 'recurs' => isset($data['recurs']) ? $data['recurs'] : '');
                         if (!isset($rule['recurs'])) {
                             $rule['recurs'] = '';
                         }
                         $rule['all_day'] = $rule['all_day'];
                         $view_data['rule'] = $rule;
                         $output .= self::rule($view_data);
                     }
                 }
             } elseif ((!isset($data['start_date']) or $data['start_date'] == $data['end_date']) and !empty($data['occurrences']) and isset($data['occurrences'][$data['start_time']][$data['end_time']])) {
                 $count++;
                 $view_data['occurrences']['dates'] = array();
                 $view_data['occurrences']['dates'][] = $data['start_date'];
                 $view_data['start_date'] = $data['start_date'];
                 $view_data['start_time'] = $data['start_time'];
                 $view_data['end_date'] = $data['end_date'];
                 $view_data['end_time'] = $data['end_time'];
                 $view_data['all_day'] = $data['all_day'];
                 $dates_added = FALSE;
                 foreach ($data['occurrences'][$data['start_time']][$data['end_time']] as $item) {
                     //if this is a single day item, lets put them all together
                     if ($item['start_date'] == $item['end_date']) {
                         $dates_added = TRUE;
                         $view_data['occurrences']['dates'][] = $item['start_date'];
                     } else {
                         $multiple_day_occurrences[] = $item;
                     }
                 }
                 $view_data['first'] = TRUE;
                 $view_data['rule_number'] = $count;
                 //we need to make sure any of the items actually got added
                 if ($dates_added) {
                     $output .= self::occurrence($view_data);
                 }
                 //need to remove this so nothing else hits it
                 unset($data['occurrences'][$data['start_time']][$data['end_time']]);
             } else {
                 $count++;
                 $rule = array('start_date' => isset($data['start_date']) ? $data['start_date'] : '', 'all_day' => isset($data['all_day']) ? $data['all_day'] : '', 'start_time' => isset($data['start_time']) ? $data['start_time'] : '', 'end_date' => isset($data['end_date']) ? $data['end_date'] : '', 'end_time' => isset($data['end_time']) ? $data['end_time'] : '', 'recurs' => '', 'rule' => array('rule_id' => '', 'rule_type' => '', 'repeat_years' => '', 'repeat_months' => '', 'repeat_days' => '', 'repeat_weeks' => '', 'days_of_week' => '', 'relative_dow' => '', 'days_of_month' => '', 'months_of_year' => '', 'repeat_dates' => '', 'end_by' => '', 'end_after' => '', 'start_date' => '', 'end_date' => '', 'start_time' => '', 'end_time' => '', 'all_day' => '', 'recurs' => ''));
                 foreach ($rule as $k => $v) {
                     $view_data[$k] = $v;
                 }
                 $view_data['first'] = TRUE;
                 $view_data['rule_id'] = 0;
                 $view_data['rule_number'] = $count;
                 $output .= self::rule($view_data);
             }
             //--------------------------------------------
             //	Do we still have some possible occurrences left?
             //--------------------------------------------
             if (!empty($data['occurrences'])) {
                 foreach ($data['occurrences'] as $start_time => $a) {
                     foreach ($a as $end_time => $items) {
                         $view_data = array();
                         if (count($items) == 1) {
                             $count++;
                             $item = $items[0];
                             $rule = array('start_date' => isset($item['start_date']) ? $item['start_date'] : '', 'all_day' => isset($item['all_day']) ? $item['all_day'] : '', 'start_time' => isset($item['start_time']) ? $item['start_time'] : '', 'end_date' => isset($item['end_date']) ? $item['end_date'] : '', 'end_time' => isset($item['end_time']) ? $item['end_time'] : '', 'recurs' => isset($item['recurs']) ? $item['recurs'] : '', 'rule' => array('rule_id' => '', 'rule_type' => '', 'repeat_years' => '', 'repeat_months' => '', 'repeat_days' => '', 'repeat_weeks' => '', 'days_of_week' => '', 'relative_dow' => '', 'days_of_month' => '', 'months_of_year' => '', 'repeat_dates' => '', 'end_by' => '', 'end_after' => '', 'start_date' => '', 'end_date' => '', 'start_time' => '', 'end_time' => '', 'all_day' => '', 'recurs' => ''));
                             foreach ($rule as $k => $v) {
                                 $view_data[$k] = $v;
                             }
                             $view_data['first'] = FALSE;
                             $view_data['rule_id'] = 0;
                             $view_data['rule_number'] = $count;
                             $output .= self::rule($view_data);
                         } else {
                             $view_data['occurrences']['dates'] = array();
                             $view_data['start_time'] = $start_time;
                             $view_data['end_time'] = $end_time;
                             $dates_added = FALSE;
                             foreach ($items as $k => $item) {
                                 //if this is a single day item, lets put them all together
                                 if ($item['start_date'] == $item['end_date']) {
                                     $dates_added = TRUE;
                                     $view_data['occurrences']['dates'][] = $item['start_date'];
                                     $view_data['all_day'] = $item['all_day'];
                                 } else {
                                     $multiple_day_occurrences[] = $item;
                                 }
                             }
                             //we need to make sure any of the items actually got added
                             if ($dates_added) {
                                 $count++;
                                 $view_data['first'] = FALSE;
                                 $view_data['rule_number'] = $count;
                                 $output .= self::occurrence($view_data);
                             }
                         }
                     }
                 }
             }
             //--------------------------------------------
             //	do we have multi day occurrences?
             //--------------------------------------------
             if (!empty($multiple_day_occurrences)) {
                 foreach ($multiple_day_occurrences as $item) {
                     $view_data = array();
                     $count++;
                     $rule = array('start_date' => isset($item['start_date']) ? $item['start_date'] : '', 'all_day' => isset($item['all_day']) ? $item['all_day'] : '', 'start_time' => isset($item['start_time']) ? $item['start_time'] : '', 'end_date' => isset($item['end_date']) ? $item['end_date'] : '', 'end_time' => isset($item['end_time']) ? $item['end_time'] : '', 'recurs' => isset($item['recurs']) ? $item['recurs'] : '', 'rule' => array('rule_id' => '', 'rule_type' => '', 'repeat_years' => '', 'repeat_months' => '', 'repeat_days' => '', 'repeat_weeks' => '', 'days_of_week' => '', 'relative_dow' => '', 'days_of_month' => '', 'months_of_year' => '', 'repeat_dates' => '', 'end_by' => '', 'end_after' => '', 'start_date' => '', 'end_date' => '', 'start_time' => '', 'end_time' => '', 'all_day' => '', 'recurs' => ''));
                     foreach ($rule as $k => $v) {
                         $view_data[$k] = $v;
                     }
                     $view_data['first'] = FALSE;
                     $view_data['rule_id'] = 0;
                     $view_data['rule_number'] = $count;
                     $output .= self::rule($view_data);
                 }
             }
             if (!empty($data['exceptions'])) {
                 $count++;
                 $view_data = array();
                 foreach ($data['exceptions'] as $k => $item) {
                     $view_data['exceptions']['dates'][] = $item['start_date'];
                 }
                 $view_data['first'] = FALSE;
                 $view_data['rule_number'] = $count;
                 $output .= self::cal_exception($view_data);
             }
         }
     }
     $view_data['start_time'] = isset($view_data['start_time']) ? str_pad($view_data['start_time'], 4, '0', STR_PAD_LEFT) : '0000';
     $view_data['end_time'] = isset($view_data['end_time']) ? str_pad($view_data['end_time'], 4, '0', STR_PAD_LEFT) : '0000';
     return $this->view('calendar_fields.html', array('event_edit_has_occurrences' => $event_edit_has_occurrences, 'output' => $output, 'lang_remove_edited_occurrences' => lang('remove_edited_occurrences'), 'lang_remove_edited_occurrences_desc' => lang('remove_edited_occurrences_desc')), TRUE);
 }