CSY2028-assignment-2/jobs/Routes.php

66 lines
2.2 KiB
PHP
Raw Normal View History

2023-01-21 23:12:42 +00:00
<?php
namespace jobs;
class Routes implements \CSY2028\Routes {
2023-01-25 13:22:20 +00:00
public function getController($controllerName, $functionName) {
$catsTable = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
2023-01-23 16:08:51 +00:00
$jobsTable = new \CSY2028\DatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \CSY2028\DatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
2023-01-23 18:34:00 +00:00
$usersTable = new \CSY2028\DatabaseTable('users', 'id', '\jobs\Entity\User');
2023-01-21 23:12:42 +00:00
$controllers = [];
//TODO: Add Controllers
2023-01-23 17:50:22 +00:00
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable, $appsTable);
2023-01-25 15:35:48 +00:00
$controllers['portal'] = new \jobs\controllers\Portal($catsTable, $jobsTable, $appsTable);
2023-01-25 13:22:20 +00:00
$controllers['user'] = new \jobs\controllers\User($usersTable, $catsTable);
2023-01-25 16:22:23 +00:00
$this->checkLogin($controllerName);
2023-01-25 13:22:20 +00:00
if (array_key_exists($controllerName, $controllers)) {
if (\method_exists($controllers[$controllerName], $functionName)) {
return $controllers[$controllerName];
}
else {
return null;
}
2023-01-23 17:50:22 +00:00
}
else {
return null;
}
2023-01-21 23:12:42 +00:00
}
public function getDefaultRoute() {
return 'jobs/home';
2023-01-21 23:12:42 +00:00
}
2023-01-25 16:22:23 +00:00
public function checkLogin($name) {
2023-01-21 23:12:42 +00:00
$loginRoutes = [];
//TODO: Add login routes
2023-01-25 16:22:23 +00:00
$loginRoutes['portal'] = true;
$requiresLogin = $loginRoutes[$name] ?? false;
2023-01-21 23:12:42 +00:00
2023-01-25 13:22:20 +00:00
if ($requiresLogin && !isset($_SESSION['loggedin'])) {
header('location: /user/login');
2023-01-21 23:12:42 +00:00
exit();
}
}
2023-01-23 17:50:22 +00:00
public function notFound() {
$cats = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
return ['template' => 'response.html.php',
'title' => 'Jo\'s Jobs- 404 Not Found',
'vars' => ['cats' => $cats->findAll(),
'response' => '404 Page Not Found']
];
}
public function nav() {
$cats = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
return ['template' => 'nav.html.php',
'vars' => ['cats' => $cats->findAll()]
];
}
2023-01-25 17:14:23 +00:00
}
?>