Skip to content

Commit

Permalink
Merge pull request #11 from Zer0xFF/alt_names
Browse files Browse the repository at this point in the history
Enable addition/editing of alt names
  • Loading branch information
Zer0xFF authored Feb 23, 2019
2 parents 9e71c44 + fece964 commit d1f0307
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 92 deletions.
239 changes: 152 additions & 87 deletions include/TGDB.API.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,55 +186,7 @@ function GetGameByID($IDs, $offset = 0, $limit = 20, $fields = array())

function SearchGamesByName($searchTerm, $offset = 0, $limit = 20, $fields = array())
{
$dbh = $this->database->dbh;

$qry = "Select id, game_title, release_date, platform ";

if(!empty($fields))
{
foreach($fields as $key => $enabled)
{
if($enabled && $this->is_valid_games_col($key))
{
$qry .= ", $key ";
}
}
}

$qry .= " FROM games WHERE game_title LIKE :name OR game_title=:name2 OR soundex(game_title) LIKE soundex(:name3) OR soundex(game_title) LIKE soundex(:name4)
GROUP BY id ORDER BY CASE
WHEN game_title like :name5 THEN 3
WHEN game_title like :name6 THEN 0
WHEN game_title like :name7 THEN 1
WHEN game_title like :name8 THEN 2
ELSE 4
END, game_title LIMIT :limit OFFSET :offset";

$sth = $dbh->prepare($qry);

$sth->bindValue(':name', "%$searchTerm%");
$sth->bindValue(':name2', $searchTerm);
$sth->bindValue(':name3', "$searchTerm%");
$sth->bindValue(':name4', "% %$searchTerm% %");

$sth->bindValue(':name5', "%$searchTerm");
$sth->bindValue(':name6', $searchTerm);
$sth->bindValue(':name7', "$searchTerm%");
$sth->bindValue(':name8', "% %$searchTerm% %");


$sth->bindValue(':offset', $offset, PDO::PARAM_INT);
$sth->bindValue(':limit', $limit, PDO::PARAM_INT);

if($sth->execute())
{
$res = $sth->fetchAll(PDO::FETCH_OBJ);
if(!empty($res))
{
$this->PopulateOtherData($res, $fields);
}
return $res;
}
return $this->SearchGamesByNameByPlatformID($searchTerm, '', $offset, $limit, $fields);
}

function SearchGamesByExactName($searchTerm, $offset = 0, $limit = 20, $fields = array())
Expand Down Expand Up @@ -278,19 +230,6 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit =
{
$dbh = $this->database->dbh;

$qry = "Select id, game_title, release_date, platform ";

if(!empty($fields))
{
foreach($fields as $key => $enabled)
{
if($enabled && $this->is_valid_games_col($key))
{
$qry .= ", $key ";
}
}
}

$PlatformIDs = array();
if(is_array($IDs))
{
Expand All @@ -307,21 +246,45 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit =
$PlatformIDs = $IDs;
}

$qry .= " FROM games WHERE ";
$qry = "Select games.id, games.game_title, games.release_date, games.platform ";

if(!empty($fields))
{
foreach($fields as $key => $enabled)
{
if($enabled && $this->is_valid_games_col($key))
{
$qry .= ", games.$key ";
}
}
}
$qry .= " FROM games,
(
SELECT games_names_merged.id, @rank := @rank + 1 AS rank FROM
(
(
SELECT games_id as id, name as game_title, SOUNDEX from games_alts
WHERE name LIKE :name OR name=:name2 OR SOUNDEX LIKE soundex(:name3) OR SOUNDEX LIKE soundex(:name4)
)
UNION
(
SELECT id, game_title, SOUNDEX from games
WHERE game_title LIKE :name_2 OR game_title=:name_2_2 OR SOUNDEX LIKE soundex(:name_2_3) OR SOUNDEX LIKE soundex(:name_2_4)
)
) games_names_merged, (SELECT @rank := 0) t1
ORDER BY CASE
WHEN game_title like :name_3_2 THEN 0
WHEN game_title like :name_3_3 THEN 1
WHEN game_title like :name_3_4 THEN 2
WHEN game_title like :name_3 THEN 3
ELSE 4
END, game_title
) games_ordered where games.id = games_ordered.id ";
if(!empty($PlatformIDs))
{
$qry .= " platform IN ($PlatformIDs) AND ";
$qry .= " AND games.platform IN ($PlatformIDs) ";
}

$qry .= " (game_title LIKE :name OR game_title=:name2 OR soundex(game_title) LIKE soundex(:name3) OR soundex(game_title) LIKE soundex(:name4))
GROUP BY id ORDER BY CASE
WHEN game_title like :name5 THEN 3
WHEN game_title like :name6 THEN 0
WHEN game_title like :name7 THEN 1
WHEN game_title like :name8 THEN 2
ELSE 4
END, game_title LIMIT :limit OFFSET :offset";
$qry .= "GROUP BY games.id ORDER BY MIN(games_ordered.rank) LIMIT :limit OFFSET :offset";

$sth = $dbh->prepare($qry);

Expand All @@ -330,11 +293,15 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit =
$sth->bindValue(':name3', "$searchTerm%");
$sth->bindValue(':name4', "% %$searchTerm% %");

$sth->bindValue(':name5', "%$searchTerm");
$sth->bindValue(':name6', $searchTerm);
$sth->bindValue(':name7', "$searchTerm%");
$sth->bindValue(':name8', "% %$searchTerm% %");
$sth->bindValue(':name_2', "%$searchTerm%");
$sth->bindValue(':name_2_2', $searchTerm);
$sth->bindValue(':name_2_3', "$searchTerm%");
$sth->bindValue(':name_2_4', "% %$searchTerm% %");

$sth->bindValue(':name_3', "%$searchTerm");
$sth->bindValue(':name_3_2', $searchTerm);
$sth->bindValue(':name_3_3', "$searchTerm%");
$sth->bindValue(':name_3_4', "% %$searchTerm% %");

$sth->bindValue(':offset', $offset, PDO::PARAM_INT);
$sth->bindValue(':limit', $limit, PDO::PARAM_INT);
Expand Down Expand Up @@ -1356,9 +1323,14 @@ function GetUserEdits($condition = '', $order = '', $offset = 0, $limit = 100)
$res = $sth->fetchAll(PDO::FETCH_OBJ);
foreach($res as $edit)
{
if($edit->type == 'genres' || $edit->type == 'developers' || $edit->type == 'publishers')
//Note, these listing return a json array of data, thus must be decoded to any array to be encoded correctly later
switch($edit->type)
{
$edit->value = json_decode($edit->value);
case 'genres':
case 'developers':
case 'publishers':
case 'alternates':
$edit->value = json_decode($edit->value);
}
}
return $res;
Expand Down Expand Up @@ -1696,6 +1668,90 @@ function InsertUserEdits($user_id, $game_id, $type, $value, $subtype = '')
return $sth->execute();
}

