<?php
  /**
   * Rating Voting
   *
   * @package Digital Downloads Pro
   * @author wojoscripts.com
   * @copyright 2010
   * @version $Id: rating.php, v2.00 2011-07-10 10:12:05 gewa Exp $
   */
  define("_VALID_PHP", true);
  require_once("../init.php");

?>
<?php
  if (isset($_POST['rating'])) {
	  $rating = (int)$_POST['rating'];
	  $id = (int)$_POST['id'];
  
      $data['rating'] = "INC($rating)";
      $db->update("products", $data, "id='" . $id . "'");
      
      if ($db->affected()) {
          print "!متشکـریـم";
      } else {
          print "!!!!رای غیـر مجـاز";
      }
  }
?>
<?php
  if (isset($_POST['voting'])) {
	  function getAllVotes($id)
	  {
		  global $db;
		  $votes = array();
		  
		  $sql = $db->query("SELECT id, vote_up, vote_down FROM products WHERE id = '" . (int)$id . "'");
		  
		  if ($db->numrows($sql) == 1) {
			  $row = $db->fetch($sql);
			  $votes[0] = $row['vote_up'];
			  $votes[1] = $row['vote_down'];
		  }
		  return $votes;
	  }
	  
	  
	  $id = (isset($_POST['id'])) ? $db->escape($_POST['id']) : "";
	  $action = (isset($_POST['action'])) ? $db->escape(sanitize($_POST['action'])) : "";
	  
	  $id = intval($id);
	  
	  $cur_votes = getAllVotes($id);
	  
	  if ($action == 'vote_up') {
		  $votes_up = $cur_votes[0] + 1;
		  
		  $data['vote_up'] = $votes_up;
		  $db->update("products", $data, "id='" . $id . "'");
	  } elseif ($action == 'vote_down') {
		  $votes_down = $cur_votes[1] + 1;
		  $data['vote_down'] = $votes_down;
		  $db->update("products", $data, "id='" . $id . "'");
	  }
	  
	  if ($db->affected()) {
		  echo "!متشکـریـم";
	  }
  }
?>