<?php

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

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'sm.*, s.*, st.svttype_name, so1.structureorganization_title AS structureorganization_title_from, so2.structureorganization_title AS structureorganization_title_to, u1.users_fio AS users_fio_from, u2.users_fio AS users_fio_to';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableSvt .' AS s, '. $this->VarTableSvtType .' AS st, '. $this->VarTableSvtMove .' AS sm';
    $ArrSqlArray['SqlAll']['leftjoin'] = $this->VarTableStructureOrganization .' AS so1 ON sm.structureorganization_id_from = so1.structureorganization_id AND sm.structureorganization_id_from IS NOT NULL
                                         LEFT JOIN '. $this->VarTableStructureOrganization .' AS so2 ON sm.structureorganization_id_to = so2.structureorganization_id AND sm.structureorganization_id_to IS NOT NULL
                                         LEFT JOIN '. $this->VarTableUsers .' AS u1 ON sm.users_id_from = u1.users_id AND sm.users_id_from IS NOT NULL
                                         LEFT JOIN '. $this->VarTableUsers .' AS u2 ON sm.users_id_to = u2.users_id AND sm.users_id_to IS NOT NULL';
    $ArrSqlArray['SqlAll']['where'] = 'sm.svt_id = s.svt_id AND s.svttype_id = st.svttype_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'] = $ArrSqlArray['SqlAll'];
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableSvtMove .'
                (svtmove_id, svt_id, structureorganization_id_from, structureorganization_id_to, users_id_from, users_id_to, svtmove_note, svtmove_date, svtmove_is_deleted)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableSvtMoveTypes, MDB2_PREPARE_MANIP);
      if (PEAR::isError($sth)) {
        return array('error' => $sth->getMessage());
      }
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['svt_id'],
        $values['structureorganization_id_from'],
        $values['structureorganization_id_to'],
        $values['users_id_from'],
        $values['users_id_to'],
        $values['svtmove_note'],
        $values['svtmove_date'],
        $values['svtmove_is_deleted']
      );
//      SiteLibs::GetVarDump($ArrData);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableSvtMove, 'svtmove_id');
      if (PEAR::isError($VarId)) {
        return array('error' => $VarId->getMessage());
      }
      return $VarId;
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'svtmove_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "UPDATE ". $this->VarTableSvtMove ." SET 
                  svtmove_is_deleted = 1
                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)){
      if(false === isset($values['svtmove_note'])){
        $values['svtmove_note'] = '';
      }
      else{
        $values['svtmove_note'] = SiteLibs::ConvertTextToSql($values['svtmove_note']);
      }
      if(false === isset($values['svtmove_date'])){
        $values['svtmove_date'] = date("Y-m-d H:i:s");
      }
      else {
        $values['svtmove_date'] = SiteLibs::ConvertSqlDatetimeInsert($values['svtmove_date']." ". date("H:i:s"));
      }
      $values['svtmove_is_deleted'] = NULL;
    }
    return $values;
  }
}
?>