From b350c9da5614b79e32d1d51c63a987dd29b00733 Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Fri, 2 Aug 2024 14:46:22 +0200 Subject: [PATCH] refactors the news controller --- application/controllers/CisHmvc/Cms.php | 42 ------ application/controllers/CisHtml/Cms.php | 45 ------- application/controllers/CisVue/Cms.php | 67 +--------- .../controllers/api/frontend/v1/Cms.php | 121 ++++++++++++------ application/views/CisHtml/Cms/Content.php | 4 +- public/js/api/cms.js | 49 ++++--- public/js/apps/Cis/Cms.js | 4 +- public/js/apps/api/cms.js | 21 --- public/js/apps/api/fhcapifactory.js | 2 - public/js/components/Cis/Cms/News.js | 23 ++-- 10 files changed, 132 insertions(+), 246 deletions(-) delete mode 100644 public/js/apps/api/cms.js diff --git a/application/controllers/CisHmvc/Cms.php b/application/controllers/CisHmvc/Cms.php index d8bb3a3db..fad3366d4 100755 --- a/application/controllers/CisHmvc/Cms.php +++ b/application/controllers/CisHmvc/Cms.php @@ -112,46 +112,4 @@ class Cms extends Auth_Controller { $this->load->view('CisHtml/Cms/Content', ['infoscreen' => $infoscreen, 'studiengang_kz' => $studiengang_kz, 'semester' => $semester, 'mischen' => $mischen, 'titel' => $titel, 'edit' => $edit, 'sichtbar' => $sichtbar, "template_kurzbz"=>"news"]); } - - public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true) - { - $get_page = intval($this->input->get('page', true)); - $get_page_size = intval($this->input->get('page_size', true)); - if ($get_page) { - $page = $get_page; - } - if ($get_page_size) { - $page_size = $get_page_size; - } else { - $page_size = $this->page_size; - } - $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $page_size); - - if (isError($news)) { - $this->terminateWithJsonError(getError($news)); - } - $news = hasData($news) ? getData($news) : null; - if ($news) { - echo json_encode($news); - } else { - show_error("News: No data found"); - } - - } - - public function getNewsRowCount($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $fachbereich_kurzbz = null, $maxalter = 0, $edit = false, $sichtbar = true, $page = 1, $page_size = 10) - { - list($studiengang_kz, $semester) = $this->cmslib->getStgAndSem($studiengang_kz, $semester); - $all = $edit; - $num_rows = $this->NewsModel->countNewsWithContent(getSprache(), $studiengang_kz, $semester, $fachbereich_kurzbz, $sichtbar, $maxalter, $page, $this->page_size, $all, $mischen); - if (isError($num_rows)) { - $this->terminateWithJsonError(getError($num_rows)); - } - $num_rows = hasData($num_rows) ? getData($num_rows) : null; - if ($num_rows) { - echo json_encode($num_rows); - } else { - show_error("News number rows: No data found"); - } - } } diff --git a/application/controllers/CisHtml/Cms.php b/application/controllers/CisHtml/Cms.php index 40126925c..102d0eff4 100755 --- a/application/controllers/CisHtml/Cms.php +++ b/application/controllers/CisHtml/Cms.php @@ -26,10 +26,6 @@ class Cms extends FHC_Controller 'global' ]); - $this->load->model('content/News_model', 'NewsModel'); - - // setting up the papgination_size - $this->page_size = 10; } // ----------------------------------------------------------------------------------------------------------------- @@ -78,45 +74,4 @@ class Cms extends FHC_Controller $this->load->view('CisHtml/Cms/Content', ['infoscreen' => $infoscreen, 'studiengang_kz' => $studiengang_kz, 'semester' => $semester, 'mischen' => $mischen, 'titel' => $titel, 'edit' => $edit, 'sichtbar' => $sichtbar, "template_kurzbz"=>"news"]); } - public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true) - { - $get_page = intval($this->input->get('page', true)); - $get_page_size = intval($this->input->get('page_size', true)); - if ($get_page) { - $page = $get_page; - } - if ($get_page_size) { - $page_size = $get_page_size; - } else { - $page_size = $this->page_size; - } - $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $page_size); - - if (isError($news)) { - $this->terminateWithJsonError(getError($news)); - } - $news = hasData($news) ? getData($news) : null; - if ($news) { - echo json_encode($news); - } else { - show_error("News: No data found"); - } - - } - - public function getNewsRowCount($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $fachbereich_kurzbz = null, $maxalter = 0, $edit = false, $sichtbar = true, $page = 1, $page_size = 10) - { - list($studiengang_kz, $semester) = $this->cmslib->getStgAndSem($studiengang_kz, $semester); - $all = $edit; - $num_rows = $this->NewsModel->countNewsWithContent(getSprache(), $studiengang_kz, $semester, $fachbereich_kurzbz, $sichtbar, $maxalter, $page, $this->page_size, $all, $mischen); - if (isError($num_rows)) { - $this->terminateWithJsonError(getError($num_rows)); - } - $num_rows = hasData($num_rows) ? getData($num_rows) : null; - if ($num_rows) { - echo json_encode($num_rows); - } else { - show_error("News number rows: No data found"); - } - } } diff --git a/application/controllers/CisVue/Cms.php b/application/controllers/CisVue/Cms.php index f4ce98e89..4770a8399 100755 --- a/application/controllers/CisVue/Cms.php +++ b/application/controllers/CisVue/Cms.php @@ -25,8 +25,6 @@ class Cms extends FHC_Controller 'global' ]); - $this->load->model('content/Content_model', 'ContentModel'); - } // ----------------------------------------------------------------------------------------------------------------- @@ -42,35 +40,15 @@ class Cms extends FHC_Controller */ public function content($content_id, $version = null, $sprache = null, $sichtbar = true) { - // return early if the content_id for the content is missing - if(!isset($content_id)) - $this->terminateWithError("content_id is missing"); - $content = $this->ContentModel->load($content_id); - if (isError($content)) - $this->terminateWithError(getError($content)); - - $content = getData($content); - if(NULL === $content) - $this->terminateWithError("Content not found"); - + $content = $this->getDataOrTerminateWithError($content); + $content = current($content); $this->load->view('CisVue/Cms/Content', ['content_id' => $content_id, 'template_kurzbz'=>$content->template_kurzbz , 'version' => $version, 'sprache' => $sprache, 'sichtbar' => $sichtbar]); } - /** - * @param boolean $infoscreen - * @param string | null $studiengang_kz - * @param int | null $semester - * @param boolean $mischen - * @param string $titel - * @param boolean $edit - * @param boolean $sichtbar - * - * @return void - */ /** * @param boolean $infoscreen * @param string | null $studiengang_kz @@ -87,45 +65,4 @@ class Cms extends FHC_Controller $this->load->view('CisHtml/Cms/Content', ['infoscreen' => $infoscreen, 'studiengang_kz' => $studiengang_kz, 'semester' => $semester, 'mischen' => $mischen, 'titel' => $titel, 'edit' => $edit, 'sichtbar' => $sichtbar, "template_kurzbz"=>"news"]); } - public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true) - { - $get_page = intval($this->input->get('page', true)); - $get_page_size = intval($this->input->get('page_size', true)); - if ($get_page) { - $page = $get_page; - } - if ($get_page_size) { - $page_size = $get_page_size; - } else { - $page_size = $this->page_size; - } - $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $page_size); - - if (isError($news)) { - $this->terminateWithJsonError(getError($news)); - } - $news = hasData($news) ? getData($news) : null; - if ($news) { - echo json_encode($news); - } else { - show_error("News: No data found"); - } - - } - - public function getNewsRowCount($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $fachbereich_kurzbz = null, $maxalter = 0, $edit = false, $sichtbar = true, $page = 1, $page_size = 10) - { - list($studiengang_kz, $semester) = $this->cmslib->getStgAndSem($studiengang_kz, $semester); - $all = $edit; - $num_rows = $this->NewsModel->countNewsWithContent(getSprache(), $studiengang_kz, $semester, $fachbereich_kurzbz, $sichtbar, $maxalter, $page, $this->page_size, $all, $mischen); - if (isError($num_rows)) { - $this->terminateWithJsonError(getError($num_rows)); - } - $num_rows = hasData($num_rows) ? getData($num_rows) : null; - if ($num_rows) { - echo json_encode($num_rows); - } else { - show_error("News number rows: No data found"); - } - } } diff --git a/application/controllers/api/frontend/v1/Cms.php b/application/controllers/api/frontend/v1/Cms.php index 50ce50805..5ba635a8a 100644 --- a/application/controllers/api/frontend/v1/Cms.php +++ b/application/controllers/api/frontend/v1/Cms.php @@ -37,8 +37,16 @@ class Cms extends FHCAPI_Controller 'getOrtKurzbzContent' => self::PERM_LOGGED, 'content' => self::PERM_LOGGED, 'news' => self::PERM_LOGGED, + 'getNewsRowCount' => self::PERM_LOGGED, + 'getNews' => self::PERM_LOGGED, + ]); + $this->load->model('content/News_model', 'NewsModel'); + + // setting up the papgination_size + $this->page_size = 10; + $this->load->library('CmsLib'); } @@ -46,19 +54,6 @@ class Cms extends FHCAPI_Controller //------------------------------------------------------------------------------------------------------------------ // Private methods - private function fetchContent($content_id){ - $content = $this->cmslib->getContent($content_id); - - if (isError($content)){ - $this->terminateWithError(getError($content), self::ERROR_TYPE_GENERAL); - } - - if(getData($content)){ - return getData($content); - }else{ - $this->terminateWithError("No content was found", self::ERROR_TYPE_GENERAL); - } - } //------------------------------------------------------------------------------------------------------------------ // Public methods @@ -68,17 +63,21 @@ class Cms extends FHCAPI_Controller */ public function content() { - // getting the get parameters + // form validation + $this->load->library('form_validation'); + $this->form_validation->set_data($_GET); + $this->form_validation->set_rules('content_id','Content ID','required|is_natural'); + if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + + // getting the get parameters $content_id = $this->input->get("content_id",TRUE); $version = $this->input->get("version",TRUE); $sprache = $this->input->get("sprache",TRUE); $sichtbar = $this->input->get("sichtbar",TRUE); - // return early if the content_id is missing - if(!isset($content_id)) - $this->terminateWithError("content_id is missing", self::ERROR_TYPE_GENERAL); + $content = $this->cmslib->getContent($content_id); - $content = $this->fetchContent($content_id); + $content = $this->getDataOrTerminateWithError($content); $this->terminateWithSuccess($content); } @@ -88,57 +87,95 @@ class Cms extends FHCAPI_Controller */ public function ContentID() { - // if error - //$this->terminateWithError(SearchBarLib::ERROR_WRONG_JSON, self::ERROR_TYPE_GENERAL); - + // form validation + $this->load->library('form_validation'); + $this->form_validation->set_data($_GET); + $this->form_validation->set_rules('ort_kurzbz', 'Ort', 'required'); + if ($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + $ort_kurzbz = $this->input->get('ort_kurzbz',TRUE); - if(!$ort_kurzbz){ - $this->terminateWithError("missing ort_kurzbz parameter", self::ERROR_TYPE_GENERAL); - } + $content_id = $this->OrtModel->getContentID($ort_kurzbz); - $result = $this->OrtModel->getContentID($ort_kurzbz); + $content_id = current($this->getDataOrTerminateWithError($content_id))->content_id; - if(isError($result)){ - $this->terminateWithError(getError($result), self::ERROR_TYPE_GENERAL); - } - - $result = hasData($result) ? current(getData($result)) : null; - - $this->terminateWithSuccess($result->content_id ?? NULL); + $this->terminateWithSuccess($content_id); } + //todo: there is the method news and getNews but only one should exist public function news() { + // form validation + $this->load->library('form_validation'); + $this->form_validation->set_data($_GET); + $this->form_validation->set_rules('limit','Limit','required|is_natural_no_zero'); + if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + $this->load->model('content/news_model', 'NewsModel'); $limit = $this->input->get('limit',TRUE); - // return early if the limit parameter is missing or is not greater than 0 - if(!isset($limit) || $limit < 1) - $this->terminateWithError("API parameters are missing", self::ERROR_TYPE_GENERAL); - //query the news $news = $this->NewsModel->getAll($limit); //get the data or terminate with error $news = $this->getDataOrTerminateWithError($news); - // check if any news are there - if($news === NULL){ - $this->terminateWithError("No news content has been found"); - } - // collect the content of the news foreach($news as $news_element){ $this->addMeta("content_id",$news_element->content_id); - $news_element->content_obj = $this->fetchContent($news_element->content_id); + + $content = $this->cmslib->getContent($news_element->content_id); + + $content = $this->getDataOrTerminateWithError($content); + + $news_element->content_obj = $content; } $this->terminateWithSuccess($news); } + public function getNewsRowCount($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $fachbereich_kurzbz = null, $maxalter = 0, $edit = false, $sichtbar = true, $page = 1, $page_size = 10) + { + list($studiengang_kz, $semester) = $this->cmslib->getStgAndSem($studiengang_kz, $semester); + $all = $edit; + + $this->load->model('content/News_model','NewsModel'); + + $num_rows = $this->NewsModel->countNewsWithContent(getSprache(), $studiengang_kz, $semester, $fachbereich_kurzbz, $sichtbar, $maxalter, $page, $page_size, $all, $mischen); + + $num_rows = $this->getDataOrTerminateWithError($num_rows); + + $this->terminateWithSuccess($num_rows); + + } + + + public function getNews($infoscreen = false, $studiengang_kz = null, $semester = null, $mischen = true, $titel = '', $edit = false, $sichtbar = true) + { + //form validation + $this->load->library('form_validation'); + $this->form_validation->set_data($_GET); + $this->form_validation->set_rules('page','Page','required|is_natural'); + $this->form_validation->set_rules('page_size', 'PageSize', 'is_natural'); + if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + + // getting the GET parameters + $page = intval($this->input->get('page', true)); + $page_size = intval($this->input->get('page_size', true)); + + // default value for the page_size is 10 + $page_size = $page_size ?? 10; + + $news = $this->cmslib->getNews($infoscreen, $studiengang_kz, $semester, $mischen, $titel, $edit, $sichtbar, $page, $page_size); + + $news = $this->getDataOrTerminateWithError($news); + + $this->terminateWithSuccess($news); + + } + } diff --git a/application/views/CisHtml/Cms/Content.php b/application/views/CisHtml/Cms/Content.php index a132addbf..037f21cb6 100755 --- a/application/views/CisHtml/Cms/Content.php +++ b/application/views/CisHtml/Cms/Content.php @@ -14,7 +14,7 @@ switch($template_kurzbz){ break; } -$this->load->view('templates/CISVUE-Header', $includesArray); +$this->load->view('templates/CISHTML-Header', $includesArray); ?>

