Skip to content

Commit b85dcb9

Browse files
Merge pull request #7079 from christianbeeznest/fixes-updates186
Course: Improve course picture upload and live preview
2 parents 662b89b + 77e33f5 commit b85dcb9

File tree

1 file changed

+76
-35
lines changed

1 file changed

+76
-35
lines changed

public/main/course_info/infocours.php

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
</div>
8686
</div>';
8787

88-
8988
// Course settings panel (top section of the page)
9089
$courseSettingsPanel = [];
9190

@@ -133,30 +132,42 @@
133132
$showOnlyTheseFields
134133
);
135134

136-
$extraReady = isset($extra['jquery_ready_content']) ? $extra['jquery_ready_content'] : '';
135+
if (!empty($extra['jquery_ready_content'])) {
136+
$htmlHeadXtra[] = $extra['jquery_ready_content'];
137+
}
137138

138-
// JS: hide legacy header, open first panel, init extra-fields and picture preview
139139
$htmlHeadXtra[] = '
140+
<style>
141+
.display-panel-collapse__header {
142+
width: 100%;
143+
}
144+
145+
.display-panel-collapse__header a {
146+
display: flex;
147+
align-items: center;
148+
width: 100%;
149+
gap: .5rem;
150+
}
151+
152+
.display-panel-collapse__header a .mdi {
153+
line-height: 1;
154+
}
155+
</style>
140156
<script>
141157
window.addEventListener("load", function () {
142-
// Init course extra-fields widgets (tags, etc.)
143-
'.$extraReady.'
144-
145158
var form = document.getElementById("update_course");
146159
if (!form) {
147-
console.log("[COURSE SETTINGS] No form with id update_course found");
148160
return;
149161
}
150162
151-
// First new collapsible panel: <div class=\"display-panel-collapse field\">
152163
var firstPanel = form.querySelector(".display-panel-collapse.field");
153-
console.log("[COURSE SETTINGS] First panel found:", !!firstPanel);
154-
155164
if (!firstPanel) {
156165
return;
157166
}
158167
159-
// Hide legacy blocks before the first new panel
168+
// Preview image element
169+
var picturePreview = document.getElementById("course-picture-preview");
170+
160171
var node = firstPanel.previousElementSibling;
161172
var hiddenCount = 0;
162173
@@ -178,28 +189,51 @@
178189
header.classList.remove("mdi-chevron-down");
179190
header.classList.add("mdi-chevron-up");
180191
body.classList.add("active");
181-
console.log("[COURSE SETTINGS] First panel forced open");
182192
} else {
183193
console.log("[COURSE SETTINGS] Header/body not found inside firstPanel");
184194
}
185195
186-
// --- Live preview for course picture ---
187-
var pictureInput = document.getElementById("picture");
188-
var picturePreview = document.getElementById("course-picture-preview");
196+
// Ensure previously saved picture is visible
197+
if (picturePreview && picturePreview.getAttribute("src")) {
198+
picturePreview.style.display = "block";
199+
}
200+
201+
// Live preview for course picture
202+
if (form && picturePreview) {
203+
form.addEventListener("change", function (event) {
204+
var input = event.target;
205+
if (!input || input.tagName !== "INPUT" || input.type !== "file") {
206+
return;
207+
}
189208
190-
if (pictureInput && picturePreview) {
191-
pictureInput.addEventListener("change", function (event) {
192-
var file = event.target.files && event.target.files[0];
193-
if (!file || !file.type || !file.type.match(/^image\\//)) {
209+
var inputName = input.getAttribute("name") || "";
210+
if (inputName.indexOf("picture") === -1) {
211+
return;
212+
}
213+
214+
var file = input.files && input.files[0];
215+
216+
if (!file) {
217+
picturePreview.removeAttribute("src");
218+
picturePreview.style.display = "none";
219+
return;
220+
}
221+
222+
// Only handle image files
223+
if (!file.type || !file.type.match(/^image\\//)) {
194224
return;
195225
}
196226
197227
var reader = new FileReader();
198228
reader.onload = function (e) {
199229
picturePreview.src = e.target.result;
230+
picturePreview.style.display = "block";
231+
picturePreview.removeAttribute("hidden");
200232
};
201233
reader.readAsDataURL(file);
202234
});
235+
} else {
236+
console.log("[COURSE SETTINGS] Form or preview not found, live preview disabled");
203237
}
204238
});
205239
</script>';
@@ -215,28 +249,31 @@
215249
// Course picture (upload + preview + delete)
216250
$allowed_picture_types = api_get_supported_image_extensions(false);
217251

218-
// Current picture preview (if any)
219-
if (!empty($image)) {
220-
// Use addElement("html", ...) so the element belongs to the form
221-
$picturePreviewElement = $form->addElement(
222-
'html',
223-
$image
224-
);
225-
$courseSettingsPanel[] = $picturePreviewElement;
252+
$acceptPictureTypes = '';
253+
if (!empty($allowed_picture_types) && is_array($allowed_picture_types)) {
254+
$acceptPictureTypes = '.'.implode(',.', $allowed_picture_types);
226255
}
227256

228-
// File input for new picture
229257
$pictureElement = $form->addFile(
230258
'picture',
231259
get_lang('Add a picture'),
232260
[
233261
'id' => 'picture',
234262
'class' => 'picture-form',
235263
'crop_image' => true,
264+
'accept' => !empty($acceptPictureTypes) ? $acceptPictureTypes : 'image/*',
236265
]
237266
);
238267
$courseSettingsPanel[] = $pictureElement;
239268

269+
if (!empty($image)) {
270+
$picturePreviewElement = $form->createElement(
271+
'html',
272+
$image
273+
);
274+
$courseSettingsPanel[] = $picturePreviewElement;
275+
}
276+
240277
// Validation rules for picture type
241278
$form->addRule(
242279
'picture',
@@ -1147,14 +1184,17 @@
11471184
? $updateValues['course_registration_password']
11481185
: $courseEntity->getRegistrationCode();
11491186

1187+
$visibility = $updateValues['visibility'] ?? $courseEntity->getVisibility();
1188+
$deletePicture = !empty($updateValues['delete_picture'] ?? null);
1189+
11501190
$request = Container::getRequest();
1151-
/** @var UploadedFile $uploadFile */
1191+
/** @var UploadedFile|null $uploadFile */
11521192
$uploadFile = $request->files->get('picture');
11531193

11541194
// Handle course picture upload / update
11551195
if (null !== $uploadFile) {
1156-
$hasIllustration = $illustrationRepo->hasIllustration($courseEntity);
1157-
if ($hasIllustration) {
1196+
// Replace existing illustration with the new one
1197+
if ($illustrationRepo->hasIllustration($courseEntity)) {
11581198
$illustrationRepo->deleteIllustration($courseEntity);
11591199
}
11601200

@@ -1179,13 +1219,14 @@
11791219
$uploadFile->getFilename()
11801220
);
11811221
}
1182-
}
11831222

1184-
$visibility = $updateValues['visibility'] ?? '';
1185-
$deletePicture = $updateValues['delete_picture'] ?? '';
1223+
$deletePicture = false;
1224+
}
11861225

11871226
if ($deletePicture) {
1188-
$illustrationRepo->deleteIllustration($courseEntity);
1227+
if ($illustrationRepo->hasIllustration($courseEntity)) {
1228+
$illustrationRepo->deleteIllustration($courseEntity);
1229+
}
11891230
}
11901231

11911232
$access_url_id = api_get_current_access_url_id();

0 commit comments

Comments
 (0)