<?php
/* $Id: ce_comment.class.php,v 1.6 2009/06/26 08:56:33 Criss Exp $ */
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');

/**
 * CE_Plugin class
 */
class CE_Comment {
  var $id;
  var $infos = array();
  var $query;
  private static $s_user_list = null;

  private static function updateUsers() {
    $users = array();
    $query = '
      SELECT username
      FROM '. USERS_TABLE .'
      ORDER BY username';
    $result = pwg_query( $query );
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $users = array_merge($users, array($row['username'] => $row['username']));
    }
    return $users;
  }

  static function getUsers() {
    if (null == self::$s_user_list) {
      // Init user list
      self::$s_user_list = self::updateUsers();
    }
    return self::$s_user_list;
  }

  function CE_Comment($comment_id) {
    $this->id = $comment_id;
    $this->query = '
      SELECT com.id AS comment_id
         , com.image_id
         , com.author
         , com.date
         , com.content
         , com.validated
        FROM '.COMMENTS_TABLE.' AS com
        WHERE com.id = ' . $comment_id;
    $result = pwg_query($this->query);
    $this->infos = mysql_fetch_array($result, MYSQL_ASSOC);
  }

  function validateAuthor($author) {
    $user_list = self::getUsers();
    if (array_key_exists( strtolower($author),
                          array_change_key_case($user_list, CASE_LOWER))) {
      // Look in user list to have right case !
      foreach ($user_list as $user) {
        if (0 == strcasecmp($author, $user)) {
          return $user;
        }
      }
    }
    return $author;
  }

  function isKnownAuthor() {
    if (in_array($this->infos['author'], self::getUsers())) {
      return true;
    }
    return false;
  }

  function setInfo($key, $value) {
    if (null == $key) {
      return;
    }
    $this->infos[$key] = $value;
  }

  function getInfo($key = null) {
    if (null == $key) {
      return $this->infos;
    }
    return $this->infos[$key];
  }

  function isAuthor($user) {
    if (!isset ($this->infos['author'])) {
      return false;
    }
    return (0 == strcmp($this->infos['author'], $user))?true:false;
  }

  function __toString() {
    return print_r($this->infos, true);
  }
}
?>