Exemplo n.º 1
0
 public function import_ics_data($calendar_id)
 {
     // -------------------------------------
     //	Get some basic info to use later
     // -------------------------------------
     $cbasics = $this->data->calendar_basics();
     $cbasics = $cbasics[$calendar_id];
     $urls = $cbasics['ics_url'];
     if ($urls == '') {
         return FALSE;
     }
     $tz_offset = $cbasics['tz_offset'] != '' ? $cbasics['tz_offset'] : '0000';
     /*
     
     		This shouldn't be happening because DST is only something that
     		would need to be applied when generating the users current local time.
     		If an event were at 7pm EST or EDT, it would still be at 7pm either way.
     		I hate DST.
     
     		if ($tz_offset != '0000' AND ee()->config->item('daylight_savings') == 'y')
     		{
     			$tz_offset += 100;
     		}
     */
     $channel_id = $this->data->channel_is_events_channel();
     $author_id = $cbasics['author_id'];
     // -------------------------------------
     //	Prepare the URLs
     // -------------------------------------
     if (!is_array($urls)) {
         $urls = explode("\n", $urls);
     }
     foreach ($urls as $k => $url) {
         $urls[$k] = trim($url);
     }
     // -------------------------------------
     //	Load iCalCreator
     // -------------------------------------
     if (!class_exists('vcalendar')) {
         require_once CALENDAR_PATH_ASSETS . 'icalcreator/iCalcreator.class.php';
     }
     // -------------------------------------
     //	Load Calendar_datetime
     // -------------------------------------
     if (!class_exists('Calendar_datetime')) {
         require_once CALENDAR_PATH . 'calendar.datetime' . EXT;
     }
     $CDT = new Calendar_datetime();
     $CDT_end = new Calendar_datetime();
     // -------------------------------------
     //	Load Publish
     // -------------------------------------
     if (APP_VER < 2.0) {
         //need to set DSP if not present
         if (!isset($GLOBALS['DSP']) or !is_object($GLOBALS['DSP'])) {
             if (!class_exists('Display')) {
                 require_once PATH_CP . 'cp.display' . EXT;
             }
             $GLOBALS['DSP'] = new Display();
         }
         if (!class_exists('Publish')) {
             require_once PATH_CP . 'cp.publish' . EXT;
         }
         $PB = new Publish();
         $PB->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE;
     } else {
         ee()->load->library('api');
         ee()->api->instantiate(array('channel_entries', 'channel_categories', 'channel_fields'));
         ee()->api_channel_entries->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE;
     }
     // -------------------------------------
     //	Tell our extensions that we're running the icalendar import
     // -------------------------------------
     $this->cache['ical'] = TRUE;
     // -------------------------------------
     //	Get already-imported events
     // -------------------------------------
     $imported = $this->data->get_imported_events($calendar_id);
     // -------------------------------------
     //	Don't let EXT drop us early
     // -------------------------------------
     ee()->extensions->in_progress = '';
     // -------------------------------------
     //	Cycle through the URLs
     // -------------------------------------
     foreach ($urls as $url) {
         $ICAL = new vcalendar();
         $ICAL->parse($this->fetch_url($url));
         // -------------------------------------
         //	Iterate among the events
         // -------------------------------------
         while ($event = $ICAL->getComponent('vevent')) {
             // -------------------------------------
             //	Times
             // -------------------------------------
             $hour = isset($event->dtstart['value']['hour']) ? $event->dtstart['value']['hour'] : 00;
             $minute = isset($event->dtstart['value']['min']) ? $event->dtstart['value']['min'] : 00;
             $end_hour = isset($event->dtend['value']['hour']) ? $event->dtend['value']['hour'] : $hour;
             $end_minute = isset($event->dtend['value']['min']) ? $event->dtend['value']['min'] : $minute;
             // -------------------------------------
             //	Last-modified date
             // -------------------------------------
             if (isset($event->lastmodified['value'])) {
                 $lm_date = $event->lastmodified['value']['year'] . $event->lastmodified['value']['month'] . $event->lastmodified['value']['day'] . $event->lastmodified['value']['hour'] . $event->lastmodified['value']['min'];
             } elseif (isset($event->dtstamp['value'])) {
                 $lm_date = $event->dtstamp['value']['year'] . $event->dtstamp['value']['month'] . $event->dtstamp['value']['day'] . $event->dtstamp['value']['hour'] . $event->dtstamp['value']['min'];
             } else {
                 $lm_date = $event->created['value']['year'] . $event->created['value']['month'] . $event->created['value']['day'] . $event->created['value']['hour'] . $event->created['value']['min'];
             }
             // -------------------------------------
             //	Does this event already exist? Do we need to update?
             // -------------------------------------
             if (isset($imported[$event->uid['value']])) {
                 // -------------------------------------
                 //	Has the event been updated? No reason
                 //	to do any work if it's the same old stuff.
                 // -------------------------------------
                 if ($lm_date == $imported[$event->uid['value']]['last_mod']) {
                     continue;
                 } elseif ($lm_date == $imported[$event->uid['value']]['last_mod']) {
                     continue;
                 }
                 $entry_id = $imported[$event->uid['value']]['entry_id'];
             } else {
                 $entry_id = '';
             }
             // -------------------------------------
             //	Adjust CDT
             // -------------------------------------
             $CDT->change_datetime($event->dtstart['value']['year'], $event->dtstart['value']['month'], $event->dtstart['value']['day'], $hour, $minute);
             if (isset($event->dtend['value'])) {
                 $CDT_end->change_datetime($event->dtend['value']['year'], $event->dtend['value']['month'], $event->dtend['value']['day'], $end_hour, $end_minute);
             } else {
                 $CDT_end->change_ymd($CDT->ymd);
                 $CDT_end->change_time($end_hour, $end_minute);
             }
             // -------------------------------------
             //	Adjust to the correct timezone for thie calendar
             // -------------------------------------
             if (!isset($event->dtstart['params']['TZID']) or $event->dtstart['params']['TZID'] == '') {
                 if (isset($event->dtstart['value']['hour'])) {
                     $CDT->add_time($tz_offset);
                     $CDT_end->add_time($tz_offset);
                 } else {
                     $CDT_end->add_day(-1);
                 }
             }
             // -------------------------------------
             //	Variableification
             // -------------------------------------
             $title = isset($event->summary['value']) ? $event->summary['value'] : lang('no_title');
             $summary = (isset($event->description) and is_array($event->description) and isset($event->description[0]['value'])) ? $event->description[0]['value'] : '';
             $location = isset($event->location['value']) ? $event->location['value'] : '';
             $rules = $this->ical_rule_to_calendar_rule($event->rrule);
             $exceptions = array('date' => array());
             if (mb_strlen($title) > 100) {
                 $title = substr($title, 0, 100);
             }
             if (is_array($event->exdate) and !empty($event->exdate)) {
                 $exceptions = $this->ical_exdate_to_calendar_exception($event->exdate);
             }
             $recurs = (is_array($event->rrule) and !empty($event->rrule)) ? 'y' : 'n';
             // -------------------------------------
             //	Fix some linebreak problems
             // -------------------------------------
             $summary = str_replace(array("\r", "\n"), '', $summary);
             $summary = str_replace('\\n', "\n", $summary);
             // -------------------------------------
             //	Set up $_POST
             // -------------------------------------
             $_POST = $post_data = array('site_id' => $this->data->get_site_id(), 'author_id' => $author_id, 'entry_id' => $entry_id, 'weblog_id' => $channel_id, 'channel_id' => $channel_id, 'status' => 'open', 'entry_date' => date('Y-m-d H:i a', ee()->localize->now - 3600 * 24 * 2), 'title' => $title, 'calendar_id' => $calendar_id, 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'summary') => $summary, 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'location') => $location, 'rule_id' => array(), 'start_date' => array($CDT->ymd), 'start_time' => array($CDT->hour . $CDT->minute), 'end_date' => array($CDT_end->ymd), 'end_time' => array($CDT_end->hour . $CDT_end->minute), 'all_day' => !isset($event->dtstart['value']['hour']) ? 'y' : 'n', 'rule_type' => $rules['rule_type'], 'repeat_years' => $rules['repeat_years'], 'repeat_months' => $rules['repeat_months'], 'repeat_weeks' => $rules['repeat_weeks'], 'repeat_days' => $rules['repeat_days'], 'days_of_week' => $rules['days_of_week'], 'relative_dow' => $rules['relative_dow'], 'days_of_month' => $rules['days_of_month'], 'months_of_year' => $rules['months_of_year'], 'end_by' => $rules['end_by'], 'end_after' => $rules['end_after'], 'occurrences' => $exceptions, 'expiration_date' => '', 'comment_expiration_date' => '', 'allow_comments' => 'n');
             // -------------------------------------
             //	Let Publish do its things
             // -------------------------------------
             if (APP_VER < 2.0) {
                 $PB->submit_new_entry(FALSE);
                 //<- LOOK HOW EASY IT USED TO BE >:|
             } else {
                 //EE 1.x doesn't have this field
                 $opt_field = 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'dates_and_options');
                 $_POST[$opt_field] = $calendar_id;
                 $post_data[$opt_field] = $calendar_id;
                 //this worked pre EE 2.1.3, then stopped working? *sigh*
                 //now we have to do all of this mess manually for field
                 //settings before inserting new entries via the API
                 //ee()->api_channel_fields->fetch_custom_channel_fields();
                 //--------------------------------------------
                 //	Check for custom field group
                 //--------------------------------------------
                 $fg_query = ee()->db->query("SELECT field_group\n\t\t\t\t\t\t FROM\texp_channels\n\t\t\t\t\t\t WHERE\tchannel_id = '" . ee()->db->escape_str($channel_id) . "'");
                 if ($fg_query->num_rows() > 0) {
                     $field_group = $fg_query->row('field_group');
                     ee()->lang->loadfile('channel');
                     ee()->lang->loadfile('content');
                     ee()->load->model('field_model');
                     ee()->load->model('channel_model');
                     // Rudimentary handling of custom fields
                     $field_query = ee()->channel_model->get_channel_fields($field_group);
                     $dst_enabled = ee()->session->userdata('daylight_savings');
                     foreach ($field_query->result_array() as $row) {
                         $field_data = '';
                         $field_dt = '';
                         $field_fmt = $row['field_fmt'];
                         // Settings that need to be prepped
                         $settings = array('field_instructions' => trim($row['field_instructions']), 'field_text_direction' => $row['field_text_direction'] == 'rtl' ? 'rtl' : 'ltr', 'field_fmt' => $field_fmt, 'field_dt' => $field_dt, 'field_data' => $field_data, 'field_name' => 'field_id_' . $row['field_id'], 'dst_enabled' => $dst_enabled);
                         $ft_settings = array();
                         if (isset($row['field_settings']) and strlen($row['field_settings'])) {
                             $ft_settings = unserialize(base64_decode($row['field_settings']));
                         }
                         $settings = array_merge($row, $settings, $ft_settings);
                         ee()->api_channel_fields->set_settings($row['field_id'], $settings);
                     }
                 }
                 //now we can do the new entry
                 ee()->api_channel_entries->submit_new_entry($channel_id, $post_data);
             }
             // -------------------------------------
             //	Update the imports table
             // -------------------------------------
             $data = array('calendar_id' => $calendar_id, 'event_id' => $this->cache['ical_event_id'], 'entry_id' => $this->cache['ical_entry_id'], 'uid' => $event->uid['value'], 'last_mod' => $lm_date);
             if ($entry_id != '') {
                 $data['import_id'] = $imported[$event->uid['value']]['import_id'];
                 $this->data->update_imported_event($data);
             } else {
                 //$data['import_id'] = '0';
                 $this->data->add_imported_event($data);
             }
         }
     }
     $this->data->update_ics_updated($calendar_id);
     ee()->extensions->end_script = FALSE;
     ee()->extensions->in_progress = APP_VER < 2.0 ? 'submit_new_entry_end' : 'entry_submission_end';
     return TRUE;
 }
