2021-12-28 21:22:32 +00:00
< ? php
namespace api ;
use PDO ;
2023-02-06 01:26:35 +00:00
use Psr\Http\Message\UploadedFileInterface ;
2021-12-28 21:22:32 +00:00
require_once " ./config.php " ;
/**
* Project Data Class
* Define all functions which either get , update , create or delete timeline data
*/
class projectData
{
2022-10-09 02:40:06 +01:00
/**
* Get all project data
* @ return array - Array of all project data or error message
*/
2022-07-29 20:00:36 +01:00
function getProjectData () : array
2021-12-28 21:22:32 +00:00
{
$conn = dbConn ();
2023-02-06 01:26:35 +00:00
$stmt = $conn -> prepare ( " SELECT ID, title, isMainProject, information, imgLocation, projectLink, gitLink FROM projects; " );
2021-12-28 21:22:32 +00: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-12-28 21:22:32 +00:00
}
2022-11-11 14:04:43 +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
* @ param bool $isMainProject - Is the project a main project or not
* @ param string $information - Information about the project
* @ param string $imgLocation - Location of the image
* @ param string $projectLink - Link to the project
* @ param string $gitLink - Link to the github repository
* @ return bool - True if project was updated , false if not and there was an error
*/
function updateProjectData ( string $ID , string $title , bool $isMainProject , string $information , string $imgLocation , string $projectLink , string $gitLink ) : bool
2022-11-11 14:04:43 +00:00
{
$conn = dbConn ();
2023-02-06 01:26:35 +00:00
$stmt = $conn -> prepare ( " UPDATE projects SET title = :title, isMainProject = :isMainProject, information = :information, imgLocation = :imgLocation, projectLink = :projectLink, gitLink = :gitLink WHERE ID = :ID " );
2022-11-11 14:04:43 +00:00
$stmt -> bindParam ( " :title " , $title );
$stmt -> bindParam ( " :isMainProject " , $isMainProject );
$stmt -> bindParam ( " :information " , $information );
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :imgLocation " , $imgLocation );
2022-11-11 14:04:43 +00:00
$stmt -> bindParam ( " :projectLink " , $projectLink );
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :gitLink " , $gitLink );
$stmt -> bindParam ( " :ID " , $ID );
2022-11-11 14:04:43 +00:00
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
return true ;
}
2023-02-06 01:26:35 +00:00
2022-11-11 14:04:43 +00:00
return false ;
}
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
* @ return bool - True if project was deleted , false if not and there was an error
*/
function deleteProjectData ( int $ID ) : bool
2022-11-11 14:04:43 +00:00
{
$conn = dbConn ();
2023-02-06 01:26:35 +00:00
$stmt = $conn -> prepare ( " DELETE FROM projects WHERE ID = :ID " );
$stmt -> bindParam ( " :ID " , $ID );
2022-11-11 14:04:43 +00:00
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
return true ;
}
2023-02-06 01:26:35 +00:00
2022-11-11 14:04:43 +00:00
return false ;
}
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
*/
function addProjectData ( string $title , string $isMainProject , string $information , string $projectLink , string $gitLink ) : int | bool
2022-11-11 14:04:43 +00:00
{
$conn = dbConn ();
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 14:04:43 +00:00
$stmt -> bindParam ( " :title " , $title );
$stmt -> bindParam ( " :isMainProject " , $isMainProject );
$stmt -> bindParam ( " :information " , $information );
$stmt -> bindParam ( " :projectLink " , $projectLink );
2023-02-06 01:26:35 +00:00
$stmt -> bindParam ( " :gitLink " , $gitLink );
2022-11-11 14:04:43 +00:00
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
2023-02-06 01:26:35 +00:00
return $conn -> lastInsertId ();
2022-11-11 14:04:43 +00:00
}
2023-02-06 01:26:35 +00:00
2022-11-11 14:04:43 +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
*/
public function uploadImage ( int $ID , UploadedFileInterface $img ) : string | array
{
$targetDir = " ../imgs/projects/ " ;
$targetFile = $targetDir . basename ( $img -> getClientFilename ());
$uploadOk = 1 ;
$imageFileType = strtolower ( pathinfo ( $targetFile , PATHINFO_EXTENSION ));
// Check if file already exists
if ( file_exists ( $targetFile ))
{
return " The file already exists " ;
}
// Check file size
if ( $img -> getSize () > 2000000 )
{
return " The file is too large, max 2MB " ;
}
// Allow certain file formats
if ( $imageFileType != " jpg " && $imageFileType != " png " && $imageFileType != " jpeg " && $imageFileType != " gif " )
{
return " Only JPG, JPEG, PNG & GIF files are allowed " ;
}
$img -> moveTo ( $targetFile );
if ( file_exists ( $targetFile ))
{
// update the database with the new image location
$conn = dbConn ();
$stmt = $conn -> prepare ( " UPDATE projects SET imgLocation = :imgLocation WHERE ID = :ID " );
$stmt -> bindParam ( " :imgLocation " , $targetFile );
$stmt -> bindParam ( " :ID " , $ID );
$stmt -> execute ();
if ( $stmt -> rowCount () > 0 )
{
return array ( " imgLocation " => $targetFile );
}
return " Couldn't update the database " ;
}
return " Couldn't upload the image " ;
}
2022-07-29 20:00:36 +01:00
}