-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-export.php
More file actions
390 lines (356 loc) · 17.1 KB
/
Copy pathimport-export.php
File metadata and controls
390 lines (356 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* NewUI v4.0 - Import/Export Tool
*
* Admin-only tool for CSV import and export of personnel, constituent,
* vehicle, and equipment data.
*/
require_once __DIR__ . '/config.php';
// 2026-07-04 (GH #13) — pick the session profile matching the
// client's cookie (TCADMOBILE vs PHPSESSID). Without this, a
// browser holding a mobile cookie opens an empty desktop session
// here and bounces to login -> redirect loop.
require_once __DIR__ . '/inc/session-bootstrap.php';
sess_bootstrap_auto();
session_start();
if (empty($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once __DIR__ . '/inc/force-pw-change.php';
force_pw_change_redirect();
// Admin only
require_once __DIR__ . '/inc/rbac.php';
if (!is_admin()) {
header('Location: index.php');
exit;
}
$user = e($_SESSION['user']);
$level = current_role_name();
$theme = $_SESSION['day_night'] ?? 'Day';
$bs_theme = ($theme === 'Night') ? 'dark' : 'light';
$csrf = csrf_token();
$active_page = 'config';
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="<?php echo $bs_theme; ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="<?php echo e($csrf); ?>">
<title>Import / Export — Tickets NewUI <?php echo newui_version(); ?></title>
<!-- Vendor CSS -->
<link rel="stylesheet" href="assets/vendor/bootstrap/bootstrap.min.css">
<link rel="stylesheet" href="assets/vendor/bootstrap/bootstrap-icons.min.css">
<!-- App CSS -->
<link rel="stylesheet" href="assets/css/dashboard.css?v=<?php echo newui_version(); ?>">
<link rel="stylesheet" href="assets/css/config.css?v=<?php echo newui_version(); ?>">
<style>
.imex-step { display: none; }
.imex-step.active { display: block; }
.step-indicator {
display: flex; gap: 8px; margin-bottom: 16px;
}
.step-dot {
width: 28px; height: 28px; border-radius: 50%;
display: flex; align-items: center; justify-content: center;
font-size: 0.72rem; font-weight: 700;
background: var(--bs-secondary-bg); color: var(--bs-secondary);
border: 2px solid var(--bs-border-color);
}
.step-dot.active { background: var(--bs-primary); color: #fff; border-color: var(--bs-primary); }
.step-dot.done { background: var(--bs-success); color: #fff; border-color: var(--bs-success); }
.step-label { font-size: 0.72rem; margin-top: 2px; }
.mapping-row {
display: flex; align-items: center; gap: 8px;
padding: 4px 0; border-bottom: 1px solid var(--bs-border-color-translucent);
font-size: 0.8rem;
}
.mapping-row:last-child { border-bottom: none; }
.mapping-arrow { opacity: 0.4; font-size: 0.7rem; }
.preview-table {
font-size: 0.72rem; width: 100%; border-collapse: collapse;
}
.preview-table th {
background: var(--bs-secondary-bg); padding: 4px 8px;
border: 1px solid var(--bs-border-color); font-weight: 600;
white-space: nowrap; font-size: 0.68rem; text-transform: uppercase;
}
.preview-table td {
padding: 3px 8px; border: 1px solid var(--bs-border-color);
max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.target-card {
cursor: pointer; border: 2px solid var(--bs-border-color);
border-radius: 8px; padding: 16px; text-align: center;
transition: border-color 0.2s, background 0.2s;
}
.target-card:hover { border-color: var(--bs-primary); background: rgba(13,110,253,0.05); }
.target-card.selected { border-color: var(--bs-primary); background: rgba(13,110,253,0.1); }
.target-card .icon { font-size: 1.5rem; margin-bottom: 6px; }
/* Mapping column labels */
.mapping-label-csv {
font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em;
color: var(--bs-info); font-weight: 600; margin-bottom: 2px;
}
.mapping-label-db {
font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.03em;
color: var(--bs-success); font-weight: 600; margin-bottom: 2px;
}
/* Error review editor */
.error-row-card {
border: 1px solid var(--bs-danger);
border-radius: 6px; padding: 8px 12px; margin-bottom: 8px;
background: rgba(220,53,69,0.04);
}
.error-row-card.fixed {
border-color: var(--bs-success);
background: rgba(25,135,84,0.04);
}
.error-row-card .error-msg {
font-size: 0.72rem; color: var(--bs-danger); margin-bottom: 4px;
}
.error-row-card.fixed .error-msg {
color: var(--bs-success);
}
.error-row-fields {
display: flex; flex-wrap: wrap; gap: 6px;
}
.error-row-fields .field-group {
flex: 0 0 auto; min-width: 120px;
}
.error-row-fields label {
font-size: 0.65rem; text-transform: uppercase; color: var(--bs-body-secondary);
margin-bottom: 1px; display: block;
}
.error-row-fields input {
font-size: 0.75rem; padding: 2px 6px; height: auto;
}
.error-row-fields input.is-invalid {
border-color: var(--bs-danger);
}
</style>
</head>
<body>
<?php include_once NEWUI_ROOT . '/inc/navbar.php'; ?>
</header>
<!-- Config Layout: Sidebar + Content -->
<div class="config-layout">
<?php $configActivePage = 'import-export'; include_once NEWUI_ROOT . '/inc/config-sidebar.php'; ?>
<main class="config-content" id="configContent" style="padding: 1rem 1.5rem;">
<div class="d-flex align-items-center justify-content-between mb-3">
<h5 class="mb-0">
<i class="bi bi-arrow-left-right text-primary me-2"></i>Import / Export
</h5>
</div>
<!-- Tab: Import / Export toggle -->
<ul class="nav nav-tabs nav-tabs-sm mb-3">
<li class="nav-item">
<button class="nav-link active small py-1 px-3" id="btnTabImport" data-bs-toggle="tab" data-bs-target="#panelImport">
<i class="bi bi-upload me-1"></i>Import
</button>
</li>
<li class="nav-item">
<button class="nav-link small py-1 px-3" id="btnTabExport" data-bs-toggle="tab" data-bs-target="#panelExport">
<i class="bi bi-download me-1"></i>Export
</button>
</li>
</ul>
<div class="tab-content">
<!-- ═══ IMPORT PANEL ═════════════════════════════════════ -->
<div class="tab-pane fade show active" id="panelImport">
<!-- Step indicators -->
<div class="step-indicator" id="stepIndicator">
<div class="text-center">
<div class="step-dot active" id="stepDot1">1</div>
<div class="step-label">Target</div>
</div>
<div class="text-center">
<div class="step-dot" id="stepDot2">2</div>
<div class="step-label">Upload</div>
</div>
<div class="text-center">
<div class="step-dot" id="stepDot3">3</div>
<div class="step-label">Map</div>
</div>
<div class="text-center">
<div class="step-dot" id="stepDot4">4</div>
<div class="step-label">Import</div>
</div>
</div>
<!-- Step 1: Select target -->
<div class="imex-step active" id="step1">
<h6 class="mb-3">Select what you want to import</h6>
<div class="row g-3" id="targetCards">
<!-- Populated by JS -->
</div>
<div class="mt-3">
<button class="btn btn-primary" id="btnStep1Next" disabled>
Next <i class="bi bi-arrow-right ms-1"></i>
</button>
</div>
</div>
<!-- Step 2: Upload file -->
<div class="imex-step" id="step2">
<h6 class="mb-2">Upload CSV File</h6>
<p class="text-body-secondary small mb-3">
Upload a CSV file with a header row. The system will try to auto-match your column headers to the database fields.
</p>
<div class="mb-3">
<input type="file" class="form-control form-control-sm" id="csvFile" accept=".csv,.txt,.tsv" aria-label="Upload CSV file">
</div>
<div id="uploadStatus" class="mb-3" style="display:none"></div>
<div class="d-flex gap-2">
<button class="btn btn-outline-secondary btn-sm" id="btnStep2Back">
<i class="bi bi-arrow-left me-1"></i>Back
</button>
<button class="btn btn-primary btn-sm" id="btnStep2Upload" disabled>
<i class="bi bi-upload me-1"></i>Upload & Preview
</button>
</div>
</div>
<!-- Step 3: Column mapping + preview -->
<div class="imex-step" id="step3">
<div class="row g-3">
<div class="col-md-5">
<h6 class="mb-2">Column Mapping</h6>
<p class="text-body-secondary small mb-2">
Map columns from your <span class="text-info fw-semibold">CSV file</span>
to <span class="text-success fw-semibold">database fields</span>.
Auto-matched columns are pre-selected. Use <em>(skip)</em> to ignore a column.
</p>
<div class="d-flex gap-4 mb-2 small">
<span class="mapping-label-csv"><i class="bi bi-file-earmark-spreadsheet me-1"></i>Your CSV Column</span>
<span class="mapping-label-db"><i class="bi bi-database me-1"></i>Database Field</span>
</div>
<div id="mappingContainer"></div>
<div class="mt-3">
<div class="form-check mb-2">
<input class="form-check-input" type="radio" name="importMode" id="modeInsert" value="insert" checked>
<label class="form-check-label small" for="modeInsert">
<strong>Insert only</strong> — skip rows that match existing records
</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="radio" name="importMode" id="modeUpsert" value="upsert">
<label class="form-check-label small" for="modeUpsert">
<strong>Insert + Update</strong> — update existing records if matched
</label>
</div>
</div>
</div>
<div class="col-md-7">
<h6 class="mb-2">Data Preview <span class="text-body-secondary small" id="previewCount"></span></h6>
<div id="validationSummary" class="mb-2"></div>
<div style="max-height:300px;overflow:auto">
<table class="preview-table" id="previewTable">
<thead id="previewHead"></thead>
<tbody id="previewBody"></tbody>
</table>
</div>
<!-- Error Row Editor -->
<div id="errorReviewSection" style="display:none" class="mt-3">
<h6 class="mb-2 text-danger">
<i class="bi bi-exclamation-triangle me-1"></i>Review Problem Rows
<span class="badge bg-danger ms-1" id="errorRowCountBadge">0</span>
</h6>
<p class="text-body-secondary small mb-2">
These rows failed validation. Edit the fields below to fix them, then click
<strong>Import Fixed Rows</strong> to include_once them in the import.
</p>
<div id="errorRowEditor"></div>
<div class="d-flex gap-2 mt-2">
<button class="btn btn-sm btn-outline-success" id="btnImportFixed" disabled>
<i class="bi bi-check-circle me-1"></i>Import Fixed Rows
</button>
<button class="btn btn-sm btn-outline-secondary" id="btnSkipErrors">
<i class="bi bi-skip-forward me-1"></i>Skip All Error Rows
</button>
</div>
</div>
</div>
</div>
<div class="d-flex gap-2 mt-3">
<button class="btn btn-outline-secondary btn-sm" id="btnStep3Back">
<i class="bi bi-arrow-left me-1"></i>Back
</button>
<button class="btn btn-primary btn-sm" id="btnStep3Import">
<i class="bi bi-check-circle me-1"></i>Import <span id="importRowCount"></span> rows
</button>
</div>
</div>
<!-- Step 4: Results -->
<div class="imex-step" id="step4">
<div id="importResults"></div>
<div class="mt-3 d-flex gap-2">
<button class="btn btn-primary btn-sm" id="btnStartOver">
<i class="bi bi-arrow-repeat me-1"></i>Import More
</button>
<button class="btn btn-outline-secondary btn-sm" id="btnDownloadLog" style="display:none">
<i class="bi bi-download me-1"></i>Download Import Log
</button>
</div>
</div>
</div>
<!-- ═══ EXPORT PANEL ═════════════════════════════════════ -->
<div class="tab-pane fade" id="panelExport">
<h6 class="mb-3">Export Data as CSV</h6>
<div class="row g-3">
<div class="col-md-4">
<label class="form-label small" for="exportTarget">Table</label>
<select class="form-select form-select-sm" id="exportTarget">
<!-- Populated by JS -->
</select>
</div>
<div class="col-md-4">
<label class="form-label small" for="exportSearch">Search filter (optional)</label>
<input type="text" class="form-control form-control-sm" id="exportSearch" placeholder="Filter by keyword...">
</div>
<div class="col-md-4 d-flex align-items-end">
<button class="btn btn-primary btn-sm" id="btnExport">
<i class="bi bi-download me-1"></i>Download CSV
</button>
</div>
</div>
<div class="mt-3" id="exportInfo">
<p class="text-body-secondary small">
<i class="bi bi-info-circle me-1"></i>
Exports all records from the selected table as a CSV file. Use the search filter to export a subset.
</p>
</div>
</div>
</div>
</main>
</div>
<!-- CSRF token -->
<input type="hidden" id="csrfToken" value="<?php echo e($csrf); ?>">
<!-- Vendor JS -->
<script src="assets/vendor/bootstrap/bootstrap.bundle.min.js"></script>
<script src="assets/js/toolbar.js?v=<?php echo newui_version(); ?>"></script>
<script src="assets/js/import-export.js?v=<?php echo asset_v('assets/js/import-export.js'); ?>"></script>
<!-- Sidebar section expand/collapse -->
<script>
(function () {
'use strict';
var headers = document.querySelectorAll('.config-section-header');
for (var i = 0; i < headers.length; i++) {
headers[i].addEventListener('click', function () {
var section = this.getAttribute('data-section');
var list = document.querySelector('.config-tab-list[data-section="' + section + '"]');
if (list) {
this.classList.toggle('collapsed');
list.classList.toggle('collapsed');
}
});
}
var tabBtns = document.querySelectorAll('.config-tab-link[data-tab]');
for (var j = 0; j < tabBtns.length; j++) {
tabBtns[j].addEventListener('click', function () {
var tab = this.getAttribute('data-tab');
if (tab) window.location.href = 'settings.php#' + tab;
});
}
})();
</script>
</body>
</html>