refactors the news controller

This commit is contained in:
SimonGschnell
2024-08-02 14:46:22 +02:00
parent bd7e45547f
commit b350c9da56
10 changed files with 132 additions and 246 deletions
+79 -42
View File
@@ -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);
}
}