<?php
define(&#039;THIS_SCRIPT&#039;, &#039;ft_story_reactors&#039;);
require_once(&#039;./global.php&#039;);

header(&#039;Content-Type: application/json; charset=UTF-8&#039;);

function ft_story_json_exit($arr)
{
	echo json_encode($arr);
	exit;
}

function ft_story_fail($error)
{
	ft_story_json_exit(array(
		&#039;success&#039; => false,
		&#039;error&#039;   => $error
	));
}

function ft_story_get_valid_reactions()
{
	return array(
		&#039;like&#039;    => &#039;????&#039;,
		&#039;dislike&#039; => &#039;????&#039;,
		&#039;love&#039;    => &#039;❤️&#039;,
		&#039;laugh&#039;   => &#039;????&#039;,
		&#039;wow&#039;     => &#039;????&#039;,
		&#039;sad&#039;     => &#039;????&#039;,
		&#039;angry&#039;   => &#039;????&#039;
	);
}

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[&#039;reaction&#039;]);
		$cnt      = intval($row[&#039;cnt&#039;]);

		if (isset($counts[$reaction]))
		{
			$counts[$reaction] = $cnt;
			$total += $cnt;
		}
	}

	return array(
		&#039;counts&#039; => $counts,
		&#039;total&#039;  => $total
	);
}

$vbulletin->input->clean_array_gpc(&#039;g&#039;, array(
	&#039;storyid&#039; => TYPE_UINT,
));

if (intval($vbulletin->userinfo[&#039;userid&#039;]) < 1)
{
	ft_story_fail(&#039;not_logged_in&#039;);
}

$storyid = intval($vbulletin->GPC[&#039;storyid&#039;]);
$userid  = intval($vbulletin->userinfo[&#039;userid&#039;]);
$db      = $vbulletin->db;

if ($storyid < 1)
{
	ft_story_fail(&#039;invalid_story&#039;);
}

$story = $db->query_first("
	SELECT storyid, userid, dateline
	FROM " . TABLE_PREFIX . "ft_story
	WHERE storyid = $storyid
	LIMIT 1
");

if (!$story OR empty($story[&#039;storyid&#039;]))
{
	ft_story_fail(&#039;story_not_found&#039;);
}

if (intval($story[&#039;userid&#039;]) !== $userid)
{
	ft_story_fail(&#039;no_permission&#039;);
}

$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[&#039;reaction&#039;]);

	$items[] = array(
		&#039;userid&#039;   => intval($row[&#039;userid&#039;]),
		&#039;username&#039; => $row[&#039;username&#039;],
		&#039;reaction&#039; => $reaction_key,
		&#039;emoji&#039;    => (isset($valid_reactions[$reaction_key]) ? $valid_reactions[$reaction_key] : &#039;&#039;),
		&#039;dateline&#039; => intval($row[&#039;dateline&#039;])
	);
}

ft_story_json_exit(array(
	&#039;success&#039;         => true,
	&#039;storyid&#039;         => $storyid,
	&#039;reaction_total&#039;  => $stats[&#039;total&#039;],
	&#039;reaction_counts&#039; => $stats[&#039;counts&#039;],
	&#039;reaction_labels&#039; => $valid_reactions,
	&#039;items&#039;           => $items
));

Add a code snippet to your website: www.paste.org