From c046e8a8c1bc782cda1722b39d774ba92b393d73 Mon Sep 17 00:00:00 2001 From: Roman Kelesidis Date: Tue, 21 Nov 2023 00:29:02 +0700 Subject: [PATCH] Vote button code improvements (#1140) * Vote button code improvements * Update thanks.php --- library/ajax/thanks.php | 53 +++++++++++++++++++++ library/includes/cron/jobs/clean_dlstat.php | 2 +- src/Ajax.php | 39 +-------------- 3 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 library/ajax/thanks.php diff --git a/library/ajax/thanks.php b/library/ajax/thanks.php new file mode 100644 index 000000000..3033fb27b --- /dev/null +++ b/library/ajax/thanks.php @@ -0,0 +1,53 @@ +ajax_die($lang['DISABLED']); +} + +$mode = (string)$this->request['mode']; +$topic_id = (int)$this->request['topic_id']; + +switch ($mode) { + case 'add': + if (DB()->fetch_row('SELECT * FROM ' . BB_THX . " WHERE topic_id = $topic_id AND user_id = " . $userdata['user_id'])) { + $this->ajax_die($lang['LIKE_ALREADY']); + } + + if (DB()->fetch_row('SELECT poster_id FROM ' . BB_BT_TORRENTS . " WHERE topic_id = $topic_id AND poster_id = " . $userdata['user_id'])) { + $this->ajax_die($lang['LIKE_OWN_POST']); + } + + $columns = 'topic_id, user_id, time'; + $values = "$topic_id, {$userdata['user_id']}, " . TIMENOW; + DB()->query('INSERT IGNORE INTO ' . BB_THX . " ($columns) VALUES ($values)"); + + $this->response['html'] = '' . profile_url($userdata) . ' (' . bb_date(TIMENOW) . ')'; + break; + + case 'get': + $sql = DB()->fetch_rowset('SELECT u.username, u.user_rank, u.user_id, t.* FROM ' . BB_THX . ' t, ' . BB_USERS . " u WHERE t.topic_id = $topic_id AND t.user_id = u.user_id"); + + $user_list = []; + foreach ($sql as $row) { + $user_list[] = '' . profile_url($row) . ' (' . bb_date($row['time']) . ')'; + } + + $this->response['html'] = join(', ', $user_list) ?: $lang['NO_LIKES']; + break; + + default: + $this->ajax_die('Invalid mode'); +} diff --git a/library/includes/cron/jobs/clean_dlstat.php b/library/includes/cron/jobs/clean_dlstat.php index b7d97726b..d63674ff4 100644 --- a/library/includes/cron/jobs/clean_dlstat.php +++ b/library/includes/cron/jobs/clean_dlstat.php @@ -38,7 +38,7 @@ if ($delete_dlstat_sql = implode(') OR (', $delete_dlstat_sql)) { // Save the last 50 votes for topics DB()->query(' DELETE t1 - FROM' . BB_THX . ' t1 + FROM ' . BB_THX . ' t1 JOIN ( SELECT topic_id, MAX(time) as max_time FROM ' . BB_THX . ' diff --git a/src/Ajax.php b/src/Ajax.php index 469d021c0..08bbd4087 100644 --- a/src/Ajax.php +++ b/src/Ajax.php @@ -520,48 +520,11 @@ class Ajax /** * Get / Set votes * - * * @return void */ public function thx() { - global $bb_cfg, $lang, $userdata; - - if (!$bb_cfg['tor_thank']) { - $this->ajax_die($lang['DISABLED']); - } - - $mode = (string) $this->request['mode']; - $topic_id = (int) $this->request['topic_id']; - - switch($mode) { - case 'add': - $row = DB()->fetch_row('SELECT * FROM '. BB_THX ." WHERE topic_id = $topic_id AND user_id = ". $userdata['user_id']); - - if ($row) { - $this->ajax_die($lang['LIKE_ALREADY']); - } - - if (DB()->fetch_row('SELECT poster_id FROM ' . BB_BT_TORRENTS . " WHERE topic_id = $topic_id AND poster_id = " . $userdata['user_id'])) { - $this->ajax_die($lang['LIKE_OWN_POST']); - } - - $columns = 'topic_id, user_id, time'; - $values = "$topic_id, {$userdata['user_id']}, " . TIMENOW; - DB()->query('INSERT IGNORE INTO ' . BB_THX . " ($columns) VALUES ($values)"); - $this->response['html'] = '' . profile_url($userdata) . ' ('. bb_date(TIMENOW) . ')'; - break; - - case 'get': - $sql = DB()->fetch_rowset('SELECT u.username, u.user_rank, u.user_id, t.* FROM ' . BB_THX . ' t, '. BB_USERS . " u WHERE t.topic_id = $topic_id AND t.user_id = u.user_id"); - $user_list = []; - foreach ($sql as $row) { - $user_list[] = '' . profile_url($row) . ' (' . bb_date($row['time']) . ')'; - } - $thx_list = join(' ', $user_list); - $this->response['html'] = ($thx_list) ? $thx_list : $lang['NO_LIKES']; - break; - } + require AJAX_DIR . '/thanks.php'; } }