示例#1
0
function saveVote($option)
{
    global $rsgConfig, $mainframe;
    $database = JFactory::getDBO();
    $my = JFactory::getUser();
    if ($rsgConfig->get('voting') < 1) {
        $mainframe->redirect(JRoute::_("index.php?option=com_rsgallery2"), JText::_('Voting is disabled!'));
    } else {
        $rating = rsgInstance::getInt('rating', '');
        $id = rsgInstance::getInt('id', '');
        $vote = new rsgVoting();
        //Check if user can vote
        if (!$vote->voteAllowed()) {
            $mainframe->redirect(JRoute::_("index.php?option=com_rsgallery2&page=inline&id={$id}"), JText::_('You are not authorized to vote!'));
        }
        //Check if user has already voted for this image
        if ($vote->alreadyVoted($id)) {
            $mainframe->redirect(JRoute::_("index.php?option=com_rsgallery2&page=inline&id={$id}"), JText::_('You already voted for this item!'));
        }
        //All checks OK, store vote in DB
        $total = $vote->getTotal($id) + $rating;
        $votecount = $vote->getVoteCount($id) + 1;
        $sql = "UPDATE #__rsgallery2_files SET rating = '{$total}', votes = '{$votecount}' WHERE id = '{$id}'";
        $database->setQuery($sql);
        if (!$database->query()) {
            $msg = JText::_('Vote could not be added to the database!');
        } else {
            $msg = JText::_('Vote added to database!');
            //Store cookie on system
            setcookie($rsgConfig->get('cookie_prefix') . $id, $my->id, time() + 60 * 60 * 24 * 365, "/");
        }
        $mainframe->redirect(JRoute::_("index.php?option=com_rsgallery2&page=inline&id={$id}"), $msg);
    }
}