<?php
class sfDatabaseOnlineUser {

  protected static $instance = null;

  public static function getInstance() {
    if (self::$instance==null) {
      self::$instance = new sfDatabaseOnlineUser();
    }
    return self::$instance;
  }

  public function addUser(myUser $user) {
    if (sfDatabaseSessionStorage::$is_bot) return;
    $this->user = $user;
    $pdo = sfDatabaseSessionStorage::$pdo;
    $blog = blogStore::$blogId;
    $time = time();

    if ($user->isAuthenticated()) {
      $id = $pdo->quote($user->getUserId());
      $fullname = $pdo->quote($user->getUserFullname());
      $pdo->exec("insert into online_user values($blog, $id, $fullname, $time) on duplicate key update ou_time=$time");
    } else {
      $crc = crc32(myUtility::getRealIpAddr());
      $pdo->exec("insert into online_guest values($blog, $crc, $time) on duplicate key update og_time=$time");
    }
  }

  public function removeCurrentUser() {
    if ($this->user) {
      if ($this->user->isAuthenticated()) {
        sfDatabaseSessionStorage::$pdo->exec('delete from online_user where ou_userid = '.$this->user->getUserId());
      } else {
        $crc = crc32(myUtility::getRealIpAddr());
        sfDatabaseSessionStorage::$pdo->exec('delete from online_guest where og_crc = '.$crc);
      }
    }
  }

  public function getOnlineUsers($baseLink, &$numGuest, &$numUser, $maxNum=100) {
    $ret = '';
    $bl_id = blogStore::$blogId;
    $pdo = sfDatabaseSessionStorage::$pdo;
    $numGuest = $pdo->query("select count(*) from online_guest where og_blog=$bl_id")->fetchColumn();
    $numUser = $pdo->query("select count(*) from online_user where ou_blog=$bl_id")->fetchColumn();
    $stmt = $pdo->query("select ou_userid, ou_user_fullname from online_user where ou_blog=$bl_id limit $maxNum");
    while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
      $ret .= '<li>'.link_to($row[1], $baseLink.$row[0]).'</li>';
    }
    return $ret;
  }

  public function getAllOnlineUsers($baseLink, &$numGuest, &$numUser, $maxNum=100) {
    $ret = '';
    $bl_id = blogStore::$blogId;
    $pdo = sfDatabaseSessionStorage::$pdo;
    $numGuest = $pdo->query("select count(*) from sessions where ss_userid=0")->fetchColumn();
    $numUser = $pdo->query("select count(*) from sessions where ss_userid>0")->fetchColumn();
    $stmt = $pdo->query("select ss_userid, ss_user_fullname from sessions where ss_userid>0 limit $maxNum");
    $order = 0;
    while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
      if (++$order == 10) $ret .="<div id='user_online' style='display:none; cursor:pointer'>";
      $ret .= '<li>'.link_to($row[1], $baseLink.$row[0]).'</li>';
    }
    if ($numUser>=10) {
      $ret .="</div><span id='user_off' style=\"display:none; cursor:pointer\" onClick=\"document.getElementById('user_online').style.display='none';document.getElementById('user_on').style.display='inline';  document.getElementById('user_off').style.display='none'; \" > << </span>    <span style=\"cursor:pointer\" id='user_on' onClick=\"document.getElementById('user_online').style.display='inline'; document.getElementById('user_on').style.display='none';document.getElementById('user_off').style.display='inline'; \" > >> </span>";
    } 
    return $ret;
  }
}
