chore: drop raw queries (#266)

* chore: eloquent migrate

* fix: typos
This commit is contained in:
Gabriel Pedro 2024-08-25 07:38:58 -04:00 committed by GitHub
parent 760c3ab017
commit 6183b7ee52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 81 deletions

View File

@ -57,37 +57,39 @@ if (isset($_REQUEST['id']))
$id = (int)$_REQUEST['id']; $id = (int)$_REQUEST['id'];
else if (isset($_REQUEST['search_email'])) { else if (isset($_REQUEST['search_email'])) {
$search_account_email = $_REQUEST['search_email']; $search_account_email = $_REQUEST['search_email'];
$accountModel = AccountModel::where('email', $search_account_email)->get(); $accountModel = AccountModel::where('email', $search_account_email)->limit(11)->get(['email', 'id']);
if ($accountModel->count() == 1) { if (count($accountModel) == 0) {
$id = (int)$accountModel[0]->id;
} else if ($accountModel->count() > 10) {
echo_error('Specified e-mail resulted with too many accounts.');
}
else {
echo_error('No entries found.'); echo_error('No entries found.');
} else if (count($accountModel) == 1) {
$id = $accountModel->first()->getKey();
} else if (count($accountModel) > 10) {
echo_error('Specified e-mail resulted with too many accounts.');
} }
} }
else if (isset($_REQUEST['search'])) { else if (isset($_REQUEST['search'])) {
$search_account = $_REQUEST['search']; $search_account = $_REQUEST['search'];
if (strlen($search_account) < 3 && !Validator::number($search_account)) { $min_size = 3;
echo_error('Player name is too short.'); if ($nameOrNumberColumn == 'number') {
$min_size = 1;
}
if (strlen($search_account) < $min_size && !Validator::number($search_account)) {
echo_error('Account ' . $nameOrNumberColumn . ' is too short.');
} else { } else {
$query = $db->query('SELECT `id` FROM `accounts` WHERE `' . $nameOrNumberColumn . '` = ' . $db->quote($search_account)); $query = AccountModel::where($nameOrNumberColumn, '=', $search_account)->limit(11)->get(['id', $nameOrNumberColumn]);
if ($query->rowCount() == 1) { if (count($query) == 0) {
$query = $query->fetch(); echo_error('No entries found.');
$id = (int)$query['id']; } else if (count($query) == 1) {
$id = $query->first()->getKey();
} else if (count($query) > 10) {
echo_error('Specified name resulted with too many accounts.');
} else { } else {
$query = $db->query('SELECT `id`, `' . $nameOrNumberColumn . '` FROM `accounts` WHERE `' . $nameOrNumberColumn . '` LIKE ' . $db->quote('%' . $search_account . '%'));
if ($query->rowCount() > 0 && $query->rowCount() <= 10) {
$str_construct = 'Do you mean?<ul class="mb-0">'; $str_construct = 'Do you mean?<ul class="mb-0">';
foreach ($query as $row) foreach ($query as $row) {
$str_construct .= '<li><a href="' . $admin_base . '&id=' . $row['id'] . '">' . $row[$nameOrNumberColumn] . '</a></li>'; $str_construct .= '<li><a href="' . $admin_base . '&id=' . $row->getKey() . '">' . $row->attributes[$nameOrNumberColumn] . '</a></li>';
}
$str_construct .= '</ul>'; $str_construct .= '</ul>';
echo_error($str_construct); echo_error($str_construct);
} else if ($query->rowCount() > 10)
echo_error('Specified name resulted with too many accounts.');
else
echo_error('No entries found.');
} }
} }
} }

View File

@ -7,6 +7,9 @@
* @copyright 2019 MyAAC * @copyright 2019 MyAAC
* @link https://my-aac.org * @link https://my-aac.org
*/ */
use MyAAC\Models\Account;
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
$title = 'Mailer'; $title = 'Mailer';
@ -61,15 +64,15 @@ if (!empty($mail_content) && !empty($mail_subject) && empty($mail_to)) {
$add = ' AND `email_verified` = 1'; $add = ' AND `email_verified` = 1';
} }
$query = $db->query('SELECT `email` FROM `accounts` WHERE `email` != ""' . $add); $query = Account::where('email', '!=', '')->get(['email']);
foreach ($query as $email) { foreach ($query as $email) {
if (_mail($email['email'], $mail_subject, $mail_content)) { if (_mail($email->email, $mail_subject, $mail_content)) {
$success++; $success++;
} }
else { else {
$failed++; $failed++;
echo '<br />'; echo '<br />';
error('An error occorred while sending email to <b>' . $email['email'] . '</b>. For Admin: More info can be found in system/logs/mailer-error.log'); error('An error occorred while sending email to <b>' . $email->email . '</b>. For Admin: More info can be found in system/logs/mailer-error.log');
} }
} }

