Paste
Pasted as PHP by registered user atmaca ( 1 month ago )
<?php
define('THIS_SCRIPT', 'ft_story_reactors');
require_once('./global.php');
header('Content-Type: application/json; charset=UTF-8');
function ft_story_json_exit($arr)
{
echo json_encode($arr);
exit;
}
function ft_story_fail($error)
{
ft_story_json_exit(array(
'success' => false,
'error' => $error
));
}
function ft_story_get_valid_reactions()
{
return array(
'like' => '????',
'dislike' => '????',
'love' => '❤️',
'laugh' => '????',
'wow' => '????',
'sad' => '????',
'angry' => '????'
);
}
function ft_story_empty_counts($valid_reactions)
{
$out = array();
foreach ($valid_reactions as $k => $v)
{
$out[$k] = 0;
}
return $out;
}
function ft_story_fetch_reaction_counts($storyid, $db, $valid_reactions)
{
$counts = ft_story_empty_counts($valid_reactions);
$total = 0;
$result = $db->query_read("
SELECT reaction, COUNT(*) AS cnt
FROM " . TABLE_PREFIX . "ft_story_reaction
WHERE storyid = " . intval($storyid) . "
GROUP BY reaction
");
while ($row = $db->fetch_array($result))
{
$reaction = trim($row['reaction']);
$cnt = intval($row['cnt']);
if (isset($counts[$reaction]))
{
$counts[$reaction] = $cnt;
$total += $cnt;
}
}
return array(
'counts' => $counts,
'total' => $total
);
}
$vbulletin->input->clean_array_gpc('g', array(
'storyid' => TYPE_UINT,
));
if (intval($vbulletin->userinfo['userid']) < 1)
{
ft_story_fail('not_logged_in');
}
$storyid = intval($vbulletin->GPC['storyid']);
$userid = intval($vbulletin->userinfo['userid']);
$db = $vbulletin->db;
if ($storyid < 1)
{
ft_story_fail('invalid_story');
}
$story = $db->query_first("
SELECT storyid, userid, dateline
FROM " . TABLE_PREFIX . "ft_story
WHERE storyid = $storyid
LIMIT 1
");
if (!$story OR empty($story['storyid']))
{
ft_story_fail('story_not_found');
}
if (intval($story['userid']) !== $userid)
{
ft_story_fail('no_permission');
}
$valid_reactions = ft_story_get_valid_reactions();
$stats = ft_story_fetch_reaction_counts($storyid, $db, $valid_reactions);
$items = array();
$result = $db->query_read("
SELECT r.userid, r.reaction, r.dateline, u.username
FROM " . TABLE_PREFIX . "ft_story_reaction AS r
INNER JOIN " . TABLE_PREFIX . "user AS u ON (u.userid = r.userid)
WHERE r.storyid = $storyid
ORDER BY r.dateline DESC
");
while ($row = $db->fetch_array($result))
{
$reaction_key = trim($row['reaction']);
$items[] = array(
'userid' => intval($row['userid']),
'username' => $row['username'],
'reaction' => $reaction_key,
'emoji' => (isset($valid_reactions[$reaction_key]) ? $valid_reactions[$reaction_key] : ''),
'dateline' => intval($row['dateline'])
);
}
ft_story_json_exit(array(
'success' => true,
'storyid' => $storyid,
'reaction_total' => $stats['total'],
'reaction_counts' => $stats['counts'],
'reaction_labels' => $valid_reactions,
'items' => $items
));
Revise this Paste