16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
}
elseif (qg('export') == 'ods')
{
$import->toODS();
exit;
}
$champs = $config->get('champs_membres')->getAll();
$csv_file = false;
if (f('csv_encoded'))
{
$form->check('membres_import', [
'csv_encoded' => 'required|json',
'csv_translate' => 'required|array',
'skip_first_line' => 'boolean',
]);
$csv_file = json_decode(f('csv_encoded'), true);
if (!$form->hasErrors())
{
try
{
$import->fromArray($csv_file, f('csv_translate'), $user->id, f('skip_first_line') ? 1 : 0);
Utils::redirect(ADMIN_URL . 'membres/import.php?ok');
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
}
elseif (f('import'))
{
$form->check('membres_import', [
'upload' => 'file|required',
'type' => 'required|in:csv,garradin',
]);
if (!$form->hasErrors())
{
try
{
if (f('type') == 'garradin')
{
$import->fromGarradinCSV($_FILES['upload']['tmp_name'], $user->id);
Utils::redirect(ADMIN_URL . 'membres/import.php?ok');
}
elseif (f('type') == 'csv')
{
$csv_file = CSV::readAsArray($_FILES['upload']['tmp_name']);
}
else
{
throw new UserException('Import inconnu.');
}
}
catch (UserException $e)
{
$form->addError($e->getMessage());
}
}
}
$tpl->assign('ok', null !== qg('ok') ? true : false);
$tpl->assign('csv_file', $csv_file);
$tpl->assign('csv_first_line', $csv_file ? reset($csv_file) : null);
$tpl->assign('max_upload_size', Utils::getMaxUploadSize());
$tpl->assign('garradin_champs', $champs);
$tpl->display('admin/membres/import.tpl');
|
>
|
<
|
|
<
<
<
<
<
|
<
|
<
<
|
<
<
<
|
<
|
<
>
|
|
<
<
<
>
<
<
<
<
|
<
>
>
>
|
>
|
|
<
|
|
|
|
<
|
<
<
<
|
<
<
<
<
<
<
<
<
|
<
<
<
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
}
elseif (qg('export') == 'ods')
{
$import->toODS();
exit;
}
$csv = new CSV_Custom($session, 'users_import');
$champs = $config->get('champs_membres')->getAll();
$csrf_key = 'membres_import';
$columns = [];
foreach ($champs as $key => $config) {
if (!isset($config->title)) {
continue;
}
$columns[$key] = $config->title;
}
$csv->setColumns($columns);
$form->runIf(f('import') && $csv->ready(), function () use ($csv, $import, $user) {
$csv->setTranslationTable(f('translation_table'));
$csv->skip((int)f('skip_first_line'));
$import->fromCustomCSV($csv, $user->id);
$csv->clear();
}, $csrf_key, ADMIN_URL . 'membres/import.php?ok');
$form->runIf(f('import') && f('type') == 'garradin' && !empty($_FILES['upload']['tmp_name']), function () use ($import, $user) {
$import->fromGarradinCSV($_FILES['upload']['tmp_name'], $user->id);
}, $csrf_key, ADMIN_URL . 'membres/import.php?ok');
$form->runIf(f('import') && f('type') == 'custom' && !empty($_FILES['upload']['tmp_name']), function () use ($csv) {
$csv->load($_FILES['upload']);
}, $csrf_key, ADMIN_URL . 'membres/import.php?ok');
$tpl->assign('ok', null !== qg('ok') ? true : false);
$tpl->assign(compact('csv', 'csrf_key'));
$tpl->assign('max_upload_size', Utils::getMaxUploadSize());
$tpl->display('admin/membres/import.tpl');
|