diff --git a/library/includes/functions.php b/library/includes/functions.php index ab25e86c6..c5de654e2 100644 --- a/library/includes/functions.php +++ b/library/includes/functions.php @@ -1574,45 +1574,6 @@ function build_topic_pagination($url, $replies, $per_page) return $pg; } -// -// Poll -// -function get_poll_data_items_js($topic_id) -{ - if (!$topic_id_csv = get_id_csv($topic_id)) { - return is_array($topic_id) ? array() : false; - } - $items = array(); - - if (!$poll_data = CACHE('bb_poll_data')->get("poll_$topic_id")) { - $poll_data = DB()->fetch_rowset(" - SELECT topic_id, vote_id, vote_text, vote_result - FROM " . BB_POLL_VOTES . " - WHERE topic_id IN($topic_id_csv) - ORDER BY topic_id, vote_id - "); - CACHE('bb_poll_data')->set("poll_$topic_id", $poll_data); - } - - foreach ($poll_data as $row) { - $opt_text_for_js = htmlCHR($row['vote_text']); - $opt_result_for_js = (int)$row['vote_result']; - - $items[$row['topic_id']][$row['vote_id']] = array($opt_text_for_js, $opt_result_for_js); - } - foreach ($items as $k => $v) { - $items[$k] = json_encode($v, JSON_THROW_ON_ERROR); - } - - return is_array($topic_id) ? $items : $items[$topic_id]; -} - -function poll_is_active($t_data) -{ - global $bb_cfg; - return ($t_data['topic_vote'] == 1 && $t_data['topic_time'] > TIMENOW - $bb_cfg['poll_max_days'] * 86400); -} - function print_confirmation($tpl_vars) { global $template, $lang; diff --git a/poll.php b/poll.php index ff97c4df1..6c4d42834 100644 --- a/poll.php +++ b/poll.php @@ -60,7 +60,7 @@ switch ($mode) { if ($t_data['topic_status'] == TOPIC_LOCKED) { bb_die($lang['TOPIC_LOCKED_SHORT']); } - if (!poll_is_active($t_data)) { + if (!\TorrentPier\Legacy\Poll::poll_is_active($t_data)) { bb_die($lang['NEW_POLL_ENDED']); } if (!$vote_id) { @@ -128,7 +128,7 @@ switch ($mode) { bb_die($lang['NEW_POLL_ADDED']); break; - // редакторование + // редактирование case 'poll_edit': if (!$t_data['topic_vote']) { bb_die($lang['POST_HAS_NO_POLL']); diff --git a/src/Legacy/Poll.php b/src/Legacy/Poll.php index fd15f8017..3e22dd402 100644 --- a/src/Legacy/Poll.php +++ b/src/Legacy/Poll.php @@ -104,4 +104,53 @@ class Poll DB()->query("DELETE FROM " . BB_POLL_USERS . " WHERE topic_id = $topic_id"); CACHE('bb_poll_data')->rm("poll_$topic_id"); } + + /** + * Get poll items + * + * @param $topic_id + * @return array|false|mixed|string + * @throws \JsonException + */ + public static function get_poll_data_items_js($topic_id) + { + if (!$topic_id_csv = get_id_csv($topic_id)) { + return is_array($topic_id) ? array() : false; + } + $items = array(); + + if (!$poll_data = CACHE('bb_poll_data')->get("poll_$topic_id")) { + $poll_data = DB()->fetch_rowset(" + SELECT topic_id, vote_id, vote_text, vote_result + FROM " . BB_POLL_VOTES . " + WHERE topic_id IN($topic_id_csv) + ORDER BY topic_id, vote_id + "); + CACHE('bb_poll_data')->set("poll_$topic_id", $poll_data); + } + + foreach ($poll_data as $row) { + $opt_text_for_js = htmlCHR($row['vote_text']); + $opt_result_for_js = (int)$row['vote_result']; + + $items[$row['topic_id']][$row['vote_id']] = array($opt_text_for_js, $opt_result_for_js); + } + foreach ($items as $k => $v) { + $items[$k] = json_encode($v, JSON_THROW_ON_ERROR); + } + + return is_array($topic_id) ? $items : $items[$topic_id]; + } + + /** + * Check whether poll is active + * + * @param array $t_data + * @return bool + */ + public static function poll_is_active(array $t_data): bool + { + global $bb_cfg; + return ($t_data['topic_vote'] == 1 && $t_data['topic_time'] > TIMENOW - $bb_cfg['poll_max_days'] * 86400); + } } diff --git a/viewtopic.php b/viewtopic.php index f08c770ab..74f43ff34 100644 --- a/viewtopic.php +++ b/viewtopic.php @@ -549,7 +549,7 @@ if (!DB()->sql_query($sql)) { // Does this topic contain a poll? // if ($topic_has_poll) { - $poll_votes_js = get_poll_data_items_js($topic_id); + $poll_votes_js = \TorrentPier\Legacy\Poll::get_poll_data_items_js($topic_id); if (!$poll_votes_js) { $template->assign_vars(array( @@ -557,7 +557,7 @@ if ($topic_has_poll) { )); } else { $template->assign_vars(array( - 'SHOW_VOTE_BTN' => poll_is_active($t_data), + 'SHOW_VOTE_BTN' => \TorrentPier\Legacy\Poll::poll_is_active($t_data), 'POLL_VOTES_JS' => $poll_votes_js, )); }