Skip to content

Commit

Permalink
feat: Check feature and better error handling and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
dploeger committed Dec 4, 2022
1 parent ea6829b commit 60506b6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
24 changes: 14 additions & 10 deletions actions/wabue/appointment/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Abend",
];

$check_only = get_input('check', 'yes') == 'yes';
$file = elgg_get_uploaded_file('import');
$spreadsheet = IOFactory::load($file);

Expand All @@ -63,6 +64,7 @@ function parse_input_date(string $value): ?array
];
}
$errors = [];
$events_imported = 0;

foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$current_user = elgg_get_logged_in_user_entity();
Expand All @@ -77,7 +79,7 @@ function parse_input_date(string $value): ?array
if (!$headersValid) {
foreach ($headers as $index => $header) {
if (!($worksheet->getCellByColumnAndRow($index + 1, $row->getRowIndex())->getValue() == $header)) {
$errors[] = "Worksheet " . $worksheet->getTitle() . "doesn't match header validation. Skipping";
$errors[] = "Worksheet " . $worksheet->getTitle() . " doesn't match header validation. Skipping";
continue 3;
}
}
Expand Down Expand Up @@ -148,7 +150,7 @@ function parse_input_date(string $value): ?array
$errors[] = "Did not catch all required event cells. Skipping row: $row_text";
continue;
}
elgg_call(ELGG_IGNORE_ACCESS, function () use ($value, $fromDate, $toDate, $organizer, $event, $current_user, $type) {
elgg_call(ELGG_IGNORE_ACCESS, function () use ($value, $fromDate, $toDate, $organizer, $event, $current_user, $type, $check_only, $events_imported) {
set_input('schedule_type', 'fixed');
set_input('start_date', $fromDate['date']);
set_input('end_date', $toDate['date']);
Expand All @@ -161,24 +163,26 @@ function parse_input_date(string $value): ?array
set_input('description', $event['Wer']);
set_input('venue', $event['Wo']);
set_input('region', $type);
$session = elgg()->session;
$session->setLoggedInUser($organizer);
event_calendar_set_event_from_form(0, 0);
$session->setLoggedInUser($current_user);
if (!$check_only) {
$session = elgg()->session;
$session->setLoggedInUser($organizer);
event_calendar_set_event_from_form(0, 0);
$session->setLoggedInUser($current_user);
}
});

$events_imported++;
}
}

if (count($errors) > 0) {
return elgg_error_response(
elgg_echo('wabue:appointment:import:error'),
elgg_generate_url('view:uploadappointments', ['errors' => substr(join('<br />', $errors),0, 2000)])
elgg_echo('wabue:appointment:import:error', [$events_imported]),
elgg_generate_url('view:uploadappointments', ['errors' => substr(join('<br />', $errors),0, 2000), 'events_imported' => $events_imported])
);
} else {
return elgg_ok_response(
'',
elgg_echo('wabue:appointment:import:successful'),
elgg_echo('wabue:appointment:import:successful', [$events_imported]),
elgg_generate_url('view:uploadappointments')
);
}
7 changes: 5 additions & 2 deletions languages/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@
'wabue:settings:appointmentusers:label' => 'Benutzer zum Terminupload',
'wabue:settings:appointmentusers:help' => 'Benutzer*innen, die den Excel-Terminupload nutzen können. Ein Benutzername pro Zeile.',
'wabue:appointments:gatekeeper:error' => 'Du bist nicht für diesen Service berechtigt',
'wabue:appointment:import:error' => 'Es traten Fehler beim Importieren der Termine auf',
'wabue:appointment:import:successful' => 'Termine erfolgreich hochgeladen',
'wabue:appointment:import:error' => 'Es traten Fehler beim Importieren der Termine auf. %d Termine waren erfolgreich',
'wabue:appointment:import:successful' => '%d Termine erfolgreich hochgeladen',
'wabue:appointment:import:file:label' => 'Termin-Datei',
'wabue:appointment:import:file:help' => 'Eine Excel-Datei mit Arbeitsblättern mit den zu importierenden Terminen gemäß der Kopfzeilen Was, Von Wann, Bis Wann, Wer, Wo, Art and Ansprechpartner',
'wabue:appointment:import:submit' => 'Importieren',

'wabue:appointment:import:check:label' => 'Nur prüfen',
'wabue:appointment:import:check:help' => 'Termine nicht importieren, Importdatei nur prüfen',
];
6 changes: 4 additions & 2 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
'wabue:settings:appointmentusers:label' => 'Users that can upload appointments',
'wabue:settings:appointmentusers:help' => 'Usernames that can use the excel upload for appointments. One user per line',
'wabue:appointments:gatekeeper:error' => 'You are not allowed to use this service',
'wabue:appointment:import:error' => 'There were errors loading the appointments',
'wabue:appointment:import:successful' => 'Successfully loaded the appointments',
'wabue:appointment:import:error' => 'There were errors loading the appointments. %d appointments loaded successfully',
'wabue:appointment:import:successful' => 'Successfully loaded %d appointments',
'wabue:appointment:import:file:label' => 'Event-File',
'wabue:appointment:import:file:help' => 'An Excel file containing worksheets that hold the appointments to be imported using the headers Was, Von Wann, Bis Wann, Wer, Wo, Art and Ansprechpartner',
'wabue:appointment:import:submit' => 'Import',

'wabue:appointment:import:check:label' => 'Only check import',
'wabue:appointment:import:check:help' => "Don't import events, only validate import",
];
13 changes: 12 additions & 1 deletion views/default/forms/wabue/appointment/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Wabue\Membership\Tools;

$errors = get_input('errors','');
$events_imported = get_input('events_imported', 0);

if (!empty($errors)) {
echo elgg_view_module('error', elgg_echo('wabue:appointment:import:error'), $errors);
echo elgg_view_message('error', $errors);
}

echo elgg_view_field([
Expand All @@ -18,6 +19,16 @@
'required' => true,
]);

echo elgg_view_field([
'#type' => 'checkbox',
'#label' => elgg_echo('wabue:appointment:import:check:label'),
'#help' => elgg_echo('wabue:appointment:import:check:help'),
'name' => 'check',
'value' => 'yes',
'checked' => true,
'switch' => true
]);

elgg_set_form_footer(
elgg_view_field([
'#type' => 'submit',
Expand Down

0 comments on commit 60506b6

Please sign in to comment.