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

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


  $lst->pageBegin(LS_NEWSTUFF_DOCUMENT_TITLE, "Ch-ch-ch-changes", array("js/listopia-lists.js"));
  
  $sidebar = array(
    SideBarItem::BasicListNavigator,
    SideBarItem::Profile);
 
  $lst->contentsBeginWithSidebar($sidebar, LS_NEWSTUFF_TITLE, LS_NEWSTUFF_DESC);

  
  { 
    // ---------------------------------------------------------------------------------------------
    $lst->UI_Misc()->openGenericBlock("Recent Posts");
    {
      $listsCounter = array();
      $postToDisplay = array();
      $listOrder = array();
      
      $query = $lst->Database()->select(array(
                          'fields'    => 't_items.item_blurb, t_items.item_createdby, t_items.item_id, t_items.item_extra, t_items.item_votesup, t_items.item_votesdown, t_lists.list_id, t_lists.list_headline, users.user_name, users.user_id',
                          'table'     => 't_items',
                          'join'      => 't_lists on t_lists.list_id = t_items.item_list and t_lists.list_id != 3171 and t_lists.list_featured != "deleted" join users on users.user_id = t_items.item_createdby',
                          'condition' => 't_items.item_rejected = "n"',
                          'order'     => 't_items.item_created desc',
                          'limit'     => 64
                          ));
      if (!empty($query[0]))
      {
        // shuffle the results into unique buckets; each time we hit a new list ID, we 
        // store the ID squentially in another array so that the temporal ordering is maintained 
        // when we build the html below
        foreach ($query as &$thePost)
        {
          $listID = $thePost['t_lists']['list_id'];
          if (!isset($listsCounter[$listID]))
          {
            // first time we hit this list ID
            $listsCounter[$listID] = 1;
            array_push($listOrder, $listID);
          }
          else
          {
            $listsCounter[$listID] ++;
          }
          
          // store the first hit post so we record the latest shit from the DB
          if (!isset($postToDisplay[$listID]))
            $postToDisplay[$listID] = $thePost;
        }
      
        $firstEntry = null;
        $maxCount = 0;
        
        // build the grouped HTML
        foreach ($listOrder as $listID)
        {
          $maxCount += 1;
          if ($maxCount > 15)
            break;
            
          if ($firstEntry != null)
            print("<div style='border-top:1px dashed #ddd';><br>");
            
          if ($listsCounter[$listID] > 1)
          {
            $groupTitles = $GLOBALS['LS_LATEST_STUFF_GROUPINGS'];
            $groupTitle = $groupTitles[array_rand($groupTitles)];          
            
            printf("<p class='smaller-centered'><strong style='font-size:1.25em'>%d</strong> %s</p>", $listsCounter[$listID], $groupTitle);
          }
          
          $thePost = $postToDisplay[$listID];          

          $lst->defaultFormatListPost($thePost);
                    
          $entryID = $thePost['t_items']['item_id'];
        
if ($lst->isLoggedIn())        
{
        $voteUp = sprintf("%02d", intval( $thePost['t_items']['item_votesup'] ));
        $voteDown = sprintf("%02d", intval( $thePost['t_items']['item_votesdown'] ));
          
        $wasMadeByCurrentUser = ($thePost['t_items']['item_createdby'] === Session::getInstance()->gl_userid);
        $thumbDownOnClick = (!$wasMadeByCurrentUser) ? " onclick='list_HandleVote(this, false, $entryID); return false;'" : "";
        $thumbUpOnClick   = (!$wasMadeByCurrentUser) ? " onclick='list_HandleVote(this, true, $entryID); return false;'" : "";
        $thumbButtonClass = "button thumb-button" . ($wasMadeByCurrentUser ? " thumb-button-own" : "");
        
echo <<<HTML
        <div class='thumb-down'>
          <a class='$thumbButtonClass' $thumbDownOnClick ><img src='images/thumb-dn.png' width=14 height=15/><span id='vote-down-num-$entryID'>$voteDown</span></a>
        </div>
        
        <div class='thumb-up'>
          <a class='$thumbButtonClass' $thumbUpOnClick ><span id='vote-up-num-$entryID'>$voteUp</span><img src='images/thumb-up.png' width=14 height=15/></a>
        </div>
HTML;

}
          
          $profileLink = ProfileLink($thePost['users']['user_id'], '#PageContent');
          printf("<p style='font-size:0.8em;'>- <a %s>%s</a></p>", 
            $profileLink, $thePost['users']['user_name']);

          printf("<p style='text-align:right'><a %s>...</a> on <a %s>%s</a></p>", 
            ListLink($thePost['t_lists']['list_id'], "#entry_" . $entryID), 
            ListLink($thePost['t_lists']['list_id'], "#PageContent"), 
            stripslashes($thePost['t_lists']['list_headline']));

          if ($firstEntry != null)
            print("</div>");
          $firstEntry = $thePost;
        }
      }
    }
    $lst->UI_Misc()->closeGenericBlock();    
    
    print("<img src='images/linebreak.gif' width=234 height=32 class='block-divider'/>");
    
    // ---------------------------------------------------------------------------------------------
    $lst->UI_Misc()->openGenericBlock("Most Recently Thumbed Lists");
    {
      $query = $lst->Database()->select(array(
                    'fields'    => '*',
                    'table'     => 'listvotes',
                    'join'      => 't_lists on t_lists.list_id = listvotes.list',
                    'order'     => 'listvotes.timestamp desc',
                    'limit'     => 4
                    ));
      if (!empty($query[0]))
      {
        foreach ($query as &$dsc)
        {
          $thType = $dsc['listvotes']['vote'] == 1 ? "up" : "dn";
          $thImg = "<img style='top: 3px; position: relative; padding-right: 0px;' src='http://www.listopia.co.uk/images/thumb-" . $thType. ".png' width='14' height='15'>";
          
          printf("<p style='text-align:right; font-size:0.9em;'><strong><a %s>%s</a></strong> got a %s</p>", 
            ListLink($dsc['listvotes']['list'], "#PageContent"), 
            stripslashes($dsc['t_lists']['list_headline']),
            $thImg);      
        }
      }
    }
    $lst->UI_Misc()->closeGenericBlock();    
    
    print("<img src='images/linebreak.gif' width=234 height=32 class='block-divider'/>");
    
    // ---------------------------------------------------------------------------------------------
    $lst->UI_Misc()->openGenericBlock("Recent Comments");
    {
      $query = $lst->Database()->select(array(
                          'fields' => 't_discussion.disc_comment, t_discussion.disc_username, t_discussion.disc_user, t_items.item_id, t_items.item_extra, t_items.item_blurb, t_items.item_list, t_lists.list_headline',
                          'table' => 't_discussion',
                          'join' => 't_items on t_items.item_id = t_discussion.disc_item join t_lists on t_items.item_list = t_lists.list_id',
                          'order' => 't_discussion.disc_datetime desc',
                          'limit' => 45
                          ));
      if (!empty($query[0]))
      {
        $firstEntry = null;
        
        foreach ($query as &$dsc)
        {
          if ($firstEntry != null)
            print("<div style='border-top:1px dashed #ddd';><br>");
          
          $content = ConditionCommentForDisplay($lst->Database(), $dsc['t_discussion']['disc_comment']);
          $profileLink = ProfileLink($dsc['t_discussion']['disc_user'], '#PageContent');
          
          if ($dsc['t_items']['item_extra'] != 'image')
            $itemBlurb = Truncate(stripslashes($dsc['t_items']['item_blurb']), 60);
          else
            $itemBlurb = "[image]";
          
          printf("<p>&quot;%s&quot;</p>", $content);
          printf("<p style='font-size:0.8em;'>- <a %s>%s</a></p>", $profileLink, $dsc['t_discussion']['disc_username']);

          printf("<p style='text-align:right; font-size:0.9em;'><a %s>%s</a> on <strong><a %s>%s</a></strong></p>", 
            ListLink($dsc['t_items']['item_list'], "#entry_" . $dsc['t_items']['item_id']), 
            $itemBlurb,
            ListLink($dsc['t_items']['item_list'], "#PageContent"), 
            stripslashes($dsc['t_lists']['list_headline']));

          if ($firstEntry != null)
            print("</div>");
          $firstEntry = $dsc;
        }      
      }
    }
    $lst->UI_Misc()->closeGenericBlock();
    
    print("<img src='images/linebreak.gif' width=234 height=32 class='block-divider'/>");

    // ---------------------------------------------------------------------------------------------
    $lst->UI_Misc()->openGenericBlock("Recent List Comments");
    {
      $query = $lst->Database()->select(array(
                          'fields' => 't_discussion.disc_comment, t_discussion.disc_username, t_discussion.disc_user, t_lists.list_id, t_lists.list_headline',
                          'table' => 't_discussion',
                          'join' => 't_lists on t_discussion.disc_list = t_lists.list_id ',
                          'order' => 't_discussion.disc_datetime desc',
                          'limit' => 10
                          ));
      if (!empty($query[0]))
      {
        $firstEntry = null;
        
        foreach ($query as &$dsc)
        {
          if ($firstEntry != null)
            print("<div style='border-top:1px dashed #ddd';><br>");
          
          $content = ConditionCommentForDisplay($lst->Database(), $dsc['t_discussion']['disc_comment']);
          $profileLink = ProfileLink($dsc['t_discussion']['disc_user'], '#PageContent');
          
          printf("<p>&quot;%s&quot;</p>", $content);
          printf("<p style='font-size:0.8em;'>- <a %s>%s</a></p>", $profileLink, $dsc['t_discussion']['disc_username']);

          printf("<p style='text-align:right; font-size:0.9em;'> on <strong><a %s>%s</a></strong></p>", 
            ListLink($dsc['t_lists']['list_id'], "#PageContent"), 
            stripslashes($dsc['t_lists']['list_headline']));

          if ($firstEntry != null)
            print("</div>");
          $firstEntry = $dsc;
        }      
      }
    }
    $lst->UI_Misc()->closeGenericBlock();    
  }

  
  $lst->contentsEnd();

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

<!-- Local JS -->
<script type="text/javascript">
</script>

<?php
  $lst->pageEnd();

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