File size: 22,461 Bytes
a1be761
 
 
 
 
 
50b44f1
 
 
a1be761
 
 
 
50b44f1
a1be761
 
 
 
50b44f1
a1be761
 
 
50b44f1
a1be761
50b44f1
a1be761
 
 
 
 
 
50b44f1
a1be761
 
 
50b44f1
a1be761
 
 
50b44f1
 
 
a1be761
50b44f1
a1be761
 
 
50b44f1
 
 
a1be761
50b44f1
a1be761
 
50b44f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50b44f1
a1be761
 
 
50b44f1
a1be761
 
 
 
 
 
 
 
50b44f1
a1be761
 
 
 
 
 
 
50b44f1
a1be761
50b44f1
a1be761
 
 
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
 
50b44f1
a1be761
 
 
 
 
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
 
50b44f1
 
a1be761
50b44f1
a1be761
50b44f1
a1be761
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
50b44f1
 
 
 
 
 
 
a1be761
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
50b44f1
a1be761
50b44f1
 
 
 
 
a1be761
50b44f1
 
a1be761
 
 
 
 
 
 
 
 
 
 
50b44f1
a1be761
 
50b44f1
a1be761
 
 
 
 
50b44f1
a1be761
 
 
50b44f1
a1be761
 
50b44f1
 
 
 
 
a1be761
 
50b44f1
a1be761
50b44f1
a1be761
 
50b44f1
a1be761
50b44f1
 
 
 
a1be761
50b44f1
a1be761
 
50b44f1
 
 
 
 
 
 
a1be761
 
 
 
 
 
 
 
50b44f1
a1be761
 
50b44f1
a1be761
 
 
50b44f1
 
a1be761
50b44f1
 
 
 
a1be761
 
 
 
 
50b44f1
a1be761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50b44f1
 
 
 
 
 
 
a1be761
50b44f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{% extends 'base.html' %}

{% block title %}Éditer {{ texte.titre }} - Administration{% endblock %}

