csvxml/public/csv.php

84 lines
2.4 KiB
PHP

<?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";
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);
}
$lang = $_SESSION['lang'];
if (!empty($_POST) and !empty($_POST['selectedFields'])) {
$selectionActive = true;
$selectedFields = explode(",", trim($_POST['selectedFields'], ","));
}
else {
$selectionActive = false;
$selectedFields = [];
}
$fieldsGetter = new CsvxmlAvailableFields($lang);
$availableFields = $fieldsGetter->getFields();
$line1 = $line2 = $line3 = $line4 = [];
foreach ($availableFields as $headline => $fields) {
$i = 0;
$tLine1 = $tLine2 = $tLine3 = $tLine4 = [];
$tLine1[] = $headline;
foreach($fields as $fieldName => $field) {
if ($selectionActive === true and !in_array($fieldName, $selectedFields)) {
continue;
}
if ($i !== 0) $tLine1[] = "";
$tLine2[] = $fieldName;
$tLine3[] = $field->name_human_readable;
# $tLine4[] = $field->remark;
if (!empty($field->allowedValues)) $tLine4[] = end($field->allowedValues);
else $tLine4[] = $field->name_human_readable;
$i++;
}
if (empty($tLine2) or count($tLine2) === 0) continue;
for ($i = 1; $i <= 4; $i++) {
${"line$i"} = array_merge(${"line$i"}, ${"tLine$i"});
}
}
header('Content-Type: text/csv; charset=utf-8');
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++) {
echo '"' . implode("\";\"", ${"line$i"}) . '"' . PHP_EOL;
# echo mb_convert_encoding('"' . implode("\";\"", ${"line$i"}) . '"' . PHP_EOL, 'utf-16', 'utf-8');
}