Skip to content

Commit 4c99dc5

Browse files
author
erika
committed
Refactor: Change catalogue toggle from GET to POST method
I suspected the flash messages to not work here either and I was right. I also removed the text-warning class because somehow with the get method it didn't work and the icon was still red. (And in yellow it's weird ?)
1 parent f1c0321 commit 4c99dc5

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

public/main/admin/course_list.php

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -269,24 +269,23 @@ function get_course_data(
269269
]);
270270

271271
$isInCatalogue = null !== $record;
272-
$catalogueUrl = api_get_self().'?toggle_catalogue='.$course['id'].'&sec_token='.Security::getTokenFromSession();
273272

274-
$actions[] = Display::url(
275-
Display::getMdiIcon(
276-
$isInCatalogue ? StateIcon::CATALOGUE_OFF : StateIcon::CATALOGUE_ON,
277-
'ch-tool-icon',
278-
null,
279-
ICON_SIZE_SMALL,
280-
$isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue'),
281-
[
282-
'class' => $isInCatalogue ? 'text-warning' : 'text-muted',
283-
]
284-
),
285-
$catalogueUrl,
286-
[
287-
'title' => $isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue'),
288-
]
289-
);
273+
$actions[] = '
274+
<form method="post" style="display:inline;">
275+
<input type="hidden" name="action" value="toggle_catalogue">
276+
<input type="hidden" name="course_id" value="' . $course['id'] . '">
277+
<input type="hidden" name="sec_token" value="' . Security::getTokenFromSession() . '">
278+
<button type="submit" class="btn btn-link p-0 text-decoration-none cursor-pointer" title="' .
279+
($isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue')) . '">
280+
' . Display::getMdiIcon(
281+
$isInCatalogue ? StateIcon::CATALOGUE_OFF : StateIcon::CATALOGUE_ON,
282+
'ch-tool-icon',
283+
null,
284+
ICON_SIZE_SMALL,
285+
$isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue')
286+
) . '
287+
</button>
288+
</form>';
290289

291290
$courseItem = [
292291
$course['col0'],
@@ -376,41 +375,41 @@ function get_course_visibility_icon(int $visibility): string
376375
Display::addFlash(Display::return_message(get_lang('Deleted')));
377376
}
378377
}
379-
}
380-
381-
if (isset($_GET['toggle_catalogue']) && Security::check_token('get')) {
382-
$courseId = (int) $_GET['toggle_catalogue'];
383-
$accessUrlId = api_get_current_access_url_id();
384-
$em = Database::getManager();
385-
$repo = $em->getRepository(CatalogueCourseRelAccessUrlRelUsergroup::class);
386-
$course = api_get_course_entity($courseId);
387-
$accessUrl = $em->getRepository(AccessUrl::class)->find($accessUrlId);
388378

389-
if ($course && $accessUrl) {
390-
$record = $repo->findOneBy([
391-
'course' => $course,
392-
'accessUrl' => $accessUrl,
393-
'usergroup' => null,
394-
]);
379+
// Toggle catalogue
380+
if ('toggle_catalogue' == $_POST['action']) {
381+
$courseId = (int) $_POST['course_id'];
382+
$accessUrlId = api_get_current_access_url_id();
383+
$em = Database::getManager();
384+
$repo = $em->getRepository(CatalogueCourseRelAccessUrlRelUsergroup::class);
385+
$course = api_get_course_entity($courseId);
386+
$accessUrl = $em->getRepository(AccessUrl::class)->find($accessUrlId);
387+
388+
if ($course && $accessUrl) {
389+
$record = $repo->findOneBy([
390+
'course' => $course,
391+
'accessUrl' => $accessUrl,
392+
'usergroup' => null,
393+
]);
394+
395+
if ($record) {
396+
$em->remove($record);
397+
Display::addFlash(Display::return_message(get_lang('Removed from catalogue')));
398+
} else {
399+
$newRel = new CatalogueCourseRelAccessUrlRelUsergroup();
400+
$newRel->setCourse($course);
401+
$newRel->setAccessUrl($accessUrl);
402+
$newRel->setUsergroup(null);
403+
404+
$em->persist($newRel);
405+
Display::addFlash(Display::return_message(get_lang('Added to catalogue'), 'success'));
406+
}
395407

396-
if ($record) {
397-
$em->remove($record);
398-
Display::addFlash(Display::return_message(get_lang('Removed from catalogue')));
399-
} else {
400-
$newRel = new CatalogueCourseRelAccessUrlRelUsergroup();
401-
$newRel->setCourse($course);
402-
$newRel->setAccessUrl($accessUrl);
403-
$newRel->setUsergroup(null);
404-
405-
$em->persist($newRel);
406-
Display::addFlash(Display::return_message(get_lang('Added to catalogue'), 'success'));
408+
$em->flush();
407409
}
408-
409-
$em->flush();
410410
}
411-
412-
api_location(api_get_self());
413411
}
412+
414413
$content = '';
415414
$message = '';
416415
$actions = '';

0 commit comments

Comments
 (0)