<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
// Author of File: 							     //
// Kazumi Ono (http://www.mywebaddons.com/ , http://www.myweb.ne.jp/)        //
// ------------------------------------------------------------------------- //
include_once(XOOPS_ROOT_PATH."/class/xoopsobject.php");
class XoopsPM extends XoopsObject {

	// public
	function XoopsPM($id=NULL){
		$this->XoopsObject();
		$this->initVar("msg_id", "int", NULL, false);
		$this->initVar("msg_image", "other", "icon1.gif", false, 100, false);
		$this->initVar("subject", "textbox", NULL, true, 255, true);
		$this->initVar("from_userid", "int", NULL, true);
		$this->initVar("to_userid", "int", NULL, true);
		$this->initVar("msg_time", "other", NULL, false);
		$this->initVar("msg_text", "textarea", NULL, true, NULL, true);
		$this->initVar("read_msg", "int", 0, false);
		if ( !empty($id) ) {
			if ( is_array($id) ) {
				$this->set($id);
			} else {
				$this->load(intval($id));
			}
		}
	}

	// public
	function store(){
		if ( !$this->isCleaned() ) {
			if ( !$this->cleanVars() ) {
				return false;
			}
		}
		foreach ( $this->cleanVars as $k=>$v ) {
			$$k = $v;
		}
		if ( empty($msg_id) ) {
			$msg_id = $this->db->genId($this->db->prefix("priv_msgs")."_msg_id_seq");
			$sql = "INSERT INTO ".$this->db->prefix("priv_msgs")." (msg_id, msg_image, subject, from_userid, to_userid, msg_time, msg_text, read_msg) VALUES ($msg_id, '$msg_image', '$subject', '$from_userid', '$to_userid', ".time().", '$msg_text', 0)";
		}
		//echo $sql;
		if ( !$result = $this->db->query($sql) ) {
			$this->errors[] = "Could not store message in the database.";
			return false;
		}
		return true;
	}

	// private
	function load($id){
		$sql = "SELECT * FROM ".$this->db->prefix("priv_msgs")." WHERE msg_id=".$id."";
		$myrow = $this->db->fetchArray($this->db->query($sql));
		$this->set($myrow);
	}

	// public
	function delete(){
		$sql = "DELETE FROM ".$this->db->prefix("priv_msgs")." WHERE msg_id=".$this->getVar("msg_id")."";
        	if ( !$this->db->query($sql) ) {
			return false;
		}
		return true;
	}

	// private, static
	function getAllPM($criteria=array(), $asobject=false, $sort="msg_time", $order="ASC", $limit=0, $start=0){
		$db =& Database::getInstance();
		$ret = array();
		$where_query = "";
		if ( is_array($criteria) && count($criteria) > 0 ) {
			$where_query = " WHERE";
			foreach ( $criteria as $c ) {
				$where_query .= " $c AND";
			}
			$where_query = substr($where_query, 0, -4);
		}
		if ( !$asobject ) {
			$sql = "SELECT msg_id FROM ".$db->prefix("priv_msgs")."$where_query ORDER BY $sort $order";
			$result = $db->query($sql,$limit,$start);
			while ( $myrow = $db->fetchArray($result) ) {
				$ret[] = $myrow['msg_id'];
			}
		} else {
			$sql = "SELECT * FROM ".$db->prefix("priv_msgs")."".$where_query." ORDER BY $sort $order";
			$result = $db->query($sql,$limit,$start);
			while ( $myrow = $db->fetchArray($result) ) {
				$ret[] = new XoopsPM($myrow);
			}
		}
		//echo $sql;
		return $ret;
	}

	// public
	function setRead(){
		$sql = "UPDATE ".$this->db->prefix("priv_msgs")." SET read_msg=1 WHERE msg_id=".$this->getVar("msg_id")."";
		if (!$this->db->queryF($sql)) {
			return false;
		}
		return true;
	}
}
?>