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

  error_reporting(E_ALL);
  ini_set('display_errors', '1');


  $lst = Listopia::getInstance();
  // -------------------------------
  if (!$lst->isLoggedIn())
  {
    $lst->unloggedInPage();
    return;
  }
  // -------------------------------

  
  $lst->pageBegin(LS_INDEX_DOCUMENT_TITLE, "Love means never having to say you're sorry");
  
  $sidebar = array(
    SideBarItem::Welcome,
    SideBarItem::BasicListNavigator,
    SideBarItem::Profile
    );
  
  $lst->contentsBeginWithSidebar($sidebar, LS_INDEX_TITLE, LS_INDEX_DESC);

  
  // -----------------------------------------------------------------------------------------------
  // function to emit a list entry block, given a title (what this list represents), the list data
  // from the db and a function to print some relevant statistic as a suffix
  function writeFrontPageListEntry($title, $list, $addendumFn)
  {
    $htmlTitle = htmlspecialchars($title);
    $listID = $list['list_id'];
    $headline = stripslashes($list['list_headline']);
    $desc = stripslashes($list['list_standfirst']);
    
  ?>
    <div class="lst-fp-suggestion">
      <p id="type"><?=$htmlTitle?></p>
      <p id="headline"><a <?php echo ListLink($listID); ?> ><?=$headline?></a></p>
      <p id="desc"><?=$desc?></p>
      
      <p class="ui-corner-all" id="factoid"><?php call_user_func($addendumFn, $list); ?></p>
    </div>
  <?php
  }


  // -----------------------------------------------------------------------------------------------
  // write out some lists sorted in various ways - newest, oldest, etc
  
  // -----------------------------------------------------------------------------------------------
  $query = $lst->Database()->select(array(
                          'table' => 't_lists',
                          'condition' => DBQ_CONDITION_PUBLIC_LIST,
                          'order' => 'list_created desc',
                          'limit' => 1
                          ));
  if (!empty($query[0]))
    writeFrontPageListEntry(
      "Our newest list...",
      $query[0]['t_lists'],
      function($list) { printf("Started %s", PleasantDate($list['list_created']) ); }
      );

  // -----------------------------------------------------------------------------------------------
  $query = $lst->Database()->select(array(
                          'table' => 't_lists',
                          'condition' => DBQ_CONDITION_PUBLIC_LIST,
                          'order' => 'list_updated desc',
                          'limit' => 1
                          ));
  if (!empty($query[0]))
    writeFrontPageListEntry(
      "Most recently updated...",
      $query[0]['t_lists'],
      function($list) { printf("Updated %s", PleasantDate($list['list_updated']) ); }
      );
  
  // -----------------------------------------------------------------------------------------------
  $query = $lst->Database()->select(array(
                          'fields' => 't_lists.*, users.user_name',
                          'table' => 't_lists',
                          'join' => 'users on list_createdby = user_id',
                          'condition' => DBQ_CONDITION_PUBLIC_LIST,
                          'order' => 'rand()',
                          'limit' => 1
                          ));
  if (!empty($query[0]))
  {
    // store this for the closure to use later
    $query[0]['t_lists']['creator_username'] = $query[0]['users']['user_name'];
    
    writeFrontPageListEntry(
      "A list from the lucky dip...",
      $query[0]['t_lists'],
      function($list) { printf("It was started by %s", $list['creator_username'] ); }
      );
  }

  // -----------------------------------------------------------------------------------------------
  $query = $lst->Database()->select(array(
                          'table' => 't_lists',
                          'condition' => DBQ_CONDITION_PUBLIC_LIST,
                          'order' => 'list_updated asc',
                          'limit' => 50
                          ));
  if (!empty($query[0]))
    writeFrontPageListEntry(
      "Revive a dusty old list...",
      $query[array_rand($query)]['t_lists'],
      function($list) { printf("Last Updated %s", PleasantDate($list['list_updated']) ); }
      );
            

  // -----------------------------------------------------------------------------------------------
  // this triple-shufflin' mighty-morphin' monster of a query finds the lists with the most discussed items
  // (we ignore Bugs & Wish List, ID 12)
  $query = $lst->Database()->select(array(
                          'fields' => 'count(t_discussion.disc_item) as disc_count, t_lists.list_id, t_lists.list_headline, t_lists.list_standfirst',
                          'table' => 't_discussion, t_items, t_lists',
                          'condition' => '(t_discussion.disc_item != -1 and t_discussion.disc_item = t_items.item_id and t_items.item_list = t_lists.list_id and t_lists.list_id != 12)',
                          'group' => 't_items.item_list',
                          'order' => 'disc_count desc',
                          'limit' => 30
                          ));

  if (!empty($query[0]))
  {
    $randomChoice = $query[array_rand($query)];
    
    // store this for the closure to use later
    $randomChoice['t_lists']['disc_count'] = $randomChoice[0]['disc_count'];
  
    writeFrontPageListEntry(
      "Join the \"debate\"...",
      $randomChoice['t_lists'],
      function($list) { printf("This list is beseiged by %s comments", $list['disc_count'] ); }
      );
  }
  
  // -----------------------------------------------------------------------------------------------
  $query = $lst->Database()->select(array(
                          'fields' => 't_lists.*, count(*) as item_count',
                          'table' => 't_lists, t_items',
                          'condition' => 't_items.item_list = t_lists.list_id and ' . DBQ_CONDITION_PUBLIC_LIST,
                          'group' => 'list_id',
                          'order' => 'item_count asc',
                          'limit' => 50
                          ));
                          
  if (!empty($query[0]))
  {
    $randomChoice = $query[array_rand($query)];
    
    // store this for the closure to use later
    $randomChoice['t_lists']['item_count'] = $randomChoice[0]['item_count'];
    
    writeFrontPageListEntry(
      "Help out a tiny list...",
      $randomChoice['t_lists'],
      function($list) { printf("This brave list has just %s entries", $list['item_count'] ); }
      );
  }

  // -----------------------------------------------------------------------------------------------
  $lst->contentsEnd();

  $lst->UI_Login()->GenLoginDialog();
  
  // default script loading
  $lst->pageEndScripts();
  
  $lst->UI_Login()->GenLoginScripting();
?>

<?php
  $lst->pageEnd();

  require("php/common_end.php");
?>