@@ -24,5 +24,5 @@ $this->load->view('templates/CISVUE-Header', $includesArray); -load->view('templates/CISVUE-Footer', $includesArray); ?> +load->view('templates/CISHTML-Footer', $includesArray); ?> diff --git a/public/js/api/cms.js b/public/js/api/cms.js index 490023053..28cb56961 100644 --- a/public/js/api/cms.js +++ b/public/js/api/cms.js @@ -1,25 +1,40 @@ export default { content(content_id, version=null, sprache=null, sichtbar=null) { - return this.$fhcApi.get( - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - "/api/frontend/v1/Cms/content", - { content_id: content_id, - ...(version?{version}:{}), - ...(sprache?{sprache}:{}), - ...(sichtbar?{sichtbar}:{}), - } - ); - }, - news(limit) { return this.$fhcApi.get( - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - "/api/frontend/v1/Cms/news", - { - limit: limit + "/api/frontend/v1/Cms/content", + { + content_id: content_id, + ...(version?{version}:{}), + ...(sprache?{sprache}:{}), + ...(sichtbar?{sichtbar}:{}), } ); }, + + news(limit) { + return this.$fhcApi.get( + "/api/frontend/v1/Cms/news", + { + limit: limit + } + ); + }, + + getNews(page = 1, page_size = 10) { + return this.$fhcApi.get( + "/api/frontend/v1/Cms/getNews", + { + page, + page_size, + }, + ); + }, + + getNewsRowCount: function () { + return this.$fhcApi.get( + "/api/frontend/v1/Cms/getNewsRowCount", + {} + ); + }, } \ No newline at end of file diff --git a/public/js/apps/Cis/Cms.js b/public/js/apps/Cis/Cms.js index dd059fb92..a2f9691b3 100755 --- a/public/js/apps/Cis/Cms.js +++ b/public/js/apps/Cis/Cms.js @@ -2,7 +2,7 @@ import BsConfirm from "../../components/Bootstrap/Confirm.js"; //import Pagination from "../../components/Pagination/Pagination.js"; import CmsNews from "../../components/Cis/Cms/News.js"; import CmsContent from "../../components/Cis/Cms/Content.js"; -import Fhcapi from "../../plugin/FhcApi.js"; +import Phrasen from "../../plugin/Phrasen.js"; const app = Vue.createApp({ @@ -35,6 +35,6 @@ const app = Vue.createApp({ }, }); app.use(primevue.config.default, { zIndex: { overlay: 9999 } }); -app.use(Fhcapi); +app.use(Phrasen); app.mount("#cms"); //#cms [data-confirm], #cms [data-href] diff --git a/public/js/apps/api/cms.js b/public/js/apps/api/cms.js deleted file mode 100644 index 6fbb0e727..000000000 --- a/public/js/apps/api/cms.js +++ /dev/null @@ -1,21 +0,0 @@ -export default { - getNews: function (page = 1, page_size = 10) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - "/CisHtml/Cms/getNews"; - return axios.get(url, { - params: { - page, - page_size, - }, - }); - }, - getNewsRowCount: function () { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - "/CisHtml/Cms/getNewsRowCount"; - return axios.get(url); - }, -}; diff --git a/public/js/apps/api/fhcapifactory.js b/public/js/apps/api/fhcapifactory.js index 35120779f..4b35b807a 100755 --- a/public/js/apps/api/fhcapifactory.js +++ b/public/js/apps/api/fhcapifactory.js @@ -1,7 +1,5 @@ import Search from "./search.js"; -import Cms from "./cms.js"; export default { "Search": Search, - "Cms": Cms, }; diff --git a/public/js/components/Cis/Cms/News.js b/public/js/components/Cis/Cms/News.js index bac28c23c..819f617b4 100644 --- a/public/js/components/Cis/Cms/News.js +++ b/public/js/components/Cis/Cms/News.js @@ -13,18 +13,25 @@ export default { }, methods: { loadNewPageContent: function (data) { - Vue.$fhcapi.Cms.getNews(data.page, data.rows).then((result) => { - this.content = result.data; - }); + this.$fhcApi.factory.cms.getNews(data.page, data.rows) + .then(res => res.data) + .then(result => { + this.content = result; + }); + }, }, created() { - Vue.$fhcapi.Cms.getNews(1, this.page_size).then((result) => { - this.content = result.data; - }); + this.$fhcApi.factory.cms.getNews(1, this.page_size) + .then(res => res.data) + .then(result => { + this.content = result; + }); - Vue.$fhcapi.Cms.getNewsRowCount().then((result) => { - this.maxPageCount = result.data; + this.$fhcApi.factory.cms.getNewsRowCount() + .then(res => res.data) + .then(result => { + this.maxPageCount = result; }); }, template: /*html*/ `