Exemplo n.º 1
0
 private function postUpload()
 {
     $error = false;
     if (empty($_FILES['upload_file']['tmp_name'])) {
         $error = true;
         $content[] = dgettext('calendar', 'Missing filename.');
     } elseif ($_FILES['upload_file']['type'] != 'text/calendar') {
         $error = true;
         $content[] = dgettext('calendar', 'Improper file format.');
     }
     if (!$error) {
         $result = file($_FILES['upload_file']['tmp_name']);
         if (!is_array($result)) {
             $error = true;
             $content[] = dgettext('calendar', 'Unable to parse file for events.');
         } elseif (trim($result[0]) != 'BEGIN:VCALENDAR') {
             $error = true;
             $content[] = dgettext('calendar', 'File does not appear to be in iCal/vCal format.');
         }
     }
     if ($error) {
         $content[] = $this->calendar->schedule->uploadEventsLink(false, dgettext('calendar', 'Return to upload form...'));
         $this->title = dgettext('calendar', 'Error');
         $this->content = implode('<br />', $content);
         return;
     }
     PHPWS_Core::initModClass('calendar', 'Event.php');
     $table = $this->calendar->schedule->getEventTable();
     $db = new PHPWS_DB($table);
     $success = 0;
     $duplicates = 0;
     foreach ($result as $cal) {
         $cal = trim($cal);
         $colon = strpos($cal, ':');
         if (!$colon) {
             continue;
         }
         $command = substr($cal, 0, $colon);
         if ($semicolon = strpos($cal, ';')) {
             $command = substr($cal, 0, $semicolon);
         }
         $value = substr($cal, $colon + 1, strlen($cal));
         if (empty($value)) {
             continue;
         }
         switch ($command) {
             case 'BEGIN':
                 if ($value == 'VEVENT' && !isset($event)) {
                     $event = new Calendar_Event(0, $this->calendar->schedule);
                     $event->start_time = 0;
                     $event->end_time = 0;
                 }
                 break;
             case 'DTSTART':
                 if (isset($event)) {
                     $event->start_time = strtotime($value);
                 }
                 break;
             case 'DTSTART;VALUE=DATE':
                 if (isset($event)) {
                     $event->start_time = strtotime($value);
                 }
                 break;
             case 'DTEND':
                 if (isset($event)) {
                     $event->end_time = strtotime($value);
                 }
                 break;
             case 'DTEND;VALUE=DATE':
                 if (isset($event)) {
                     $event->end_time = strtotime($value);
                 }
                 break;
             case 'SUMMARY':
                 if (isset($event)) {
                     $value = str_replace('\\,', ',', $value);
                     $event->setSummary($value, true);
                 }
                 break;
             case 'LOCATION':
                 if (isset($event)) {
                     $event->setLocation($value);
                 }
                 break;
             case 'DESCRIPTION':
                 if (isset($event)) {
                     $value = str_replace('\\,', ',', $value);
                     $value = str_replace('\\n', "\n", $value);
                     $event->setDescription($value, null, true);
                 }
                 break;
             case 'END':
                 if ($value == 'VEVENT' && isset($event)) {
                     if (empty($event->end_time)) {
                         //start time should be midnight so add 23h 23min 59 sec
                         $event->end_time = $event->start_time + 86399;
                         $event->all_day = 1;
                     }
                     $db->reset();
                     $db->addWhere('start_time', $event->start_time);
                     $db->addWhere('end_time', $event->end_time);
                     $db->addWhere('summary', $event->summary);
                     $db->addColumn('id');
                     $result = $db->select('one');
                     if (!empty($result)) {
                         if (PHPWS_Error::logIfError($result)) {
                             $parse_errors[] = dgettext('calendar', 'Error accessing event table.');
                         } else {
                             $duplicates++;
                         }
                     } else {
                         $save = $event->save();
                         if (PHPWS_Error::logIfError($save) || !$save) {
                             $parse_errors[] = dgettext('calendar', 'Error saving new event.');
                         } else {
                             $success++;
                         }
                     }
                     unset($event);
                 }
                 break;
         }
     }
     $this->title = dgettext('calendar', 'Import complete!');
     if (isset($parse_errors)) {
         $content[] = dgettext('calendar', 'The following errors occurred when trying to import your events:');
         $content[] = '<ul><li>' . implode('</li><li>', $parse_errors) . '</li></ul>';
     }
     $content[] = sprintf(dgettext('calendar', '%s event(s) were successfully imported.'), $success);
     $content[] = sprintf(dgettext('calendar', '%s duplicate event(s) were ignored.'), $duplicates);
     $content[] = javascript('close_window');
     $this->content = implode('<br />', $content);
 }