diff --git a/lib/class.model.php b/lib/class.model.php index 6954f6e..5fc29ec 100644 --- a/lib/class.model.php +++ b/lib/class.model.php @@ -46,4 +46,49 @@ abstract class model { return new $model_class($this->db, $this->auth, $this->router, $this->view); } + + /* show ap[i output in json format + * + * INPUT: array + * OUTPUT: string + * ERROR: null + */ + protected function renderOutput($variables, $jsonout = false) { + + $contentt = 'Content-Type: text/plain'; + $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); + $httpcode = '412'; + $sucess = false; + $message = 'No message was specified, the server could not process the content or the page does not exist'; + $page = 1; + $pages = 1; + $per_page = 1; + $data = array($message); + + if(is_array($variables) ) { + foreach($variables as $key => $value) { + $$key = $value; + } + } + + $GLOBALS['http_response_code'] = $httpcode; + if( !is_null($jsonout) and !empty($jsonout)) { + $contentt = 'Content-Type: application/json; charset=utf-8'; + header($contentt); + $jsondata = array( + 'sucess' => $sucess, + 'message' => $message, + 'page' => $page, + 'pages' => $pages, + 'per_page' => $per_page, + 'data'=> $data + ); + print(json_encode($jsondata)); + } + else { + $contentt = 'Content-Type: text/html; charset=UTF-8'; + header($contentt); + include(DIR_VIEWS."notfoundview.php"); + } + } } diff --git a/models/index.php b/models/index.php index fed1b2d..d84f639 100644 --- a/models/index.php +++ b/models/index.php @@ -17,6 +17,7 @@ class index_model extends model { } public function show() { + $httpcode = 200; include(DIR_VIEWS."index.php"); } } diff --git a/models/notFound.php b/models/notFound.php index 7726d91..2c6bcd2 100644 --- a/models/notFound.php +++ b/models/notFound.php @@ -12,14 +12,9 @@ Copyright (c) 2018 Díaz Víctor aka (Máster Vitronic) class notFound_model extends model { - public function show() { - $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); - header($protocol . ' 404'); - $GLOBALS['http_response_code'] = '404'; - header('Content-Type: application/json; charset=utf-8'); - print(json_encode([ - 'msg'=>'Not Found' - ])); + public function show($errormessage = 'The page could not be found or was moved, or your request is not valid, please consult upstream') { + $variables = array('message'=>$errormessage,'httpcode'=>'404'); + $this->renderOutput($variables); } } diff --git a/public/views/index.php b/public/views/index.php index cb76608..c98b8b0 100644 --- a/public/views/index.php +++ b/public/views/index.php @@ -11,7 +11,7 @@