|
|
|
@ -10,104 +10,154 @@ function admin_import() {
|
|
|
|
|
$html = "";
|
|
|
|
|
|
|
|
|
|
$step = "input";
|
|
|
|
|
if (isset($_REQUEST['step']))
|
|
|
|
|
if (isset($_REQUEST['step']) && in_array($step, [
|
|
|
|
|
'input',
|
|
|
|
|
'check',
|
|
|
|
|
'import'
|
|
|
|
|
]))
|
|
|
|
|
$step = $_REQUEST['step'];
|
|
|
|
|
|
|
|
|
|
$html .= '<p>';
|
|
|
|
|
$html .= $step == "input" ? '<b>1. Input</b>' : '1. Input';
|
|
|
|
|
$html .= ' » ';
|
|
|
|
|
$html .= $step == "check" ? '<b>2. Validate</b>' : '2. Validate';
|
|
|
|
|
$html .= ' » ';
|
|
|
|
|
$html .= $step == "import" ? '<b>3. Import</b>' : '3. Import';
|
|
|
|
|
$html .= '</p>';
|
|
|
|
|
|
|
|
|
|
$import_file = '../import/import_' . $user['UID'] . '.xml';
|
|
|
|
|
|
|
|
|
|
switch ($step) {
|
|
|
|
|
case "input":
|
|
|
|
|
$ok = false;
|
|
|
|
|
if ($test_handle = fopen('../import/tmp', 'w')) {
|
|
|
|
|
fclose($test_handle);
|
|
|
|
|
unlink('../import/tmp');
|
|
|
|
|
} else {
|
|
|
|
|
error("Webserver has no write-permission on import directory.");
|
|
|
|
|
error(_('Webserver has no write-permission on import directory.'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$import_file = '../import/import_' . $user['UID'] . '.xml';
|
|
|
|
|
$shifttype_id = null;
|
|
|
|
|
|
|
|
|
|
$shifttypes_source = ShiftTypes();
|
|
|
|
|
if ($shifttypes_source === false)
|
|
|
|
|
engelsystem_error('Unable to load shifttypes.');
|
|
|
|
|
$shifttypes = [];
|
|
|
|
|
foreach ($shifttypes_source as $shifttype)
|
|
|
|
|
$shifttypes[$shifttype['id']] = $shifttype['name'];
|
|
|
|
|
|
|
|
|
|
switch ($step) {
|
|
|
|
|
case 'input':
|
|
|
|
|
$ok = false;
|
|
|
|
|
|
|
|
|
|
if (isset($_REQUEST['submit'])) {
|
|
|
|
|
$ok = true;
|
|
|
|
|
|
|
|
|
|
if (isset($_REQUEST['shifttype_id']) && isset($shifttypes[$_REQUEST['shifttype_id']]))
|
|
|
|
|
$shifttype_id = $_REQUEST['shifttype_id'];
|
|
|
|
|
else {
|
|
|
|
|
$ok = false;
|
|
|
|
|
error(_('Please select a shift type.'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($_FILES['xcal_file']) && ($_FILES['xcal_file']['error'] == 0)) {
|
|
|
|
|
if (move_uploaded_file($_FILES['xcal_file']['tmp_name'], $import_file)) {
|
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
|
if (simplexml_load_file($import_file) === false) {
|
|
|
|
|
$ok = false;
|
|
|
|
|
error("No valid xml/xcal file provided.");
|
|
|
|
|
error(_('No valid xml/xcal file provided.'));
|
|
|
|
|
unlink($import_file);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$ok = false;
|
|
|
|
|
error("File upload went wrong.");
|
|
|
|
|
error(_('File upload went wrong.'));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$ok = false;
|
|
|
|
|
error("Please provide some data.");
|
|
|
|
|
error(_('Please provide some data.'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($ok)
|
|
|
|
|
redirect(page_link_to('admin_import') . "&step=check");
|
|
|
|
|
else {
|
|
|
|
|
$html .= form(array(
|
|
|
|
|
if ($ok) {
|
|
|
|
|
redirect(page_link_to('admin_import') . "&step=check&shifttype_id=" . $shifttype_id);
|
|
|
|
|
} else {
|
|
|
|
|
$html .= div('well well-sm text-center', [
|
|
|
|
|
_('File Upload') . mute(glyph('arrow-right')) . mute(_('Validation')) . mute(glyph('arrow-right')) . mute(_('Import'))
|
|
|
|
|
]) . div('row', [
|
|
|
|
|
div('col-md-offset-3 col-md-6', [
|
|
|
|
|
form(array(
|
|
|
|
|
form_info('', _("This import will create/update/delete rooms and shifts by given FRAB-export file. The needed file format is xcal.")),
|
|
|
|
|
form_select('shifttype_id', _('Shifttype'), $shifttypes, $shifttype_id),
|
|
|
|
|
form_file('xcal_file', _("xcal-File (.xcal)")),
|
|
|
|
|
form_submit('submit', _("Import"))
|
|
|
|
|
));
|
|
|
|
|
))
|
|
|
|
|
])
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "check":
|
|
|
|
|
if (! file_exists($import_file))
|
|
|
|
|
case 'check':
|
|
|
|
|
if (! file_exists($import_file)) {
|
|
|
|
|
error(_('Missing import file.'));
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($_REQUEST['shifttype_id']) && isset($shifttypes[$_REQUEST['shifttype_id']]))
|
|
|
|
|
$shifttype_id = $_REQUEST['shifttype_id'];
|
|
|
|
|
else {
|
|
|
|
|
error(_('Please select a shift type.'));
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list($rooms_new, $rooms_deleted) = prepare_rooms($import_file);
|
|
|
|
|
list($events_new, $events_updated, $events_deleted) = prepare_events($import_file);
|
|
|
|
|
list($events_new, $events_updated, $events_deleted) = prepare_events($import_file, $shifttype_id);
|
|
|
|
|
|
|
|
|
|
$html .= form(array(
|
|
|
|
|
$html .= form([
|
|
|
|
|
div('row', [
|
|
|
|
|
div('col-sm-6', [
|
|
|
|
|
'<h3>' . _("Rooms to create") . '</h3>',
|
|
|
|
|
table(_("Name"), $rooms_new),
|
|
|
|
|
table(_("Name"), $rooms_new)
|
|
|
|
|
]),
|
|
|
|
|
div('col-sm-6', [
|
|
|
|
|
'<h3>' . _("Rooms to delete") . '</h3>',
|
|
|
|
|
table(_("Name"), $rooms_deleted),
|
|
|
|
|
table(_("Name"), $rooms_deleted)
|
|
|
|
|
])
|
|
|
|
|
]),
|
|
|
|
|
'<h3>' . _("Shifts to create") . '</h3>',
|
|
|
|
|
table(array(
|
|
|
|
|
'day' => _("Day"),
|
|
|
|
|
'start' => _("Start"),
|
|
|
|
|
'end' => _("End"),
|
|
|
|
|
'name' => _("Name"),
|
|
|
|
|
'shifttype' => _('Shift type'),
|
|
|
|
|
'title' => _("Title"),
|
|
|
|
|
'room' => _("Room")
|
|
|
|
|
), shifts_printable($events_new)),
|
|
|
|
|
), shifts_printable($events_new, $shifttypes)),
|
|
|
|
|
'<h3>' . _("Shifts to update") . '</h3>',
|
|
|
|
|
table(array(
|
|
|
|
|
'day' => _("Day"),
|
|
|
|
|
'start' => _("Start"),
|
|
|
|
|
'end' => _("End"),
|
|
|
|
|
'name' => _("Name"),
|
|
|
|
|
'shifttype' => _('Shift type'),
|
|
|
|
|
'title' => _("Title"),
|
|
|
|
|
'room' => _("Room")
|
|
|
|
|
), shifts_printable($events_updated)),
|
|
|
|
|
), shifts_printable($events_updated, $shifttypes)),
|
|
|
|
|
'<h3>' . _("Shifts to delete") . '</h3>',
|
|
|
|
|
table(array(
|
|
|
|
|
'day' => _("Day"),
|
|
|
|
|
'start' => _("Start"),
|
|
|
|
|
'end' => _("End"),
|
|
|
|
|
'name' => _("Name"),
|
|
|
|
|
'shifttype' => _('Shift type'),
|
|
|
|
|
'title' => _("Title"),
|
|
|
|
|
'room' => _("Room")
|
|
|
|
|
), shifts_printable($events_deleted)),
|
|
|
|
|
), shifts_printable($events_deleted, $shifttypes)),
|
|
|
|
|
form_submit('submit', _("Import"))
|
|
|
|
|
), page_link_to('admin_import') . '&step=import');
|
|
|
|
|
], page_link_to('admin_import') . '&step=import&shifttype_id=' . $shifttype_id);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "import":
|
|
|
|
|
case 'import':
|
|
|
|
|
if (! file_exists($import_file)) {
|
|
|
|
|
error(_('Missing import file.'));
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! file_exists($import_file))
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
|
|
|
|
|
if (isset($_REQUEST['shifttype_id']) && isset($shifttypes[$_REQUEST['shifttype_id']]))
|
|
|
|
|
$shifttype_id = $_REQUEST['shifttype_id'];
|
|
|
|
|
else {
|
|
|
|
|
error(_('Please select a shift type.'));
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list($rooms_new, $rooms_deleted) = prepare_rooms($import_file);
|
|
|
|
|
foreach ($rooms_new as $room) {
|
|
|
|
|
sql_query("INSERT INTO `Room` SET `Name`='" . sql_escape($room) . "', `FromPentabarf`='Y', `Show`='Y'");
|
|
|
|
@ -116,7 +166,7 @@ function admin_import() {
|
|
|
|
|
foreach ($rooms_deleted as $room)
|
|
|
|
|
sql_query("DELETE FROM `Room` WHERE `Name`='" . sql_escape($room) . "' LIMIT 1");
|
|
|
|
|
|
|
|
|
|
list($events_new, $events_updated, $events_deleted) = prepare_events($import_file);
|
|
|
|
|
list($events_new, $events_updated, $events_deleted) = prepare_events($import_file, $shifttype_id);
|
|
|
|
|
foreach ($events_new as $event) {
|
|
|
|
|
$result = Shift_create($event);
|
|
|
|
|
if ($result === false)
|
|
|
|
@ -145,7 +195,10 @@ function admin_import() {
|
|
|
|
|
redirect(page_link_to('admin_import'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
return page_with_title(admin_import_title(), [
|
|
|
|
|
msg(),
|
|
|
|
|
$html
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prepare_rooms($file) {
|
|
|
|
@ -179,7 +232,7 @@ function prepare_rooms($file) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prepare_events($file) {
|
|
|
|
|
function prepare_events($file, $shifttype_id) {
|
|
|
|
|
global $rooms_import;
|
|
|
|
|
$data = read_xml($file);
|
|
|
|
|
|
|
|
|
@ -195,10 +248,11 @@ function prepare_events($file) {
|
|
|
|
|
$event_id = trim($event_pb->{
|
|
|
|
|
'event-id' });
|
|
|
|
|
$shifts_pb[$event_id] = array(
|
|
|
|
|
'shifttype_id' => $shifttype_id,
|
|
|
|
|
'start' => DateTime::createFromFormat("Ymd\THis", $event->dtstart)->getTimestamp(),
|
|
|
|
|
'end' => DateTime::createFromFormat("Ymd\THis", $event->dtend)->getTimestamp(),
|
|
|
|
|
'RID' => $rooms_import[trim($event->location)],
|
|
|
|
|
'name' => trim($event->summary),
|
|
|
|
|
'title' => trim($event->summary),
|
|
|
|
|
'URL' => trim($event->url),
|
|
|
|
|
'PSID' => $event_id
|
|
|
|
|
);
|
|
|
|
@ -209,14 +263,14 @@ function prepare_events($file) {
|
|
|
|
|
foreach ($shifts as $shift)
|
|
|
|
|
$shifts_db[$shift['PSID']] = $shift;
|
|
|
|
|
|
|
|
|
|
$shifts_new = array();
|
|
|
|
|
$shifts_updated = array();
|
|
|
|
|
$shifts_new = [];
|
|
|
|
|
$shifts_updated = [];
|
|
|
|
|
foreach ($shifts_pb as $shift)
|
|
|
|
|
if (! isset($shifts_db[$shift['PSID']]))
|
|
|
|
|
$shifts_new[] = $shift;
|
|
|
|
|
else {
|
|
|
|
|
$tmp = $shifts_db[$shift['PSID']];
|
|
|
|
|
if ($shift['name'] != $tmp['name'] || $shift['start'] != $tmp['start'] || $shift['end'] != $tmp['end'] || $shift['RID'] != $tmp['RID'] || $shift['URL'] != $tmp['URL'])
|
|
|
|
|
if ($shift['shifttype_id'] != $tmp['shifttype_id'] || $shift['title'] != $tmp['title'] || $shift['start'] != $tmp['start'] || $shift['end'] != $tmp['end'] || $shift['RID'] != $tmp['RID'] || $shift['URL'] != $tmp['URL'])
|
|
|
|
|
$shifts_updated[] = $shift;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -239,7 +293,7 @@ function read_xml($file) {
|
|
|
|
|
return $xml_import;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shifts_printable($shifts) {
|
|
|
|
|
function shifts_printable($shifts, $shifttypes) {
|
|
|
|
|
global $rooms_import;
|
|
|
|
|
$rooms = array_flip($rooms_import);
|
|
|
|
|
|
|
|
|
@ -250,7 +304,11 @@ function shifts_printable($shifts) {
|
|
|
|
|
$shifts_printable[] = array(
|
|
|
|
|
'day' => date("l, Y-m-d", $shift['start']),
|
|
|
|
|
'start' => date("H:i", $shift['start']),
|
|
|
|
|
'name' => shorten($shift['name']),
|
|
|
|
|
'shifttype' => ShiftType_name_render([
|
|
|
|
|
'id' => $shift['shifttype_id'],
|
|
|
|
|
'name' => $shifttypes[$shift['shifttype_id']]
|
|
|
|
|
]),
|
|
|
|
|
'title' => shorten($shift['title']),
|
|
|
|
|
'end' => date("H:i", $shift['end']),
|
|
|
|
|
'room' => $rooms[$shift['RID']]
|
|
|
|
|
);
|
|
|
|
|