function InsertGamesAltName($game_id, $alt_name)
{
$dbh = $this->database->dbh;
$sth = $dbh->prepare("INSERT IGNORE INTO games_alts (games_id, name, SOUNDEX) VALUES (:games_id, :alt_name, soundex(:alt_name2));");
$sth->bindValue(':games_id', $game_id, PDO::PARAM_INT);
$sth->bindValue(':alt_name', $alt_name, PDO::PARAM_STR);
$sth->bindValue(':alt_name2', $alt_name, PDO::PARAM_STR);
return $sth->execute();
}

function DeleteGamesAltName($game_id, $alt_name)
{
$dbh = $this->database->dbh;
$sth = $dbh->prepare("DELETE FROM games_alts WHERE games_id=:games_id AND name=:alt_name");
$sth->bindValue(':games_id', $game_id, PDO::PARAM_INT);
$sth->bindValue(':alt_name', $alt_name, PDO::PARAM_STR);
return $sth->execute();
}

function UpdateGamesAltName($user_id, $games_id, $new_alt_names)
{
$dbh = $this->database->dbh;

$is_changed = false;
$valid_alt_name = array();

$current_alt_names = $this->GetGamesAlts($games_id, false);
if(!empty($current_alt_names[$games_id]))
{
$current_alt_names = $current_alt_names[$games_id];
}
if(!empty($new_alt_names))
{
foreach($new_alt_names as &$new_alt_name)
{
$new_alt_name = trim($new_alt_name);
}
unset($new_alt_name);
foreach($new_alt_names as $new_alt_name)
{
if(!empty($new_alt_name))
{
$valid_alt_name[] = $new_alt_name;

if(!in_array($new_alt_name, $current_alt_names, true))
{
$res = $this->InsertGamesAltName($games_id, $new_alt_name);
if(!$dbh->inTransaction() && !$res)
{
return false;
}
$is_changed = true;
}
}
}
}

if(!empty($current_alt_names))
{
foreach($current_alt_names as $current_alt_name)
{
if(!in_array($current_alt_name, $new_alt_names, true))
{
$res = $this->DeleteGamesAltName($games_id, $current_alt_name);
if(!$dbh->inTransaction() && !$res)
{
return false;
}
$is_changed = true;
}
}
}

if($is_changed)
{
$valid_alt_name = array_unique($valid_alt_name);
if(!empty($valid_alt_name))
{
$this->InsertUserEdits($user_id, $games_id, "alternates", json_encode($valid_alt_name));
}
}
return true;
}

