<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  define("_VALID_PHP", true);
  require_once("../../init.php");
  
  require_once(ZAZAVILITE . "admin/modules/comments/admin_class.php");
  require_once(ZAZAVILITE . "admin/modules/comments/lang/" . $core->language . ".lang.php");
  $com = new Comments();
  
  $page = sanitize($_GET['pg']);
  $page = (int)$page;

  $page_id = sanitize($_GET['pageid']);
  $page_id = (int)$page_id;
  
  $commentTree = array();

  /**
   * getCommentTree()
   *
   * @param integer $page
   * @param integer $page_id
   * @return
   */
  function getCommentTree($page, $page_id)
  {
      global $db, $com;

      $start = ($page - 1) * $com->perpage;
	  $limit = $start.','.$com->perpage;

      $sql = "SELECT *, DATE_FORMAT(created, '" . $com->dateformat . "') as cdate" 
	  . "\n FROM mod_comments" 
	  . "\n WHERE page_id = " . $db->escape($page_id)
	  . "\n AND active = '1'" 
	  . "\n ORDER BY created " . $com->sorting . " LIMIT  ". $limit;
	  
      $query = $db->query($sql);
      $commentTree = '';
      while ($row = $db->fetch($query)) {
          $commentTree[$row['id']] = array(
			  'id' => $row['id'], 
			  'parent_id' => $row['parent_id'], 
			  'page_id' => $row['page_id'], 
			  'username' => $row['username'], 
			  'email' => $row['email'], 
			  'www' => $row['www'],
			  'body' => $row['body'], 
			  'cdate' => $row['cdate']
		  );
      }
      return $commentTree;
  }
  
  /**
   * renderComments()
   *
   * @param mixed $array
   * @param integer $parent_id
   * @return
   */
  function renderComments($array, $parent_id = 0)
  {
      global $com;
      
      if ($array) {
          $subtree = false;
          
          foreach ($array as $key => $row) {
              if ($row['parent_id'] == $parent_id) {
                  if ($subtree === false) {
                      $subtree = true;
                      
                      print "<ul>\n";
                  }
                  
                  ob_start();
                  include(MODDIR . "/comments/template.tpl.php");
                  $html = ob_get_contents();
                  ob_end_clean();
                  
                  print '<li class="comment" id="comment-' . $row['id'] . '">';
                  print $html;
                  renderComments($array, $key);
                  print "</li>\n";
              }
          }
          unset($row);
          
          if ($subtree === true)
              print "</ul>\n";
      }
  }
  $data = getCommentTree($page, $page_id);
?>
<?php echo renderComments($data,0);?>