From 87bc659c2a8bd56043dfac71eb1fc4cdf8c8151a Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 10 Sep 2018 14:21:39 +0200 Subject: [PATCH] Move system announcements as a controller - Use symfony twig to render tpls - Remove double code --- config/packages/sonata_page.yaml | 2 + index.php | 3 +- main/inc/lib/system_announcements.lib.php | 30 +++---- main/inc/lib/userportal.lib.php | 45 ----------- .../default/announcement/view.html.twig | 16 ---- news_list.php | 31 ------- src/CoreBundle/Controller/IndexController.php | 6 +- src/CoreBundle/Controller/NewsController.php | 81 +++++++++++++++++++ src/CoreBundle/Framework/PageController.php | 49 ----------- .../Resources/views/Index/index.html.twig | 6 +- .../views/Index/userportal.html.twig | 1 - .../Resources/views/News/index.html.twig | 8 ++ .../Resources/views/News}/slider.html.twig | 4 +- .../Resources/views/News/view.html.twig | 24 ++++++ 14 files changed, 139 insertions(+), 167 deletions(-) delete mode 100644 main/template/default/announcement/view.html.twig delete mode 100755 news_list.php create mode 100644 src/CoreBundle/Controller/NewsController.php create mode 100644 src/CoreBundle/Resources/views/News/index.html.twig rename {main/template/default/announcement => src/CoreBundle/Resources/views/News}/slider.html.twig (97%) create mode 100644 src/CoreBundle/Resources/views/News/view.html.twig diff --git a/config/packages/sonata_page.yaml b/config/packages/sonata_page.yaml index d8da62e4a6..0d7bd3ee7a 100644 --- a/config/packages/sonata_page.yaml +++ b/config/packages/sonata_page.yaml @@ -37,6 +37,8 @@ sonata_page: - ^/js/(.*) - ^/faq/(.*) - ^/faq + - ^/news + - ^/news/(.*) - ^/courses/(.*) - ^/editor/(.*) - ^/resource/(.*) diff --git a/index.php b/index.php index fbc05e9659..4f40df6317 100755 --- a/index.php +++ b/index.php @@ -145,14 +145,15 @@ if (!isset($_REQUEST['include'])) { if (api_get_setting('show_hot_courses') == 'true') { $hotCourses = $controller->return_hot_courses(); } + $announcements_block = $controller->return_announcements(); + } if (api_get_configuration_value('show_hot_sessions') === true) { $hotSessions = SessionManager::getHotSessions(); $controller->tpl->assign('hot_sessions', $hotSessions); } $controller->tpl->assign('hot_courses', $hotCourses); -$controller->tpl->assign('announcements_block', $announcements_block); if ($includeFile) { // If we are including a static page, then home_welcome is empty $controller->tpl->assign('home_welcome', ''); diff --git a/main/inc/lib/system_announcements.lib.php b/main/inc/lib/system_announcements.lib.php index 937cfc26c9..a4080002bb 100755 --- a/main/inc/lib/system_announcements.lib.php +++ b/main/inc/lib/system_announcements.lib.php @@ -820,9 +820,9 @@ class SystemAnnouncementManager * @param string $visible see self::VISIBLE_* constants * @param int $id The identifier of the announcement to display * - * @return string + * @return array */ - public static function displayAnnouncementsSlider($visible, $id = null) + public static function getAnnouncements($visible, $id = null) : array { $user_selected_language = Database::escape_string(api_get_interface_language()); $table = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); @@ -837,7 +837,7 @@ class SystemAnnouncementManager $sql .= self::getVisibilityCondition($visible); if (isset($id) && !empty($id)) { - $id = intval($id); + $id = (int) $id; $sql .= " AND id = $id "; } @@ -846,7 +846,7 @@ class SystemAnnouncementManager $sql .= " AND access_url_id IN ('1', '$current_url_id') "; } - $sql .= " ORDER BY date_start DESC"; + $sql .= ' ORDER BY date_start DESC'; $result = Database::query($sql); $announcements = []; @@ -871,14 +871,10 @@ class SystemAnnouncementManager } if (count($announcements) === 0) { - return null; + return []; } - $template = new Template(null, false, false); - $template->assign('announcements', $announcements); - $layout = $template->get_template('announcement/slider.tpl'); - - return $template->fetch($layout); + return $announcements; } /** @@ -887,18 +883,19 @@ class SystemAnnouncementManager * @param int $announcementId The announcement ID * @param int $visibility The announcement visibility * - * @return string The HTML code + * @return array */ - public static function displayAnnouncement($announcementId, $visibility) + public static function getAnnouncement($announcementId, $visibility): array { $selectedUserLanguage = Database::escape_string(api_get_interface_language()); $announcementTable = Database::get_main_table(TABLE_MAIN_SYSTEM_ANNOUNCEMENTS); $now = api_get_utc_datetime(); + $announcementId = (int) $announcementId; $whereConditions = [ "(lang = ? OR lang IS NULL OR lang = '') " => $selectedUserLanguage, "AND (? >= date_start AND ? <= date_end) " => [$now, $now], - "AND id = ? " => intval($announcementId), + "AND id = ? " => $announcementId, ]; $condition = self::getVisibilityCondition($visibility); @@ -918,13 +915,10 @@ class SystemAnnouncementManager 'first' ); - $template = new Template(null, false, false); - $template->assign('announcement', $announcement); - $layout = $template->get_template('announcement/view.tpl'); - - return $template->fetch($layout); + return $announcement; } + /** * @return string */ diff --git a/main/inc/lib/userportal.lib.php b/main/inc/lib/userportal.lib.php index fbcf1505e7..80b17f4fac 100755 --- a/main/inc/lib/userportal.lib.php +++ b/main/inc/lib/userportal.lib.php @@ -91,51 +91,6 @@ class IndexManager } } - /** - * @param bool $show_slide - * - * @return null|string - */ - public function return_announcements($show_slide = true) - { - $hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected'); - $currentUserId = api_get_user_id(); - if ($hideAnnouncements == 'true' && empty($currentUserId)) { - return null; - } - $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null; - $announcement = intval($announcement); - - if (!api_is_anonymous() && $this->user_id) { - $visibility = SystemAnnouncementManager::getCurrentUserVisibility(); - if ($show_slide) { - $announcements = SystemAnnouncementManager::displayAnnouncementsSlider( - $visibility, - $announcement - ); - } else { - $announcements = SystemAnnouncementManager::displayAllAnnouncements( - $visibility, - $announcement - ); - } - } else { - if ($show_slide) { - $announcements = SystemAnnouncementManager::displayAnnouncementsSlider( - SystemAnnouncementManager::VISIBLE_GUEST, - $announcement - ); - } else { - $announcements = SystemAnnouncementManager::displayAllAnnouncements( - SystemAnnouncementManager::VISIBLE_GUEST, - $announcement - ); - } - } - - return $announcements; - } - /** * Alias for the online_logout() function. * diff --git a/main/template/default/announcement/view.html.twig b/main/template/default/announcement/view.html.twig deleted file mode 100644 index 0747b48445..0000000000 --- a/main/template/default/announcement/view.html.twig +++ /dev/null @@ -1,16 +0,0 @@ - - -{% if not announcement is empty %} -
- -
-{% else %} - -{% endif %} diff --git a/news_list.php b/news_list.php deleted file mode 100755 index c5b72ab897..0000000000 --- a/news_list.php +++ /dev/null @@ -1,31 +0,0 @@ -assign( - 'actions', - Display::toolbarAction('toolbar', [$actionEdit]) - ); -} - -$tpl->assign('content', $content); -$tpl->display_one_col_template(); diff --git a/src/CoreBundle/Controller/IndexController.php b/src/CoreBundle/Controller/IndexController.php index 14ab6e44ff..520c17c07c 100644 --- a/src/CoreBundle/Controller/IndexController.php +++ b/src/CoreBundle/Controller/IndexController.php @@ -10,6 +10,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; /** * Class IndexController @@ -34,13 +35,11 @@ class IndexController extends BaseController if ($user) { $userId = $this->getUser()->getId(); } - $announcementsBlock = $pageController->getAnnouncements($userId); return $this->render( '@ChamiloCore/Index/index.html.twig', [ - 'content' => '', - 'announcements_block' => $announcementsBlock, + 'content' => '' //'home_page_block' => $pageController->returnHomePage() ] ); @@ -50,6 +49,7 @@ class IndexController extends BaseController * Toggle the student view action. * * @Route("/toggle_student_view", methods={"GET"}) + * * @Security("has_role('ROLE_TEACHER')") * * @param Request $request diff --git a/src/CoreBundle/Controller/NewsController.php b/src/CoreBundle/Controller/NewsController.php new file mode 100644 index 0000000000..7df68b6b46 --- /dev/null +++ b/src/CoreBundle/Controller/NewsController.php @@ -0,0 +1,81 @@ +. + * @Route("/news") + * + * @package Chamilo\CoreBundle\Controller + */ +class NewsController extends BaseController +{ + /** + * The Chamilo index home page. + * + * @Route("/", name="news_index", methods={"GET", "POST"}, options={"expose"=true}) + * + * @return Response + */ + public function indexAction(Request $request): Response + { + $toolBar = ''; + if ($this->isGranted('ROLE_ADMIN')) { + $actionEdit = \Display::url( + \Display::return_icon('edit.png', get_lang('EditSystemAnnouncement'), [], ICON_SIZE_MEDIUM), + api_get_path(WEB_PATH).'main/admin/system_announcements.php' + ); + $toolBar = \Display::toolbarAction('toolbar', [$actionEdit]); + } + + return $this->render( + '@ChamiloCore/News/index.html.twig', + [ + 'toolbar' => $toolBar + ] + ); + } + + /** + * The Chamilo index home page. + * + * @Route("/{id}", name="news", methods={"GET", "POST"}, options={"expose"=true}) + * + * @return Response + */ + public function newsAction($id = null) + { + $visibility = \SystemAnnouncementManager::getCurrentUserVisibility(); + + $toolBar = ''; + if (empty($id)) { + $content = \SystemAnnouncementManager::getAnnouncements($visibility); + + return $this->render( + '@ChamiloCore/News/slider.html.twig', + [ + 'announcements' => $content + ] + ); + } else { + $content = \SystemAnnouncementManager::getAnnouncement($id, $visibility); + + return $this->render( + '@ChamiloCore/News/view.html.twig', + [ + 'announcement' => $content + ] + ); + } + } +} diff --git a/src/CoreBundle/Framework/PageController.php b/src/CoreBundle/Framework/PageController.php index c73f03efcd..ba4db1b9dd 100644 --- a/src/CoreBundle/Framework/PageController.php +++ b/src/CoreBundle/Framework/PageController.php @@ -157,55 +157,6 @@ class PageController return $html; } - /** - * Returns a list of announcements. - * - * @param int User ID - * @param bool True: show the announcements as a slider. False: show them as a vertical list - * - * @return string HTML list of announcements - */ - public function getAnnouncements($user_id = null, $show_slide = true) - { - // Display System announcements - $hideAnnouncements = api_get_setting('hide_global_announcements_when_not_connected'); - if ($hideAnnouncements == 'true' && empty($user_id)) { - return null; - } - - $announcement = isset($_GET['announcement']) ? $_GET['announcement'] : null; - $announcement = intval($announcement); - - if (!api_is_anonymous() && $user_id) { - $visibility = api_is_allowed_to_create_course() ? SystemAnnouncementManager::VISIBLE_TEACHER : SystemAnnouncementManager::VISIBLE_STUDENT; - if ($show_slide) { - $announcements = SystemAnnouncementManager::displayAnnouncementsSlider( - $visibility, - $announcement - ); - } else { - $announcements = SystemAnnouncementManager::displayAllAnnouncements( - $visibility, - $announcement - ); - } - } else { - if ($show_slide) { - $announcements = SystemAnnouncementManager::displayAnnouncementsSlider( - SystemAnnouncementManager::VISIBLE_GUEST, - $announcement - ); - } else { - $announcements = SystemAnnouncementManager::displayAllAnnouncements( - SystemAnnouncementManager::VISIBLE_GUEST, - $announcement - ); - } - } - - return $announcements; - } - /** * Return the homepage, including announcements. * diff --git a/src/CoreBundle/Resources/views/Index/index.html.twig b/src/CoreBundle/Resources/views/Index/index.html.twig index 013bf6246a..a0dc31dcf9 100644 --- a/src/CoreBundle/Resources/views/Index/index.html.twig +++ b/src/CoreBundle/Resources/views/Index/index.html.twig @@ -4,12 +4,14 @@ {% autoescape false %} {{ render(controller('ChamiloPageBundle:Page:renderPage', {'slug': 'welcome'} )) }} - {{ announcements_block }} + {% if chamilo_settings_get('announcement.hide_global_announcements_when_not_connected') == 'false' %} + {{ render(controller('ChamiloCoreBundle:News:news')) }} + {% endif %} + {{ content }} {% if chamilo_settings_get('display.show_hot_courses') == 'true' %} {# See CourseBlockService.php #} {{ sonata_block_render({'type': 'chamilo_core.block.course'}) }} {% endif %} - {% endautoescape %} {% endblock %} diff --git a/src/CoreBundle/Resources/views/Index/userportal.html.twig b/src/CoreBundle/Resources/views/Index/userportal.html.twig index bb1ebcde03..2c1198163f 100644 --- a/src/CoreBundle/Resources/views/Index/userportal.html.twig +++ b/src/CoreBundle/Resources/views/Index/userportal.html.twig @@ -7,6 +7,5 @@ {% if content is null %} {% include '@ChamiloCore/default/layout/welcome_to_course.html.twig' %} {% endif %} - {% endautoescape %} {% endblock %} diff --git a/src/CoreBundle/Resources/views/News/index.html.twig b/src/CoreBundle/Resources/views/News/index.html.twig new file mode 100644 index 0000000000..e5356da42a --- /dev/null +++ b/src/CoreBundle/Resources/views/News/index.html.twig @@ -0,0 +1,8 @@ +{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %} + +{% block content %} + {% autoescape false %} + {{ toolbar }} + {{ render(controller('ChamiloCoreBundle:News:news')) }} + {% endautoescape %} +{% endblock %} diff --git a/main/template/default/announcement/slider.html.twig b/src/CoreBundle/Resources/views/News/slider.html.twig similarity index 97% rename from main/template/default/announcement/slider.html.twig rename to src/CoreBundle/Resources/views/News/slider.html.twig index a9b4ed06c5..5a441a2dde 100644 --- a/main/template/default/announcement/slider.html.twig +++ b/src/CoreBundle/Resources/views/News/slider.html.twig @@ -1,3 +1,4 @@ +{% autoescape false %} @@ -41,4 +42,5 @@ Next - \ No newline at end of file + +{% endautoescape %} \ No newline at end of file diff --git a/src/CoreBundle/Resources/views/News/view.html.twig b/src/CoreBundle/Resources/views/News/view.html.twig new file mode 100644 index 0000000000..c2bfa32957 --- /dev/null +++ b/src/CoreBundle/Resources/views/News/view.html.twig @@ -0,0 +1,24 @@ +{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %} + +{% block content %} + {% autoescape false %} + + + {% if not announcement is empty %} +
+ +
+ {% else %} + + {% endif %} + {% endautoescape %} +{% endblock %} + +