<?php

class PassMod extends DbMod{
  var $VarTablePassTypes = array('integer', 'integer', 'text', 'text', 'text', 'text', 'date', 'integer');
  
  function PassMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'p.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTablePass .' AS p, '. $this->VarTableUsers .' AS u';
    $ArrSqlArray['SqlAll']['where'] = 'p.users_id = u.users_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTablePass .' AS p, '. $this->VarTableUsers .' AS u';
    $ArrSqlArray['SqlCount']['where'] = 'p.users_id = u.users_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 1 <= count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTablePass .'
                (pass_id, users_id, pass_name, pass_login, pass_pass, pass_note, pass_date, pass_share)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['users_id'],
        $values['pass_name'],
        $values['pass_login'],
        $values['pass_pass'],
        $values['pass_note'],
        $values['pass_date'],
        $values['pass_share']
      );
      //SiteLibs::GetVarDump($data);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTablePassTypes, MDB2_PREPARE_MANIP);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTablePass, 'pass_id');
      if (PEAR::isError($VarId)) {
        return array('error' => $VarId->getMessage());
      }
      return $VarId;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Edit($values){
    if(true === is_array($values) && 1 <= count($values)){
      $VarSql = "UPDATE ". $this->VarTablePass ." SET
                  pass_name = ?,
                  pass_login = ?,
                  pass_pass = ?,
                  pass_note = ?,
                  pass_share = ?
                  WHERE pass_id = '".  $values['pass_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['pass_name'],
        $values['pass_login'],
        $values['pass_pass'],
        $values['pass_note'],
        $values['pass_share']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['pass_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'pass_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTablePass ." WHERE ". $VarFiledName ." = '". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarQueryResult = $this->ArrMsSp->SP_DbConnect->exec($sql);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return true;
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}
  
  function PrepareData($values){
    if(true === is_array($values) && 1 <= count($values)){
      $values['users_id'] = $_SESSION['SessionAuth']['id'];
      $values['pass_date'] = date('YmdHis');
       if (true === isset($values['pass_share']) && 1 == $values['pass_share']){
        $values['pass_share'] = 1;
      }
      else{
        $values['pass_share'] = 0;
      }
    }
    return $values;
  }
}
?>