1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
<?php
/**
* $Horde: kronolith/data.php,v 1.72.2.16 2008/01/10 16:16:49 jan Exp $
*
* Copyright 2001-2008 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Jan Schneider <jan@horde.org>
*/
function _cleanup()
{
global $import_step;
$import_step = 1;
return IMPORT_FILE;
}
@define('KRONOLITH_BASE', dirname(__FILE__));
require_once KRONOLITH_BASE . '/lib/base.php';
require_once 'Horde/Data.php';
if (!$conf['menu']['import_export']) {
require KRONOLITH_BASE . '/index.php';
exit;
}
/* Importable file types. */
$file_types = array('csv' => _("Comma separated values"),
'icalendar' => _("vCalendar/iCalendar"));
/* Templates for the different import steps. */
$templates = array(
IMPORT_CSV => array($registry->get('templates', 'horde') . '/data/csvinfo.inc'),
IMPORT_MAPPED => array($registry->get('templates', 'horde') . '/data/csvmap.inc'),
IMPORT_DATETIME => array($registry->get('templates', 'horde') . '/data/datemap.inc')
);
if (Kronolith::hasPermission('max_events') !== true &&
Kronolith::hasPermission('max_events') <= Kronolith::countEvents()) {
$message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d events."), Kronolith::hasPermission('max_events')), ENT_COMPAT, NLS::getCharset());
if (!empty($conf['hooks']['permsdenied'])) {
$message = Horde::callHook('_perms_hook_denied', array('kronolith:max_events'), 'horde', $message);
}
$notification->push($message, 'horde.warning', array('content.raw'));
$templates[IMPORT_FILE] = array(KRONOLITH_TEMPLATES . '/data/export.inc');
} else {
$templates[IMPORT_FILE] = array(KRONOLITH_TEMPLATES . '/data/import.inc', KRONOLITH_TEMPLATES . '/data/export.inc');
}
/* Initial values. */
$import_step = Util::getFormData('import_step', 0) + 1;
$actionID = Util::getFormData('actionID');
$next_step = IMPORT_FILE;
$app_fields = array('title' => _("Title"),
'start_date' => _("Start Date"),
'start_time' => _("Start Time"),
'end_date' => _("End Date"),
'end_time' => _("End Time"),
'alarm' => _("Alarm Span (minutes)"),
'alarm_date' => _("Alarm Date"),
'alarm_time' => _("Alarm Time"),
'description' => _("Description"),
'category' => _("Category"),
'location' => _("Location"),
'keywords' => _("Keywords"),
'recur_type' => _("Recurrence Type"),
'recur_end_date' => _("Recurrence End Date"),
'recur_interval' => _("Recurrence Interval"),
'recur_data' => _("Recurrence Data"));
$time_fields = array('start_date' => 'date',
'start_time' => 'time',
'end_date' => 'date',
'end_time' => 'time',
'recur_end_date' => 'date');
$param = array('time_fields' => $time_fields,
'file_types' => $file_types);
$import_format = Util::getFormData('import_format', '');
$error = false;
/* Loop through the action handlers. */
switch ($actionID) {
case 'export':
if (Util::getFormData('all_events')) {
$start = null;
$end = null;
} else {
$start->mday = Util::getFormData('start_day');
$start->month = Util::getFormData('start_month');
$start->year = Util::getFormData('start_year');
$end->mday = Util::getFormData('end_day');
$end->month = Util::getFormData('end_month');
$end->year = Util::getFormData('end_year');
}
$events = array();
$calendars = Util::getFormData('exportCal', $display_calendars);
if (!is_array($calendars)) {
$calendars = array($calendars);
}
foreach ($calendars as $cal) {
if ($kronolith_driver->getCalendar() != $cal) {
$kronolith_driver->open($cal);
}
$events[$cal] = $kronolith_driver->listEvents($start, $end);
}
if (!$events) {
$notification->push(_("There were no events to export."), 'horde.message');
$error = true;
break;
}
$exportID = Util::getFormData('exportID');
switch ($exportID) {
case EXPORT_CSV:
$data = array();
foreach ($events as $cal => $calevents) {
if ($kronolith_driver->getCalendar() != $cal) {
$kronolith_driver->open($cal);
}
foreach ($calevents as $eventId) {
$event = &$kronolith_driver->getEvent($eventId);
if (is_a($event, 'PEAR_Error')) {
continue;
}
$row = array();
$row['title'] = $event->getTitle();
$row['category'] = $event->category;
$row['location'] = $event->location;
$row['description'] = $event->description;
$row['keywords'] = implode(',', $event->keywords);
$row['start_date'] = sprintf('%d-%02d-%02d', $event->start->year, $event->start->month, $event->start->mday);
$row['start_time'] = sprintf('%02d:%02d:%02d', $event->start->hour, $event->start->min, $event->start->sec);
$row['end_date'] = sprintf('%d-%02d-%02d', $event->end->year, $event->end->month, $event->end->mday);
$row['end_time'] = sprintf('%02d:%02d:%02d', $event->end->hour, $event->end->min, $event->end->sec);
$row['alarm'] = $event->alarm;
if ($event->recurs()) {
$row['recur_type'] = $event->recurrence->getRecurType();
$row['recur_end_date'] = sprintf('%d-%02d-%02d',
$event->recurrence->recurEnd->year,
$event->recurrence->recurEnd->month,
$event->recurrence->recurEnd->mday);
$row['recur_interval'] = $event->recurrence->getRecurInterval();
$row['recur_data'] = $event->recurrence->recurData;
} else {
$row['recur_type'] = null;
$row['recur_end_date'] = null;
$row['recur_interval'] = null;
$row['recur_data'] = null;
}
$data[] = $row;
}
}
$csv = &Horde_Data::singleton('csv');
$csv->exportFile(_("events.csv"), $data, true);
exit;
case EXPORT_ICALENDAR:
require_once 'Horde/Identity.php';
require_once 'Horde/iCalendar.php';
$iCal = new Horde_iCalendar();
$calNames = array();
foreach ($events as $cal => $calevents) {
if ($kronolith_driver->getCalendar() != $cal) {
$kronolith_driver->open($cal);
}
$share = &$kronolith_shares->getShare($cal);
$calNames[] = $share->get('name');
foreach ($calevents as $id) {
$event = &$kronolith_driver->getEvent($id);
if (is_a($event, 'PEAR_Error')) {
continue;
}
$iCal->addComponent($event->toiCalendar($iCal));
}
}
$iCal->setAttribute('X-WR-CALNAME', String::convertCharset(implode(', ', $calNames), NLS::getCharset(), 'utf-8'));
$data = $iCal->exportvCalendar();
$browser->downloadHeaders(_("events.ics"), 'text/calendar', false, strlen($data));
echo $data;
exit;
}
break;
case IMPORT_FILE:
$_SESSION['import_data']['import_cal'] = Util::getFormData('importCal');
$_SESSION['import_data']['purge'] = Util::getFormData('purge');
break;
}
if (!$error) {
$data = &Horde_Data::singleton($import_format);
if (is_a($data, 'PEAR_Error')) {
$notification->push(_("This file format is not supported."), 'horde.error');
$next_step = IMPORT_FILE;
} else {
if ($actionID == IMPORT_FILE) {
$share = &$kronolith_shares->getShare($_SESSION['import_data']['import_cal']);
if (is_a($share, 'PEAR_Error')) {
$notification->push(_("You have specified an invalid calendar."), 'horde.error');
$next_step = $data->cleanup();
} elseif (!$share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
$notification->push(_("You do not have permission to add events to the selected calendar."), 'horde.error');
$next_step = $data->cleanup();
} else {
$next_step = $data->nextStep($actionID, $param);
if (is_a($next_step, 'PEAR_Error')) {
$notification->push($next_step->getMessage(), 'horde.error');
$next_step = $data->cleanup();
}
}
} else {
$next_step = $data->nextStep($actionID, $param);
if (is_a($next_step, 'PEAR_Error')) {
$notification->push($next_step->getMessage(), 'horde.error');
$next_step = $data->cleanup();
}
}
}
}
/* We have a final result set. */
if (is_array($next_step)) {
/* Create a category manager. */
require_once 'Horde/Prefs/CategoryManager.php';
$cManager = new Prefs_CategoryManager();
$categories = $cManager->get();
$events = array();
$error = false;
$max_events = Kronolith::hasPermission('max_events');
if ($max_events !== true) {
$num_events = Kronolith::countEvents();
}
$kronolith_driver->open($_SESSION['import_data']['import_cal']);
if (!count($next_step)) {
$notification->push(sprintf(_("The %s file didn't contain any events."),
$file_types[$_SESSION['import_data']['format']]), 'horde.error');
$error = true;
} else {
/* Purge old calendar if requested. */
if ($_SESSION['import_data']['purge']) {
$result = $kronolith_driver->delete($_SESSION['import_data']['import_cal']);
if (is_a($result, 'PEAR_Error')) {
$notification->push(sprintf(_("The calendar could not be purged: %s"), $result->getMessage()), 'horde.error');
} else {
$notification->push(_("Calendar successfully purged."), 'horde.success');
}
}
}
foreach ($next_step as $row) {
if ($max_events !== true && $num_events >= $max_events) {
$message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d events."), Kronolith::hasPermission('max_events')), ENT_COMPAT, NLS::getCharset());
if (!empty($conf['hooks']['permsdenied'])) {
$message = Horde::callHook('_perms_hook_denied', array('kronolith:max_events'), 'horde', $message);
}
$notification->push($message, 'horde.error', array('content.raw'));
break;
}
$event = &$kronolith_driver->getEvent();
if (!$event || is_a($event, 'PEAR_Error')) {
$msg = _("Can't create a new event.");
if (is_a($event, 'PEAR_Error')) {
$msg .= ' ' . sprintf(_("This is what the server said: %s"), $event->getMessage());
}
$notification->push($msg, 'horde.error');
$error = true;
break;
}
if (is_a($row, 'Horde_iCalendar_vevent')) {
$event->fromiCalendar($row);
} elseif (is_a($row, 'Horde_iCalendar')) {
// Skip other iCalendar components for now.
continue;
} else {
$valid = $event->fromHash($row);
if (is_a($valid, 'PEAR_Error')) {
$notification->push($valid, 'horde.error');
$error = true;
break;
}
}
$success = $event->save();
if (is_a($success, 'PEAR_Error')) {
$notification->push($success, 'horde.error');
$error = true;
break;
}
$category = $event->getCategory();
if (!empty($category) && !in_array($category, $categories)) {
$cManager->add($category);
$categories[] = $category;
}
if ($max_events !== true) {
$num_events++;
}
}
if (!$error) {
$notification->push(sprintf(_("%s file successfully imported"),
$file_types[$_SESSION['import_data']['format']]), 'horde.success');
}
$next_step = $data->cleanup();
}
$title = _("Import/Export Calendar");
require KRONOLITH_TEMPLATES . '/common-header.inc';
require KRONOLITH_TEMPLATES . '/menu.inc';
echo '<div id="page">';
foreach ($templates[$next_step] as $template) {
require $template;
}
echo '</div>';
require $registry->get('templates', 'horde') . '/common-footer.inc';
|