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

$itemID = $_POST['id'];
if (!isset($itemID))
  die("item ID not sent");

$rejectMessage = $_POST['msg'];
if (!isset($rejectMessage))
  die("rejection message not sent");

$lst = Listopia::getInstance();

if ($lst->isLoggedIn())
{
  $query = $lst->Database()->select(array(
                      'fields' => 't_lists.list_createdby, t_items.*',
                      'table' => 't_items',
                      'join' => 't_lists on t_lists.list_id = t_items.item_list',
                      'condition' => 't_items.item_id = ' . ((int)$itemID),
                      'limit' => 1
                      ));
  if (!empty($query[0]))
  {
    if ($query[0]['t_lists']['list_createdby'] == Session::getInstance()->gl_userid || $lst->isAdminUser())
    {
      if ($query[0]['t_items']['item_rejected'] == 'y')
      {
        $data = array(
          'item_rejected' => 'n',
          'item_rejected_reason' => ''
        );
      }
      else
      {
        $data = array(
          'item_rejected' => 'y',
          'item_rejected_reason' => htmlspecialchars($rejectMessage)
        );
      }
      
      $okA = $lst->Database()->update('t_items', $data, 't_items.item_id = ' . $itemID);
    }
    else
    {
      echo "[this isn't yours to reject]";
    }
  }
  else
  {
    echo "[entry not found]";
  }
}
else
{
  echo "[not logged in]";
}

require("common_end.php");

