<?php

class MessageMod extends DbMod{
  var $VarTableMessageTypes = array('integer', 'integer', 'integer', 'date', 'date', 'text');

	function MessageMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}
	
	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    $ArrSqlArray['SqlAll']['select'] = 'a.*, b.users_fio AS users_fio_to, b.users_lasttime AS users_lasttime_to,c.users_fio AS users_fio_from';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableMessage .' AS a, '. $this->VarTableUsers .' AS b, '. $this->VarTableUsers .' AS c';
    $ArrSqlArray['SqlAll']['where'] = 'a.users_id_to = b.users_id AND a.users_id_from = c.users_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    ///
//    $ArrSqlArray['SqlRow']['select'] = 'a.*';
//    $ArrSqlArray['SqlRow']['from'] = $this->VarTableMessage .' AS a';
//    if('' != $VarWhere){
//      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
//    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableMessage .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlOne']['select'] = 'a.message_id';
    $ArrSqlArray['SqlOne']['from'] = $this->VarTableMessage .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlOne']['where'] = $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($ArrValues = ''){
    if(true === is_array($ArrValues) && 0 < count($ArrValues)){
      $sth = $this->ArrMsSp->SP_DbConnect->prepare('INSERT INTO '. $this->VarTableMessage .' VALUES (?, ?, ?, ?, ?, ?)', $this->VarTableMessageTypes, MDB2_PREPARE_MANIP);
      $data = array('', $ArrValues['users_id'], $ArrValues['users_id_from'], date('YmdHis'), '', $ArrValues['message_text']);
      $result = $sth->execute($data);
			if (PEAR::isError($result)) {
   			die($result->getMessage());
			}
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function Del($VarId = '', $VarFiledName = 'message_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableMessage ." WHERE ". $VarFiledName ." = '". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarResult =& $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function Familiarize($ArrValues = ''){
    if(true === isset($ArrValues['UrlId']) && true === is_numeric($ArrValues['UrlId']) && 0 < $ArrValues['UrlId']){
      $VarSql = "UPDATE ". $this->VarTableMessage ." SET message_date_familiarize = '". date('YmdHis') ."' WHERE message_id = '". $ArrValues['UrlId'] ."'";
      //echo "VarSql: ". $VarSql ."<br />";
      $res =& $this->ArrMsSp->SP_DbConnect->query($VarSql);
      if (PEAR::isError($res)) {
        die($res->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
}
?>