Skip to content

Commit

Permalink
Merge pull request #5 from Thunder07/website_ui
Browse files Browse the repository at this point in the history
Website UI update 3
  • Loading branch information
darklightindigo authored May 8, 2018
2 parents b13532c + 3034107 commit bc58004
Show file tree
Hide file tree
Showing 16 changed files with 1,829 additions and 50 deletions.
202 changes: 194 additions & 8 deletions include/TGDB.API.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function getInstance()

function GetGameListByPlatform($IDs = 0, $offset = 0, $limit = 20, $fields = array(), $OrderBy = '', $ASCDESC = 'ASC')
{
$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -107,7 +107,7 @@ function GetGameByID($IDs, $offset = 0, $limit = 20, $fields = array())
return array();
}

$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -141,7 +141,7 @@ function SearchGamesByName($searchTerm, $offset = 0, $limit = 20, $fields = arra
{
$dbh = $this->database->dbh;

$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -190,7 +190,7 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit =
{
$dbh = $this->database->dbh;

$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -265,7 +265,7 @@ function GetGamesByDate($date, $offset = 0, $limit = 20, $fields = array(), $Ord

function GetGamesByDateByPlatform($IDs, $date, $offset = 0, $limit = 20, $fields = array(), $OrderBy = '', $ASCDESC = 'ASC')
{
$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -334,7 +334,7 @@ function GetGamesByDateByPlatform($IDs, $date, $offset = 0, $limit = 20, $fields

function GetAllGames($offset = 0, $limit = 20, $fields = array(), $OrderBy = '', $ASCDESC = 'ASC')
{
$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -375,7 +375,7 @@ function GetAllGames($offset = 0, $limit = 20, $fields = array(), $OrderBy = '',

function GetGamesByLatestUpdatedDate($minutes, $offset = 0, $limit = 20, $fields = array())
{
$qry = "Select id, GameTitle, Developer, ReleaseDate, Platform ";
$qry = "Select id, GameTitle, Developer, ReleaseDate, ReleaseDateRevised, Platform ";

if(!empty($fields))
{
Expand Down Expand Up @@ -754,7 +754,193 @@ function is_valid_games_col($name)
}
return isset($this->GamesTblCols[$name]);
}
}

/* Everything belowis not planned to be exposed through external API */
function InsertUserEdits($user_id, $game_id, $type, $diff, $subtype = '')
{
$dbh = $this->database->dbh;
$sth = $dbh->prepare("INSERT INTO user_edits (users_id, games_id, type, diff) VALUES (:users_id, :games_id, :type, :diff);");
$sth->bindValue(':users_id', $user_id, PDO::PARAM_INT);
$sth->bindValue(':games_id', $game_id, PDO::PARAM_INT);
$sth->bindValue(':type', $type, PDO::PARAM_INT);
$sth->bindValue(':diff', $diff, PDO::PARAM_STR);
return $sth->execute();
}

function UpdateGame($user_id, $game_id, $GameTitle, $Overview, $Youtube, $ReleaseDateRevised, $Players, $coop, $Developer, $Publisher)
{
$dbh = $this->database->dbh;
{
$sth = $dbh->prepare("Select * FROM games WHERE id = :game_id");
$sth->bindValue(':game_id', $game_id, PDO::PARAM_INT);

if($sth->execute())
{
$Game = $sth->fetch(PDO::FETCH_ASSOC);
}
if(!isset($Game) || empty($Game))
{
return false;
}
}

{
$dbh->beginTransaction();

$sth = $dbh->prepare("UPDATE games SET GameTitle=:GameTitle, Overview=:Overview, ReleaseDateRevised=:ReleaseDateRevised, ReleaseDate=:ReleaseDate, Players=:Players, coop=:coop,
Developer=:Developer, Publisher=:Publisher, Youtube=:YouTube WHERE id=:game_id");
$sth->bindValue(':game_id', $game_id, PDO::PARAM_INT);
$sth->bindValue(':GameTitle', htmlspecialchars($GameTitle), PDO::PARAM_STR);
$sth->bindValue(':Overview', htmlspecialchars($Overview), PDO::PARAM_STR);
$sth->bindValue(':ReleaseDateRevised', $ReleaseDateRevised, PDO::PARAM_STR);
$date = explode('-', $ReleaseDateRevised);
$sth->bindValue(':ReleaseDate', "$date[1]/$date[2]/$date[0]", PDO::PARAM_STR);
$sth->bindValue(':Players', $Players, PDO::PARAM_INT);
$sth->bindValue(':YouTube', htmlspecialchars($Youtube), PDO::PARAM_STR);
$sth->bindValue(':coop', $coop, PDO::PARAM_INT);

// NOTE: these will be moved to own table, as a single game can have multiple devs/publishers
// it will also mean, we will be able to standardise devs/publishers names
// this will allow their selection from a menu as oppose to being provided by the user
$sth->bindValue(':Developer', htmlspecialchars($Developer), PDO::PARAM_STR);
$sth->bindValue(':Publisher', htmlspecialchars($Publisher), PDO::PARAM_STR);

$sth->execute();
{
foreach($Game as $key => $value)
{
if(isset($$key) && htmlspecialchars($$key) != $value)
{
if($key == 'Overview')
{
$diff = xdiff_string_diff($Game['Overview'], htmlspecialchars($Overview), 1);
if(empty($diff))
{
continue;
}
}
else
{
$diff = htmlspecialchars($$key);
}
$this->InsertUserEdits($user_id, $game_id, $key, $diff);
}
}
}
return $dbh->commit();
}
}

function DeleteGameImages($user_id, $game_id, $id, $type)
{
$dbh = $this->database->dbh;

$sth = $dbh->prepare("DELETE FROM banners WHERE id=:id;");
$sth->bindValue(':id', $id, PDO::PARAM_INT);
$res = $sth->execute();
if($dbh->inTransaction() || $res)
{
$this->InsertUserEdits($user_id, $game_id, $type, "[REMOVED]");
}
return ($dbh->inTransaction() || $res);
}

function DeleteAllGameImages($user_id, $game_id)
{
$dbh = $this->database->dbh;

$sth = $dbh->prepare("DELETE FROM banners WHERE keyvalue=:game_id;");
$sth->bindValue(':game_id', $game_id, PDO::PARAM_INT);
$res = $sth->execute();
if($dbh->inTransaction() || $res)
{
$this->InsertUserEdits($user_id, $game_id, "all_images", "[REMOVED]");
}
return ($dbh->inTransaction() || $res);
}

function DeleteAndInsertGameImages($user_id, $id, $game_id, $type, $filename, $side = NULL)
{
$dbh = $this->database->dbh;
$dbh->beginTransaction();
$this->DeleteGameImages($user_id, $game_id, $id, $type);
$this->InsertGameImages($user_id, $game_id, $type, $filename, $side);
return $dbh->commit();

}

function InsertGameImages($user_id, $game_id, $type, $filename, $side = NULL)
{
$dbh = $this->database->dbh;

$sth = $dbh->prepare("INSERT INTO banners (keyvalue, keytype, side, filename, userid) VALUES (:keyvalue, :keytype, :side, :filename, :user_id); ");
$sth->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$sth->bindValue(':keyvalue', $game_id, PDO::PARAM_INT);
$sth->bindValue(':keytype', $type, PDO::PARAM_STR);
$sth->bindValue(':side', $side, PDO::PARAM_STR);
$sth->bindValue(':filename', $filename, PDO::PARAM_STR);
$res = $sth->execute();

if($dbh->inTransaction() || $res)
{
$this->InsertUserEdits($user_id, $game_id, $type, $filename);
}
return ($dbh->inTransaction() || $res);
}
function DeleteGame($user_id, $game_id)
{
$dbh = $this->database->dbh;

$sth = $dbh->prepare("DELETE FROM games WHERE id=:game_id;");
$sth->bindValue(':game_id', $game_id, PDO::PARAM_INT);
$res = $sth->execute();
if($dbh->inTransaction() || $res)
{
$this->InsertUserEdits($user_id, $game_id, "game", "[REMOVED]");
}
return ($dbh->inTransaction() || $res);
}

function InsertGame($user_id, $GameTitle, $Overview, $Youtube, $ReleaseDateRevised, $Players, $coop, $Developer, $Publisher)
{
$game_id = 0;
$dbh = $this->database->dbh;
{
$sth = $dbh->prepare("INSERT INTO games(GameTitle, Overview, ReleaseDateRevised, ReleaseDate, Players, coop, Developer, Publisher, Youtube, Alternates)
values (:GameTitle, :Overview, :ReleaseDateRevised, :ReleaseDate, :Players, :coop, :Developer, :Publisher, :YouTube, :Alternates)");
$sth->bindValue(':GameTitle', htmlspecialchars($GameTitle), PDO::PARAM_STR);
$sth->bindValue(':Overview', htmlspecialchars($Overview), PDO::PARAM_STR);
$sth->bindValue(':ReleaseDateRevised', $ReleaseDateRevised, PDO::PARAM_STR);
$date = explode('-', $ReleaseDateRevised);
$sth->bindValue(':ReleaseDate', "$date[1]/$date[2]/$date[0]", PDO::PARAM_STR);
$sth->bindValue(':Players', $Players, PDO::PARAM_INT);
$sth->bindValue(':YouTube', htmlspecialchars($Youtube), PDO::PARAM_STR);
$sth->bindValue(':coop', $coop, PDO::PARAM_INT);
$sth->bindValue(':Alternates', "", PDO::PARAM_STR);

// NOTE: these will be moved to own table, as a single game can have multiple devs/publishers
// it will also mean, we will be able to standardise devs/publishers names
// this will allow their selection from a menu as oppose to being provided by the user
$sth->bindValue(':Developer', htmlspecialchars($Developer), PDO::PARAM_STR);
$sth->bindValue(':Publisher', htmlspecialchars($Publisher), PDO::PARAM_STR);

if($sth->execute())
{
$game_id = $dbh->lastInsertId();
$dbh->beginTransaction();
$this->InsertUserEdits($user_id, $game_id, 'game', '[NEW]');

$GameArrayFields = ['GameTitle', 'Overview', 'ReleaseDateRevised', 'Players', 'coop', 'Developer', 'Publisher', 'Youtube'];
foreach($GameArrayFields as $key)
{
$diff = htmlspecialchars($$key);
$this->InsertUserEdits($user_id, $game_id, $key, $diff);
}
$dbh->commit();
}
}
return $game_id;
}
}

?>
64 changes: 64 additions & 0 deletions website/actions/add_game.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
require_once __DIR__ . "/../include/ErrorPage.class.php";
require_once __DIR__ . "/../include/login.phpbb.class.php";

function returnJSONAndDie($code, $msg)
{
echo json_encode(array("code" => $code, "msg" => $msg));
die();
}

$_user = phpBBuser::getInstance();
if(!$_user->isLoggedIn())
{
returnJSONAndDie(-1, ErrorPage::$MSG_NOT_LOGGED_IN_EDIT_ERROR);
}
else
{
if(!$_user->hasPermission('u_edit_games'))
{
returnJSONAndDie(-1, ErrorPage::$MSG_NO_PERMISSION_TO_EDIT_ERROR);
}
}


$GameArrayFields = ['GameTitle', 'Overview', 'ReleaseDateRevised', 'Players', 'coop', 'Developer', 'Publisher', 'Youtube'];
foreach($GameArrayFields as $field)
{
if(!isset($_REQUEST[$field]))
{
returnJSONAndDie(-1, ErrorPage::$MSG_MISSING_PARAM_ERROR . ": ($field).");
}
if(empty($_REQUEST[$field]) && ($field != 'Youtube' && $field != 'Overview' && $field != 'Publisher'))
{
returnJSONAndDie(-1, "field is empty: ($field).");
}
}

$date = explode('-', $_REQUEST['ReleaseDateRevised']);
if(!checkdate($date[1], $date[2], $date[0]))
{
returnJSONAndDie(-1, "Invalid Date Format");
}


require_once __DIR__ . "/../../include/TGDB.API.php";

try
{

$API = TGDB::getInstance();
$res = $API->InsertGame($_user->GetUserID(), $_REQUEST['GameTitle'], $_REQUEST['Overview'], $_REQUEST['Youtube'], $_REQUEST['ReleaseDateRevised'],
$_REQUEST['Players'], $_REQUEST['coop'], $_REQUEST['Developer'], $_REQUEST['Publisher']);

if($res)
{
returnJSONAndDie(1, $res);
}

}
catch (Exception $e)
{
error_log($e);
}
returnJSONAndDie(-1, "Unexpected Error has occured, Please try again!!");
69 changes: 69 additions & 0 deletions website/actions/delete_game.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
require_once __DIR__ . "/../include/ErrorPage.class.php";
require_once __DIR__ . "/../include/login.phpbb.class.php";

function returnJSONAndDie($code, $msg)
{
echo json_encode(array("code" => $code, "msg" => $msg));
die();
}

$_user = phpBBuser::getInstance();
if(!$_user->isLoggedIn())
{
returnJSONAndDie(-1, ErrorPage::$MSG_NOT_LOGGED_IN_EDIT_ERROR);
}
else
{
if(!$_user->hasPermission('m_delete_games'))
{
returnJSONAndDie(-1, ErrorPage::$MSG_NO_PERMISSION_TO_EDIT_ERROR);
}
}

if(!isset($_REQUEST['game_id']) || !is_numeric($_REQUEST['game_id']))
{
returnJSONAndDie(-1, ErrorPage::$MSG_MISSING_PARAM_ERROR);
}

require_once __DIR__ . "/../../include/TGDB.API.php";

try
{

$API = TGDB::getInstance();
if(empty($API->GetGameByID($_REQUEST['game_id'], 0, 1)))
{
returnJSONAndDie(0, "No game in record to delete.");
}

$covers = $API->GetGameBoxartByID($_REQUEST['game_id'], 0, 99, 'ALL');

if(!empty($covers) && ($covers = $covers[$_REQUEST['game_id']]))
{
$sizes = ["original", "small", "thumb", "cropped_center_thumb", "medium", "large"];
foreach($covers as $cover)
{
foreach($sizes as $size)
{
$image_to_delete = __DIR__ . "/../../cdn/images/$size/" . $cover->filename;
if(file_exists($image_to_delete))
{
unlink($image_to_delete);
}
}
}
}

$API->DeleteAllGameImages($_user->GetUserID(), $_REQUEST['game_id']);
$res = $API->DeleteGame($_user->GetUserID(), $_REQUEST['game_id']);

returnJSONAndDie(1, "success!!");


}
catch (Exception $e)
{
error_log($e);
}
returnJSONAndDie(-1, "Unexpected Error has occured, Please try again!!");
Loading

0 comments on commit bc58004

Please sign in to comment.