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

$lst = Listopia::getInstance();

$result = array();
        
if ($lst->isLoggedIn())
{
  if ($lst->isUserOnProbation())
  {
    die("user is on probation");
  }
  
  $listTitle    = $_POST['title'];
  $listDesc     = $_POST['desc'];
  $listRules    = $_POST['rules'];
  $listMax      = $_POST['maxchars'];
  $listOrdered  = $_POST['ordered'];
  $listSketch   = $_POST['sketch'];

  // check our inputs
  if (!isset($listTitle))
  {
    $result['result'] = "No title";
  }
  else if (!isset($listDesc))
  {
    $result['result'] = "No desc";
  }
  else if (!isset($listRules))
  {
    $result['result'] = "No rules";
  }
  else if (!isset($listMax))
  {
    $result['result'] = "No max characters specified";
  }
  else if (!isset($listOrdered))
  {
    $result['result'] = "No list ordering specified";
  }
  else if (strlen($listTitle) < 2)
  {
    $result['result'] = "Post is too big";
  }
  else if (strlen($listDesc) < 2)
  {
    $result['result'] = "Post is too small";
  }
  // we're okay to plug this in
  else
  {
    $newListHeadline = htmlspecialchars($listTitle);
    $newListDesc = htmlspecialchars($listDesc);
    $newListRules = htmlspecialchars($listRules);
    $newListRules = "<p>" . str_replace("@@", "</p><p>", $newListRules) . "</p>";

    $date = date("Y-m-d H:i:s", time());

    
    $data = array(
      'list_headline'     => $newListHeadline,
      'list_standfirst'   => $newListDesc,
      'list_blurb'        => $newListRules,
      'list_maxchars'     => array( $listMax ),
      
      'list_createdby'    => array(Session::getInstance()->gl_userid),

      'list_created'      => $date,
      'list_updated'      => $date,
      'list_submission'   => 'members',
      'list_featured'     => 'only you',
      
      'list_type'         => (($listSketch == 'true') ? 'image' : 'normal'),
      'list_orderby'      => (($listOrdered == 'true') ? 'time' : 'score')
    );
    $insertResult = $lst->Database()->insert('t_lists', $data);
    
    if (isset($insertResult))
    {
      $result['success'] = true;
    }
      
    $result['result'] = $insertResult;
  }
}
else
{
  $result['result'] = "Not logged in";
}

echo json_encode($result);

require("common_end.php");

