diff --git a/CSY2028/Routes.php b/CSY2028/Routes.php index 066c1b5..18e9f30 100644 --- a/CSY2028/Routes.php +++ b/CSY2028/Routes.php @@ -1,8 +1,53 @@ 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'] + ]; + } } ?> \ No newline at end of file diff --git a/jobs/Routes.php b/jobs/Routes.php index 1e371c4..07fe6c0 100644 --- a/jobs/Routes.php +++ b/jobs/Routes.php @@ -1,64 +1,29 @@ 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'); } } ?> \ No newline at end of file