2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Base Controller
|
|
|
|
*/
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use \Aviat\Ion\Di\ContainerInterface;
|
|
|
|
use \Aviat\Ion\View\HttpView;
|
|
|
|
use \Aviat\Ion\View\HtmlView;
|
|
|
|
use \Aviat\Ion\View\JsonView;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
2015-09-17 23:11:18 -04:00
|
|
|
* Controller base, defines output methods
|
2015-10-06 10:44:33 -04:00
|
|
|
*
|
|
|
|
* @property Response object $response
|
|
|
|
* @property Config object $config
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
class Controller {
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use \Aviat\Ion\Di\ContainerAware;
|
|
|
|
|
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-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* Response object
|
|
|
|
* @var object $response
|
|
|
|
*/
|
|
|
|
protected $response;
|
|
|
|
|
2015-06-26 16:39:10 -04:00
|
|
|
/**
|
|
|
|
* The api model for the current controller
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
/**
|
|
|
|
* Url generatation class
|
|
|
|
* @var UrlGenerator
|
|
|
|
*/
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
2015-06-26 16:39:10 -04:00
|
|
|
/**
|
|
|
|
* Common data to be sent to views
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-06-30 13:03:20 -04:00
|
|
|
protected $base_data = [
|
|
|
|
'url_type' => 'anime',
|
|
|
|
'other_type' => 'manga',
|
|
|
|
'nav_routes' => []
|
|
|
|
];
|
2015-06-26 16:39:10 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-06-30 13:03:20 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$this->setContainer($container);
|
|
|
|
$urlGenerator = $container->get('url-generator');
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->config = $container->get('config');
|
|
|
|
$this->request = $container->get('request');
|
|
|
|
$this->response = $container->get('response');
|
2015-09-17 23:11:18 -04:00
|
|
|
$this->base_data['urlGenerator'] = $urlGenerator;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:04:04 -04:00
|
|
|
/**
|
|
|
|
* Get a class member
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$allowed = ['response', 'config'];
|
2015-07-02 14:04:04 -04:00
|
|
|
|
|
|
|
if (in_array($key, $allowed))
|
|
|
|
{
|
|
|
|
return $this->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
/**
|
2015-06-24 16:01:35 -04:00
|
|
|
* Get the string output of a partial template
|
2015-05-22 12:36:26 -04:00
|
|
|
*
|
2015-09-17 23:11:18 -04:00
|
|
|
* @param HTMLView $view
|
2015-05-22 12:36:26 -04:00
|
|
|
* @param string $template
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param array|object $data
|
2015-06-24 16:01:35 -04:00
|
|
|
* @return string
|
2015-05-22 12:36:26 -04:00
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function load_partial($view, $template, $data = [])
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$errorHandler = $this->container->get('error-handler');
|
2015-10-05 16:54:25 -04:00
|
|
|
$errorHandler->addDataTable('Template Data', $data);
|
2015-09-17 23:11:18 -04:00
|
|
|
$router = $this->container->get('router');
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
if (isset($this->base_data))
|
|
|
|
{
|
|
|
|
$data = array_merge($this->base_data, $data);
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
$route = $router->get_route();
|
|
|
|
$data['route_path'] = ($route) ? $router->get_route()->path : "";
|
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
$template_path = _dir($this->config->__get('view_path'), "{$template}.php");
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
if ( ! is_file($template_path))
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
throw new \InvalidArgumentException("Invalid template : {$template}");
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
return $view->render_template($template_path, $data);
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
/**
|
|
|
|
* Render a template with header and footer
|
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param HtmlView $view
|
2015-09-17 23:11:18 -04:00
|
|
|
* @param string $template
|
|
|
|
* @param array|object $data
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function render_full_page($view, $template, $data)
|
|
|
|
{
|
|
|
|
$view->appendOutput($this->load_partial($view, 'header', $data));
|
|
|
|
$view->appendOutput($this->load_partial($view, $template, $data));
|
|
|
|
$view->appendOutput($this->load_partial($view, 'footer', $data));
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a template to HTML, using the provided data
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
* @param array|object $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function outputHTML($template, $data = [])
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$view = new HtmlView($this->container);
|
|
|
|
$this->render_full_page($view, $template, $data);
|
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
/**
|
|
|
|
* Output a JSON Response
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function outputJSON($data = [])
|
2015-09-17 23:11:18 -04:00
|
|
|
{
|
|
|
|
$view = new JsonView($this->container);
|
|
|
|
$view->setOutput($data);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
|
|
|
* Redirect to the selected page
|
|
|
|
*
|
2015-09-17 23:11:18 -04:00
|
|
|
* @param string $path
|
2015-06-16 11:11:35 -04:00
|
|
|
* @param int $code
|
2015-09-14 10:54:50 -04:00
|
|
|
* @param string $type
|
2015-06-16 11:11:35 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function redirect($path, $code, $type = "anime")
|
2015-06-16 11:11:35 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$url = $this->urlGenerator->full_url($path, $type);
|
|
|
|
$http = new HttpView($this->container);
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
$http->redirect($url, $code);
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a message box to the page
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $message
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function show_message($type, $message)
|
|
|
|
{
|
|
|
|
return $this->load_partial('message', [
|
|
|
|
'stat_class' => $type,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
2015-06-16 11:11:35 -04:00
|
|
|
}
|
2015-06-17 08:50:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the api session
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
session_destroy();
|
2015-09-14 15:49:20 -04:00
|
|
|
$this->response->redirect->seeOther($this->urlGenerator->full_url(''));
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the login form
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-10-06 10:24:48 -04:00
|
|
|
public function login($status = "")
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
|
|
|
$message = "";
|
|
|
|
|
|
|
|
if ($status != "")
|
|
|
|
{
|
|
|
|
$message = $this->show_message('error', $status);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputHTML('login', [
|
|
|
|
'title' => 'Api login',
|
|
|
|
'message' => $message
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to log in with the api
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function login_action()
|
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$request = $this->container->get('request');
|
|
|
|
|
2015-06-26 16:39:10 -04:00
|
|
|
if (
|
|
|
|
$this->model->authenticate(
|
|
|
|
$this->config->hummingbird_username,
|
2015-09-17 23:11:18 -04:00
|
|
|
$request->post->get('password')
|
2015-06-26 16:39:10 -04:00
|
|
|
)
|
|
|
|
)
|
2015-06-24 16:01:35 -04:00
|
|
|
{
|
2015-09-17 23:11:18 -04:00
|
|
|
$this->response->redirect->afterPost(
|
|
|
|
$this->urlGenerator->full_url('', $this->base_data['url_type'])
|
|
|
|
);
|
2015-06-24 16:01:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->login("Invalid username or password.");
|
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
}
|
2015-09-15 13:19:29 -04:00
|
|
|
// End of BaseController.php
|