2021-09-28 11:29:26 +01:00
< ? php
2023-10-18 23:58:21 +01:00
2023-06-08 15:10:27 +01:00
namespace api\project ;
2023-10-18 23:58:21 +01:00
2023-06-26 03:54:25 +01:00
use api\utils\imgUtils ;
2021-12-28 21:22:32 +00:00
use PDO ;
2023-02-06 01:26:35 +00:00
use Psr\Http\Message\UploadedFileInterface ;
2021-12-28 21:22:32 +00:00
2023-06-08 15:10:27 +01:00
require_once __DIR__ . " /../utils/config.php " ;
2023-06-26 03:54:25 +01:00
require_once __DIR__ . " /../utils/imgUtils.php " ;
2021-09-28 11:29:26 +01:00
/**
* Project Data Class
* Define all functions which either get , update , create or delete timeline data
*/
2021-12-28 21:22:32 +00:00
class projectData
2021-09-28 11:29:26 +01:00
{
2022-10-09 02:40:06 +01:00
/**
* Get all project data
2023-11-14 01:02:27 +00:00
* @ return array < array > - Array of all project data or error message
2022-10-09 02:40:06 +01:00
*/
2023-06-26 03:54:25 +01:00
public function getProjectData () : array
2021-09-28 11:29:26 +01:00
{
$conn = dbConn ();
2023-02-07 03:09:14 +00:00
$stmt = $conn -> prepare ( " SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects ORDER BY isMainProject DESC; " );
2021-09-28 11:29:26 +01:00
$stmt -> execute ();
// set the resulting array to associative
$result = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
if ( $result )
{
return $result ;
}
2023-02-06 01:26:35 +00:00
2022-07-29 20:00:36 +01:00
return array ( " errorMessage " => " Error, project data not found " );
2021-09-28 11:29:26 +01:00
}
2022-11-11 13:56:42 +00:00
2023-02-06 01:26:35 +00:00
/**
* Update project data in the database with the given ID
* @ param string $ID - ID of the project in the database to update
* @ param string $title - Title of the project
2023-07-12 03:29:56 +01:00
* @ param bool $isMainProject - Is the project a main project or not
2023-02-06 01:26:35 +00:00
* @ param string $information - Information about the project
* @ param string $projectLink - Link to the project
2023-02-07 03:09:14 +00:00
* @ param string $gitLink - Link to the git repository
2023-02-23 18:53:28 +00:00
* @ return bool | string - True if project was updated , false if not and there was an error , or an error string
2023-02-06 01:26:35 +00:00
*/
2023-07-12 03:29:56 +01:00
public function updateProjectData ( string $ID , string $title , bool $isMainProject , string $information , string $projectLink , string $gitLink ) : bool | string
2022-11-11 13:56:42 +00:00
{
$conn = dbConn ();
2023-02-07 03:09:14 +00:00
2023-07-12 03:29:56 +01:00
$stmtMainProject = $conn -> prepare ( " SELECT isMainProject FROM projects WHERE ID = :ID " );
$stmtMainProject -> bindParam ( " :ID " , $ID );
$stmtMainProject -> execute ();
$result = $stmtMainProject -> fetch ( PDO :: FETCH_ASSOC );
if ( ! $result )
2023-02-23 18:53:28 +00:00
{
2023-07-12 03:29:56 +01:00
return " project not found " ;
}
2023-02-23 18:53:28 +00:00
2023-07-12 03:29:56 +01:00
if ( ! $isMainProject && $result [ " isMainProject " ] === " 1 " )
{
return " unset main project " ;
2023-02-23 18:53:28 +00:00
}
2023-07-12 03:29:56 +01:00
if ( $isMainProject )
2023-02-07 03:09:14 +00:00
{
2023-06-26 18:11:43 +01:00
$stmtMainProject = $conn -> prepare ( " UPDATE projects SET isMainProject = 0 WHERE isMainProject = 1; " );
2023-02-07 03:09:14 +00:00
$stmtMainProject -> execute ();
}
$stmt = $conn -> prepare ( " UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID " );
2022-11-11 13:56:42 +00:00
$stmt -> bindParam ( " :title " , $title );
2023-07-12 03:29:56 +01:00
$isMainProj = $isMainProject ? 1 : 0 ;
2023-02-07 03:09:14 +00:00
$stmt -> bindParam ( " :isMainProject " , $isMainProj );
2022-11-11 13:56:42 +00:00
$stmt -> bindParam ( " :information " , $information );
$stmt -> bindParam ( " :projectLink " , $projectLink );
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :gitLink " , $gitLink );
$stmt -> bindParam ( " :ID " , $ID );
2023-02-07 03:09:14 +00:00
return $stmt -> execute ();
2022-11-11 13:56:42 +00:00
}
2023-02-06 01:26:35 +00:00
/**
* Delete project data from the database
* @ param int $ID - ID of the project in the database to delete
2023-02-07 03:09:14 +00:00
* @ return string - True if project was deleted , false if not and there was an error
2023-02-06 01:26:35 +00:00
*/
2023-06-26 03:54:25 +01:00
public function deleteProjectData ( int $ID ) : string
2022-11-11 13:56:42 +00:00
{
$conn = dbConn ();
2023-02-07 03:09:14 +00:00
// check if the project is a main project if it is return false
$stmtMainProject = $conn -> prepare ( " SELECT isMainProject FROM projects WHERE ID = :ID " );
$stmtMainProject -> bindParam ( " :ID " , $ID );
$stmtMainProject -> execute ();
$result = $stmtMainProject -> fetch ( PDO :: FETCH_ASSOC );
2023-07-12 03:29:56 +01:00
if ( ! $result )
{
return " project not found " ;
}
2023-02-07 03:09:14 +00:00
if ( $result [ " isMainProject " ] === " 1 " )
{
return " cannot delete " ;
}
$this -> deleteImage ( $ID );
2023-02-06 01:26:35 +00:00
$stmt = $conn -> prepare ( " DELETE FROM projects WHERE ID = :ID " );
$stmt -> bindParam ( " :ID " , $ID );
2022-11-11 13:56:42 +00:00
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
2023-02-07 03:09:14 +00:00
return " ok " ;
2022-11-11 13:56:42 +00:00
}
2023-02-06 01:26:35 +00:00
2023-02-07 03:09:14 +00:00
return " error " ;
2022-11-11 13:56:42 +00:00
}
2023-02-06 01:26:35 +00:00
/**
* Add project data to the database
* @ param string $title - Title of the project
* @ param string $isMainProject - Is the project a main project or not
* @ param string $information - Information about the project
* @ param string $projectLink - Link to the project
* @ param string $gitLink - Link to the github repository
* @ return int | bool - ID of the project if it was added , false if not and there was an error
*/
2023-06-26 03:54:25 +01:00
public function addProjectData ( string $title , string $isMainProject , string $information , string $projectLink , string $gitLink ) : int | bool
2022-11-11 13:56:42 +00:00
{
2023-02-07 03:09:14 +00:00
2022-11-11 13:56:42 +00:00
$conn = dbConn ();
2023-02-07 03:09:14 +00:00
if ( $isMainProject === " true " )
{
2023-06-26 18:11:43 +01:00
$stmtMainProject = $conn -> prepare ( " UPDATE projects SET isMainProject = 0 WHERE isMainProject = 1; " );
2023-02-07 03:09:14 +00:00
$stmtMainProject -> execute ();
}
2023-02-06 01:26:35 +00:00
$stmt = $conn -> prepare ( " INSERT INTO projects (title, isMainProject, information, projectLink, gitLink) VALUES (:title, :isMainProject, :information, :projectLink, :gitLink) " );
2022-11-11 13:56:42 +00:00
$stmt -> bindParam ( " :title " , $title );
2023-02-23 18:53:28 +00:00
$isMainProj = ( $isMainProject === " true " ) ? 1 : 0 ;
2023-02-07 03:09:14 +00:00
$stmt -> bindParam ( " :isMainProject " , $isMainProj );
2022-11-11 13:56:42 +00:00
$stmt -> bindParam ( " :information " , $information );
$stmt -> bindParam ( " :projectLink " , $projectLink );
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :gitLink " , $gitLink );
2022-11-11 13:56:42 +00:00
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
2023-02-06 01:26:35 +00:00
return $conn -> lastInsertId ();
2022-11-11 13:56:42 +00:00
}
2023-02-06 01:26:35 +00:00
2022-11-11 13:56:42 +00:00
return false ;
}
2023-02-06 01:26:35 +00:00
/**
* Upload the image to the server and update the database with the new image location
* @ param int $ID - ID of the project in the database to update
* @ param UploadedFileInterface $img - Image preview of the project
* @ return string | array - String with error message or array with the new image location
*/
2023-10-18 23:58:21 +01:00
public function uploadImage ( int $ID , UploadedFileInterface $img ) : string | array
2023-02-06 01:26:35 +00:00
{
2023-07-12 03:29:56 +01:00
$conn = dbConn ();
$stmt = $conn -> prepare ( " SELECT ID FROM projects WHERE ID = :ID " );
$stmt -> bindParam ( " :ID " , $ID );
$stmt -> execute ();
$result = $stmt -> fetch ( PDO :: FETCH_ASSOC );
if ( ! $result )
{
return " Project with ID $ID not found " ;
}
2023-02-06 01:26:35 +00:00
$targetDir = " ../imgs/projects/ " ;
2023-06-26 03:54:25 +01:00
$imgUtils = new imgUtils ();
$targetFile = $imgUtils -> uploadFile ( $targetDir , $img );
2023-02-06 01:26:35 +00:00
2023-06-26 03:54:25 +01:00
if ( ! is_array ( $targetFile ))
2023-02-06 01:26:35 +00:00
{
2023-06-26 03:54:25 +01:00
return $targetFile ;
2023-02-06 01:26:35 +00:00
}
2023-06-26 03:54:25 +01:00
if ( file_exists ( $targetFile [ " imgLocation " ]))
2023-02-06 01:26:35 +00:00
{
2023-02-07 03:09:14 +00:00
$this -> deleteImage ( $ID );
2023-02-06 01:26:35 +00:00
// update the database with the new image location
$stmt = $conn -> prepare ( " UPDATE projects SET imgLocation = :imgLocation WHERE ID = :ID " );
2023-06-26 18:08:53 +01:00
$stmt -> bindParam ( " :imgLocation " , $targetFile [ " imgLocation " ]);
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :ID " , $ID );
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
2023-06-26 03:54:25 +01:00
return array ( " imgLocation " => $targetFile [ " imgLocation " ]);
2023-02-06 01:26:35 +00:00
}
return " Couldn't update the database " ;
}
return " Couldn't upload the image " ;
}
2023-02-07 03:09:14 +00:00
/**
* Delete the image from the server
* @ param int $ID - ID of the project in the database
*/
private function deleteImage ( int $ID ) : void
{
$conn = dbConn ();
$imgStmt = $conn -> prepare ( " SELECT imgLocation FROM projects WHERE ID = :ID " );
$imgStmt -> bindParam ( " :ID " , $ID );
$imgStmt -> execute ();
$imgLocation = $imgStmt -> fetch ( PDO :: FETCH_ASSOC )[ " imgLocation " ];
if ( $imgLocation != null )
{
unlink ( $imgLocation );
}
}
2022-03-31 21:19:30 +01:00
}