From ad956bef8ebb56a1eb418387d11fa4eee8362f03 Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Fri, 12 Apr 2024 14:38:01 +0200 Subject: [PATCH] adds pagination logic to C4 news --- application/controllers/CisHmvc/Cms.php | 7 ++- application/controllers/CisHtml/Cms.php | 8 ++- application/controllers/CisVue/Cms.php | 7 ++- application/libraries/CmsLib.php | 36 ++++++------- application/models/content/News_model.php | 66 +++++++++++++---------- 5 files changed, 70 insertions(+), 54 deletions(-) diff --git a/application/controllers/CisHmvc/Cms.php b/application/controllers/CisHmvc/Cms.php index 7f2a03a0f..d15f398a4 100644 --- a/application/controllers/CisHmvc/Cms.php +++ b/application/controllers/CisHmvc/Cms.php @@ -1,6 +1,7 @@ cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar); + $page = intval($this->input->get('page', true)); + $pagination_size = 10; + $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $pagination_size); if (isError($news)) return $this->load->view('CisHmvc/Error', ['error' => getError($news)]); diff --git a/application/controllers/CisHtml/Cms.php b/application/controllers/CisHtml/Cms.php index 96057559f..2dd05dbae 100644 --- a/application/controllers/CisHtml/Cms.php +++ b/application/controllers/CisHtml/Cms.php @@ -1,6 +1,7 @@ cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar); + $page = intval($this->input->get('page', true)); + $pagination_size = 10; + $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $pagination_size); if (isError($news)) return $this->load->view('CisHtml/Error', ['error' => getError($news)]); diff --git a/application/controllers/CisVue/Cms.php b/application/controllers/CisVue/Cms.php index 96057559f..0f3fc061a 100644 --- a/application/controllers/CisVue/Cms.php +++ b/application/controllers/CisVue/Cms.php @@ -1,6 +1,7 @@ cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar); + $page = intval($this->input->get('page', true)); + $pagination_size = 10; + $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $pagination_size); if (isError($news)) return $this->load->view('CisHtml/Error', ['error' => getError($news)]); diff --git a/application/libraries/CmsLib.php b/application/libraries/CmsLib.php index 4e27b94af..dfe8d5e4f 100644 --- a/application/libraries/CmsLib.php +++ b/application/libraries/CmsLib.php @@ -1,6 +1,7 @@ ci->ContentgruppeModel->loadWhere(['content_id' => $content_id]); if (isError($islocked)) return $islocked; - + if (getData($islocked)) { $uid = getAuthUID(); $isberechtigt = $this->ci->ContentgruppeModel->berechtigt($content_id, $uid); @@ -72,11 +73,9 @@ class CmsLib return $content; // Legt einen Logeintrag für die Klickstatistik an - if (defined('LOG_CONTENT') && LOG_CONTENT) - { + if (defined('LOG_CONTENT') && LOG_CONTENT) { // Nur eingeloggte User werden geloggt, das sonst auch alle Infoscreenaufrufe und dgl. mitgeloggt werden - if (isLogged()) - { + if (isLogged()) { $request_data = 'content_id=' . $content_id; if ($version !== null) $request_data .= '&version=' . $version; @@ -185,13 +184,12 @@ class CmsLib * * @return void */ - public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true) + public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true, $page = 1, $page_size = 10) { $this->ci->load->model('crm/Student_model', 'StudentModel'); $this->ci->load->model('organisation/Studiengang_model', 'StudiengangModel'); - if (!$infoscreen && ($studiengang_kz === null || $semester === null)) - { + if (!$infoscreen && ($studiengang_kz === null || $semester === null)) { //Zum anzeigen der Studiengang-Details neben den News $student = $this->ci->StudentModel->loadWhere(['student_uid' => get_uid()]); if (isError($student)) @@ -209,7 +207,7 @@ class CmsLib $xml = ''; $this->ci->load->model('content/News_model', 'NewsModel'); - $news = $this->ci->NewsModel->getNewsWithContent(getSprache(), $studiengang_kz, $semester, null, $sichtbar, 0, 0, $all, $mischen); + $news = $this->ci->NewsModel->getNewsWithContent(getSprache(), $studiengang_kz, $semester, null, $sichtbar, 0, $page, $page_size, $all, $mischen); if (isError($news)) return $news; @@ -225,17 +223,14 @@ class CmsLib $xml .= "" . $newsobj->content . $datum . $id . ""; } - if ($studiengang_kz != 0) - { + if ($studiengang_kz != 0) { $stg_obj = $this->ci->StudiengangModel->load($studiengang_kz); if (isError($stg_obj)) return $stg_obj; $stg_obj = current(getData($stg_obj) ?: []); - if ($stg_obj) - { - if (!$edit && !$infoscreen) - { + if ($stg_obj) { + if (!$edit && !$infoscreen) { $extras = $this->getNewsExtras($stg_obj, $semester); if (isError($extras)) return $extras; @@ -245,8 +240,7 @@ class CmsLib } } - if ($titel != '') - { + if ($titel != '') { $xml .= '' . $titel . ''; } @@ -260,7 +254,7 @@ class CmsLib $XML = new DOMDocument(); $XML->loadXML($xml); - + $xsltemplate = new DOMDocument(); $xsltemplate->loadXML($template->xslt_xhtml_c4); diff --git a/application/models/content/News_model.php b/application/models/content/News_model.php index 3199a0ac3..51ef16053 100644 --- a/application/models/content/News_model.php +++ b/application/models/content/News_model.php @@ -23,10 +23,21 @@ class News_model extends DB_Model text IS NOT NULL AND datum <= NOW() AND (datum_bis IS NULL OR datum_bis >= now()::date) ORDER BY datum DESC - LIMIT '. $this->escape($limit) + LIMIT ' . $this->escape($limit) ); } + private function getMaxPageCount($page_size) + { + $this->addSelect(['Count(*)']); + $count = $this->load(); + $count = hasData($count) ? getData($count)[0]->count : null; + $floatMaxPageCount = $count / $page_size; + + // ceil, to include remaining rows into the last page + return ceil($floatMaxPageCount); + } + /** * @param string $sprache * @param string $studiengang_kz @@ -34,7 +45,7 @@ class News_model extends DB_Model * @param string $fachbereich_kurzbz * @param boolean $sichtbar * @param integer $maxalter - * @param integer $maxnews + * @param integer $page_size * @param boolean $all * @param boolean $mischen * @@ -42,46 +53,52 @@ class News_model extends DB_Model * * @return stdObj */ - public function getNewsWithContent($sprache, $studiengang_kz, $semester, $fachbereich_kurzbz = null, $sichtbar = true, $maxalter = 0, $maxnews = 0, $all = false, $mischen = true) + public function getNewsWithContent($sprache, $studiengang_kz, $semester, $fachbereich_kurzbz = null, $sichtbar = true, $maxalter = 0, $page = 1, $page_size = 10, $all = false, $mischen = true) { + if (isset($page) && is_numeric($page) && isset($page_size) && is_numeric($page_size)) { + if ($page > 0 && $page_size > 0) { + $maxPageCount = $this->getMaxPageCount($page_size); + if ($maxPageCount) { + $page = $page % $maxPageCount; + } + $offset = $page * $page_size; + $this->addLimit($page_size, $offset); + } + } else { + $this->addLimit($page_size); + } + + $this->addOrder('datum', 'DESC'); + $studiengang_kz = trim($studiengang_kz); $fachbereich_kurzbz = trim($fachbereich_kurzbz); $where = []; $params = []; - if (trim($maxalter) != '0') - { + if (trim($maxalter) != '0') { $where[] = "(now()-datum) < interval ? days"; $params[] = $maxalter; } - if (!$all) - { + if (!$all) { $where[] = "datum <= now()"; $where[] = "(datum_bis >= now()::date OR datum_bis IS NULL)"; } - if ($fachbereich_kurzbz != '*') - { - if ($fachbereich_kurzbz == '') - { + if ($fachbereich_kurzbz != '*') { + if ($fachbereich_kurzbz == '') { $where[] = "fachbereich_kurzbz IS NULL"; - } - else - { + } else { $where[] = "fachbereich_kurzbz = ?"; $params[] = $fachbereich_kurzbz; } } - if ($studiengang_kz == '0') - { + if ($studiengang_kz == '0') { $where[] = "studiengang_kz = ?"; $params[] = $studiengang_kz; if ($semester === NULL) $where[] = "semester IS NULL"; elseif ($semester === 0) $where[] = "semester = 0"; - } - elseif ($studiengang_kz != '') - { + } elseif ($studiengang_kz != '') { $add = $mischen === true ? " OR (studiengang_kz = 0 AND semester IS NULL)" : ""; $where[] = "((studiengang_kz = ? AND semester = ?) OR (studiengang_kz = ? AND semester = 0) OR (studiengang_kz = 0 AND semester = ?)" . $add . ")"; $params[] = $studiengang_kz; @@ -90,14 +107,14 @@ class News_model extends DB_Model $params[] = $semester; } $this->addJoin('campus.tbl_contentsprache cs', 'content_id'); - + $where[] = "cs.sichtbar = " . ($sichtbar ? "true" : "false"); - + $where[] = "cs.sprache = (CASE WHEN EXISTS(SELECT 1 FROM campus.tbl_contentsprache cs2 WHERE cs2.content_id=" . $this->dbTable . ".content_id AND sprache=?) THEN ? ELSE ? END)"; $params[] = $sprache; $params[] = $sprache; $params[] = DEFAULT_LANGUAGE; - + $where[] = "cs.version = (SELECT MAX(version) FROM campus.tbl_contentsprache cs3 WHERE cs3.content_id=" . $this->dbTable . ".content_id AND cs3.sprache = (CASE WHEN EXISTS(SELECT 1 FROM campus.tbl_contentsprache cs2 WHERE cs2.content_id=" . $this->dbTable . ".content_id AND sprache=?) THEN ? ELSE ? END))"; $params[] = $sprache; $params[] = $sprache; @@ -105,11 +122,6 @@ class News_model extends DB_Model $where = implode(" AND ", $where); - $this->addOrder('datum', 'DESC'); - if ($maxnews) - $this->addLimit($maxnews); - - $this->db->where($where, NULL, FALSE); $sql = $this->db->get_compiled_select($this->dbTable);