Skip to content

Commit

Permalink
Applications logic
Browse files Browse the repository at this point in the history
Backend:
  - Added the application logic
  - Added the update process of application data

Frontend:
  - Added form for application information serving
  - Added form of application edit process for the admins
  • Loading branch information
fet1sov committed May 30, 2024
1 parent 625102a commit 3909f87
Show file tree
Hide file tree
Showing 16 changed files with 608 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Ignoring all uploads on the website
www/uploads/avatars/*.png
!www/uploads/avatars/no_avatar.png
!www/uploads/avatars/no_avatar.png
.vscode
10 changes: 5 additions & 5 deletions database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ CREATE TABLE IF NOT EXISTS `applications` (
PRIMARY KEY (`id`)
);

INSERT INTO `role`(`name`, `admin_right`, `applications_list`) VALUES('administrator', '1', '1');
INSERT INTO `role`(`name`, `admin_right`, `applications_list`) VALUES('manager', '0', '1');
INSERT INTO `applications_statuses`(`name`, `color`) VALUES('waiting', '#ffd500');
INSERT INTO `applications_statuses`(`name`, `color`) VALUES('desclined', '#FF0000');
INSERT INTO `applications_statuses`(`name`, `color`) VALUES('success', '#00FF00')
INSERT INTO `role`(`name`, `admin_rights`, `applications_list`) VALUES('administrator', '1', '1');
INSERT INTO `role`(`name`, `admin_rights`, `applications_list`) VALUES('manager', '0', '1');
INSERT INTO `application_statuses`(`name`, `color`) VALUES('waiting', '#ffd500');
INSERT INTO `application_statuses`(`name`, `color`) VALUES('desclined', '#FF0000');
INSERT INTO `application_statuses`(`name`, `color`) VALUES('success', '#00FF00');
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
volumes:
- ./www:/var/www/html
- ./apache-conf.conf:/etc/apache2/sites-enabled/000-default.conf
- ./www/data/uploads:/var/www/html/data/uploads/
develop:
watch:
- path: www/
Expand Down
49 changes: 46 additions & 3 deletions www/_styles/_css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ span.red-text {
background-color: var(--secondary-color-02);
}

textarea {
outline: none;
border: none;

width: 100%;
resize: none;

margin-top: 20px;
margin-bottom: 20px;

background-color: var(--secondary-color-04);
border-radius: 10px;

padding: 10px;
}

select {
background-color: var(--secondary-color-04);
border: none;
border-radius: 10px;

outline: none;
padding: 10px;
}

body {
background-color: var(--secondary-color-01);

Expand All @@ -109,6 +134,27 @@ header {
animation-duration: 0.5s;
}

table, th, td {
text-align: center;
border: 10px transparent;

text-align: center;
}

tr td {
display: flex;
align-items: center;

text-align: center;
}

td, th {
text-align: center;
vertical-align: middle;

padding: 10px;
}

header nav.navigation {
position: fixed;

Expand Down Expand Up @@ -242,9 +288,6 @@ table tbody tr {

table tbody :first-child th {
font-weight: bold;
margin-right: 10px;
margin-left: 10px;

border-bottom: 2px #000000;
}

Expand Down
125 changes: 113 additions & 12 deletions www/backend/models/application.mdl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,157 @@

class Application extends DatabaseEntity {
public $id = 0;
public $authorName = "";
public $managerName = "";
public $authorId = 0;
public $managerId = 0;
public $text = "";
public $status = 0;

public function __construct($id = 0, $applicationData = array()) {
$this->id = $id;

$createRowFlag = false;
if ($id >= 1)
{
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT COUNT(`id`) FROM `application` WHERE `id`=?");
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT COUNT(`id`) FROM `applications` WHERE `id`=?");
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows() >= 1)
{
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `user` WHERE `id`=?");
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `applications` WHERE `applications`.`id`=?");
$stmt->bind_param('i', $id);
$stmt->execute();

$applicationRes = $stmt->get_result();
$applicationRow = $applicationRes->fetch_array(MYSQLI_ASSOC);

$this->id = $applicationRow["id"];
$this->authorId = $applicationRow["author_id"];
$this->managerId = $applicationRow["manager_id"];
$this->text = $applicationRow["text"];
$this->status = $applicationRow["status"];
} else {
//if (!count($userData))
//{
$createRowFlag = true;
//}
$createRowFlag = true;
}
} else {
$createRowFlag = true;
}

if ($createRowFlag)
{

$stmt = $GLOBALS["dbAdapter"]->prepare("INSERT INTO `applications`(`author_id`, `creation_date`, `text`) VALUES (?, ?, ?)");
$stmt->bind_param(
'iis',
$applicationData["author_id"],
$applicationData["creation_date"],
$applicationData["text"]
);
$stmt->execute();

$this->id = $GLOBALS["dbAdapter"]->insert_id;
$this->authorId = $applicationData["author_id"];
$this->text = $applicationData["text"];
}
}

public static function getFullList() {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT `applications`.*, `user`.`username` AS `user_author`, `user`.`username` AS `user_manager`, `application_statuses`.`name` FROM `applications` INNER JOIN `application_statuses` ON `applications`.`status` = `application_statuses`.`id` INNER JOIN `user` ON `applications`.`author_id` = `user`.`id` LEFT JOIN `user` manager_user ON `applications`.`manager_id`=`user`.`id`");
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT `applications`.*, `user`.`username` AS `user_author`, `manager_user`.`username` AS `user_manager`, `application_statuses`.`name` AS `status_name` FROM `applications` LEFT JOIN `application_statuses` ON `applications`.`status` = `application_statuses`.`id` LEFT JOIN `user` ON `applications`.`author_id` = `user`.`id` LEFT JOIN `user` manager_user ON `applications`.`manager_id`=`user`.`id`");
$stmt->execute();

$applicationsList = array();

$applicationsResult = $stmt->get_result();
return $applicationsResult->fetch_array(MYSQLI_ASSOC);
while ($row = $applicationsResult->fetch_array(MYSQLI_ASSOC)) {
array_push($applicationsList, $row);
}

return $applicationsList;
}

public function saveData() : void
public static function fetchByUserId($userID) {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT `applications`.*, `user`.`username` AS `user_author`, `manager_user`.`username` AS `user_manager`, `application_statuses`.`name` AS `status_name` FROM `applications` LEFT JOIN `application_statuses` ON `applications`.`status` = `application_statuses`.`id` LEFT JOIN `user` ON `applications`.`author_id` = `user`.`id` LEFT JOIN `user` manager_user ON `applications`.`manager_id`=`user`.`id` WHERE `applications`.`author_id`=?");
$stmt->bind_param('i', $userID);
$stmt->execute();

$applicationsList = array();

$applicationsResult = $stmt->get_result();
while ($row = $applicationsResult->fetch_array(MYSQLI_ASSOC)) {
array_push($applicationsList, $row);
}

return $applicationsList;
}

public function setStatus($statusID) : void
{

}

public function getStatus() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `application_statuses` WHERE `id`=?");
$stmt->bind_param('i', $this->status);
$stmt->execute();

$statusResult = $stmt->get_result();
return $statusResult->fetch_array(MYSQLI_ASSOC);
}

public static function getStatusList() : ?array
{
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `application_statuses`");
$stmt->execute();

$statusesList = array();

$statusResult = $stmt->get_result();
while ($row = $statusResult->fetch_array(MYSQLI_ASSOC)) {
array_push($statusesList, $row);
}

return $statusesList;
}

public function getAuthorInfo() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `user` WHERE `id`=?");
$stmt->bind_param('i', $this->authorId);
$stmt->execute();

$userResult = $stmt->get_result();
return $userResult->fetch_array(MYSQLI_ASSOC);
}

public function getManagerInfo() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `user` WHERE `id`=?");
$stmt->bind_param('i', $this->managerId);
$stmt->execute();

$userResult = $stmt->get_result();
return $userResult->fetch_array(MYSQLI_ASSOC);
}

public function saveData() : void
{
$stmt = $GLOBALS["dbAdapter"]->prepare("UPDATE `applications` SET `author_id`=?, `manager_id`=?, `status`=?, `text`=? WHERE `id`=?");
$stmt->bind_param(
'iiisi',
$this->authorId,
$this->managerId,
$this->status,
$this->text,
$this->id
);
$stmt->execute();
}

public function remove() : void
{
$stmt = $GLOBALS["dbAdapter"]->prepare("DELETE FROM `applications` WHERE `id`=?");
$stmt->bind_param(
'i',
$this->id
);
$stmt->execute();
}
}
20 changes: 17 additions & 3 deletions www/backend/models/user.mdl.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct($id = 0, $userData = array()) {
$this->password = $row["password"];
$this->company = $row["company"];
$this->email = $row["email"];
$this->roleid = $row["role_id"] ? $row["role_id"] : 0;
$this->roleid = $row["role_id"];
} else {
if (!count($userData))
{
Expand Down Expand Up @@ -73,15 +73,29 @@ public function __construct($id = 0, $userData = array()) {
}
}

public function getPermissions() : array {
public function getPermissions() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT `role`.`admin_rights`, `role`.`applications_list` FROM `user` INNER JOIN `role` ON `user`.`role_id` = `role`.`id` WHERE `user`.`id`=?");
$stmt->bind_param('i', $this->id);
$stmt->bind_param('i', $this->roleid);
$stmt->execute();

$rightResult = $stmt->get_result();
return $rightResult->fetch_array(MYSQLI_ASSOC);
}

public static function getFullList($condition = "") {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT `role`.`admin_rights`, `role`.`applications_list`, `user`.* FROM `user` LEFT JOIN `role` ON `user`.`role_id` = `role`.`id` " . $condition);
$stmt->execute();

$userList = array();

$userResult = $stmt->get_result();
while ($row = $userResult->fetch_array(MYSQLI_ASSOC)) {
array_push($userList, $row);
}

return $userList;
}

public function saveData() : void
{
$stmt = $GLOBALS["dbAdapter"]->prepare("UPDATE `user` SET `username`=?, `password`=?, `company`=?, `email`=? WHERE `id`=?");
Expand Down
77 changes: 68 additions & 9 deletions www/backend/routes/admin/admin.ctrl.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
<?php

$applications = Application::getFullList();

Renderer::includeTemplate("frontend/components/layout.php", [
"layout_path" => ROUTE_ROOT . "admin/admin.view.php",
"layout_data" => [
"footerShow" => false,
"applications" => $applications ? $applications : []
]
]);
if (!isset($_SESSION["userData"])) {
header('Location: /');
}

$userData = unserialize($_SESSION["userData"]);

if($userData->roleid == NULL
&& !$userData->getPermissions()["admin_rights"])
{
header('Location: /');
die();
}

if (isset($params['category'])) {
switch ($params['category']) {
case "application": {
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
$application = new Application(intval($_GET["id"]));

Renderer::includeTemplate("frontend/components/layout.php", [
"layout_path" => ROUTE_ROOT . "admin/admin.view.php",
"layout_data" => [
"footerShow" => false,
"category" => $params['category'],
"applicationData" => $application,
"authorData" => $application->getAuthorInfo(),
"managerList" => User::getFullList("WHERE `role`.`admin_rights`=1"),
"statusList" => Application::getStatusList(),
]
]);
} else {
$application = new Application(intval($_POST["id"]));

if ($_POST["action"] == "update")
{
$application->status = intval($_POST["statuses"]);
$application->managerId = intval($_POST["manager"]);

$application->saveData();
} else {
$application->remove();
}


Renderer::includeTemplate("frontend/components/layout.php", [
"layout_path" => ROUTE_ROOT . "admin/admin.view.php",
"layout_data" => [
"footerShow" => false,
"category" => $params['category'],
"applicationData" => $application,
"authorData" => $application->getAuthorInfo(),
"managerList" => User::getFullList("WHERE `role`.`admin_rights`=1"),
"statusList" => Application::getStatusList(),
]
]);
}
}
}
} else {
Renderer::includeTemplate("frontend/components/layout.php", [
"layout_path" => ROUTE_ROOT . "admin/admin.view.php",
"layout_data" => [
"footerShow" => false,
"applications" => Application::getFullList()
]
]);
}
Loading

0 comments on commit 3909f87

Please sign in to comment.