PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/kronolith-h3-2.3.5/data.php

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