<?php 
require("common_begin.php");

$lst = Listopia::getInstance();

if ($lst->isLoggedIn())
{
  $result = array();

  $sesh = Session::getInstance();
  
  $currentUserID = $sesh->gl_userid;
  
  if ($lst->isUserOnProbation())
  {
    die("{ }");
  }
  
  
  $voteUp = ($_POST['voteUp'] === "true");
  $listID = $_POST['list'];
  
  if (gettype($voteUp) !== "boolean")
    die("vote type unrecognised");
  if (!isset($listID))
    die("no list ID specified");
    
  $voteDBValue = $voteUp ? 1 : -1;
 
  
  
  // go check for an existing vote 
  $listVotesQueryCondition = 'listvotes.list = ' . ((int)$listID) . ' and listvotes.user = ' . ((int)$currentUserID);
  $query = $lst->Database()->select(array(
                      'fields' => '*',
                      'table' => 'listvotes',
                      'condition' => $listVotesQueryCondition,
                      'limit' => 1
                      ));
                      
  // we have already cast a vote on this list
  $query = $query[0];
  if (!empty($query))
  {
    FB::log("current vote : " . $query['listvotes']['vote']);
    FB::log("new vote : " . $voteDBValue);
        
    $alreadyVotedThisWay = ($voteUp && $query['listvotes']['vote'] === 1) ||
                           (!$voteUp && $query['listvotes']['vote'] === -1);
                           
    // we have already cast an identical vote; discard. possible feature would be to undo this vote entirely
    if ($alreadyVotedThisWay)
    {
      FB::log("already voted on this list " . $listID);
      $result['problem'] = "Duplicate Vote";
    }
    // otherwise we need to recast this. 
    else
    {
      // first up, rewrite the entry in 'listvotes'
      $data = array(
        'vote' => $voteDBValue
      );
      $okA = $lst->Database()->update('listvotes', $data, $listVotesQueryCondition);
      
      if ($okA !== false)
      {
        FB::log("changing vote on " . $listID);
      }
    }
  }
  // we've never voted on this before
  else
  {
    // create new entry in itemlikes
    $data = array(
      'user'       => array(((int)Session::getInstance()->gl_userid)),
      'list'       => array(((int)$listID)),
      'vote'       => $voteDBValue
    );
    $okA = $lst->Database()->insert('listvotes', $data);
    
    if ($okA !== false)
    {
      FB::log("adding vote to " . $listID);
    }
  }
  
  // we return the state of both like/hate counters, incase we swapped our vote
  $upQuery = $lst->Database()->select(array(
                      'fields' => 'sum(vote) as upvote',
                      'table' => 'listvotes',
                      'condition' => 'listvotes.list = ' . ((int)$listID) . ' and vote = 1'
                      ));
  $dnQuery = $lst->Database()->select(array(
                      'fields' => 'sum(vote) as downvote',
                      'table' => 'listvotes',
                      'condition' => 'listvotes.list = ' . ((int)$listID) . ' and vote = -1'
                      ));
                      
  if (!empty($upQuery[0]) && !empty($dnQuery[0]))
  {
    $result['up'] = sprintf("%02d", intval( $upQuery[0][0]['upvote'] ));
    $result['down'] = sprintf("%02d", - intval( $dnQuery[0][0]['downvote'] ));
  }
}

echo json_encode($result);

require("common_end.php");

