refactored db find
This commit is contained in:
parent
650e678d9b
commit
136aac803d
|
|
@ -36,26 +36,16 @@ class DatabaseTable {
|
||||||
$this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
|
$this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find($column, $value, $column2 = "", $value2 = "", $comparator = "=", $comparator2 = "=", $order = "ASC", $orderColumn = "id") {
|
public function find($columns, $values, $comparators = ['=', '='], $order = "ASC", $orderColumn = "id") {
|
||||||
if ($column2 == "" || $value2 == "") {
|
$string = 'SELECT * FROM '.$this->table.' WHERE ';
|
||||||
$values = [
|
for ($i = 0; $i < count($values); $i++) {
|
||||||
'value' => $value
|
$string .= $columns[$i].' '.$comparators[$i].' :value'.$i.' ';
|
||||||
];
|
}
|
||||||
$stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value ORDER BY '.$orderColumn." ".$order);
|
$string .= 'ORDER BY '.$orderColumn.' '.$order;
|
||||||
|
$stmt = $this->pdo->prepare($string);
|
||||||
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
|
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
|
||||||
$stmt->execute($values);
|
$stmt->execute($values);
|
||||||
return $stmt->fetchAll();
|
return $stmt->fetchAll();
|
||||||
}
|
|
||||||
else {
|
|
||||||
$values = [
|
|
||||||
'value' => $value,
|
|
||||||
'value2' => $value2
|
|
||||||
];
|
|
||||||
$stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value AND '.$column2.' '.$comparator2.' :value2 ORDER BY '.$orderColumn." ".$order);
|
|
||||||
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
|
|
||||||
$stmt->execute($values);
|
|
||||||
return $stmt->fetchAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findAll() {
|
public function findAll() {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class Applicant {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getJob() {
|
public function getJob() {
|
||||||
return $this->jobsTable->find('id', $this->jobId)[0];
|
return $this->jobsTable->find(['id'], ['value0' => $this->jobId])[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
@ -16,7 +16,7 @@ class Job {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCat() {
|
public function getCat() {
|
||||||
return $this->catsTable->find('id', $this->categoryId)[0];
|
return $this->catsTable->find(['id'], ['value0' => $this->categoryId])[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
@ -14,7 +14,7 @@ class Jobs {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function home() {
|
public function home() {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find("closingDate", date("y-m-d"), "", "", ">", "", "DESC", "closingDate");
|
$this->vars['jobs'] = $this->jobsTable->find(["closingDate"], ['value0' => date('y-m-d')], ['>'], "DESC", "closingDate");
|
||||||
return ['template' => 'home.html.php',
|
return ['template' => 'home.html.php',
|
||||||
'title' => 'Jo\'s Jobs- Home',
|
'title' => 'Jo\'s Jobs- Home',
|
||||||
'vars' => $this->vars
|
'vars' => $this->vars
|
||||||
|
|
@ -22,16 +22,22 @@ class Jobs {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function category() {
|
public function category() {
|
||||||
$cat = $this->catsTable->find('name', $_GET['page']);
|
$cat = $this->catsTable->find(['name'], ['value0' => $_GET['page']]);
|
||||||
if ($cat == null) {
|
if ($cat == null) {
|
||||||
return $this->notFound();
|
return $this->notFound();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (isset($_GET['filter'])) {
|
if (isset($_GET['filter'])) {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "location", $_GET['filter']);
|
$columns = ['categoryId', "location", 'closingDate'];
|
||||||
|
$values = ['value0' => $cat[0]->id,
|
||||||
|
'value1' => $_GET['filter'],
|
||||||
|
'value2' => date('y-m-d')
|
||||||
|
];
|
||||||
|
$comparators = ["=","=",">"];
|
||||||
|
$this->vars['jobs'] = $this->jobsTable->find($columns, $values, $comparators);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "closingDate", date("y-m-d"), "=", ">");
|
$this->vars['jobs'] = $this->jobsTable->find(['categoryId', "closingDate"], ["value0" => $cat[0]->id, "value1" => date("y-m-d")], ["=", ">"]);
|
||||||
|
|
||||||
}
|
}
|
||||||
$this->vars['heading'] = $cat[0]->name;
|
$this->vars['heading'] = $cat[0]->name;
|
||||||
|
|
@ -59,7 +65,7 @@ class Jobs {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function apply() {
|
public function apply() {
|
||||||
$this->vars['job'] = $this->jobsTable->find('id', $_GET['id'])[0];
|
$this->vars['job'] = $this->jobsTable->find(['id'], ["value0" => $_GET['id']])[0];
|
||||||
return ['template' => 'apply.html.php',
|
return ['template' => 'apply.html.php',
|
||||||
'title' => 'Jo\'s Jobs- Apply',
|
'title' => 'Jo\'s Jobs- Apply',
|
||||||
'vars' => $this->vars];
|
'vars' => $this->vars];
|
||||||
|
|
|
||||||
|
|
@ -18,15 +18,15 @@ class Portal {
|
||||||
$this->vars['table'] = 'job_table.html.php';
|
$this->vars['table'] = 'job_table.html.php';
|
||||||
if (isset($_GET['filter'])) {
|
if (isset($_GET['filter'])) {
|
||||||
if ($_SESSION['userType'] == 'client') {
|
if ($_SESSION['userType'] == 'client') {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin'], "categoryId", $_GET['filter']);
|
$this->vars['jobs'] = $this->jobsTable->find(['clientId', 'categoryId'], ['value0' => $_SESSION['loggedin'],'value1' => $_GET['filter']]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find("categoryId", $_GET['filter']);
|
$this->vars['jobs'] = $this->jobsTable->find(["categoryId"], ['value0' => $_GET['filter']]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ($_SESSION['userType'] == 'client') {
|
if ($_SESSION['userType'] == 'client') {
|
||||||
$this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin']);
|
$this->vars['jobs'] = $this->jobsTable->find(['clientId'], ['value0' => $_SESSION['loggedin']]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->vars['jobs'] = $this->jobsTable->findAll();
|
$this->vars['jobs'] = $this->jobsTable->findAll();
|
||||||
|
|
@ -66,7 +66,7 @@ class Portal {
|
||||||
|
|
||||||
public function applicants() {
|
public function applicants() {
|
||||||
$this->vars['table'] = 'applicant_table.html.php';
|
$this->vars['table'] = 'applicant_table.html.php';
|
||||||
$this->vars['apps'] = $this->appsTable->find('jobId', $_GET['app_id']);
|
$this->vars['apps'] = $this->appsTable->find(['jobId'], ['value0' => $_GET['app_id']]);
|
||||||
return ['template' => 'portal.html.php',
|
return ['template' => 'portal.html.php',
|
||||||
'title' => 'Jo\'s Jobs- Applicants',
|
'title' => 'Jo\'s Jobs- Applicants',
|
||||||
'vars' => $this->vars];
|
'vars' => $this->vars];
|
||||||
|
|
@ -74,10 +74,10 @@ class Portal {
|
||||||
|
|
||||||
public function edit() { //TODO: finish this function
|
public function edit() { //TODO: finish this function
|
||||||
if (isset($_GET['job_id'])) {
|
if (isset($_GET['job_id'])) {
|
||||||
$this->vars['job'] = $this->jobsTable->find("id", $_GET['jod_id']);
|
$this->vars['job'] = $this->jobsTable->find(["id"], ['value0' => $_GET['jod_id']]);
|
||||||
}
|
}
|
||||||
if (isset($_GET['cat_id'])) {
|
if (isset($_GET['cat_id'])) {
|
||||||
$this->vars['cat'] = $this->catsTable->find("id", $_GET['cat_id']);
|
$this->vars['cat'] = $this->catsTable->find(["id"], ['value0' => $_GET['cat_id']]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ class User {
|
||||||
|
|
||||||
public function loginSubmit() {
|
public function loginSubmit() {
|
||||||
if ($_POST['username'] != '' && $_POST['password'] != '') {
|
if ($_POST['username'] != '' && $_POST['password'] != '') {
|
||||||
$user = $this->usersTable->find("username", $_POST['username']);
|
$user = $this->usersTable->find(["username"], ['value0' => $_POST['username']]);
|
||||||
|
|
||||||
if (count($user) > 0 && $_POST['submit'] == 'Register') {
|
if (count($user) > 0 && $_POST['submit'] == 'Register') {
|
||||||
$this->vars['response'] = "Account already exists";
|
$this->vars['response'] = "Account already exists";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue