Skip to content

Commit

Permalink
Application: Documented model class
Browse files Browse the repository at this point in the history
  • Loading branch information
fet1sov authored Jun 4, 2024
1 parent 07013ab commit 3a851df
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions www/backend/models/application.mdl.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
<?php

/**
* application.php
* Entity model of advertisment applications
*
* @author fet1sov <prodaugust21@gmail.com>
*/
class Application extends DatabaseEntity {
public $id = 0;
public $authorId = 0;
public $managerId = 0;
public $text = "";
public $status = 0;

/**
* Creates the object of applications
* If ID will be equal zero - model will create a new row
* with applicationData inside
* @example
* Creating object with a new Application
* $application = new Application(0, [
* "author_id" => "1",
* "manager_id" => null,
* "text" => "EXAMPLE TEXT",
* "status" => "1"
* ]);
*
* Creating object with a existing Application
* $application = new Application(5);
*/
public function __construct($id = 0, $applicationData = array()) {
$this->id = $id;

Expand Down Expand Up @@ -70,6 +92,10 @@ public static function getFullList() {
return $applicationsList;
}

/**
* Getting user applications by indetifier (id)
* @param int $userID User ID
*/
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`=`manager_user`.`id` WHERE `applications`.`author_id`=?");
$stmt->bind_param('i', $userID);
Expand All @@ -85,11 +111,24 @@ public static function fetchByUserId($userID) {
return $applicationsList;
}

/**
* Updates application status by indetifier (id)
* @param int $statusID Status ID
*/
public function setStatus($statusID) : void
{

$stmt = $GLOBALS["dbAdapter"]->prepare("UPDATE `applications` SET `status`=? WHERE `id`=?");
$stmt->bind_param(
'ii',
$this->status,
$this->id
);
$stmt->execute();
}

/**
* Getting current status information
*/
public function getStatus() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `application_statuses` WHERE `id`=?");
$stmt->bind_param('i', $this->status);
Expand All @@ -99,6 +138,10 @@ public function getStatus() : ?array {
return $statusResult->fetch_array(MYSQLI_ASSOC);
}

/**
* Static method which returns
* All status list
*/
public static function getStatusList() : ?array
{
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `application_statuses`");
Expand All @@ -114,6 +157,9 @@ public static function getStatusList() : ?array
return $statusesList;
}

/**
* Getting all user data by author indetifier (id)
*/
public function getAuthorInfo() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `user` WHERE `id`=?");
$stmt->bind_param('i', $this->authorId);
Expand All @@ -123,6 +169,9 @@ public function getAuthorInfo() : ?array {
return $userResult->fetch_array(MYSQLI_ASSOC);
}

/**
* Getting all user data by manager indetifier (id)
*/
public function getManagerInfo() : ?array {
$stmt = $GLOBALS["dbAdapter"]->prepare("SELECT * FROM `user` WHERE `id`=?");
$stmt->bind_param('i', $this->managerId);
Expand All @@ -132,6 +181,9 @@ public function getManagerInfo() : ?array {
return $userResult->fetch_array(MYSQLI_ASSOC);
}

/**
* Method which updates all attribute of application
*/
public function saveData() : void
{
if ($this->managerId == -1)
Expand Down Expand Up @@ -159,6 +211,9 @@ public function saveData() : void
}
}

/**
* Removes the application object from the database
*/
public function remove() : void
{
$stmt = $GLOBALS["dbAdapter"]->prepare("DELETE FROM `applications` WHERE `id`=?");
Expand All @@ -168,4 +223,4 @@ public function remove() : void
);
$stmt->execute();
}
}
}

0 comments on commit 3a851df

Please sign in to comment.