From 4e802b2127e2561b5c9b3a59c19aff22e64567d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20H=C3=A4usler?= Date: Mon, 11 Jul 2011 22:41:58 +0200 Subject: [PATCH] #4 pentabarf xml import done --- includes/pages/admin_import.php | 57 +++++++++++++++++++++++++------ templates/admin_import_check.html | 44 ++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 11 deletions(-) diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index c603a1ba..ef75dc8a 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -1,5 +1,6 @@ count($rooms_new) == 0 ? "None" : table_body($rooms_new), 'rooms_deleted' => count($rooms_deleted) == 0 ? "None" : table_body($rooms_deleted), 'events_new' => count($events_new) == 0 ? "None" : table_body(shifts_printable($events_new)), - 'events_updated' => count($events_updated) == 0 ? "None" : table_body($events_updated), - 'events_deleted' => count($events_deleted) == 0 ? "None" : table_body($events_deleted) + 'events_updated' => count($events_updated) == 0 ? "None" : table_body(shifts_printable($events_updated)), + 'events_deleted' => count($events_deleted) == 0 ? "None" : table_body(shifts_printable($events_deleted)) )); break; case "import" : list ($rooms_new, $rooms_deleted) = prepare_rooms(); - foreach ($rooms_new as $room) + foreach ($rooms_new as $room) { sql_query("INSERT INTO `Room` SET `Name`='" . sql_escape($room) . "', `FromPentabarf`='Y', `Show`='Y'"); + $rooms_import[trim($room)] = sql_id(); + } foreach ($rooms_deleted as $room) sql_query("DELETE FROM `Room` WHERE `Name`='" . sql_escape($room) . "' LIMIT 1"); @@ -49,6 +52,13 @@ function admin_import() { foreach ($events_new as $event) sql_query("INSERT INTO `Shifts` SET `start`=" . sql_escape($event['start']) . ", `end`=" . sql_escape($event['end']) . ", `RID`=" . sql_escape($event['RID']) . ", `PSID`=" . sql_escape($event['PSID']) . ", `URL`='" . sql_escape($event['URL']) . "'"); + foreach ($events_updated as $event) + sql_query("UPDATE `Shifts` SET `start`=" . sql_escape($event['start']) . ", `end`=" . sql_escape($event['end']) . ", `RID`=" . sql_escape($event['RID']) . ", `PSID`=" . sql_escape($event['PSID']) . ", `URL`='" . sql_escape($event['URL']) . "' WHERE `PSID`=" . sql_escape($event['PSID']) . " LIMIT 1"); + + foreach ($events_deleted as $event) + sql_query("DELETE FROM `Shifts` WHERE `PSID`=" . + sql_escape($event['PSID']) . " LIMIT 1"); + $html .= template_render('../templates/admin_import_import.html', array ()); break; } @@ -246,8 +256,11 @@ function prepare_rooms() { $events = $data->vcalendar->vevent; $rooms_pb = array (); - foreach ($events as $event) + foreach ($events as $event) { $rooms_pb[] = $event->location; + if (!isset ($rooms_import[trim($event->location)])) + $rooms_import[trim($event->location)] = trim($event->location); + } $rooms_pb = array_unique($rooms_pb); $rooms_new = array_diff($rooms_pb, $rooms_db); @@ -272,20 +285,42 @@ function prepare_events() { $shifts_pb = array (); foreach ($events as $event) { $event_pb = $event->children("http://pentabarf.org"); - $shifts_pb[] = array ( + $event_id = trim($event_pb-> { + 'event-id' }); + $shifts_pb[$event_id] = array ( 'start' => DateTime :: createFromFormat("Ymd\THis", $event->dtstart)->getTimestamp(), 'end' => DateTime :: createFromFormat("Ymd\THis", $event->dtend)->getTimestamp(), 'RID' => $rooms_import[trim($event->location)], 'URL' => trim($event->url), - 'PSID' => trim($event_pb-> { - 'event-id' }) + 'PSID' => $event_id ); } + $shifts = sql_select("SELECT * FROM `Shifts` WHERE `PSID` IS NOT NULL ORDER BY `start`"); + $shifts_db = array (); + foreach ($shifts as $shift) + $shifts_db[$shift['PSID']] = $shift; + + $shifts_new = array (); + $shifts_updated = array (); + foreach ($shifts_pb as $shift) + if (!isset ($shifts_db[$shift['PSID']])) + $shifts_new[] = $shift; + else { + $tmp = $shifts_db[$shift['PSID']]; + if ($shift['start'] != $tmp['start'] || $shift['end'] != $tmp['end'] || $shift['RID'] != $tmp['RID'] || $shift['URL'] != $tmp['URL']) + $shifts_updated[] = $shift; + } + + $shifts_deleted = array(); + foreach ($shifts_db as $shift) + if (!isset ($shifts_pb[$shift['PSID']])) + $shifts_deleted[] = $shift; + return array ( - $shifts_pb, - array (), - array () + $shifts_new, + $shifts_updated, + $shifts_deleted ); } @@ -300,7 +335,7 @@ function shifts_printable($shifts) { global $rooms_import; $rooms = array_flip($rooms_import); - usort($shifts, 'shift_sort'); + uasort($shifts, 'shift_sort'); $shifts_printable = array (); foreach ($shifts as $shift) diff --git a/templates/admin_import_check.html b/templates/admin_import_check.html index a74f1693..291ad9df 100644 --- a/templates/admin_import_check.html +++ b/templates/admin_import_check.html @@ -48,6 +48,50 @@ %events_new% +

Shifts to update

+ + + + + + + + + + + %events_updated% + +
+ Day + + Start + + End + + Room +
+

Shifts to delete

+ + + + + + + + + + + %events_deleted% + +
+ Day + + Start + + End + + Room +
\ No newline at end of file