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

$lst = Listopia::getInstance();

$result = array();
        
if ($lst->isLoggedIn())
{
  $listID   = $_POST['id'];
  $opMod    = $_POST['op'];

  // check our inputs
  if (!isset($listID) || !$lst->listIDIsValid($listID))
  {
    $result['result'] = "Missing or invalid list ID " . $listID;
  }
  else if (!isset($opMod))
  {
    $result['result'] = "Missing operation";  
  }
  else if (!($opMod == 'everyone' || $opMod == 'deleted'))
  {
    $result['result'] = "Invalid operation";  
  }  
  else 
  {
    $listData = $lst->getListData($listID);
    $entryCount = $lst->getNumListEntries($listID);
    $minimumEntries = $lst->getListMinimumEntries($listID);
    
    if (!empty($listData[0]) && !empty($entryCount[0]))
    {
      $listFeat = $listData[0]['t_lists']['list_featured'];
      $entryCount = $entryCount[0][0]['counter'];
      
      if ($listFeat != 'only you' && !$lst->isAdminUser())
      {
        $result['result'] = "this list is already open";
      }
      else if ($opMod == 'everyone' && $entryCount < $minimumEntries)
      {
        $result['result'] = "Needs five entries to open";
      }
      else
      {
        $date = date("Y-m-d H:i:s", time());
        
        $data = array(
          'list_featured' => $opMod,
          'list_updated'  => $date,
        );
        $okA = $lst->Database()->update('t_lists', $data, 't_lists.list_id = '.((int)$listID));
        
        if (isset($okA))
        {
          $result['success'] = true;
        }
          
        $result['result'] = $okA;        
      }
    }
    else
    {
      $result['result'] = "Couldn't fetch list data";
    }
  }
}
else
{
  $result['result'] = "Not logged in";
}

echo json_encode($result);

require("common_end.php");