View File

@ -24,20 +24,13 @@ $freePremium = $config['lua']['freePremium'];
function admin_give_points($points) function admin_give_points($points)
{ {
global $db, $hasPointsColumn; global $hasPointsColumn;
if (!$hasPointsColumn) { if (!$hasPointsColumn) {
displayMessage('Points not supported.'); displayMessage('Points not supported.');
return; return;
} }
$statement = $db->prepare('UPDATE `accounts` SET `premium_points` = `premium_points` + :points');
if (!$statement) {
displayMessage('Failed to prepare query statement.');
return;
}
if (!Account::query()->increment('premium_points', $points)) { if (!Account::query()->increment('premium_points', $points)) {
displayMessage('Failed to add points.'); displayMessage('Failed to add points.');
return; return;
@ -47,7 +40,7 @@ function admin_give_points($points)
function admin_give_coins($coins) function admin_give_coins($coins)
{ {
global $db, $hasCoinsColumn; global $hasCoinsColumn;
if (!$hasCoinsColumn) { if (!$hasCoinsColumn) {
displayMessage('Coins not supported.'); displayMessage('Coins not supported.');
@ -62,24 +55,6 @@ function admin_give_coins($coins)
displayMessage($coins . ' coins added to all accounts.', true); displayMessage($coins . ' coins added to all accounts.', true);
} }
function query_add_premium($column, $value_query, $condition_query = '1=1', $params = [])
{
global $db;
$statement = $db->prepare("UPDATE `accounts` SET `{$column}` = $value_query WHERE $condition_query");
if (!$statement) {
displayMessage('Failed to prepare query statement.');
return false;
}
if (!$statement->execute($params)) {
displayMessage('Failed to add premium days.');
return false;
}
return true;
}
function admin_give_premdays($days) function admin_give_premdays($days)
{ {
global $db, $freePremium; global $db, $freePremium;
@ -94,9 +69,9 @@ function admin_give_premdays($days)
// othire // othire
if ($db->hasColumn('accounts', 'premend')) { if ($db->hasColumn('accounts', 'premend')) {
// append premend // append premend
if (query_add_premium('premend', '`premend` + :value', '`premend` > :now', ['value' => $value, 'now' => $now])) { if (Account::where('premend', '>', $now)->increment('premend', $value)) {
// set premend // set premend
if (query_add_premium('premend', ':value', '`premend` <= :now', ['value' => $now + $value, 'now' => $now])) { if (Account::where('premend', '<=', $now)->update(['premend' => $now + $value])) {
displayMessage($days . ' premium days added to all accounts.', true); displayMessage($days . ' premium days added to all accounts.', true);
return; return;
} else { } else {
@ -114,11 +89,11 @@ function admin_give_premdays($days)
// tfs 0.x // tfs 0.x
if ($db->hasColumn('accounts', 'premdays')) { if ($db->hasColumn('accounts', 'premdays')) {
// append premdays // append premdays
if (query_add_premium('premdays', '`premdays` + :value', '1=1', ['value' => $days])) { if (Account::query()->update(['premdays' => $days])) {
// append lastday // append lastday
if (query_add_premium('lastday', '`lastday` + :value', '`lastday` > :now', ['value' => $value, 'now' => $now])) { if (Account::where('lastday', '>', $now)->increment('lastday', $value)) {
// set lastday // set lastday
if (query_add_premium('lastday', ':value', '`lastday` <= :now', ['value' => $now + $value, 'now' => $now])) { if (Account::where('lastday', '<=', $now)->update(['lastday' => $now + $value])) {
displayMessage($days . ' premium days added to all accounts.', true); displayMessage($days . ' premium days added to all accounts.', true);
return; return;
} else { } else {
@ -142,9 +117,9 @@ function admin_give_premdays($days)
// tfs 1.x // tfs 1.x
if ($db->hasColumn('accounts', 'premium_ends_at')) { if ($db->hasColumn('accounts', 'premium_ends_at')) {
// append premium_ends_at // append premium_ends_at
if (query_add_premium('premium_ends_at', '`premium_ends_at` + :value', '`premium_ends_at` > :now', ['value' => $value, 'now' => $now])) { if (Account::where('premium_ends_at', '>', $now)->increment('premium_ends_at', $value)) {
// set premium_ends_at // set premium_ends_at
if (query_add_premium('premium_ends_at', ':value', '`premium_ends_at` <= :now', ['value' => $now + $value, 'now' => $now])) { if (Account::where('premium_ends_at', '<=', $now)->update(['premium_ends_at' => $now + $value])) {
displayMessage($days . ' premium days added to all accounts.', true); displayMessage($days . ' premium days added to all accounts.', true);
return; return;
} else { } else {

View File

@ -51,22 +51,20 @@ else if (isset($_REQUEST['search'])) {
if (strlen($search_player) < 3 && !Validator::number($search_player)) { if (strlen($search_player) < 3 && !Validator::number($search_player)) {
echo_error('Player name is too short.'); echo_error('Player name is too short.');
} else { } else {
$query = $db->query('SELECT `id` FROM `players` WHERE `name` = ' . $db->quote($search_player)); $query = Player::where('name', 'like', '%' . $search_player . '%')->orderBy('name')->limit(11)->get(['id', 'name']);
if ($query->rowCount() == 1) { if (count($query) == 0) {
$query = $query->fetch(); echo_error('No entries found.');
$id = (int)$query['id']; } else if (count($query) == 1) {
$id = $query->first()->getKey();
} else if (count($query) > 10) {
echo_error('Specified name resulted with too many players.');
} else { } else {
$query = $db->query('SELECT `id`, `name` FROM `players` WHERE `name` LIKE ' . $db->quote('%' . $search_player . '%'));
if ($query->rowCount() > 0 && $query->rowCount() <= 10) {
$str_construct = 'Do you mean?<ul>'; $str_construct = 'Do you mean?<ul>';
foreach ($query as $row) foreach ($query as $row) {
$str_construct .= '<li><a href="' . $player_base . '&id=' . $row['id'] . '">' . $row['name'] . '</a></li>'; $str_construct .= '<li><a href="' . $player_base . '&id=' . $row->getKey() . '">' . $row->name . '</a></li>';
}
$str_construct .= '</ul>'; $str_construct .= '</ul>';
echo_error($str_construct); echo_error($str_construct);
} else if ($query->rowCount() > 10)
echo_error('Specified name resulted with too many players.');
else
echo_error('No entries found.');
} }
} }
} }
@ -307,7 +305,7 @@ else if (isset($_REQUEST['search'])) {
} }
} }
} else if ($id == 0) { } else if ($id == 0) {
$players_db = $db->query('SELECT `id`, `name`, `level` FROM `players` ORDER BY `id` asc'); $players_db = Player::orderBy('id')->get(['id','name', 'level']);
?> ?>
<div class="col-12 col-sm-12 col-lg-10"> <div class="col-12 col-sm-12 col-lg-10">
<div class="card card-info card-outline"> <div class="card card-info card-outline">
@ -327,11 +325,11 @@ else if (isset($_REQUEST['search'])) {
<tbody> <tbody>
<?php foreach ($players_db as $player_db): ?> <?php foreach ($players_db as $player_db): ?>
<tr> <tr>
<th><?php echo $player_db['id']; ?></th> <th><?php echo $player_db->id; ?></th>
<td><?php echo $player_db['name']; ?></a></td> <td><?php echo $player_db->name; ?></a></td>
<td><?php echo $player_db['level']; ?></a></td> <td><?php echo $player_db->level; ?></a></td>
<td><a href="?p=players&id=<?php echo $player_db['id']; ?>" class="btn btn-success btn-sm" title="Edit"> <td><a href="?p=players&id=<?php echo $player_db->id; ?>" class="btn btn-success btn-sm" title="Edit">
<i class="fas fa-pencil-alt"></i> <i class="fas fa-pencil-alt"></i>
</a> </a>
</td> </td>