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

$lst = Listopia::getInstance();

$sQuery = $_POST['query'];
if (!isset($sQuery))
  die("no query set");

$qMode = $_POST['mode'];
if (!isset($qMode))
  die("no mode set");

$searchMode = ((int)$qMode);

$lst = Listopia::getInstance();


  function emitEntry($lst, $entryID, $commentary = null) 
  { 
    $query = $lst->Database()->select(array(
                        'fields' => 't_items.*, users.user_name, users.user_id, t_lists.list_headline',
                        'table' => 't_items',
                        'join' => 'users on users.user_id = t_items.item_createdby join t_lists on t_lists.list_id = t_items.item_list',
                        'condition' => 't_items.item_id = ' . $entryID
                        ));
    
    $query = $query[0];
    
    $mainHTML = "";
    if ($commentary != null)
    {
      $mainHTML = "<div class='guide-entry'>" . $commentary . "</div>";
    }
    
    $listAnch = ListLink($query['t_items']['item_list'], "#entry_" . $entryID );

    $entryComments = $lst->getListEntryComments($query['t_items']['item_list']);
    
    $isImagePost = ($query['t_items']['item_extra'] == 'image');
    $posterProfileLink = ProfileLink($query['t_items']['item_createdby'], '#PageContent');

    $entryDisc = $entryComments[$entryID];
    $commentCount = 'No';
    if (isset($entryDisc))
    {
      $commentCount = count($entryDisc);
    }
    
    if ($isImagePost)
      $postContent = stripslashes($query['t_items']['item_blurb']);
    else
      $postContent = ConditionTextForDisplay($query['t_items']['item_blurb']);    

      if ($isImagePost)
      {
$mainHTML .= <<<HTML
        <div class='lst-entry'><div>
          <a id='entry_$entryID' name='entry_$entryID'></a>
          <img title='#$entryID' style='margin:4px auto 4px auto;' class='ui-corner-all lst-avatar-img' src='{$postContent}'/>
HTML;
      }
      else
      {
        $contentPrefix = "<p title='#$entryID'>";
        $contentSuffix = "</p>";
      
$mainHTML .= <<<HTML
        <div class='lst-entry'><div>
          <a id='entry_$entryID' name='entry_$entryID'></a>
          {$contentPrefix}{$postContent}{$contentSuffix}
HTML;
      }

      $commentPrefix = "<img src='images/chat-guide.png' style='vertical-align: text-bottom; padding-right: 4px;'/>";
      
$mainHTML .= <<<HTML
          <div class='lst-post-author'><a $posterProfileLink >- {$query['users']['user_name']}</a></div>
          <code><a tabindex='0' style='cursor:pointer;' onclick='$(this.parentNode.parentNode.parentNode).next().toggle("blind");'>
          $commentPrefix $commentCount Comments
        </a></code>
        </div></div>
      
      <div class='lst-comment-pile-entries'>
      <div id='lst-comment-replacable-$entryID'>
HTML;
      if ($commentCount > 0)
      {
        foreach ($entryDisc as &$comment)
        {
          $mainHTML .= $lst->generateDiscussionCommentHTML($comment);
        }
      }
      $mainHTML .= "</div></div>";
      $mainHTML .= "<div style='font-size:0.8em; float:right;'>... from '<a " . $listAnch . ">" . stripslashes($query['t_lists']['list_headline']) . "</a>'</div><div style='clear:right;'></div><br>";

      echo $mainHTML;
  }


if ($lst->isLoggedIn())
{
  // SELECT item_id FROM t_items WHERE MATCH(item_blurb) AGAINST ('boob*' IN BOOLEAN MODE) ORDER BY item_created DESC
  // SELECT disc_id FROM t_discussion WHERE MATCH(disc_comment) AGAINST ('boob*' IN BOOLEAN MODE) ORDER BY disc_datetime DESC


  $query = $lst->searchQuery( $sQuery, $searchMode );

  if ( $searchMode == 0 )
  {
    $lst->UI_Misc()->openToggleBlock("POSTS", "FOUND " . sizeof($query) );

    foreach ($query as &$listIDs)
    {
      emitEntry($lst, $listIDs["t_items"]["item_id"] );
    }
  }
  if ( $searchMode == 1 )
  {
    $lst->UI_Misc()->openToggleBlock("COMMENTS", "FOUND " . sizeof($query) );

    foreach ($query as &$listIDs)
    {
      emitEntry($lst, $listIDs["t_discussion"]["disc_item"] );

    }
    
  }


  $lst->UI_Misc()->closeGenericBlock();


}
else
{
  echo "<b>NOPE</b>";
}
  
require("common_end.php");