function InsertGamesGenre($game_id, $genres_id)
{
$dbh = $this->database->dbh;
Expand All @@ -1718,6 +1774,7 @@ function UpdateGamesGenre($user_id, $games_id, $new_ids)
{
$dbh = $this->database->dbh;
$is_changed = false;
$valid_ids = array();

$list = $this->GetGenres();

Expand Down Expand Up @@ -1764,8 +1821,7 @@ function UpdateGamesGenre($user_id, $games_id, $new_ids)
$valid_ids = array_unique($valid_ids);
if(!empty($valid_ids))
{
$genres_str = implode(",", $valid_ids);
$this->InsertUserEdits($user_id, $games_id, "genres", "[$genres_str]");
$this->InsertUserEdits($user_id, $games_id, "genres", json_encode($valid_ids, JSON_NUMERIC_CHECK));
}
}
return true;
Expand Down Expand Up @@ -1794,6 +1850,8 @@ function UpdateGamesDev($user_id, $games_id, $new_ids)
$dbh = $this->database->dbh;

$is_changed = false;
$valid_ids = array();

$list = $this->GetDevsListByIDs($new_ids);
$current_ids = $this->GetGamesDevs($games_id);
if(!empty($current_ids[$games_id]))
Expand Down Expand Up @@ -1837,8 +1895,7 @@ function UpdateGamesDev($user_id, $games_id, $new_ids)
$valid_ids = array_unique($valid_ids);
if(!empty($valid_ids))
{
$ids_str = implode(",", $valid_ids);
$this->InsertUserEdits($user_id, $games_id, "developers", "[$ids_str]");
$this->InsertUserEdits($user_id, $games_id, "developers", json_encode($valid_ids, JSON_NUMERIC_CHECK));
}
}
return true;
Expand Down Expand Up @@ -1867,6 +1924,8 @@ function UpdateGamesPub($user_id, $games_id, $new_ids)
$dbh = $this->database->dbh;

$is_changed = false;
$valid_ids = array();

$list = $this->GetPubsListByIDs($new_ids);
$current_ids = $this->GetGamesPubs($games_id);
if(!empty($current_ids[$games_id]))
Expand Down Expand Up @@ -1910,14 +1969,13 @@ function UpdateGamesPub($user_id, $games_id, $new_ids)
$valid_ids = array_unique($valid_ids);
if(!empty($valid_ids))
{
$ids_str = implode(",", $valid_ids);
$this->InsertUserEdits($user_id, $games_id, "publishers", "[$ids_str]");
$this->InsertUserEdits($user_id, $games_id, "publishers", json_encode($valid_ids, JSON_NUMERIC_CHECK));
}
}
return true;
}

function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $new_genres, $ratings)
function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $new_genres, $ratings, $alternate_names)
{
$dbh = $this->database->dbh;
{
Expand All @@ -1935,6 +1993,8 @@ function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $relea
}

{
$this->UpdateGamesAltName($user_id, $game_id, $alternate_names);

if(!empty($new_genres))
{
$this->UpdateGamesGenre($user_id, $game_id, $new_genres);
Expand Down Expand Up @@ -2095,7 +2155,7 @@ function DeleteGame($user_id, $games_id)
return $dbh->commit();
}

function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $platform, $new_genres, $ratings)
function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $platform, $new_genres, $ratings, $alternate_names)
{
$game_id = 0;
$dbh = $this->database->dbh;
Expand Down Expand Up @@ -2165,6 +2225,11 @@ function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $
$this->UpdateGamesPub($user_id, $game_id, $new_publishers);
}

if(!empty($alternate_names))
{
$this->UpdateGamesAltName($user_id, $game_id, $alternate_names);
}

$GameArrayFields = ['game_title', 'overview', 'release_date', 'players', 'coop', 'youtube', 'platform', 'rating'];
foreach($GameArrayFields as $key)
{
Expand Down
4 changes: 2 additions & 2 deletions website/actions/add_game.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ function returnJSONAndDie($code, $msg)

$API = TGDB::getInstance();
$res = $API->InsertGame($_user->GetUserID(), $_REQUEST['game_title'], $_REQUEST['overview'], $_REQUEST['youtube'], $_REQUEST['release_date'],
$_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['platform'], $_REQUEST['genres'], $_REQUEST['rating']);
$_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['platform'], $_REQUEST['genres'], $_REQUEST['rating'], $_REQUEST['alternate_names']);

if($res)
{
$filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true];
$filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true, 'alternates' => true];
$new_game_data = $API->GetGameByID($res, 0, 1, $filters)[0];
DiscordUtils::PostGameUpdate($_user, [], $new_game_data, 0);
returnJSONAndDie(1, $res);
Expand Down
4 changes: 2 additions & 2 deletions website/actions/edit_game.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ function returnJSONAndDie($code, $msg)

try
{
$filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true];
$filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true, 'alternates' => true];
$API = TGDB::getInstance();
$old_game_data = $API->GetGameByID($_REQUEST['game_id'], 0, 1, $filters)[0];

$res = $API->UpdateGame( $_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['game_title'], $_REQUEST['overview'], $_REQUEST['youtube'], $_REQUEST['release_date'],
$_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['genres'], $_REQUEST['rating']);
$_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['genres'], $_REQUEST['rating'], $_REQUEST['alternate_names']);

if($res)
{
Expand Down
Loading

0 comments on commit d1f0307

Please sign in to comment.