CSY2028-assignment-2/jobs/controllers/Portal.php

268 lines
9.8 KiB
PHP
Raw Normal View History

2023-01-25 15:38:19 +00:00
<?php
namespace jobs\controllers;
class Portal {
private $catsTable;
private $jobsTable;
private $appsTable;
2023-02-05 15:36:11 +00:00
private $usersTable;
2023-02-05 16:13:36 +00:00
private $enquiryTable;
2023-01-25 15:38:19 +00:00
private $vars;
2023-02-05 16:13:36 +00:00
public function __construct(\jobs\JobDatabaseTable $catsTable, \jobs\JobDatabaseTable $jobsTable, \jobs\JobDatabaseTable $appsTable, \jobs\JobDatabaseTable $usersTable, \jobs\JobDatabaseTable $enquiryTable) {
2023-01-25 15:38:19 +00:00
$this->catsTable = $catsTable;
$this->jobsTable = $jobsTable;
$this->appsTable = $appsTable;
2023-02-05 15:36:11 +00:00
$this->usersTable = $usersTable;
2023-02-05 16:13:36 +00:00
$this->enquiryTable = $enquiryTable;
2023-01-25 15:38:19 +00:00
$this->vars['cats'] = $this->catsTable->findAll();
$this->vars['table'] = 'job_table.html.php';
}
public function home() {
$this->vars['table'] = 'job_table.html.php';
2023-01-25 16:16:14 +00:00
if (isset($_GET['filter'])) {
if ($_SESSION['userType'] == 'client') {
2023-02-04 22:19:29 +00:00
$this->vars['jobs'] = $this->jobsTable->find(['clientId', 'categoryId'], ['value0' => $_SESSION['loggedin'],'value1' => $_GET['filter']]);
2023-01-25 16:16:14 +00:00
}
else {
2023-02-04 22:19:29 +00:00
$this->vars['jobs'] = $this->jobsTable->find(["categoryId"], ['value0' => $_GET['filter']]);
2023-01-25 16:16:14 +00:00
}
}
2023-01-25 15:38:19 +00:00
else {
2023-01-25 16:16:14 +00:00
if ($_SESSION['userType'] == 'client') {
2023-02-04 22:19:29 +00:00
$this->vars['jobs'] = $this->jobsTable->find(['clientId'], ['value0' => $_SESSION['loggedin']]);
2023-01-25 16:16:14 +00:00
}
else {
$this->vars['jobs'] = $this->jobsTable->findAll();
}
2023-01-25 15:38:19 +00:00
}
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Jobs',
'vars' => $this->vars];
}
public function homeSubmit() {
2023-02-05 14:09:02 +00:00
if ($_POST['submit'] == "List") {
$this->vars['job'] = $this->jobsTable->find(['id'], ['value0' => $_POST['job_id']])[0];
2023-02-05 14:58:16 +00:00
$this->vars['archive'] = true;
$this->vars['update'] = true;
return ['template' => 'job_add.html.php',
2023-02-05 14:09:02 +00:00
'title' => 'Jo\'s Jobs- Update Job',
'vars' => $this->vars];
2023-01-25 15:38:19 +00:00
}
2023-02-05 13:50:57 +00:00
else {
if (isset($_POST['job_id'])) {
2023-02-05 13:50:57 +00:00
$record = [
'id' => $_POST['job_id'],
'archived' => 'y'
];
$this->jobsTable->save($record);
return $this->home();
}
if (isset($_POST['cat_id'])) {
$this->catsTable->delete("id", $_POST['cat_id']);
2023-02-05 14:58:16 +00:00
$jobs = $this->jobsTable->find(['categoryId'], ['value0' => $_POST['cat_id']]);
foreach ($jobs as $job) {
$this->jobsTable->delete("id", $job->id);
}
return $this->categories();
}
2023-02-05 15:36:11 +00:00
if (isset($_POST['user_id'])) {
if($_POST['user_type'] == 'client') {
$this->usersTable->delete('id', $_POST['user_id']);
$jobs = $this->jobsTable->find(['clientId'], ['value0' => $_POST['user_id']]);
foreach ($jobs as $job) {
$this->jobsTable->delete('id', $job->id);
}
return $this->users();
}
}
2023-01-25 15:38:19 +00:00
}
}
public function categories() {
if ($_SESSION['userType'] == 'admin') {
$this->vars['table'] = 'category_table.html.php';
$this->vars['cats'] = $this->catsTable->findAll();
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Categories',
'vars' => $this->vars];
}
}
public function applicants() {
2023-02-05 12:11:41 +00:00
$job = $this->jobsTable->find(['id'], ['value0' => $_GET['job_id']])[0];
2023-01-25 15:38:19 +00:00
$this->vars['table'] = 'applicant_table.html.php';
2023-02-05 12:11:41 +00:00
$this->vars['apps'] = $job->getApps();
$this->vars['job'] = $job->title;
2023-01-25 15:38:19 +00:00
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Applicants',
'vars' => $this->vars];
}
2023-02-05 15:36:11 +00:00
public function users() {
if ($_SESSION['userType'] == 'admin') {
$this->vars['table'] = 'user_table.html.php';
$this->vars['users'] = $this->usersTable->findAll();
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Users',
'vars' => $this->vars
];
}
}
2023-02-05 16:13:36 +00:00
public function enquiries() {
if ($_SESSION['userType'] == 'admin') {
$this->vars['table'] = 'enquiry_table.html.php';
$this->vars['enqs'] = $this->enquiryTable->findAll();
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Enquiries',
'vars' => $this->vars
];
}
}
public function enquiriesSubmit() {
$record = [
'id' => $_POST['enq_id'],
'completed' => 'y',
'admin_id' => $_SESSION['loggedin']
];
$this->enquiryTable->save($record);
$this->enquiries();
}
2023-02-05 15:36:11 +00:00
public function addUser() {
if ($_SESSION['userType'] == 'admin') {
if (isset($_GET['user_id'])) {
$this->vars['user'] = $this->usersTable->find(['id'], ['value0' => $_GET['user_id']])[0];
$this->vars['update'] = true;
}
else {
$this->vars['update'] = false;
}
return ['template' => 'user_add.html.php',
'title' => 'Jo\'s Jobs- Edit user',
'vars' => $this->vars
];
}
}
public function addUserSubmit() {
if ($_SESSION['userType'] == 'admin') {
$record = [
'username' => $_POST['username'],
'password' => password_hash($_POST['password'], PASSWORD_DEFAULT),
'userType' => $_POST['type']
];
if ($_POST['submit'] == 'Update') {
$record['id'] = $_POST['user_id'];
$this->vars['response'] = 'User Updated Successfully';
}
else {
$this->vars['response'] = 'User Created Successfully';
}
$this->usersTable->save($record);
return [
'template' => 'response.html.php',
'title' => 'Jo\'s Jobs- Edit user',
'vars' => $this->vars
];
}
2023-01-25 15:38:19 +00:00
}
2023-01-25 17:14:23 +00:00
2023-02-04 21:34:17 +00:00
public function addJob() {
2023-02-05 14:58:16 +00:00
if (isset($_GET['job_id'])) {
$this->vars['job'] = $this->jobsTable->find(["id"], ['value0' => $_GET['job_id']])[0];
$this->vars['archive'] = false;
$this->vars['update'] = true;
}
else {
$this->vars['archive'] = false;
$this->vars['update'] = false;
}
2023-02-05 12:55:06 +00:00
return ['template' => 'job_add.html.php',
2023-02-05 15:36:11 +00:00
'title' => 'Jo\'s Jobs- Edit Job',
2023-02-05 12:55:06 +00:00
'vars' => $this->vars
];
}
public function addJobSubmit() {
2023-02-05 14:58:16 +00:00
if ($this->catsTable->find(['name'], ['value0' => $_POST['categoryName']]) != 0) {
2023-02-05 12:55:06 +00:00
$record = [
'title' => $_POST['title'],
'description' => $_POST['description'],
'salary' => $_POST['salary'],
'closingDate' => $_POST['closingDate'],
'categoryId' => $this->catsTable->find(['name'], ['value0' => $_POST['categoryName']])[0]->id,
'location' => $_POST['location'],
2023-02-05 14:58:16 +00:00
'clientId' => $_POST['client_id'],
'archived' => $_POST['archived']
2023-02-05 12:55:06 +00:00
];
2023-02-05 14:58:16 +00:00
if ($_POST['submit'] == 'Create' && count($this->jobsTable->find(['title', 'clientId'], ['value0' => $_POST['title'], 'value1' => $_POST['client_id']])) == 0) {
$this->jobsTable->save($record);
$this->vars['response'] = 'Job made successfully';
}
else if ($_POST['submit'] == 'Update') {
$record['id'] = $_POST['jobId'];
$this->jobsTable->save($record);
$this->vars['response'] = 'Job updated successfully';
}
2023-02-05 12:55:06 +00:00
}
else {
$this->vars['response'] = 'Some data was incorrect';
}
return ['template' => 'response.html.php',
2023-02-05 14:58:16 +00:00
'title' => 'Jo\'s Jobs- Edit Job',
2023-02-05 12:30:28 +00:00
'vars' => $this->vars
];
}
public function addCategory() {
if ($_SESSION['userType'] == 'admin') {
2023-02-05 15:05:54 +00:00
if (isset($_GET['cat_id'])) {
$this->vars['cat'] = $this->catsTable->find(["id"], ['value0' => $_GET['cat_id']])[0];
$this->vars['update'] = true;
}
else {
$this->vars['update'] = false;
}
2023-02-05 12:30:28 +00:00
return ['template' => 'category_add.html.php',
2023-02-05 15:36:11 +00:00
'title' => 'Jo\'s Jobs- Edit Category',
2023-02-05 12:30:28 +00:00
'vars' => $this->vars
];
}
}
public function addCategorySubmit() {
if ($_SESSION['userType'] == 'admin') {
2023-02-05 15:05:54 +00:00
if ($_POST['submit'] == 'Create') {
if (count($this->catsTable->find(['name'], ['value0' => $_POST['name']])) > 0) {
$this->vars['response'] = 'This category already exists';
}
else {
$record = [
'name' => $_POST['name']
];
$this->catsTable->save($record);
$this->vars['response'] = 'Category Created';
}
2023-02-05 12:30:28 +00:00
}
else {
$record = [
2023-02-05 15:05:54 +00:00
'id' => $_POST['id'],
2023-02-05 12:30:28 +00:00
'name' => $_POST['name']
];
$this->catsTable->save($record);
2023-02-05 15:05:54 +00:00
$this->vars['response'] = 'Category Updated';
2023-02-05 12:30:28 +00:00
}
return ['template' => 'response.html.php',
2023-02-05 15:05:54 +00:00
'title' => 'Jo\'s Jobs- Edit Category',
2023-02-05 12:30:28 +00:00
'vars' => $this->vars
];
}
2023-02-04 21:34:17 +00:00
}
2023-01-25 17:14:23 +00:00
}
?>