HummingBirdAnimeClient/src/Aviat/AnimeClient/Controller.php

388 lines
7.9 KiB
PHP
Raw Normal View History

2015-05-22 12:36:26 -04:00
<?php
/**
2015-11-16 11:40:01 -05:00
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
2015-10-19 13:26:50 -04:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\View\HttpView;
use Aviat\Ion\View\HtmlView;
use Aviat\Ion\View\JsonView;
2016-01-04 10:53:03 -05:00
use Aviat\AnimeClient\AnimeClient;
2015-09-17 23:11:18 -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
*/
class Controller {
2015-05-22 12:36:26 -04:00
2015-09-17 23:11:18 -04:00
use \Aviat\Ion\Di\ContainerAware;
/**
* The global configuration object
* @var object $config
*/
protected $config;
2015-05-22 12:36:26 -04:00
2015-10-06 11:38:20 -04:00
/**
* Request object
* @var object $request
*/
protected $request;
/**
* 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;
2016-01-04 10:53:03 -05:00
/**
* Session segment
* @var [type]
*/
protected $session;
2015-06-26 16:39:10 -04:00
/**
* Common data to be sent to views
* @var array
*/
protected $base_data = [
'url_type' => 'anime',
'other_type' => 'manga',
2015-10-09 14:34:55 -04:00
'menu_name' => ''
];
2015-06-26 16:39:10 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
*/
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');
$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;
2015-11-04 16:36:54 -05:00
$this->base_data['auth'] = $container->get('auth');
$this->base_data['config'] = $this->config;
2015-09-17 23:11:18 -04:00
$this->urlGenerator = $urlGenerator;
2016-01-04 10:53:03 -05:00
$session = $container->get('session');
$this->session = $session->getSegment(AnimeClient::SESSION_SEGMENT);
// Set a 'previous' flash value for better redirects
$this->session->setFlash('previous', $this->request->server->get('HTTP_REFERER'));
// Set a message box if available
$this->base_data['message'] = $this->session->getFlash('message');
2015-05-22 12:36:26 -04:00
}
2015-10-20 16:41:51 -04:00
/**
* Redirect to the default controller/url from an empty path
*/
public function redirect_to_default()
{
$default_type = $this->config->get(['routing', 'default_list']);
2015-10-20 16:41:51 -04:00
$this->redirect($this->urlGenerator->default_url($default_type), 303);
}
2016-01-04 10:53:03 -05:00
/**
* Redirect to the previous page
*
* @return void
*/
public function redirect_to_previous()
{
$previous = $this->session->getFlash('previous');
$this->redirect($previous, 303);
}
/**
* Set the current url in the session as the target of a future redirect
*
* @param string|null $url
* @return void
*/
public function set_session_redirect($url = NULL)
2016-01-04 10:53:03 -05:00
{
$anime_client = $this->container->get('anime-client');
$double_form_page = $this->request->server->get('HTTP_REFERER') == $this->request->url->get();
// Don't attempt to set the redirect url if
// the page is one of the form type pages,
// and the previous page is also a form type page_segments
if ($double_form_page)
{
return;
}
if (is_null($url))
{
$url = ($anime_client->is_view_page())
? $this->request->url->get()
: $this->request->server->get('HTTP_REFERER');
}
$this->session->set('redirect_url', $url);
}
/**
* Redirect to the url previously set in the session
*
* @return void
*/
public function session_redirect()
{
$target = $this->session->get('redirect_url');
if (empty($target))
{
$this->not_found();
}
else
{
$this->redirect($target, 303);
$this->session->set('redirect_url', NULL);
}
}
/**
* Get a class member
*
* @param string $key
* @return object
*/
public function __get($key)
{
2015-09-17 23:11:18 -04:00
$allowed = ['response', 'config'];
if (in_array($key, $allowed))
{
return $this->$key;
}
return NULL;
}
2015-05-22 12:36:26 -04:00
/**
* Get the string output of a partial template
2015-05-22 12:36:26 -04:00
*
2015-10-06 11:38:20 -04:00
* @param HtmlView $view
2015-05-22 12:36:26 -04:00
* @param string $template
2015-10-06 12:15:19 -04:00
* @param array $data
* @return string
2015-05-22 12:36:26 -04:00
*/
2015-11-04 16:36:54 -05:00
protected function load_partial($view, $template, array $data = [])
2015-05-22 12:36:26 -04:00
{
2015-09-17 23:11:18 -04:00
$errorHandler = $this->container->get('error-handler');
$errorHandler->addDataTable('Template Data', $data);
2015-10-09 14:34:55 -04:00
$router = $this->container->get('dispatcher');
2015-09-17 23:11:18 -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-06 11:38:20 -04:00
$template_path = _dir($this->config->get('view_path'), "{$template}.php");
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-10-06 12:15:19 -04:00
return $view->render_template($template_path, (array)$data);
2015-09-17 23:11:18 -04:00
}
2015-05-22 12:36:26 -04:00
2015-09-17 23:11:18 -04:00
/**
* Render a template with header and footer
*
* @param HtmlView $view
2015-09-17 23:11:18 -04:00
* @param string $template
2015-10-09 14:34:55 -04:00
* @param array $data
2015-09-17 23:11:18 -04:00
* @return void
*/
2015-11-04 16:36:54 -05:00
protected function render_full_page($view, $template, array $data)
2015-09-17 23:11:18 -04:00
{
$view->appendOutput($this->load_partial($view, 'header', $data));
2016-01-04 10:53:03 -05:00
if (array_key_exists('message', $data) && is_array($data['message']))
{
$view->appendOutput($this->load_partial($view, 'message', $data['message']));
}
2015-09-17 23:11:18 -04:00
$view->appendOutput($this->load_partial($view, $template, $data));
$view->appendOutput($this->load_partial($view, 'footer', $data));
}
2015-11-04 16:36:54 -05:00
/**
* Show the login form
*
* @codeCoverageIgnore
* @param string $status
* @return void
*/
public function login($status = "")
2015-11-04 16:36:54 -05:00
{
$message = "";
$view = new HtmlView($this->container);
if ($status != "")
{
$message = $this->show_message($view, 'error', $status);
}
2016-01-04 10:53:03 -05:00
// Set the redirect url
$this->set_session_redirect();
2015-11-04 16:36:54 -05:00
$this->outputHTML('login', [
'title' => 'Api login',
'message' => $message
], $view);
}
2015-11-13 11:33:27 -05:00
/**
* Attempt login authentication
*
2015-11-18 10:48:05 -05:00
* @return void
2015-11-13 11:33:27 -05:00
*/
public function login_action()
{
$auth = $this->container->get('auth');
if ($auth->authenticate($this->request->post->get('password')))
{
2016-01-04 10:53:03 -05:00
return $this->session_redirect();
2015-11-13 11:33:27 -05:00
}
$this->login("Invalid username or password.");
}
/**
* Deauthorize the current user
*
* @return void
*/
public function logout()
{
$auth = $this->container->get('auth');
$auth->logout();
$this->redirect_to_default();
}
/**
* 404 action
*
* @return void
*/
public function not_found()
{
$this->outputHTML('404', [
'title' => 'Sorry, page not found'
]);
}
2016-01-04 10:53:03 -05:00
/**
* Set a session flash variable to display a message on
* next page load
*
* @param string $message
* @param string $type
* @return void
*/
public function set_flash_message($message, $type = "info")
2016-01-04 10:53:03 -05:00
{
$this->session->setFlash('message', [
'message_type' => $type,
'message' => $message
]);
}
2015-11-04 16:36:54 -05:00
/**
* Add a message box to the page
*
* @codeCoverageIgnore
* @param HtmlView $view
* @param string $type
* @param string $message
* @return string
*/
protected function show_message($view, $type, $message)
{
return $this->load_partial($view, 'message', [
2016-01-04 10:53:03 -05:00
'message_type' => $type,
2015-11-04 16:36:54 -05:00
'message' => $message
]);
}
/**
* Output a template to HTML, using the provided data
*
* @param string $template
2015-10-09 14:34:55 -04:00
* @param array $data
2015-11-04 16:53:22 -05:00
* @param HtmlView|null $view
* @return void
*/
2015-11-04 16:36:54 -05:00
protected function outputHTML($template, array $data = [], $view = NULL)
{
2015-11-04 16:36:54 -05:00
if (is_null($view))
{
$view = new HtmlView($this->container);
}
2015-09-17 23:11:18 -04:00
$this->render_full_page($view, $template, $data);
}
2015-09-17 23:11:18 -04:00
/**
* Output a JSON Response
*
* @param mixed $data
* @return void
*/
2015-11-04 16:36:54 -05:00
protected 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
}
/**
* Redirect to the selected page
*
2015-10-20 16:41:51 -04:00
* @param string $url
* @param int $code
* @return void
*/
2015-11-04 16:36:54 -05:00
protected function redirect($url, $code)
{
2015-09-17 23:11:18 -04:00
$http = new HttpView($this->container);
$http->redirect($url, $code);
}
}
2015-09-15 13:19:29 -04:00
// End of BaseController.php