2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Base Controller
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Base class for controllers, defines output methods
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
class BaseController {
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The global configuration object
|
|
|
|
* @var object $config
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
protected $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2015-06-09 18:18:53 -04:00
|
|
|
public function __construct()
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-06-09 18:18:53 -04:00
|
|
|
global $config;
|
|
|
|
$this->config = $config;
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a template to HTML, using the provided data
|
|
|
|
*
|
|
|
|
* @param string $template
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param array|object $data
|
2015-05-22 12:36:26 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function outputHTML($template, $data=[])
|
|
|
|
{
|
2015-06-16 11:11:35 -04:00
|
|
|
global $router, $defaultHandler;
|
2015-05-22 12:36:26 -04:00
|
|
|
$route = $router->get_route();
|
|
|
|
$data['route_path'] = ($route) ? $router->get_route()->path : "";
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
$defaultHandler->addDataTable('Template Data', $data);
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
$template_path = _dir(APP_DIR, 'views', "{$template}.php");
|
|
|
|
|
|
|
|
if ( ! is_file($template_path))
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
|
|
|
throw new Exception("Invalid template : {$path}");
|
2015-06-16 11:11:35 -04:00
|
|
|
die();
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
extract($data);
|
2015-06-11 16:44:52 -04:00
|
|
|
include _dir(APP_DIR, 'views', 'header.php');
|
2015-06-16 11:11:35 -04:00
|
|
|
include $template_path;
|
|
|
|
include _dir(APP_DIR, 'views', 'footer.php');
|
2015-05-22 12:36:26 -04:00
|
|
|
$buffer = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
header("Content-type: text/html;charset=utf-8");
|
|
|
|
echo $buffer;
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output json with the proper content type
|
|
|
|
*
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param mixed $data
|
2015-05-22 12:36:26 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function outputJSON($data)
|
|
|
|
{
|
|
|
|
if ( ! is_string($data))
|
|
|
|
{
|
|
|
|
$data = json_encode($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
header("Content-type: application/json");
|
|
|
|
echo $data;
|
|
|
|
}
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the selected page
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param int $code
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function redirect($url, $code, $type="anime")
|
|
|
|
{
|
|
|
|
$url = full_url($url, $type);
|
|
|
|
|
|
|
|
$codes = [
|
|
|
|
301 => 'Moved Permanently',
|
|
|
|
302 => 'Found',
|
|
|
|
303 => 'See Other'
|
|
|
|
];
|
|
|
|
|
|
|
|
header("HTTP/1.1 {$code} {$codes[$code]}");
|
|
|
|
header("Location: {$url}");
|
|
|
|
die();
|
|
|
|
}
|
2015-06-17 08:50:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the api session
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
session_destroy();
|
|
|
|
$this->redirect('');
|
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
|
|
|
// End of BaseController.php
|