{% block styles %}
<style>
    /* Quill editor needs some height */
    .ql-container { min-height: 150px; }

    .block-editor {
        position: relative;
        transition: all 0.3s ease;
    }

    .block-ghost {
        opacity: 0.5;
        background: var(--primary-color);
    }

    .block-handle {
        cursor: move;
        color: var(--muted-color);
        padding-right: 10px; /* Add some space next to handle */
    }

    .image-position-example {
        padding: 10px;
        border: 1px dashed var(--border-color);
        margin-top: 10px;
        border-radius: 4px;
    }

    .block-image-container {
        margin-bottom: 15px;
    }

    .image-preview {
        max-height: 150px;
        object-fit: contain;
        border-radius: 4px;
        border: 1px solid var(--border-color);
        background-color: var(--background-color);
    }

    .gallery-image {
        transition: transform 0.2s;
        cursor: pointer;
        border: 1px solid var(--border-color);
        padding: 5px;
        border-radius: 4px;
    }

    .gallery-image:hover {
        transform: scale(1.05);
        border-color: var(--primary-color);
    }

    /* Style adjustments for Quill within the block */
    .block-content-editor {
        background-color: var(--input-bg); /* Match theme */
        border-radius: 4px; /* Match form controls */
        margin-bottom: 5px; /* Space between editor and hidden input if needed */
    }

    /* Style the block editor structure */
    .block-editor {
        background-color: var(--card-bg);
        padding: 20px;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        box-shadow: var(--shadow);
    }
    .block-editor-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 15px;
        padding-bottom: 10px;
        border-bottom: 1px solid var(--border-color);
    }
    .block-editor-title {
        margin: 0;
        font-size: 1.2rem;
        font-weight: 600;
    }
    .delete-block-btn i {
        pointer-events: none; /* Ensure clicking icon triggers button */
    }
    .ml-2 { /* Simple margin helper */
        margin-left: 0.5rem !important;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-3">
        <div class="admin-sidebar">
            <h3 class="mb-3">Administration</h3>
            <ul class="admin-nav">
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.dashboard') }}" class="admin-nav-link">
                        <i class="fas fa-tachometer-alt"></i> Tableau de bord
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.matieres') }}" class="admin-nav-link">
                        <i class="fas fa-book"></i> Matières
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.sous_categories') }}" class="admin-nav-link">
                        <i class="fas fa-list"></i> Sous-catégories
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.textes') }}" class="admin-nav-link active">
                        <i class="fas fa-file-alt"></i> Textes
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.images') }}" class="admin-nav-link">
                        <i class="fas fa-images"></i> Images
                    </a>
                </li>
                <li class="admin-nav-item">
                    <a href="{{ url_for('admin_bp.logout') }}" class="admin-nav-link">
                        <i class="fas fa-sign-out-alt"></i> Déconnexion
                    </a>
                </li>
            </ul>
        </div>
    </div>

    <div class="col-md-9">
        <div class="admin-container">
            <h2 class="admin-title">Éditer la méthodologie : {{ texte.titre }}</h2>

            <div class="mb-4">
                <a href="{{ url_for('admin_bp.textes') }}" class="btn btn-secondary mb-3">
                    <i class="fas fa-arrow-left"></i> Retour à la liste
                </a>
                <a href="{{ url_for('admin_bp.historique', texte_id=texte.id) }}" class="btn btn-info mb-3">
                    <i class="fas fa-history"></i> Voir l'historique
                </a>
            </div>

            <!-- Basic Information Form -->
            <div class="card mb-4">
                <div class="card-header">
                    <h4>Informations de base</h4>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('admin_bp.edit_texte', texte_id=texte.id) }}">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> {# Add CSRF token if using Flask-WTF #}
                        <input type="hidden" name="action" value="update_basic">

                        <div class="form-group mb-3">
                            <label for="titre">Titre</label>
                            <input type="text" class="form-control" id="titre" name="titre" value="{{ texte.titre }}" required>
                        </div>

                        <div class="form-group mb-3">
                            <label for="sous_categorie_id">Sous-catégorie</label>
                            <select class="form-control" id="sous_categorie_id" name="sous_categorie_id" required>
                                {% for sc in sous_categories %}
                                <option value="{{ sc.id }}" {% if sc.id == texte.sous_categorie_id %}selected{% endif %}>
                                    {{ sc.matiere.nom }} - {{ sc.nom }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Mettre à jour les informations
                        </button>
                    </form>
                </div>
            </div>

            <!-- Content Blocks Editor -->
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4>Blocs de contenu</h4>
                    <button id="add-block-button" class="btn btn-success">
                        <i class="fas fa-plus"></i> Ajouter un bloc
                    </button>
                </div>
                <div class="card-body">
                    <p class="text-muted mb-4">
                        Organisez votre contenu en blocs distincts. Chaque bloc peut contenir un titre, du texte riche et une image.
                        Vous pouvez réorganiser les blocs en les faisant glisser (<i class="fas fa-grip-vertical"></i>).
                    </p>

                    <form id="blocks-form" method="POST" action="{{ url_for('admin_bp.edit_texte', texte_id=texte.id) }}">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> {# Add CSRF token if using Flask-WTF #}
                        <input type="hidden" name="action" value="update_blocks">
                        <input type="hidden" id="blocks-data" name="blocks_data" value="">

                        <div id="blocks-container">
                            {% for block in blocks %}
                            <div class="block-editor mb-4" data-block-id="{{ block.id }}">
                                <div class="block-editor-header">
                                    <div class="d-flex align-items-center">
                                        <span class="block-handle"><i class="fas fa-grip-vertical"></i></span>
                                        <h3 class="block-editor-title">Bloc #{{ loop.index }}</h3>
                                    </div>
                                    <div class="block-editor-actions">
                                        <button type="button" class="btn btn-danger btn-sm delete-block-btn" title="Supprimer ce bloc">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </div>
                                </div>
                                <div class="form-group mb-3">
                                    <label for="block-{{ block.id }}-title">Titre du bloc (optionnel)</label>
                                    <input type="text" class="form-control block-title" id="block-{{ block.id }}-title" value="{{ block.title or '' }}">
                                </div>
                                <div class="form-group mb-3">
                                    <label for="quill-editor-{{ block.id }}">Contenu du bloc</label>
                                    <!-- Quill editor container -->
                                    <div id="quill-editor-{{ block.id }}" class="block-content-editor">
                                        {{ block.content|safe if block.content else '' }}
                                    </div>
                                    <!-- Hidden input to store Quill's HTML content -->
                                    <input type="hidden" class="block-content-hidden" id="block-{{ block.id }}-content" value="{{ block.content or '' }}">
                                </div>
                                <div class="form-group">
                                    <label>Image (optionnel)</label>
                                    <div class="d-flex align-items-center mb-2">
                                        <button type="button" class="btn btn-primary btn-sm select-image-btn" {% if block.image %}style="display:none;"{% endif %}>
                                            <i class="fas fa-image"></i> Sélectionner une image
                                        </button>
                                        <button type="button" class="btn btn-warning btn-sm remove-image-btn ml-2" {% if not block.image %}style="display:none;"{% endif %}>
                                            <i class="fas fa-times"></i> Retirer l'image
                                        </button>
                                    </div>
                                    <input type="hidden" class="block-image-id" value="{{ block.image.id if block.image else '' }}">

                                    <div class="block-image-container">
                                        {% if block.image %}
                                        <img src="{{ block.image.src }}" alt="{{ block.image.alt or 'Prévisualisation' }}" class="image-preview">
                                        {% else %}
                                        <img src="" alt="Preview" class="image-preview" style="display:none;">
                                        {% endif %}
                                    </div>

                                    <div class="form-group mt-3 {% if not block.image %}d-none{% endif %} image-position-controls"> {# Hide position if no image #}
                                        <label for="block-{{ block.id }}-image-position">Position de l'image</label>
                                        <select class="form-control image-position-select" id="block-{{ block.id }}-image-position">
                                            <option value="left" {% if block.image_position == 'left' %}selected{% endif %}>Gauche</option>
                                            <option value="right" {% if block.image_position == 'right' %}selected{% endif %}>Droite</option>
                                            <option value="top" {% if block.image_position == 'top' %}selected{% endif %}>Haut</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>

                        <div class="mt-4 text-center">
                            <button type="button" id="save-blocks-button" class="btn btn-primary btn-lg">
                                <i class="fas fa-save"></i> Enregistrer les blocs
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Image Upload Section -->
            <div class="card mb-4">
                <div class="card-header">
                    <h4>Ajouter une nouvelle image (Galerie)</h4>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('admin_bp.upload_image_action') }}" enctype="multipart/form-data" id="image-upload-form"> {# Action points to a dedicated route #}
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> {# Add CSRF token if using Flask-WTF #}
                        {# Removed 'action' hidden input, handled by route #}
                        {# Removed texte_id hidden input, not needed for general upload #}

                        <div class="form-group mb-3">
                            <label for="image">Sélectionner une image</label>
                            <input type="file" class="form-control" id="image-file" name="image" accept="image/*" required>
                        </div>

                        <div class="form-group mb-3">
                            <label for="alt_text">Texte alternatif (pour l'accessibilité)</label>
                            <input type="text" class="form-control" id="alt_text" name="alt_text" placeholder="Description de l'image" required>
                        </div>

                         <img id="upload-image-preview" src="#" alt="Image Preview" style="display: none; max-height: 100px; margin-bottom: 10px;"/>


                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-upload"></i> Télécharger et ajouter à la galerie
                        </button>
                    </form>
                    <div id="upload-status" class="mt-3"></div> {# For AJAX feedback #}

                    <div class="mt-3">
                        <a href="{{ url_for('admin_bp.images') }}" class="btn btn-info">
                            <i class="fas fa-images"></i> Gérer la galerie d'images
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Image Gallery Modal -->
<div class="modal fade" id="image-gallery-modal" tabindex="-1" aria-labelledby="imageGalleryModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl"> {# Larger modal for more images #}
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="imageGalleryModalLabel">Sélectionner une image de la galerie</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="image-gallery-content" class="row">
                    {# Images will be loaded here dynamically by JS or initially #}
                    {% for image in images %}
                    <div class="col-lg-2 col-md-3 col-sm-4 col-6 mb-3">
                        <div class="gallery-item text-center" data-image-id="{{ image.id }}">
                            <img src="{{ image.src }}" alt="{{ image.alt or 'Image galerie' }}" class="gallery-image img-fluid">
                            <small class="d-block mt-1 text-muted" style="word-wrap: break-word;">{{ image.alt or '(Sans description)'}}</small>
                        </div>
                    </div>
                    {% else %}
                    <div class="col-12">
                        <div class="alert alert-info">
                            Aucune image disponible dans la galerie. Veuillez en télécharger une via la section ci-dessus ou la page "Images".
                        </div>
                    </div>
                    {% endfor %}
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<!-- Include Sortable.js for drag and drop functionality -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/Sortable.min.js"></script>

<!-- Include Quill.js -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/quill.snow.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quill.js"></script>

<!-- Your Custom Admin JS -->
<script src="{{ url_for('static', filename='js/admin.js') }}"></script>

<script>
// Add specific JS for this page if needed,
// for example, handling the image upload AJAX response.

document.addEventListener('DOMContentLoaded', function() {
    // Show/Hide image position controls based on image presence
    function toggleImagePositionControls(blockEditor) {
        const imageIdInput = blockEditor.querySelector('.block-image-id');
        const positionControls = blockEditor.querySelector('.image-position-controls');
        if (positionControls) {
            if (imageIdInput && imageIdInput.value) {
                positionControls.classList.remove('d-none');
            } else {
                positionControls.classList.add('d-none');
            }
        }
    }

    // Initial check for all blocks
    document.querySelectorAll('.block-editor').forEach(toggleImagePositionControls);

    // Add event listeners to image selection/removal buttons to update controls
    document.querySelectorAll('.select-image-btn, .remove-image-btn').forEach(button => {
        button.addEventListener('click', function() {
            // Need a small delay for the image ID value to potentially update
            setTimeout(() => {
                toggleImagePositionControls(this.closest('.block-editor'));
            }, 50);
        });
    });

    // Update position controls when an image is selected from the gallery
    const galleryModal = document.getElementById('image-gallery-modal');
    if (galleryModal) {
        galleryModal.addEventListener('click', function(event) {
            const galleryItem = event.target.closest('.gallery-item');
            if (galleryItem) {
                 const targetBlockId = galleryModal.getAttribute('data-target-block');
                 const blockEditor = document.querySelector(`.block-editor[data-block-id="${targetBlockId}"]`);
                 if(blockEditor) {
                     // Delay necessary as the image selection logic in admin.js runs first
                     setTimeout(() => {
                         toggleImagePositionControls(blockEditor);
                     }, 100);
                 }
            }
        });
    }

     // Image Upload Preview
    const imageFileInput = document.getElementById('image-file');
    const imagePreview = document.getElementById('upload-image-preview');

    if(imageFileInput && imagePreview) {
        imageFileInput.addEventListener('change', function() {
            if (this.files && this.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    imagePreview.src = e.target.result;
                    imagePreview.style.display = 'block';
                }
                reader.readAsDataURL(this.files[0]);
            } else {
                imagePreview.src = '#';
                imagePreview.style.display = 'none';
            }
        });
    }

    // Optional: AJAX Image Upload Handling (Example)
    const imageUploadForm = document.getElementById('image-upload-form');
    const uploadStatusDiv = document.getElementById('upload-status');

    if (imageUploadForm && uploadStatusDiv) {
        imageUploadForm.addEventListener('submit', function(e) {
            e.preventDefault(); // Prevent default form submission
            uploadStatusDiv.innerHTML = '<div class="alert alert-info">Téléchargement en cours...</div>';
            const formData = new FormData(this);

            fetch(this.action, { // Use the form's action URL
                method: 'POST',
                body: formData,
                headers: {
                    // No 'Content-Type': browser sets it correctly for FormData
                    'X-CSRFToken': this.querySelector('input[name="csrf_token"]').value // Send CSRF token if needed
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    uploadStatusDiv.innerHTML = `<div class="alert alert-success">Image "${data.alt}" ajoutée avec succès !</div>`;
                    // Optionally: Clear the form
                    imageUploadForm.reset();
                    if(imagePreview) imagePreview.style.display = 'none';
                    // Optionally: Refresh the image gallery modal content (more complex)
                    // You might need another fetch call to get updated gallery HTML
                    // or just add the new image data to the modal dynamically.
                    // For simplicity, we'll just notify the user.
                    alert('Image ajoutée à la galerie. Vous pouvez maintenant la sélectionner pour un bloc.');
                } else {
                    uploadStatusDiv.innerHTML = `<div class="alert alert-danger">Erreur: ${data.error || 'Impossible de télécharger l\'image.'}</div>`;
                }
            })
            .catch(error => {
                console.error('Upload error:', error);
                uploadStatusDiv.innerHTML = '<div class="alert alert-danger">Erreur lors du téléchargement. Vérifiez la console.</div>';
            });
        });
    }


});
</script>
{% endblock %}