Exemplo n.º 2
0
 public function import_ics_data($calendar_id, $run_deletes = false)
 {
     // -------------------------------------
     //	this has the potential to take a while
     // -------------------------------------
     set_time_limit(0);
     // -------------------------------------
     //	Get some basic info to use later
     // -------------------------------------
     $tmpl_exists = isset(ee()->TMPL) && is_object(ee()->TMPL);
     $cbasics = $this->data->calendar_basics();
     $cbasics = $cbasics[$calendar_id];
     $urls = $cbasics['ics_url'];
     if ($urls == '') {
         return FALSE;
     }
     $tz_offset = $cbasics['tz_offset'] != '' ? $cbasics['tz_offset'] : '0000';
     // -------------------------------------
     //	delete missing items from update?
     // -------------------------------------
     $delete = ($this->check_yes($this->preference('ics_update_delete_default')) or $tmpl_exists && $this->check_yes(ee()->TMPL->fetch_param('delete_missing')));
     /*
     
     		This shouldn't be happening because DST is only something that
     		would need to be applied when generating the users current local time.
     		If an event were at 7pm EST or EDT, it would still be at 7pm either way.
     		I hate DST.
     
     		if ($tz_offset != '0000' AND ee()->config->item('daylight_savings') == 'y')
     		{
     			$tz_offset += 100;
     		}
     */
     $channel_id = $this->data->channel_is_events_channel();
     $author_id = $cbasics['author_id'];
     // -------------------------------------
     //	Prepare the URLs
     // -------------------------------------
     if (!is_array($urls)) {
         $urls = explode("\n", $urls);
     }
     foreach ($urls as $k => $url) {
         $urls[$k] = trim($url);
     }
     // -------------------------------------
     //	Load iCalCreator
     // -------------------------------------
     if (!class_exists('vcalendar')) {
         require_once 'libraries/icalcreator/iCalcreator.class.php';
     }
     // -------------------------------------
     //	Load Calendar_datetime
     // -------------------------------------
     if (!class_exists('Calendar_datetime')) {
         require_once CALENDAR_PATH . 'calendar.datetime' . EXT;
     }
     $CDT = new Calendar_datetime();
     $CDT_end = new Calendar_datetime();
     // -------------------------------------
     //	Load Publish
     // -------------------------------------
     ee()->load->library('api');
     ee()->api->instantiate(array('channel_entries', 'channel_categories', 'channel_fields'));
     ee()->load->helper('url');
     ee()->api_channel_entries->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE;
     // -------------------------------------
     //	Tell our extensions that we're running the icalendar import
     // -------------------------------------
     $this->cache['ical'] = TRUE;
     // -------------------------------------
     //	Get already-imported events
     // -------------------------------------
     $imported = $this->data->get_imported_events($calendar_id);
     $imported_not_found = !empty($imported) ? array_combine(array_keys($imported), array_keys($imported)) : array();
     // -------------------------------------
     //	Don't let EXT drop us early
     // -------------------------------------
     ee()->extensions->in_progress = '';
     // -------------------------------------
     //	Cycle through the URLs
     // -------------------------------------
     $errors = array();
     foreach ($urls as $url) {
         $ICAL = new vcalendar();
         $output = $this->fetch_url($url);
         //lets not do horrible things
         if (empty($output)) {
             continue;
         }
         $ICAL->parse($output);
         // -------------------------------------
         //	Iterate among the events
         // -------------------------------------
         while ($event = $ICAL->getComponent('vevent')) {
             // -------------------------------------
             //	Times
             // -------------------------------------
             $hour = isset($event->dtstart['value']['hour']) ? $event->dtstart['value']['hour'] : 00;
             $minute = isset($event->dtstart['value']['min']) ? $event->dtstart['value']['min'] : 00;
             $end_hour = isset($event->dtend['value']['hour']) ? $event->dtend['value']['hour'] : $hour;
             $end_minute = isset($event->dtend['value']['min']) ? $event->dtend['value']['min'] : $minute;
             // -------------------------------------
             //	Last-modified date
             // -------------------------------------
             if (isset($event->lastmodified['value'])) {
                 $lm_date = $event->lastmodified['value']['year'] . $event->lastmodified['value']['month'] . $event->lastmodified['value']['day'] . $event->lastmodified['value']['hour'] . $event->lastmodified['value']['min'];
             } elseif (isset($event->dtstamp['value'])) {
                 $lm_date = $event->dtstamp['value']['year'] . $event->dtstamp['value']['month'] . $event->dtstamp['value']['day'] . $event->dtstamp['value']['hour'] . $event->dtstamp['value']['min'];
             } else {
                 $lm_date = $event->created['value']['year'] . $event->created['value']['month'] . $event->created['value']['day'] . $event->created['value']['hour'] . $event->created['value']['min'];
             }
             // -------------------------------------
             //	Does this event already exist? Do we need to update?
             // -------------------------------------
             if (isset($imported[$event->uid['value']])) {
                 //for what needs to be deleted
                 unset($imported_not_found[$event->uid['value']]);
                 // -------------------------------------
                 //	Has the event been updated? No reason
                 //	to do any work if it's the same old stuff.
                 // -------------------------------------
                 if ($lm_date == $imported[$event->uid['value']]['last_mod']) {
                     continue;
                 }
                 $entry_id = $imported[$event->uid['value']]['entry_id'];
             } else {
                 $entry_id = '';
             }
             // -------------------------------------
             //	Adjust CDT
             // -------------------------------------
             $CDT->change_datetime($event->dtstart['value']['year'], $event->dtstart['value']['month'], $event->dtstart['value']['day'], $hour, $minute);
             if (isset($event->dtend['value'])) {
                 $CDT_end->change_datetime($event->dtend['value']['year'], $event->dtend['value']['month'], $event->dtend['value']['day'], $end_hour, $end_minute);
             } else {
                 $CDT_end->change_ymd($CDT->ymd);
                 $CDT_end->change_time($end_hour, $end_minute);
             }
             // -------------------------------------
             //	Adjust to the correct timezone for thie calendar
             // -------------------------------------
             if (!isset($event->dtstart['params']['TZID']) or $event->dtstart['params']['TZID'] == '') {
                 if (isset($event->dtstart['value']['hour'])) {
                     $CDT->add_time($tz_offset);
                     $CDT_end->add_time($tz_offset);
                 } else {
                     $CDT_end->add_day(-1);
                 }
             }
             // -------------------------------------
             //	Variableification
             // -------------------------------------
             $title = isset($event->summary['value']) ? $event->summary['value'] : lang('no_title');
             $summary = (isset($event->description) and is_array($event->description) and isset($event->description[0]['value'])) ? $event->description[0]['value'] : '';
             $location = isset($event->location['value']) ? $event->location['value'] : '';
             $rules = $this->ical_rule_to_calendar_rule($event->rrule);
             $exceptions = array('date' => array());
             if (mb_strlen($title) > 100) {
                 $title = substr($title, 0, 100);
             }
             if (is_array($event->exdate) and !empty($event->exdate)) {
                 $exceptions = $this->ical_exdate_to_calendar_exception($event->exdate);
             }
             $recurs = (is_array($event->rrule) and !empty($event->rrule)) ? 'y' : 'n';
             // -------------------------------------
             //	Fix some linebreak problems
             // -------------------------------------
             $summary = str_replace(array("\r", "\n"), '', $summary);
             $summary = str_replace('\\n', "\n", $summary);
             // -------------------------------------
             //	Set up $_POST
             // -------------------------------------
             $prefix = CALENDAR_EVENTS_FIELD_PREFIX;
             $sum_fid = $this->data->get_field_id($prefix . 'summary');
             $loc_fid = $this->data->get_field_id($prefix . 'location');
             $dao_fid = $this->data->get_field_id($prefix . 'dates_and_options');
             //lets not be buttwipes
             //will restore old post later
             $old_POST = $_POST;
             //must use $_POST here because many fieldtypoes use their own
             //processing via POST and ignore incoming data
             $_POST = $post_data = array('site_id' => $this->data->get_site_id(), 'author_id' => $author_id, 'entry_id' => $entry_id, 'channel_id' => $channel_id, 'status' => 'open', 'entry_date' => ee()->localize->now - 3600 * 24 * 2, 'title' => $title, 'url_title' => url_title($title), 'calendar_id' => $calendar_id, 'field_id_' . $sum_fid => $summary, 'field_id_' . $loc_fid => $location, 'field_id_' . $dao_fid => $calendar_id, 'rule_id' => array(), 'start_date' => array($CDT->ymd), 'start_time' => array($CDT->hour . $CDT->minute), 'end_date' => array($CDT_end->ymd), 'end_time' => array($CDT_end->hour . $CDT_end->minute), 'all_day' => !isset($event->dtstart['value']['hour']) ? 'y' : 'n', 'rule_type' => $rules['rule_type'], 'repeat_years' => $rules['repeat_years'], 'repeat_months' => $rules['repeat_months'], 'repeat_weeks' => $rules['repeat_weeks'], 'repeat_days' => $rules['repeat_days'], 'days_of_week' => $rules['days_of_week'], 'relative_dow' => $rules['relative_dow'], 'days_of_month' => $rules['days_of_month'], 'months_of_year' => $rules['months_of_year'], 'end_by' => $rules['end_by'], 'end_after' => $rules['end_after'], 'occurrences' => $exceptions, 'expiration_date' => '', 'comment_expiration_date' => '', 'allow_comments' => 'n');
             //--------------------------------------------
             //	Check for custom field group
             //--------------------------------------------
             ee()->api_channel_fields->setup_entry_settings($channel_id, $post_data);
             if ($entry_id) {
                 // -------------------------------------
                 //	get old data and merge
                 // -------------------------------------
                 ee()->load->model('channel_entries_model');
                 //if you don't give it the channel_id it
                 //only selects entry, channel, and author id. lol wtf?
                 $old_data_q = ee()->channel_entries_model->get_entry($entry_id, $channel_id);
                 if ($old_data_q->num_rows() > 0) {
                     $old_data = $old_data_q->row_array();
                     ee()->load->library('calendar_ics_field_update');
                     //prep special cases
                     $old_data = ee()->calendar_ics_field_update->run_prep_fields($old_data);
                     //need to let our new data override old
                     $_POST = $post_data = array_merge($old_data, $post_data);
                 }
                 //fire update and cross fingers
                 $result = ee()->api_channel_entries->update_entry($entry_id, $post_data);
             } else {
                 //now we can do the new entry
                 $result = ee()->api_channel_entries->submit_new_entry($channel_id, $post_data);
             }
             if ($result === FALSE) {
                 //restore old post
                 $_POST = $old_POST;
                 $errors[] = ee()->api_channel_entries->errors;
                 continue;
             }
             // -------------------------------------
             //	Update the imports table
             // -------------------------------------
             /*if (isset($this->cache['ical_event_id']))
             		{*/
             $data = array('calendar_id' => $calendar_id, 'event_id' => $this->cache['ical_event_id'], 'entry_id' => $this->cache['ical_entry_id'], 'uid' => $event->uid['value'], 'last_mod' => $lm_date);
             if ($entry_id != '') {
                 $data['import_id'] = $imported[$event->uid['value']]['import_id'];
                 $this->data->update_imported_event($data);
             } else {
                 //$data['import_id'] = '0';
                 $this->data->add_imported_event($data);
             }
             /*}*/
             //restore old post
             $_POST = $old_POST;
         }
         //END while ($event = $ICAL->getComponent('vevent'))
     }
     //END 	foreach ($urls as $url)
     // -------------------------------------
     //	items to delete
     // -------------------------------------
     if ($delete) {
         $deleted_ids = array();
         foreach ($imported_not_found as $uid => $value) {
             $deleted_ids[] = $imported[$uid]['entry_id'];
         }
         if (!empty($deleted_ids)) {
             ee()->api_channel_entries->delete_entry($deleted_ids);
         }
     }
     $this->data->update_ics_updated($calendar_id);
     ee()->extensions->end_script = FALSE;
     ee()->extensions->in_progress = 'entry_submission_end';
     return TRUE;
 }