csvxml/public/csv.php

81 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2019-08-27 00:32:24 +02:00
<?PHP
/**
* Generates a CSV template based on the field list provided for CSVXML.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
require_once __DIR__ . "/../functions/functions.php";
2019-08-27 00:32:24 +02:00
if (session_status() != PHP_SESSION_ACTIVE) {
session_start();
}
// This array contains all available languages
$allowed_langs = ['ar', 'de', 'en', 'hu', 'id', 'it', 'pl','pt'];
// Some languages are in translation. They will only be available for logged in users.
if (isset($_GET['navlang'])) {
$_SESSION['lang'] = $_GET['navlang'];
if (!in_array($_SESSION['lang'], $allowed_langs)) $_SESSION['lang'] = 'de';
}
else if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = MD_STD::lang_getfrombrowser($allowed_langs, 'en', "", false);
2019-08-27 00:32:24 +02:00
}
$lang = $_SESSION['lang'];
if (!empty($_POST) and !empty($_POST['selectedFields'])) {
$selectionActive = true;
$selectedFields = explode(",", trim($_POST['selectedFields'], ","));
}
else {
$selectionActive = false;
$selectedFields = [];
}
2019-08-27 00:32:24 +02:00
$fieldsGetter = new CsvxmlAvailableFields($lang);
$availableFields = $fieldsGetter->getFields();
2019-08-27 00:32:24 +02:00
$line1 = $line2 = $line3 = $line4 = [];
foreach ($availableFields as $headline => $fields) {
$i = 0;
$tLine1 = $tLine2 = $tLine3 = $tLine4 = [];
2019-08-27 00:32:24 +02:00
$tLine1[] = $headline;
foreach($fields as $fieldName => $field) {
if ($selectionActive === true and !in_array($fieldName, $selectedFields)) {
continue;
}
if ($i !== 0) $tLine1[] = "";
$tLine2[] = $fieldName;
2021-05-07 20:34:45 +02:00
$tLine3[] = $field->name_human_readable;
# $tLine4[] = $field->remark;
if (!empty($field->allowedValues)) $tLine4[] = end($field->allowedValues);
else $tLine4[] = $field->name_human_readable;
2019-08-27 00:32:24 +02:00
$i++;
}
if (empty($tLine2) or count($tLine2) === 0) continue;
2019-08-27 00:32:24 +02:00
for ($i = 1; $i <= 4; $i++) {
${"line$i"} = array_merge(${"line$i"}, ${"tLine$i"});
}
}
header('Content-Type: text/csv; charset=utf-8');
2019-08-27 00:32:24 +02:00
if ($selectionActive === true)
header("Content-Disposition: attachment; filename=csvxml_museum-digital_template-{$lang}_selection.csv");
else
header("Content-Disposition: attachment; filename=csvxml_museum-digital_template-{$lang}.csv");
for ($i = 2; $i <= 4; $i++) {
2021-05-07 20:34:45 +02:00
echo mb_convert_encoding('"' . implode("\";\"", ${"line$i"}) . '"' . PHP_EOL, 'utf-16', 'utf-8');
2019-08-27 00:32:24 +02:00
}