diff --git a/app/base/Router.php b/app/base/Router.php index 9eda4276..47981172 100644 --- a/app/base/Router.php +++ b/app/base/Router.php @@ -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']); diff --git a/app/config/routes.php b/app/config/routes.php index 0168f0d1..22189a0d 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -1,42 +1,95 @@ [ - 'path' => '/all', - 'controller' => 'AnimeController', - 'action' => 'all', - 'params' => [] - ], - 'anime_plan_to_watch' => [ - 'path' => '/plan_to_watch', - 'controller' => 'AnimeController', - 'action' => 'anime_list', - 'params' => [ - 'type' => 'plan-to-watch' + 'anime' => [ + 'index' => [ + 'path' => '/', + 'controller' => 'AnimeController' + ], + 'all' => [ + 'path' => '/all', + 'controller' => 'AnimeController', + 'action' => 'all' + ], + 'plan_to_watch' => [ + 'path' => '/plan_to_watch', + 'controller' => 'AnimeController', + 'action' => 'anime_list', + 'params' => [ + 'type' => 'plan-to-watch' + ] + ], + 'on_hold' => [ + 'path' => '/on_hold', + 'controller' => 'AnimeController', + 'action' => 'anime_list', + 'params' => [ + 'type' => 'on-hold' + ] + ], + 'dropped' => [ + 'path' => '/dropped', + 'controller' => 'AnimeController', + 'action' => 'anime_list', + 'params' => [ + 'type' => 'dropped' + ] + ], + 'completed' => [ + 'path' => '/completed', + 'controller' => 'AnimeController', + 'action' => 'anime_list', + 'params' => [ + 'type' => 'completed' + ] + ], + 'anime_login' => [ + 'path' => '/login', + 'controller' => 'AnimeController', + 'action' => 'login' ] ], - 'anime_on_hold' => [ - 'path' => '/on_hold', - 'controller' => 'AnimeController', - 'action' => 'anime_list', - 'params' => [ - 'type' => 'on-hold' - ] - ], - 'anime_dropped' => [ - 'path' => '/dropped', - 'controller' => 'AnimeController', - 'action' => 'anime_list', - 'params' => [ - 'type' => 'dropped' - ] - ], - 'anime_completed' => [ - 'path' => '/completed', - 'controller' => 'AnimeController', - 'action' => 'anime_list', - 'params' => [ - 'type' => 'completed' - ] + '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' + ] + ], ] ]; \ No newline at end of file diff --git a/app/controllers/AnimeController.php b/app/controllers/AnimeController.php index 9da9304a..e854f51d 100644 --- a/app/controllers/AnimeController.php +++ b/app/controllers/AnimeController.php @@ -37,5 +37,11 @@ class AnimeController extends BaseController { 'sections' => $data ]); } + + public function login() + { + $data = $this->model->authenticate(); + //print_r($data); + } } // End of AnimeController.php \ No newline at end of file diff --git a/app/controllers/MangaController.php b/app/controllers/MangaController.php new file mode 100644 index 00000000..c36935af --- /dev/null +++ b/app/controllers/MangaController.php @@ -0,0 +1,47 @@ +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 · 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 \ No newline at end of file diff --git a/app/models/AnimeModel.php b/app/models/AnimeModel.php index 5d50b4ef..fe9f367a 100644 --- a/app/models/AnimeModel.php +++ b/app/models/AnimeModel.php @@ -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); diff --git a/app/models/MangaModel.php b/app/models/MangaModel.php new file mode 100644 index 00000000..6d748bd8 --- /dev/null +++ b/app/models/MangaModel.php @@ -0,0 +1,130 @@ +_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 \ No newline at end of file diff --git a/app/views/anime_edit.php b/app/views/anime_edit.php new file mode 100644 index 00000000..c90e3aee --- /dev/null +++ b/app/views/anime_edit.php @@ -0,0 +1,8 @@ +
+ +