-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_projects.php
More file actions
1502 lines (1363 loc) · 93 KB
/
Copy pathedit_projects.php
File metadata and controls
1502 lines (1363 loc) · 93 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'auth.php';
check_auth();
$json_data = @file_get_contents(JSON_DATA_PATH);
$data = json_decode($json_data, true);
if (!$data) $data = ['projets' => []];
if (!isset($data['tagsDisponibles'])) {
$data['tagsDisponibles'] = ["Branding", "Design", "Logo", "2024", "Graphisme", "2025", "Art", "Dessin", "Série", "Web Dev", "Front", "Back", "Motion Design", "Montage", "2023", "Texture"];
}
$editing_id = isset($_GET['id']) ? (int)$_GET['id'] : null;
$project_to_edit = null;
// Actions POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
$id = (int)$_POST['id'];
if (isset($_POST['delete_project'])) {
foreach ($data['projets'] as $key => $p) {
if ($p['id'] == $id) {
array_splice($data['projets'], $key, 1);
break;
}
}
// Backup + save
if (file_exists(JSON_DATA_PATH)) copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
flock($fp, LOCK_UN);
}
fclose($fp);
header('Location: edit_projects.php?status=deleted');
exit;
} elseif (isset($_POST['reorder_projects'])) {
// Réorganisation des projets
$order = json_decode($_POST['project_order'], true);
if (is_array($order)) {
$reordered = [];
foreach ($order as $pid) {
foreach ($data['projets'] as $p) {
if ($p['id'] == $pid) {
$reordered[] = $p;
break;
}
}
}
$data['projets'] = $reordered;
if (file_exists(JSON_DATA_PATH)) copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
flock($fp, LOCK_UN);
}
fclose($fp);
}
header('Location: edit_projects.php?status=saved');
exit;
} elseif (isset($_POST['add_global_tag'])) {
$new_tag = trim($_POST['new_tag']);
if (!empty($new_tag) && !in_array($new_tag, $data['tagsDisponibles'])) {
$data['tagsDisponibles'][] = $new_tag;
sort($data['tagsDisponibles']);
if (file_exists(JSON_DATA_PATH)) copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
flock($fp, LOCK_UN);
}
fclose($fp);
}
header('Location: edit_projects.php?status=saved');
exit;
} elseif (isset($_POST['delete_global_tag'])) {
$tag_to_delete = trim($_POST['tag_to_delete']);
$key = array_search($tag_to_delete, $data['tagsDisponibles']);
if ($key !== false) {
array_splice($data['tagsDisponibles'], $key, 1);
if (file_exists(JSON_DATA_PATH)) copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
flock($fp, LOCK_UN);
}
fclose($fp);
}
header('Location: edit_projects.php?status=saved');
exit;
} else {
// Création ou Mise à jour
$is_new = ($id === -1);
$tags = isset($_POST['selected_tags']) ? (array)$_POST['selected_tags'] : [];
// Find existing project to preserve media formats (especially for YouTube videos)
$old_project = null;
if (!$is_new) {
foreach ($data['projets'] as $p) {
if ($p['id'] == $id) {
$old_project = $p;
break;
}
}
}
$galerie = [];
if (isset($_POST['galerie_path'])) {
foreach ($_POST['galerie_path'] as $i => $path) {
if (!empty(trim($path))) {
$type = $_POST['galerie_type'][$i] ?? 'image';
// Look up if this item already has a format (e.g. portrait) in the loaded DB to preserve it
$existing_format = '';
if ($old_project && isset($old_project['galerie'])) {
foreach ($old_project['galerie'] as $existing_item) {
if ($existing_item[0] === trim($path) && isset($existing_item[3])) {
$existing_format = $existing_item[3];
}
}
}
// If it is a local image, check the actual file dimensions to auto-detect portrait/landscape
if ($type === 'image') {
$full_img_path = IMAGES_UPLOAD_DIR . preg_replace('/^images\//', '', trim($path));
if (file_exists($full_img_path)) {
$sizes = @getimagesize($full_img_path);
if ($sizes) {
if ($sizes[1] > $sizes[0]) {
$existing_format = 'portrait';
} else {
$existing_format = ''; // reset to landscape/standard
}
}
}
}
$item_arr = [trim($path), trim($_POST['galerie_caption'][$i] ?? ''), $type];
if (!empty($existing_format)) {
$item_arr[] = $existing_format;
}
$galerie[] = $item_arr;
}
}
}
$dossier = [];
if (isset($_POST['dossier_type'])) {
foreach ($_POST['dossier_type'] as $i => $type) {
if (!empty(trim($type))) {
$dossier[] = [
'type' => trim($type),
'titre' => trim($_POST['dossier_titre'][$i] ?? ''),
'texte' => trim($_POST['dossier_texte'][$i] ?? ''),
'image1' => trim($_POST['dossier_image1'][$i] ?? ''),
'alt1' => trim($_POST['dossier_alt1'][$i] ?? ''),
'legende1' => trim($_POST['dossier_legende1'][$i] ?? ''),
'image2' => trim($_POST['dossier_image2'][$i] ?? ''),
'alt2' => trim($_POST['dossier_alt2'][$i] ?? ''),
'legende2' => trim($_POST['dossier_legende2'][$i] ?? ''),
'accent' => trim($_POST['dossier_accent'][$i] ?? '')
];
}
}
}
// Sanitize projetId (slug)
$projetId = trim($_POST['projetId']);
$projetId = preg_replace('/[^a-z0-9-]/', '-', strtolower($projetId));
$projetId = preg_replace('/-+/', '-', trim($projetId, '-'));
$project_data = [
'projetId' => $projetId,
'image' => trim($_POST['image']),
'alt' => trim($_POST['alt'] ?? ''),
'titre' => trim($_POST['titre']),
'tags' => array_values($tags),
'categorie' => trim($_POST['categorie'] ?? 'perso'),
'importance' => isset($_POST['importance']) ? (int)$_POST['importance'] : 5,
'annee' => trim($_POST['annee'] ?? ''),
'brief' => trim($_POST['brief'] ?? ''),
'sousTitre' => trim($_POST['sousTitre']),
'date' => trim($_POST['date']),
'role' => trim($_POST['role']),
'technos' => trim($_POST['technos'] ?? ''),
'concept' => trim($_POST['concept']),
'defis' => trim($_POST['defis'] ?? ''),
'resultats' => trim($_POST['resultats'] ?? ''),
'seo_title' => trim($_POST['seo_title'] ?? ''),
'seo_description' => trim($_POST['seo_description'] ?? ''),
'iframe' => trim($_POST['iframe'] ?? ''),
'youtubeId' => trim($_POST['youtubeId'] ?? ''),
'site' => [
'url' => trim($_POST['site_url'] ?? ''),
'label' => trim($_POST['site_label'] ?? '')
],
'status' => trim($_POST['status'] ?? 'published'),
'galerie' => $galerie,
'dossier' => $dossier
];
// Normaliser : si pas d'URL, garder un objet vide mais cohérent
if (empty($project_data['site']['url'])) {
$project_data['site'] = ['url' => '', 'label' => ''];
}
if ($is_new) {
$max_id = 0;
foreach ($data['projets'] as $p) {
if ($p['id'] > $max_id) $max_id = $p['id'];
}
$project_data['id'] = $max_id + 1;
$data['projets'][] = $project_data;
$id = $project_data['id'];
} else {
foreach ($data['projets'] as &$p) {
if ($p['id'] == $id) {
$project_data['id'] = $id;
$p = $project_data;
break;
}
}
}
if (file_exists(JSON_DATA_PATH)) copy(JSON_DATA_PATH, JSON_DATA_PATH . '.bak');
$fp = fopen(JSON_DATA_PATH, 'c');
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
flock($fp, LOCK_UN);
}
fclose($fp);
header('Location: edit_projects.php?id='.$id.'&status=saved');
exit;
}
}
$is_creating = isset($_GET['action']) && $_GET['action'] === 'new';
if ($editing_id !== null && !$is_creating) {
foreach ($data['projets'] as $p) {
if ($p['id'] == $editing_id) {
$project_to_edit = $p;
break;
}
}
} else if ($is_creating) {
$next_id = 1;
if (!empty($data['projets'])) {
foreach ($data['projets'] as $proj) {
if ($proj['id'] >= $next_id) $next_id = $proj['id'] + 1;
}
}
$project_to_edit = [
'id' => -1, 'projetId' => '', 'image' => '', 'alt' => '',
'titre' => '', 'tags' => [], 'categorie' => 'perso', 'importance' => 5, 'annee' => '', 'brief' => '', 'sousTitre' => '',
'date' => '', 'role' => '', 'technos' => '', 'concept' => '',
'defis' => '', 'resultats' => '', 'iframe' => '',
'youtubeId' => '', 'site' => ['url' => '', 'label' => ''], 'status' => 'published', 'galerie' => [], 'dossier' => []
];
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gérer Projets - Admin Portfolio</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="save-progress" id="save-progress"></div>
<div class="admin-layout">
<?php $active_page = 'projects'; require_once 'sidebar.php'; ?>
<main class="main-content">
<header class="header-actions">
<h1><?php echo $project_to_edit ? "Modifier : " . htmlspecialchars($project_to_edit['titre'] ?: 'Nouveau Projet') : "Mes Projets"; ?></h1>
<?php if ($project_to_edit): ?>
<div style="display:flex; gap: 1rem;">
<?php if ($project_to_edit['id'] !== -1): ?>
<a href="export_project.php?id=<?php echo $project_to_edit['id']; ?>&format=json" class="btn btn-secondary">📥 Exporter JSON</a>
<a href="export_project.php?id=<?php echo $project_to_edit['id']; ?>&format=zip" class="btn btn-secondary">📦 Exporter ZIP</a>
<?php endif; ?>
<a href="edit_projects.php" class="btn btn-secondary">← Retour aux Projets</a>
</div>
<?php else: ?>
<div style="display:flex; gap: 1rem;">
<button type="button" class="btn btn-secondary" onclick="document.getElementById('import-modal').style.display='flex'">📥 Importer Projet</button>
<a href="edit_projects.php?action=new" class="btn btn-primary">+ Nouveau Projet</a>
</div>
<?php endif; ?>
</header>
<?php if (!$project_to_edit): ?>
<!-- Vue liste avec réorganisation -->
<form method="POST" id="reorder-form">
<?php echo csrf_field(); ?>
<input type="hidden" name="reorder_projects" value="1">
<input type="hidden" name="project_order" id="project-order" value="">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
<p style="color: var(--text-muted); font-size: 0.9rem;">Glissez les cartes pour réorganiser l'ordre des projets sur le portfolio.</p>
<button type="submit" class="btn btn-primary" id="save-order-btn" style="display:none;">💾 Sauvegarder l'ordre</button>
</div>
</form>
<div style="display: flex; gap: 1rem; margin-bottom: 1.5rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem;">
<button class="btn btn-secondary tab-btn active" onclick="switchTab('published')">Publiés</button>
<button class="btn btn-secondary tab-btn" onclick="switchTab('draft')">Brouillons</button>
<button class="btn btn-secondary tab-btn" onclick="switchTab('archived')">Archives</button>
</div>
<style>
.tab-content { display: none; }
.tab-content.active { display: block; }
.tab-btn.active { background: var(--accent-color); color: white; border-color: var(--accent-color); }
.status-badge {
position: absolute; top: 10px; right: 10px;
padding: 0.2rem 0.6rem; border-radius: 4px; font-size: 0.8rem; font-weight: bold; color: white;
}
.status-draft { background: #f59e0b; }
.status-archived { background: #6b7280; }
</style>
<?php
$grouped = ['published' => [], 'draft' => [], 'archived' => []];
foreach ($data['projets'] as $p) {
$status = $p['status'] ?? 'published';
if (!isset($grouped[$status])) $status = 'published';
$grouped[$status][] = $p;
}
?>
<?php foreach (['published', 'draft', 'archived'] as $status_key): ?>
<div id="tab-<?php echo $status_key; ?>" class="tab-content <?php echo $status_key === 'published' ? 'active' : ''; ?>">
<div class="card-grid" id="projects-grid-<?php echo $status_key; ?>">
<?php foreach ($grouped[$status_key] as $p): ?>
<div class="card drag-item" draggable="true" data-id="<?php echo $p['id']; ?>" style="position: relative; text-decoration: none; color: inherit; display: block; cursor: grab;">
<?php if ($status_key === 'draft'): ?>
<div class="status-badge status-draft">Brouillon</div>
<?php elseif ($status_key === 'archived'): ?>
<div class="status-badge status-archived">Archivé</div>
<?php endif; ?>
<a href="edit_projects.php?id=<?php echo $p['id']; ?>" style="text-decoration: none; color: inherit;">
<img src="<?php echo ASSETS_BASE_URL . htmlspecialchars($p['image']); ?>" style="width: 100%; height: 160px; object-fit: cover; border-radius: 8px; margin-bottom: 1rem;" onerror="this.src='data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><rect width=\'100%\' height=\'100%\' fill=\'%23333\'/></svg>'" alt="<?php echo htmlspecialchars($p['alt'] ?? ''); ?>">
<h3 style="margin: 0; padding-right: 60px;"><?php echo htmlspecialchars($p['titre']); ?></h3>
<p style="font-size: 0.85rem; margin-top: 0.5rem; opacity: 0.7;"><?php echo htmlspecialchars($p['date']); ?> — <?php echo implode(', ', array_map('htmlspecialchars', $p['tags'] ?? [])); ?></p>
<div style="display: flex; justify-content: space-between; align-items: center; margin-top: 0.5rem; font-size: 0.8rem; opacity: 0.9; border-top: 1px solid rgba(255,255,255,0.05); padding-top: 0.5rem;">
<span style="background: rgba(255,255,255,0.06); padding: 0.2rem 0.5rem; border-radius: 4px;">
<?php
$cat = $p['categorie'] ?? 'perso';
if ($cat === 'universitaire') echo '🎓 Uni';
elseif ($cat === 'pro') echo '💼 Pro';
else echo '🎨 Perso';
?>
</span>
<span style="color: #eab308; font-weight: bold;">⭐ <?php echo isset($p['importance']) ? (int)$p['importance'] : 5; ?>/10</span>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
<?php if (empty($grouped[$status_key])): ?>
<p style="color: var(--text-muted); padding: 2rem; text-align: center; background: rgba(0,0,0,0.2); border-radius: 8px;">Aucun projet dans cette catégorie.</p>
<?php endif; ?>
</div>
<?php endforeach; ?>
<script>
function switchTab(tabId) {
document.querySelectorAll('.tab-content').forEach(el => el.classList.remove('active'));
document.querySelectorAll('.tab-btn').forEach(el => el.classList.remove('active'));
document.getElementById('tab-' + tabId).classList.add('active');
event.target.classList.add('active');
}
</script>
<div class="card" style="margin-top: 2rem;">
<h3 style="margin-bottom: 1rem;">🏷️ Gestion des Tags globaux</h3>
<p style="color: var(--text-muted); font-size: 0.9rem; margin-bottom: 1.5rem;">Ajoutez ou supprimez des tags de la liste globale. Ces tags seront sélectionnables lors de la modification de vos projets.</p>
<form method="POST" style="display: flex; gap: 1rem; margin-bottom: 1.5rem; max-width: 500px;">
<?php echo csrf_field(); ?>
<input type="text" name="new_tag" placeholder="Nouveau tag (ex: 3D, Illustration)" required style="flex: 1;">
<button type="submit" name="add_global_tag" class="btn btn-primary">Ajouter</button>
</form>
<div style="display: flex; flex-wrap: wrap; gap: 0.5rem;">
<?php foreach ($data['tagsDisponibles'] as $tag): ?>
<div class="tag" style="display: inline-flex; align-items: center; gap: 0.5rem; padding: 0.4rem 0.8rem; font-size: 0.85rem; border-radius: 20px;">
<span><?php echo htmlspecialchars($tag); ?></span>
<form method="POST" style="display: inline;" onsubmit="return confirm('Supprimer le tag "<?php echo htmlspecialchars($tag); ?>" de la liste globale ?');">
<?php echo csrf_field(); ?>
<input type="hidden" name="tag_to_delete" value="<?php echo htmlspecialchars($tag); ?>">
<button type="submit" name="delete_global_tag" style="background: none; border: none; color: var(--danger-color); cursor: pointer; font-weight: bold; font-size: 1rem; padding: 0 0.2rem;" title="Supprimer de la liste globale">×</button>
</form>
</div>
<?php endforeach; ?>
</div>
</div>
<?php else: ?>
<form id="project-form" action="edit_projects.php" method="POST">
<?php echo csrf_field(); ?>
<input type="hidden" name="id" value="<?php echo $project_to_edit['id']; ?>">
<div class="card" style="margin-bottom: 2rem;">
<h3>Informations Principales</h3>
<div class="form-row" style="display:flex; gap:1.5rem; margin-top: 1rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Titre</label>
<input type="text" name="titre" value="<?php echo htmlspecialchars($project_to_edit['titre']); ?>" required>
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Identifiant URL (projetId)</label>
<input type="text" name="projetId" value="<?php echo htmlspecialchars($project_to_edit['projetId']); ?>" placeholder="ex: mon-projet" required>
<small style="color: var(--text-muted);">Sera auto-slugifié (lettres minuscules, chiffres, tirets)</small>
</div>
</div>
<div class="form-row" style="display:flex; gap:1.5rem; margin-top: 1rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Catégorie du Projet</label>
<select name="categorie" required>
<option value="universitaire" <?php echo (isset($project_to_edit['categorie']) && $project_to_edit['categorie'] === 'universitaire') ? 'selected' : ''; ?>>🎓 Projet Universitaire</option>
<option value="pro" <?php echo (isset($project_to_edit['categorie']) && $project_to_edit['categorie'] === 'pro') ? 'selected' : ''; ?>>💼 Projet Professionnel</option>
<option value="perso" <?php echo (!isset($project_to_edit['categorie']) || $project_to_edit['categorie'] === 'perso') ? 'selected' : ''; ?>>🎨 Projet Personnel</option>
</select>
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Statut</label>
<select name="status" required>
<option value="published" <?php echo ((!isset($project_to_edit['status'])) || $project_to_edit['status'] === 'published') ? 'selected' : ''; ?>>✅ Publié (Visible)</option>
<option value="draft" <?php echo ((isset($project_to_edit['status'])) && $project_to_edit['status'] === 'draft') ? 'selected' : ''; ?>>📝 Brouillon (Caché)</option>
<option value="archived" <?php echo ((isset($project_to_edit['status'])) && $project_to_edit['status'] === 'archived') ? 'selected' : ''; ?>>🗄️ Archivé (Caché)</option>
</select>
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Score d'Importance (Poids)</label>
<select name="importance" required>
<?php
$current_importance = isset($project_to_edit['importance']) ? (int)$project_to_edit['importance'] : 5;
for ($i = 10; $i >= 1; $i--):
$selected = ($current_importance === $i) ? 'selected' : '';
echo "<option value=\"$i\" $selected>⭐ $i/10 " . ($i >= 8 ? '(Très important)' : ($i >= 5 ? '(Moyen)' : '(Faible)')) . "</option>";
endfor;
?>
</select>
</div>
</div>
<div class="form-group" style="margin-top: 1rem;">
<label>Sous-titre / En-tête</label>
<input type="text" name="sousTitre" value="<?php echo htmlspecialchars($project_to_edit['sousTitre']); ?>" placeholder="Phrase d'accroche">
</div>
<div class="form-row" style="display:flex; gap:1.5rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Date</label>
<input type="text" name="date" value="<?php echo htmlspecialchars($project_to_edit['date']); ?>" placeholder="ex: Janvier 2026">
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Rôle</label>
<input type="text" name="role" value="<?php echo htmlspecialchars($project_to_edit['role']); ?>" placeholder="ex: Développeur Front-end">
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Année (pour les filtres & tris)</label>
<input type="text" name="annee" value="<?php echo htmlspecialchars($project_to_edit['annee'] ?? ''); ?>" placeholder="ex: 2026" required>
</div>
</div>
<div class="form-row" style="display:flex; gap:1.5rem; margin-top: 1rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Technologies & Outils</label>
<input type="text" name="technos" id="technos-input" value="<?php echo htmlspecialchars($project_to_edit['technos'] ?? ''); ?>" placeholder="ex: React, Node.js">
<div class="software-picker" style="margin-top: 0.5rem; display: flex; flex-wrap: wrap; gap: 0.5rem; background: rgba(0, 0, 0, 0.2); padding: 0.8rem; border-radius: 8px; border: 1px solid var(--border-color); max-height: 180px; overflow-y: auto;">
<?php
$current_technos = array_map('trim', explode(',', $project_to_edit['technos'] ?? ''));
foreach (($data['logos'] ?? []) as $logo):
$logo_title = $logo['titre'];
$logo_path = ASSETS_BASE_URL . $logo['chemin'];
$is_selected = in_array($logo_title, $current_technos);
?>
<div class="software-badge"
data-name="<?php echo htmlspecialchars($logo_title); ?>"
style="display: flex; align-items: center; gap: 0.4rem; padding: 0.3rem 0.6rem; border-radius: 20px;
background: <?php echo $is_selected ? 'rgba(var(--accent-rgb), 0.2)' : 'rgba(255,255,255,0.05)'; ?>;
border: 1px solid <?php echo $is_selected ? 'var(--accent-color)' : 'rgba(255,255,255,0.1)'; ?>;
color: <?php echo $is_selected ? 'var(--accent-color)' : 'var(--text-color)'; ?>;
cursor: pointer; user-select: none; font-size: 0.85rem; transition: all 0.2s;"
onclick="toggleSoftware(this)">
<img src="<?php echo htmlspecialchars($logo_path); ?>" style="width: 16px; height: 16px; object-fit: contain;" alt="">
<span><?php echo htmlspecialchars($logo_title); ?></span>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Tags du Projet (Sélectionnez les tags applicables)</label>
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 0.5rem; background: rgba(0, 0, 0, 0.4); padding: 1rem; border-radius: 8px; border: 1px solid var(--border-color); max-height: 150px; overflow-y: auto;">
<?php
$project_tags = $project_to_edit['tags'] ?? [];
foreach ($data['tagsDisponibles'] as $tag):
$checked = in_array($tag, $project_tags) ? 'checked' : '';
?>
<label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer; color: var(--text-color); font-weight: normal; margin-bottom: 0; font-size: 0.85rem;">
<input type="checkbox" name="selected_tags[]" value="<?php echo htmlspecialchars($tag); ?>" <?php echo $checked; ?> style="width: auto; height: auto; -webkit-appearance: checkbox; appearance: checkbox;">
<?php echo htmlspecialchars($tag); ?>
</label>
<?php endforeach; ?>
</div>
<small style="color: var(--text-muted); display: block; margin-top: 5px;">Les tags disponibles se gèrent sur la page principale des projets.</small>
</div>
</div>
</div>
<div class="card" style="margin-bottom: 2rem;">
<h3>Médias & URL</h3>
<div class="form-row" style="display:flex; gap:1.5rem; margin-top: 1rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Image de couverture (Chemin)</label>
<div style="display: flex; gap: 0.5rem; align-items: center; flex: 1;">
<input type="text" name="image" value="<?php echo htmlspecialchars($project_to_edit['image']); ?>" placeholder="ex: images/p1/cover.webp" required style="flex: 1;" id="input-cover">
<button type="button" class="btn btn-secondary" onclick="openMediaModal(document.getElementById('input-cover'), '<?php echo $project_to_edit['id'] !== -1 ? $project_to_edit['id'] : ($next_id ?? ''); ?>')" style="padding: 0.6rem; border-radius: 6px;" title="Choisir/Uploader depuis la médiathèque" aria-label="Ouvrir la médiathèque">🖼️</button>
<?php if ($project_to_edit['image']): ?>
<img src="<?php echo ASSETS_BASE_URL . htmlspecialchars($project_to_edit['image']); ?>" class="preview-img" style="width: 46px; height: 46px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border-color);" alt="Preview">
<?php endif; ?>
</div>
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Texte alternatif (SEO)</label>
<input type="text" name="alt" value="<?php echo htmlspecialchars($project_to_edit['alt'] ?? ''); ?>" placeholder="Description de l'image">
</div>
</div>
<div class="form-row" style="display:flex; gap:1.5rem;">
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Lien du site (URL)</label>
<?php
$site_url = is_array($project_to_edit['site']) ? ($project_to_edit['site']['url'] ?? '') : '';
$site_label = is_array($project_to_edit['site']) ? ($project_to_edit['site']['label'] ?? '') : '';
?>
<input type="text" name="site_url" value="<?php echo htmlspecialchars($site_url); ?>" placeholder="https://...">
</div>
<div class="form-group" style="flex:1; margin-bottom:0;">
<label>Texte du bouton (url)</label>
<input type="text" name="site_label" value="<?php echo htmlspecialchars($site_label); ?>" placeholder="ex: Voir le site">
</div>
</div>
<div class="form-group">
<label>Vidéo YouTube (ID optionnel)</label>
<input type="text" name="youtubeId" value="<?php echo htmlspecialchars($project_to_edit['youtubeId'] ?? ''); ?>" placeholder="ex: dQw4w9WgXcQ">
</div>
<input type="hidden" name="iframe" value="<?php echo htmlspecialchars($project_to_edit['iframe'] ?? ''); ?>">
</div>
<!-- Analyse de Perf -->
<div style="margin-bottom: 2rem;">
<div class="card">
<h3>⚡ Analyse de Performance Globale</h3>
<div id="perf-metrics" style="margin-top: 1rem;">
<div id="perf-results" style="font-size: 0.9rem;">
<p style="color: var(--text-muted); font-style: italic;">En attente d'une image de couverture...</p>
</div>
</div>
</div>
</div>
<!-- Structure du Dossier Académique (Blocs Composés) -->
<div class="card" style="margin-bottom: 2rem; border: 1px solid rgba(88, 166, 255, 0.2);">
<h3 style="color: #58a6ff; display: flex; align-items: center; gap: 10px;">📚 Dossier Académique (Blocs Composés)</h3>
<p style="color: var(--text-muted); font-size: 0.9rem; margin-bottom: 1.5rem;">
Créez un dossier complet de projet universitaire en combinant librement textes, consignes de l'enseignant, captures d'écrans et présentations d'images côte à côte.
</p>
<!-- Sélecteur d'ajout de bloc -->
<div style="background: rgba(255,255,255,0.03); border: 1px dashed rgba(255,255,255,0.1); border-radius: 8px; padding: 1rem; margin-bottom: 1.5rem; display: flex; flex-direction: column; gap: 1rem;">
<div style="display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center;">
<span style="font-size: 0.85rem; font-weight: bold; color: var(--text-muted);">📝 STRUCTURE :</span>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('text')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Texte Simple</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('consigne')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Consigne (Encadré)</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('brief')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(1,180,241,0.5);">📋 Le Brief</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('concept')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(245,158,11,0.5);">💡 Concept</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('defis')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(16,185,129,0.5);">⚙️ Défis</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('resultats')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(139,92,246,0.5);">🏆 Résultats</button>
</div>
<div style="display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center;">
<span style="font-size: 0.85rem; font-weight: bold; color: var(--text-muted);">🖼️ MÉDIAS :</span>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('text_image')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Texte G. / Image D.</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('image_text')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Image G. / Texte D.</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('full_image')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Image Large</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('two_images')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">2 Images</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('code')" style="padding: 0.5rem 1rem; font-size: 0.85rem;">Code & Explication</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('mood_board')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(236, 72, 153, 0.5);">🎨 Mood Board</button>
<button type="button" class="btn btn-secondary" onclick="addDossierBlock('before_after')" style="padding: 0.5rem 1rem; font-size: 0.85rem; border-color: rgba(6, 182, 212, 0.5);">↔️ Avant / Après</button>
</div>
</div>
<!-- Conteneur des blocs -->
<div id="dossier-blocks-container" class="dossier-container">
<!-- Les blocs dynamiques seront insérés ici en Javascript -->
</div>
</div>
<div class="card" style="margin-bottom: 2rem; border: 1px solid rgba(var(--accent-rgb), 0.3);">
<h3 style="color: var(--accent-color); margin-bottom: 1.5rem;">🔍 Optimisation SEO</h3>
<div class="form-group">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px;">
<label style="margin-bottom: 0;">Titre SEO (Balise Title)</label>
<button type="button" class="btn btn-secondary btn-ai-magic" onclick="magicWand(this, 'seo_title')" style="padding: 4px 10px; font-size: 0.75rem;">🪄 Magic SEO</button>
</div>
<input type="text" name="seo_title" value="<?php echo htmlspecialchars($project_to_edit['seo_title'] ?? ''); ?>" placeholder="Le titre qui apparaîtra sur Google...">
</div>
<div class="form-group">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px;">
<label style="margin-bottom: 0;">Méta Description</label>
<button type="button" class="btn btn-secondary btn-ai-magic" onclick="magicWand(this, 'seo_description')" style="padding: 4px 10px; font-size: 0.75rem;">🪄 Magic Meta</button>
</div>
<textarea name="seo_description" rows="2" placeholder="Le petit résumé sous le titre dans les résultats Google..."><?php echo htmlspecialchars($project_to_edit['seo_description'] ?? ''); ?></textarea>
</div>
</div>
<div class="card" style="margin-bottom: 2rem; background: linear-gradient(135deg, rgba(29, 161, 242, 0.05), rgba(10, 102, 194, 0.05));">
<h3 style="margin-bottom: 1.5rem;">📱 Partage sur les Réseaux</h3>
<div class="social-generator-ui" style="background: rgba(255,255,255,0.03); border-radius: 12px; padding: 1.5rem;">
<p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 1rem;">Générez un post prêt à l'emploi pour vos réseaux preferés :</p>
<div style="display: flex; gap: 0.5rem; margin-bottom: 1.5rem;">
<button type="button" class="btn btn-secondary" onclick="generateSocial('LinkedIn')" style="flex: 1;">LinkedIn</button>
<button type="button" class="btn btn-secondary" onclick="generateSocial('Twitter')" style="flex: 1;">Twitter / X</button>
<button type="button" class="btn btn-secondary" onclick="generateSocial('Instagram')" style="flex: 1;">Instagram</button>
</div>
<div id="social-result-box" style="display: none;">
<label style="font-size: 0.8rem; color: var(--accent-color); font-weight: bold;">Post généré (<span id="social-platform-name"></span>)</label>
<textarea id="social-content" rows="6" style="margin-top: 5px; background: rgba(0,0,0,0.2); font-family: inherit; font-size: 0.9rem;"></textarea>
<div style="display: flex; gap: 10px; margin-top: 10px;">
<button type="button" class="btn btn-primary" onclick="copyAndOpenSocial()" style="flex: 2;">📋 Copier & Publier</button>
<button type="button" class="btn btn-secondary" onclick="document.getElementById('social-result-box').style.display='none'" style="flex: 1;">Fermer</button>
</div>
</div>
</div>
</div>
<div class="card" style="margin-bottom: 2rem;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
<h3>Galerie Multimédia</h3>
<div style="display: flex; gap: 0.5rem;">
<button type="button" class="btn btn-secondary" onclick="addGalleryItem()">🖼️ + Image</button>
<button type="button" class="btn btn-secondary" onclick="addGalleryVideo()">🎬 + Vidéo ID</button>
</div>
</div>
<p style="font-size: 0.9rem; color: var(--text-muted); margin-bottom: 1.5rem;">Glissez et déposez les lignes pour modifier l'ordre des images.</p>
<div class="gallery-container" id="gallery-container">
<?php foreach ($project_to_edit['galerie'] as $i => $img):
$media_type = $img[2] ?? 'image';
$is_video = ($media_type === 'youtube');
?>
<div class="drag-item gallery-item <?php echo $is_video ? 'type-video' : ''; ?>" draggable="true">
<div style="cursor: grab; color: #8b949e; padding: 0 0.5rem;" aria-label="Déplacer">☰</div>
<div style="display: flex; flex-direction: column; gap: 4px;">
<button type="button" class="btn-ctrl" onclick="moveUp(this)" style="padding: 2px 6px;" aria-label="Monter">↑</button>
<button type="button" class="btn-ctrl" onclick="moveDown(this)" style="padding: 2px 6px;" aria-label="Descendre">↓</button>
</div>
<?php if ($is_video): ?>
<div class="preview-img video-placeholder" style="display: flex; align-items: center; justify-content: center; background: #000; color: #ff0000; font-size: 1.5rem; border-radius: 6px;">▶️</div>
<?php else: ?>
<img src="<?php echo ASSETS_BASE_URL . htmlspecialchars($img[0]); ?>" class="preview-img" onerror="this.src='data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><rect width=\'100%\' height=\'100%\' fill=\'%23333\'/></svg>'" alt="Gallery image">
<?php endif; ?>
<div class="gallery-item-inputs">
<input type="hidden" name="galerie_type[]" value="<?php echo htmlspecialchars($media_type); ?>">
<div style="display: flex; gap: 0.2rem; flex: 1;">
<input type="text" name="galerie_path[]" value="<?php echo htmlspecialchars($img[0]); ?>" placeholder="<?php echo $is_video ? 'ID YouTube (ex: SlMKf02ZsMU)' : 'Chemin (ex: images/p1/01.webp)'; ?>">
<?php if (!$is_video): ?>
<button type="button" class="btn btn-secondary" onclick="openMediaModal(this.previousElementSibling, '<?php echo $project_to_edit['id'] !== -1 ? $project_to_edit['id'] : ($next_id ?? ''); ?>')" style="padding: 0.6rem;" title="Médiathèque" aria-label="Ouvrir la médiathèque">🖼️</button>
<?php endif; ?>
</div>
<input type="text" name="galerie_caption[]" value="<?php echo htmlspecialchars($img[1]); ?>" placeholder="Légende" style="flex: 1;">
</div>
<button type="button" class="btn-remove" onclick="this.closest('.gallery-item').remove(); updatePerformance();" aria-label="Supprimer">×</button>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="glass-panel" style="padding: 1.5rem; display: flex; gap: 1rem; justify-content: space-between; position: sticky; bottom: 20px; z-index: 100;">
<div>
<button type="submit" class="btn btn-primary" style="font-size: 1.1rem; padding: 1rem 2rem;">Enregistrer le projet</button>
</div>
<?php if ($project_to_edit['id'] !== -1): ?>
<button type="submit" name="delete_project" class="btn btn-danger" onclick="return confirm('Êtes-vous sûr de vouloir supprimer définitivement ce projet ? Cette action est irréversible.');">Supprimer ce projet</button>
<?php endif; ?>
</div>
</form>
<?php endif; ?>
<?php require_once 'footer.php'; ?>
</main>
</div>
<?php require_once 'media_modal.php'; ?>
<!-- Modale d'import -->
<div id="import-modal" style="display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.8); z-index: 1000; align-items: center; justify-content: center;">
<div class="card" style="width: 90%; max-width: 600px; position: relative;">
<button onclick="document.getElementById('import-modal').style.display='none'" style="position: absolute; top: 1rem; right: 1rem; background: none; border: none; color: white; cursor: pointer; font-size: 1.5rem;">×</button>
<h3>📥 Importer un projet</h3>
<div style="margin-top: 1.5rem;">
<h4>Option 1 : Importer un fichier ZIP</h4>
<p style="font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem;">Uploadez un fichier ZIP exporté depuis ce panel pour restaurer le JSON et les images.</p>
<form action="import_project.php" method="POST" enctype="multipart/form-data" style="display: flex; gap: 1rem; align-items: center;">
<?php echo csrf_field(); ?>
<input type="file" name="import_zip" accept=".zip" required style="flex: 1;">
<button type="submit" class="btn btn-primary">Importer le ZIP</button>
</form>
</div>
<hr style="border: 0; border-top: 1px solid rgba(255,255,255,0.1); margin: 2rem 0;">
<div>
<h4>Option 2 : Coller le code JSON</h4>
<p style="font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem;">Collez le code JSON brut d'un projet. Les images associées devront être uploadées manuellement si elles n'existent pas.</p>
<form action="import_project.php" method="POST">
<?php echo csrf_field(); ?>
<textarea name="import_json" rows="6" style="width: 100%; margin-bottom: 1rem; font-family: monospace; font-size: 0.85rem;" placeholder='{"projetId": "mon-projet", ...}' required></textarea>
<button type="submit" class="btn btn-primary">Importer le JSON</button>
</form>
</div>
</div>
</div>
<script src="js/dragdrop.js"></script>
<script src="js/admin.js"></script>
<script>
// Init drag-and-drop for gallery
<?php if ($project_to_edit): ?>
const galleryDD = initDragDrop('gallery-container');
<?php endif; ?>
// Init drag-and-drop for project reordering
<?php if (!$project_to_edit): ?>
const grids = ['projects-grid-published', 'projects-grid-draft', 'projects-grid-archived'];
grids.forEach(id => {
const grid = document.getElementById(id);
if (grid) {
initDragDropGrid(id);
grid.addEventListener('dragend', () => {
document.getElementById('save-order-btn').style.display = '';
const ids = [];
grids.forEach(gid => {
const g = document.getElementById(gid);
if(g) {
[...g.querySelectorAll('.drag-item')].forEach(el => ids.push(el.dataset.id));
}
});
document.getElementById('project-order').value = JSON.stringify(ids);
});
}
});
<?php endif; ?>
function addGalleryItem() {
const container = document.getElementById('gallery-container');
const div = document.createElement('div');
div.className = 'drag-item gallery-item';
div.draggable = true;
div.innerHTML = `
<div style="cursor: grab; color: #8b949e; padding: 0 0.5rem;" aria-label="Déplacer">☰</div>
<div style="display: flex; flex-direction: column; gap: 4px;">
<button type="button" class="btn-ctrl" onclick="moveUp(this)" style="padding: 2px 6px;" aria-label="Monter">↑</button>
<button type="button" class="btn-ctrl" onclick="moveDown(this)" style="padding: 2px 6px;" aria-label="Descendre">↓</button>
</div>
<div style="width: 60px; height: 40px; background: #333; border-radius: 4px;"></div>
<div class="gallery-item-inputs">
<input type="hidden" name="galerie_type[]" value="image">
<div style="display: flex; gap: 0.2rem; flex: 1;">
<input type="text" name="galerie_path[]" placeholder="images/p/img.webp">
<button type="button" class="btn btn-secondary" onclick="openMediaModal(this.previousElementSibling, '<?php echo $project_to_edit ? ($project_to_edit['id'] !== -1 ? $project_to_edit['id'] : ($next_id ?? '')) : ''; ?>')" style="padding: 0.6rem;" title="Médiathèque" aria-label="Ouvrir la médiathèque">🖼️</button>
</div>
<input type="text" name="galerie_caption[]" placeholder="Légende" style="flex: 1;">
</div>
<button type="button" class="btn-remove" onclick="this.closest('.gallery-item').remove(); updatePerformance();" aria-label="Supprimer">×</button>
`;
container.appendChild(div);
if (typeof galleryDD !== 'undefined') galleryDD.bindEvents(div);
}
function addGalleryVideo() {
const container = document.getElementById('gallery-container');
const div = document.createElement('div');
div.className = 'drag-item gallery-item type-video';
div.draggable = true;
div.innerHTML = `
<div style="cursor: grab; color: #8b949e; padding: 0 0.5rem;" aria-label="Déplacer">☰</div>
<div style="display: flex; flex-direction: column; gap: 4px;">
<button type="button" class="btn-ctrl" onclick="moveUp(this)" style="padding: 2px 6px;" aria-label="Monter">↑</button>
<button type="button" class="btn-ctrl" onclick="moveDown(this)" style="padding: 2px 6px;" aria-label="Descendre">↓</button>
</div>
<div class="preview-img video-placeholder" style="display: flex; align-items: center; justify-content: center; background: #000; color: #ff0000; font-size: 1.5rem; border-radius: 6px;">▶️</div>
<div class="gallery-item-inputs">
<input type="hidden" name="galerie_type[]" value="youtube">
<div style="display: flex; gap: 0.2rem; flex: 1;">
<input type="text" name="galerie_path[]" placeholder="ID YouTube (ex: dQw4w9WgXcQ)">
</div>
<input type="text" name="galerie_caption[]" placeholder="Titre de la vidéo" style="flex: 1;">
</div>
<button type="button" class="btn-remove" onclick="this.closest('.gallery-item').remove(); updatePerformance();" aria-label="Supprimer">×</button>
`;
container.appendChild(div);
if (typeof galleryDD !== 'undefined') galleryDD.bindEvents(div);
}
const inputCover = document.getElementById('input-cover');
const perfResults = document.getElementById('perf-results');
const updatePerformance = async () => {
const coverPath = inputCover.value.trim();
const galleryPaths = Array.from(document.querySelectorAll('input[name="galerie_path[]"]')).map(i => i.value.trim());
const allPaths = [coverPath, ...galleryPaths].filter(p => p !== '');
if (allPaths.length === 0) {
perfResults.innerHTML = '<p style="color: var(--text-muted); font-style: italic;">En attente de médias...</p>';
return;
}
perfResults.innerHTML = '<span style="color:var(--text-muted)">Analyse globale...</span>';
try {
const res = await fetch(`api_media.php`);
const allMedia = await res.json();
let totalSize = 0;
let webpCount = 0;
let missingCount = 0;
let foundPaths = [];
allPaths.forEach(path => {
const normPath = path.replace(/^\//, '').replace(/\\/g, '/');
const file = allMedia.find(m => {
const mPath = m.path.replace(/^\//, '').replace(/\\/g, '/');
return mPath === normPath || mPath === 'images/' + normPath;
});
if (file) {
totalSize += file.size;
if (file.path.endsWith('.webp')) webpCount++;
foundPaths.push(file);
} else {
missingCount++;
}
});
const totalMb = (totalSize / (1024 * 1024)).toFixed(2);
const avgSizeKb = foundPaths.length > 0 ? Math.round((totalSize / foundPaths.length) / 1024) : 0;
const statusColor = (totalMb > 5) ? 'var(--danger-color)' : (totalMb > 2 ? 'var(--warning-color)' : 'var(--success-color)');
const statusLabel = (totalMb > 5) ? '⚠️ TRÈS LOURD' : (totalMb > 2 ? '⚡ MOYEN' : '✅ LÉGER');
perfResults.innerHTML = `
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-bottom: 15px;">
<div style="background: rgba(0,0,0,0.1); padding: 12px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.05);">
<div style="font-size: 0.75rem; text-transform: uppercase; opacity: 0.6;">Poids Total</div>
<div style="font-size: 1.3rem; font-weight: 800; color: ${statusColor}">${totalMb} MB</div>
<div style="font-size: 0.7rem; font-weight: bold; margin-top: 4px;">${statusLabel}</div>
</div>
<div style="background: rgba(0,0,0,0.1); padding: 12px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.05);">
<div style="font-size: 0.75rem; text-transform: uppercase; opacity: 0.6;">Optimisation</div>
<div style="font-size: 1.3rem; font-weight: 800;">${Math.round((webpCount / allPaths.length) * 100) || 0}%</div>
<div style="font-size: 0.7rem; opacity: 0.8; margin-top: 4px;">${webpCount}/${allPaths.length} WebP (${allPaths.length} images)</div>
</div>
</div>
<div style="font-size: 0.8rem; line-height: 1.4;">
<div style="margin-bottom: 5px;">📍 <strong>Moyenne par image :</strong> ${avgSizeKb} KB</div>
${missingCount > 0 ? `<div style="color: var(--warning-color); margin-bottom: 5px;">❓ <strong>${missingCount} fichier(s)</strong> non trouvés (en attente d'upload ?)</div>` : ''}
<div style="background: rgba(var(--accent-rgb), 0.1); padding: 8px; border-radius: 4px; margin-top: 10px; border-left: 2px solid var(--accent-color);">
${totalMb > 3 ? '💡 Conseil : Réduisez le nombre d\'images ou utilisez des WebP plus compressés.' : '✨ Ton projet est parfaitement optimisé pour le web !'}
</div>
</div>
`;
} catch(e) {
perfResults.innerHTML = 'Erreur lors du calcul global.';
console.error(e);
}
};
// Ecouter les changements sur la couverture et la galerie
if (inputCover) {
inputCover.addEventListener('input', updatePerformance);
}
// Délégation d'événements pour les inputs de la galerie (car ils peuvent être ajoutés dynamiquement)
document.addEventListener('input', (e) => {
if (e.target && e.target.name === 'galerie_path[]') {
updatePerformance();
}
});
// Trigger initial
if (inputCover || document.querySelector('input[name="galerie_path[]"]')) {
setTimeout(updatePerformance, 600);
}
async function magicWand(btn, field) {
const container = btn.closest('.form-group');
const target = container.querySelector('textarea, input[type="text"]');
// On récupère TOUT le contexte possible du formulaire pour aider l'IA
const titre = document.querySelector('input[name="titre"]')?.value || '';
const sousTitre = document.querySelector('input[name="sousTitre"]')?.value || '';
const concept = document.querySelector('textarea[name="concept"]')?.value || '';
const brief = document.querySelector('textarea[name="brief"]')?.value || '';
const tags = Array.from(document.querySelectorAll('input[name="selected_tags[]"]:checked')).map(el => el.value).join(', ');
const technos = document.querySelector('input[name="technos"]')?.value || '';
let context = `Projet: ${titre}. Sous-titre: ${sousTitre}. Tags: ${tags}. Technos: ${technos}.
Contenu actuel du champ: ${target.value.trim()}.
Consigne/Brief: ${brief}.
Concept/Recherche: ${concept}`;
const originalBtnText = btn.innerHTML;
btn.innerHTML = '✨ Génération...';
btn.disabled = true;
try {
const response = await fetch(`api_ai.php?action=generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ field: field, prompt: context })
});
const data = await response.json();
if (data.status === 'success') {
let cleanText = data.generated_text.replace(/[\*#_]/g, '');
target.value = cleanText;
target.style.border = "1px solid var(--success-color)";
setTimeout(() => { target.style.border = ""; }, 2000);
} else {
let detailMsg = '';
if (data.details && data.details.error && data.details.error.message) {
detailMsg = '\n\nDétails : ' + data.details.error.message;
}
alert("Erreur : " + data.message + detailMsg);
}
} catch (e) {
console.error(e);
alert("Erreur de connexion avec l'IA.");
} finally {
btn.innerHTML = originalBtnText;
btn.disabled = false;
}
}
async function magicWandDossier(btn, type, isTitle = false) {
const container = btn.closest('.form-group');
const target = container.querySelector('textarea, input[type="text"]');
if (!target) return;
// On récupère le contexte du projet principal pour aider l'IA
const titre = document.querySelector('input[name="titre"]')?.value || '';
const sousTitre = document.querySelector('input[name="sousTitre"]')?.value || '';
const tags = Array.from(document.querySelectorAll('input[name="selected_tags[]"]:checked')).map(el => el.value).join(', ');
const technos = document.querySelector('input[name="technos"]')?.value || '';
let context = `Projet: ${titre}. Sous-titre: ${sousTitre}. Tags: ${tags}. Technos: ${technos}.`;
// S'il y a un titre de section dans ce bloc (lorsqu'on génère la description)
if (!isTitle) {
const sectionTitre = container.closest('.dossier-item').querySelector('input[name="dossier_titre[]"]')?.value || '';
if (sectionTitre) {
context += ` Section: ${sectionTitre}.`;
}
}
if (target.value.trim()) {
context += ` Base actuelle du champ: ${target.value.trim()}`;
}
const originalBtnText = btn.innerHTML;
btn.innerHTML = '✨...';
btn.disabled = true;
// Déterminer la consigne spécifique selon qu'on veut un titre ou un texte
let aiField = type;
let finalPrompt = context;
if (isTitle) {