Added manga controller

This commit is contained in:
Timothy Warren 2015-05-27 09:03:42 -04:00
parent 77d2c29e38
commit c96bbb92c5
15 changed files with 385 additions and 66 deletions

View File

@ -20,8 +20,8 @@ class Router {
*/
public function get_route()
{
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$route = $this->router->match($path, $_SERVER);
//$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$route = $this->router->match($_SERVER['REQUEST_URI'], $_SERVER);
return $route;
}
@ -34,6 +34,8 @@ class Router {
*/
public function dispatch($route = NULL)
{
global $defaultHandler;
if (is_null($route))
{
$route = $this->get_route();
@ -41,6 +43,9 @@ class Router {
if ( ! $route)
{
$failure = $this->router->getFailedRoute();
$defaultHandler->addDataTable('failed_route', (array)$failure);
$controller_name = 'BaseController';
$action_method = 'outputHTML';
$params = [
@ -49,6 +54,7 @@ class Router {
'title' => 'Page Not Found'
]
];
}
else
{
@ -66,18 +72,22 @@ class Router {
private function _setup_routes()
{
$host = $_SERVER['HTTP_HOST'];
$controller_class = ($host == "anime.timshomepage.net") ? "AnimeController" : "MangaController";
$route_type = "";
switch($host)
{
case "anime.timshomepage.net":
$route_type = "anime";
break;
case "manga.timshomepage.net":
$route_type = "manga";
break;
}
$routes = require __DIR__ . '/../config/routes.php';
// Add the default route
$this->router->add('home', '/')->addValues([
'controller' => $controller_class,
'action' => 'index'
]);
// Add routes by the configuration file
foreach($routes as $name => $route)
foreach($routes[$route_type] as $name => $route)
{
$path = $route['path'];
unset($route['path']);

View File

@ -1,13 +1,17 @@
<?php
return [
'anime_all' => [
'anime' => [
'index' => [
'path' => '/',
'controller' => 'AnimeController'
],
'all' => [
'path' => '/all',
'controller' => 'AnimeController',
'action' => 'all',
'params' => []
'action' => 'all'
],
'anime_plan_to_watch' => [
'plan_to_watch' => [
'path' => '/plan_to_watch',
'controller' => 'AnimeController',
'action' => 'anime_list',
@ -15,7 +19,7 @@ return [
'type' => 'plan-to-watch'
]
],
'anime_on_hold' => [
'on_hold' => [
'path' => '/on_hold',
'controller' => 'AnimeController',
'action' => 'anime_list',
@ -23,7 +27,7 @@ return [
'type' => 'on-hold'
]
],
'anime_dropped' => [
'dropped' => [
'path' => '/dropped',
'controller' => 'AnimeController',
'action' => 'anime_list',
@ -31,12 +35,61 @@ return [
'type' => 'dropped'
]
],
'anime_completed' => [
'completed' => [
'path' => '/completed',
'controller' => 'AnimeController',
'action' => 'anime_list',
'params' => [
'type' => 'completed'
]
],
'anime_login' => [
'path' => '/login',
'controller' => 'AnimeController',
'action' => 'login'
]
],
'manga' => [
'index' => [
'path' => '/',
'controller' => 'MangaController'
],
'all' => [
'path' => '/all',
'controller' => 'MangaController',
'action' => 'all'
],
'plan_to_read' => [
'path' => '/plan_to_read',
'controller' => 'MangaController',
'action' => 'manga_list',
'params' => [
'type' => 'Plan to Read'
]
],
'on_hold' => [
'path' => '/on_hold',
'controller' => 'MangaController',
'action' => 'manga_list',
'params' => [
'type' => 'On Hold'
]
],
'dropped' => [
'path' => '/dropped',
'controller' => 'MangaController',
'action' => 'manga_list',
'params' => [
'type' => 'Dropped'
]
],
'completed' => [
'path' => '/completed',
'controller' => 'MangaController',
'action' => 'manga_list',
'params' => [
'type' => 'Completed'
]
],
]
];

View File

@ -37,5 +37,11 @@ class AnimeController extends BaseController {
'sections' => $data
]);
}
public function login()
{
$data = $this->model->authenticate();
//print_r($data);
}
}
// End of AnimeController.php

View File

@ -0,0 +1,47 @@
<?php
class MangaController extends BaseController {
private $model;
public function __construct()
{
parent::__construct();
$this->model = new MangaModel();
}
public function __destruct()
{
parent::__destruct();
}
public function index()
{
$this->manga_list('Reading');
}
public function all()
{
$data = $this->model->get_all_lists();
$this->outputHTML('manga_list', [
'title' => "Tim's Manga List &middot; All",
'sections' => $data
]);
}
public function manga_list($type, $title="Tim's Manga List")
{
$data = $this->model->get_list($type);
$this->outputHTML('manga_list', [
'title' => $title,
'sections' => [$type => $data]
]);
}
public function login()
{
$data = $this->model->authenticate();
//print_r($data);
}
}
// End of MangaController.php

View File

@ -23,10 +23,10 @@ class AnimeModel extends BaseModel {
{
$output = [
'Watching' => [],
'Completed' => [],
'Plan to Watch' => [],
'On Hold' => [],
'Dropped' => []
'Dropped' => [],
'Completed' => [],
];
$data = $this->_get_list();
@ -76,10 +76,10 @@ class AnimeModel extends BaseModel {
{
$map = [
'currently-watching' => 'Watching',
'completed' => 'Completed',
'plan-to-watch' => 'Plan to Watch',
'on-hold' => 'On Hold',
'dropped' => 'Dropped'
'dropped' => 'Dropped',
'completed' => 'Completed',
];
$data = $this->_get_list($type);

130
app/models/MangaModel.php Normal file
View File

@ -0,0 +1,130 @@
<?php
/**
* Model for handling requests dealing with the manga list
*/
class MangaModel extends BaseModel {
protected $client;
protected $cookieJar;
protected $base_url = "https://hummingbird.me";
public function __construct()
{
parent::__construct();
}
/**
* Get the full set of anime lists
*
* @return array
*/
public function get_all_lists()
{
$data = $this->_get_list();
foreach ($data as $key => &$val)
{
$this->sort_by_name($val);
}
return $data;
}
/**
* Get a category out of the full list
*
* @param string $type
* @return array
*/
public function get_list($type)
{
$data = $this->_get_list($type);
$this->sort_by_name($data);
return $data;
}
private function _get_list($type="all")
{
global $defaultHandler;
$config = [
'query' => [
'user_id' => 'timw4mail',
],
'allow_redirects' => false
];
$response = $this->client->get($this->_url('/manga_library_entries'), $config);
$defaultHandler->addDataTable('response', (array)$response);
if ($response->getStatusCode() != 200)
{
throw new Exception($response->getEffectiveUrl());
}
// Reorganize data to be more usable
$raw_data = $response->json();
$data = [
'Reading' => [],
'Plan to Read' => [],
'On Hold' => [],
'Dropped' => [],
'Completed' => [],
];
$manga_data = [];
foreach($raw_data['manga'] as $manga)
{
$manga_data[$manga['id']] = $manga;
}
foreach($raw_data['manga_library_entries'] as $entry)
{
$entry['manga'] = $manga_data[$entry['manga_id']];
switch($entry['status'])
{
case "Plan to Read":
$data['Plan to Read'][] = $entry;
break;
case "Dropped":
$data['Dropped'][] = $entry;
break;
case "On Hold":
$data['On Hold'][] = $entry;
break;
case "Currently Reading":
$data['Reading'][] = $entry;
break;
case "Completed":
default:
$data['Completed'][] = $entry;
break;
}
}
return (array_key_exists($type, $data)) ? $data[$type] : $data;
}
private function sort_by_name(&$array)
{
$sort = array();
foreach($array as $key => $item)
{
$sort[$key] = $item['manga']['romaji_title'];
}
array_multisort($sort, SORT_ASC, $array);
}
}
// End of MangaModel.php

8
app/views/anime_edit.php Normal file
View File

@ -0,0 +1,8 @@
<body>
<?php include 'anime_nav.php' ?>
<main>
<form action="<?= $action ?>" method="post">
</form>
</main>
</body>
</html>

View File

@ -1,16 +1,7 @@
<?php include 'header.php' ?>
<body>
<h1>Tim's Anime List</h1>
<nav>
<ul>
<li class="<?= is_selected('/all', $route_path) ?>"><a href="/all">All</a></li>
<li class="<?= is_selected('/', $route_path) ?>"><a href="/">Watching</a></li>
<li class="<?= is_selected('/plan_to_watch', $route_path) ?>"><a href="/plan_to_watch">Plan to Watch</a></li>
<li class="<?= is_selected('/on_hold', $route_path) ?>"><a href="/on_hold">On Hold</a></li>
<li class="<?= is_selected('/dropped', $route_path) ?>"><a href="/dropped">Dropped</a></li>
<li class="<?= is_selected('/completed', $route_path) ?>"><a href="/completed">Completed</a></li>
</ul>
</nav>
<h1>Tim's Anime List [<a href="//manga.timshomepage.net">Manga List</a>]</h1>
<?php include 'anime_nav.php' ?>
<main>
<?php foreach ($sections as $name => $items): ?>
<section class="status">
@ -27,7 +18,7 @@
<div class="media_type"><?= $item['anime']['show_type'] ?></div>
<div class="airing_status"><?= $item['anime']['status'] ?></div>
<div class="user_rating"><?= (int)($item['rating']['value'] * 2) ?> / 10</div>
<div class="completion"><?= $item['episodes_watched'] ?> / <?= $item['anime']['episode_count'] ?></div>
<div class="completion">Episodes: <?= $item['episodes_watched'] ?> / <?= $item['anime']['episode_count'] ?></div>
</div>
</article>
<?php endforeach ?>

10
app/views/anime_nav.php Normal file
View File

@ -0,0 +1,10 @@
<nav>
<ul>
<li class="<?= is_selected('/', $route_path) ?>"><a href="/">Watching</a></li>
<li class="<?= is_selected('/plan_to_watch', $route_path) ?>"><a href="/plan_to_watch">Plan to Watch</a></li>
<li class="<?= is_selected('/on_hold', $route_path) ?>"><a href="/on_hold">On Hold</a></li>
<li class="<?= is_selected('/dropped', $route_path) ?>"><a href="/dropped">Dropped</a></li>
<li class="<?= is_selected('/completed', $route_path) ?>"><a href="/completed">Completed</a></li>
<li class="<?= is_selected('/all', $route_path) ?>"><a href="/all">All</a></li>
</ul>
</nav>

40
app/views/manga_list.php Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title><?= $title ?></title>
<link rel="stylesheet" href="/public/css/marx.css" />
<link rel="stylesheet" href="/public/css/base.css" />
<link rel="stylesheet" href="/public/css/manga.css" />
</head>
<body>
<h1>Tim's Manga List [<a href="//anime.timshomepage.net">Anime List</a>]</h1>
<?php include 'manga_nav.php' ?>
<main>
<?php foreach ($sections as $name => $items): ?>
<section class="status">
<h2><?= $name ?></h2>
<section class="media-wrap">
<?php foreach($items as $item): ?>
<article class="media" id="manga-<?= $item['manga']['id'] ?>">
<img src="<?= $item['manga']['poster_image'] ?>" />
<div class="name"><a href="https://hummingbird.me/manga/<?= $item['manga']['id'] ?>">
<?= $item['manga']['romaji_title'] ?>
<?= (isset($item['manga']['english_title'])) ? "<br />({$item['manga']['english_title']})" : ""; ?>
</a></div>
<div class="media_metadata">
<div class="media_type"><?= $item['manga']['manga_type'] ?></div>
<?php /*<div class="airing_status"><?= $item['manga']['status'] ?></div>*/ ?>
<div class="user_rating"><?= ($item['rating'] > 0) ? (int)($item['rating'] * 2) : '-' ?> / 10</div>
<div class="completion">
Chapters: <?= $item['chapters_read'] ?> / <?= ($item['manga']['chapter_count'] > 0) ? $item['manga']['chapter_count'] : "-" ?><?php /*<br />
Volumes: <?= $item['volumes_read'] ?> / <?= ($item['manga']['volume_count'] > 0) ? $item['manga']['volume_count'] : "-" ?>*/ ?>
</div>
</div>
</article>
<?php endforeach ?>
</section>
</section>
<?php endforeach ?>
</main>
</body>
</html>

10
app/views/manga_nav.php Normal file
View File

@ -0,0 +1,10 @@
<nav>
<ul>
<li class="<?= is_selected('/', $route_path) ?>"><a href="/">Reading</a></li>
<li class="<?= is_selected('/plan_to_read', $route_path) ?>"><a href="/plan_to_read">Plan to Read</a></li>
<li class="<?= is_selected('/on_hold', $route_path) ?>"><a href="/on_hold">On Hold</a></li>
<li class="<?= is_selected('/dropped', $route_path) ?>"><a href="/dropped">Dropped</a></li>
<li class="<?= is_selected('/completed', $route_path) ?>"><a href="/completed">Completed</a></li>
<li class="<?= is_selected('/all', $route_path) ?>"><a href="/all">All</a></li>
</ul>
</nav>

View File

@ -3,6 +3,8 @@
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/app/base/autoloader.php';
session_start();
use \Whoops\Handler\PrettyPageHandler;
use \Whoops\Handler\JsonResponseHandler;

View File

@ -1,3 +1,7 @@
body {
margin: 0.5em;
}
.media-wrap {
text-align:center;
margin:0 auto;
@ -59,8 +63,3 @@
.user_rating::before {
content: "Rating: ";
}
.completion::before {
content: "Episodes: ";
}

13
public/css/manga.css Normal file
View File

@ -0,0 +1,13 @@
.media {
position:relative;
vertical-align:top;
display:inline-block;
text-align:center;
width:200px;
height:290px;
margin:0.25em;
}
.completion::before {
content: "";
}