updated framework

This commit is contained in:
Joshua Perry 2023-02-04 20:57:47 +00:00
parent 2bbb598b5c
commit 9607c6be3c
2 changed files with 66 additions and 56 deletions

View File

@ -1,8 +1,53 @@
<?php
namespace CSY2028;
interface Routes {
public function getController($controllerName, $functionName);
public function getDefaultRoute();
public function checkLogin($route);
class Routes {
protected $databaseTables;
protected $controllers;
protected $loginControllers;
public function __construct() {
$this->databaseTables = [];
$this->controllers = [];
$this->loginControllers = [];
}
public function getController($controllerName, $functionName) {
$this->checkLogin($controllerName);
if (array_key_exists($controllerName, $this->controllers)) {
if (\method_exists($this->controllers[$controllerName], $functionName)) {
return $this->controllers[$controllerName];
}
else {
return null;
}
}
else {
return null;
}
}
public function getDefaultRoute() {
return 'controller/home';
}
public function checkLogin($name) {
$requiresLogin = $this->loginControllers[$name] ?? false;
if ($requiresLogin && !isset($_SESSION['loggedin'])) {
header('location: /user/login');
exit();
}
}
public function notFound() {
return ['template' => 'response.html.php',
'title' => '404 Not Found',
'vars' => ['response' => '404 Page Not Found']
];
}
}
?>

View File

@ -1,64 +1,29 @@
<?php
namespace jobs;
class Routes implements \CSY2028\Routes {
public function getController($controllerName, $functionName) {
$catsTable = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
$jobsTable = new \jobs\JobDatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \jobs\JobDatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
$usersTable = new \jobs\JobDatabaseTable('users', 'id', '\jobs\Entity\User');
$controllers = [];
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable, $appsTable);
$controllers['portal'] = new \jobs\controllers\Portal($catsTable, $jobsTable, $appsTable);
$controllers['user'] = new \jobs\controllers\User($usersTable, $catsTable);
$this->checkLogin($controllerName);
if (array_key_exists($controllerName, $controllers)) {
if (\method_exists($controllers[$controllerName], $functionName)) {
return $controllers[$controllerName];
}
else {
return null;
}
}
else {
return null;
}
class Routes extends \CSY2028\Routes {
public function __construct() {
setDbTables();
$this->controllers = [
"jobs" => new \jobs\controllers\Jobs($this->databaseTables["jobs"], $this->databaseTables["categories"], $this->databaseTables["applicants"]),
"portal" => new \jobs\controllers\Portal($this->databaseTables["categories"], $this->databaseTables["jobs"], $this->databaseTables["applicants"]),
"user" => new \jobs\controllers\User($this->databaseTables["users"], $this->databaseTables["categories"])
];
$this->loginControllers = [
"portal" => true
];
}
public function getDefaultRoute() {
return 'jobs/home';
}
public function checkLogin($name) {
$loginRoutes = [];
$loginRoutes['portal'] = true;
$requiresLogin = $loginRoutes[$name] ?? false;
if ($requiresLogin && !isset($_SESSION['loggedin'])) {
header('location: /user/login');
exit();
}
}
public function notFound() {
$cats = new \jobs\JobDatabaseTable('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 \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
return ['template' => 'nav.html.php',
'vars' => ['cats' => $cats->findAll()]
];
private function setDbTables() {
$this->databaseTables = [];
$this->databaseTables["categories"] = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category');
$this->databaseTables["jobs"] = new \jobs\JobDatabaseTable('job', 'id', '\jobs\Entity\Job', [$this->databaseTables["categories"]]);
$this->databaseTables["applicants"] = new \jobs\JobDatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$this->databaseTables["jobs"]]);
$this->databaseTables["users"] = new \jobs\JobDatabaseTable('users', 'id', '\jobs\Entity\User');
}
}
?>