mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-20 21:33:54 -07:00
Moved poll functions to Poll class (#739)
This commit is contained in:
parent
22fbae61db
commit
d2588855fa
4 changed files with 53 additions and 43 deletions
|
@ -1574,45 +1574,6 @@ function build_topic_pagination($url, $replies, $per_page)
|
||||||
return $pg;
|
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)
|
function print_confirmation($tpl_vars)
|
||||||
{
|
{
|
||||||
global $template, $lang;
|
global $template, $lang;
|
||||||
|
|
4
poll.php
4
poll.php
|
@ -60,7 +60,7 @@ switch ($mode) {
|
||||||
if ($t_data['topic_status'] == TOPIC_LOCKED) {
|
if ($t_data['topic_status'] == TOPIC_LOCKED) {
|
||||||
bb_die($lang['TOPIC_LOCKED_SHORT']);
|
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']);
|
bb_die($lang['NEW_POLL_ENDED']);
|
||||||
}
|
}
|
||||||
if (!$vote_id) {
|
if (!$vote_id) {
|
||||||
|
@ -128,7 +128,7 @@ switch ($mode) {
|
||||||
bb_die($lang['NEW_POLL_ADDED']);
|
bb_die($lang['NEW_POLL_ADDED']);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// редакторование
|
// редактирование
|
||||||
case 'poll_edit':
|
case 'poll_edit':
|
||||||
if (!$t_data['topic_vote']) {
|
if (!$t_data['topic_vote']) {
|
||||||
bb_die($lang['POST_HAS_NO_POLL']);
|
bb_die($lang['POST_HAS_NO_POLL']);
|
||||||
|
|
|
@ -104,4 +104,53 @@ class Poll
|
||||||
DB()->query("DELETE FROM " . BB_POLL_USERS . " WHERE topic_id = $topic_id");
|
DB()->query("DELETE FROM " . BB_POLL_USERS . " WHERE topic_id = $topic_id");
|
||||||
CACHE('bb_poll_data')->rm("poll_$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -549,7 +549,7 @@ if (!DB()->sql_query($sql)) {
|
||||||
// Does this topic contain a poll?
|
// Does this topic contain a poll?
|
||||||
//
|
//
|
||||||
if ($topic_has_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) {
|
if (!$poll_votes_js) {
|
||||||
$template->assign_vars(array(
|
$template->assign_vars(array(
|
||||||
|
@ -557,7 +557,7 @@ if ($topic_has_poll) {
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
$template->assign_vars(array(
|
$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,
|
'POLL_VOTES_JS' => $poll_votes_js,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue