<?php

class SBBPm {

  var $count = 0;
     
  function SBBPm() {
    global $SBBAuth, $board_config;
    $this->auth = $SBBAuth;
    $this->board_config = $board_config;
    $this->uid = $SBBAuth->uid;
  }
  
  //all private message from logged-in user
  function getPMs($show) {
    $output = array();
    if (!$show) {
      $sql = "select * from `" . $this->board_config->prefix . "pm` where to_uid = '" . $this->uid . "' and deleted_to = '0' order by sent desc";
    } elseif ($show == "outbox") {
      $sql = "select * from `" . $this->board_config->prefix . "pm` where from_uid = '" . $this->uid . "' and seen = '0' order by sent desc";
    } elseif ($show == "sent") {
      $sql = "select * from `" . $this->board_config->prefix . "pm` where from_uid = '" . $this->uid . "' and seen = '1' and deleted_from = '0' order by sent desc";
    }
    if (!$sql) return false;
    $res = dbquery($sql);
    $this->count = mysql_num_rows($res);
    while ($row = mysql_fetch_object($res)) {
      $output[] = $row;
    }
    return $output;
  }
  
  //single private message
  function getPM($id) {
    
    $sql = "select * from `" . $this->board_config->prefix . "pm` where id = '$id' and ((to_uid = '" . $this->uid . "' and deleted_to = '0') or (from_uid = '" . $this->uid . "' and deleted_from = '0'))";
    $res = dbquery($sql);
    if (!mysql_num_rows($res)) return false;
    $row = mysql_fetch_object($res);
    
    if ($row->to_uid == $this->uid && !$row->seen) dbquery("update `" . $this->board_config->prefix . "pm` set seen = '1' where id = '$id'");
    
    return $row;
  }
  
}

?>