<?php

class UsersMod extends DbMod{
  var $VarTableUsersTypes = array('integer', 'integer', 'integer', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'text', 'integer');

	function UsersMod($ArrMsSp){
    parent::__construct($ArrMsSp);
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'u.*, s.structureorganization_title';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableUsers .' AS u, '. $this->VarTableStructureOrganization .' AS s';
    $ArrSqlArray['SqlAll']['where'] = 'u.structureorganization_id = s.structureorganization_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->VarTableUsers .' AS u, '. $this->VarTableStructureOrganization .' AS s';
    $ArrSqlArray['SqlCount']['where'] = 'u.structureorganization_id = s.structureorganization_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlServices']['select'] = 'distinct u.users_id AS id, u.*';
    $ArrSqlArray['SqlServices']['from'] = $this->VarTableUsers .' AS u, '. $this->VarTableUsersServices .' AS us';
    $ArrSqlArray['SqlServices']['where'] = 'u.users_id = us.users_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlServices']['where'] .= ' AND '. $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlServices']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlServices']['limit'] = $VarLimit;
    }
    //
//    $ArrSqlArray['SqlServicesCount']['select'] = 'count(*) AS vartotal';
//    $ArrSqlArray['SqlServicesCount']['from'] = $this->VarTableUsers .' AS u, '. $this->VarTableUsersServices .' AS us';
//    $ArrSqlArray['SqlServicesCount']['where'] = 'u.users_id = us.users_id';
//    if('' != $VarWhere){
//      $ArrSqlArray['SqlServicesCount']['where'] .= ' AND '. $VarWhere;
//    }
    ///
    $ArrResult = $this->DbGet($ArrSqlArray[$VarSqlName], $VarSqlGet);
  	return $ArrResult;
	}

  function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $VarSql = 'INSERT INTO '. $this->VarTableUsers .'
                (users_id, group_id, structureorganization_id, users_name, users_password, users_passclean, users_fio, users_note, users_tel, users_telint, users_telcell, users_email, users_jobtitle, users_numofice, users_lastip, users_lasttime, users_previp, users_prevtime, users_status)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
      $values = $this->PrepareData($values);
      $ArrData = array(
        '',
        $values['group_id'],
        $values['structureorganization_id'],
        $values['users_name'],
        $values['users_password'],
        $values['users_passclean'],
        $values['users_fio'],
        $values['users_note'],
        $values['users_tel'],
        $values['users_telint'],
        $values['users_telcell'],
        $values['users_email'],
        $values['users_jobtitle'],
        $values['users_numofice'],
        $values['users_lastip'],
        $values['users_lasttime'],
        $values['users_previp'],
        $values['users_prevtime'],
        $values['users_status']
      );
      //SiteLibs::GetVarDump($ArrData);
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql, $this->VarTableUsersTypes, MDB2_PREPARE_MANIP);
      //SiteLibs::GetVarDump($sth);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableUsers, 'users_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) && 0 < count($values)){
      $VarSql = "UPDATE ". $this->VarTableUsers ." SET 
                  group_id = ?,
                  structureorganization_id = ?,
                  users_name = ?,
                  users_password = ?,
                  users_passclean = ?,
                  users_fio = ?,
                  users_note = ?,
                  users_tel = ?,
                  users_telint = ?,
                  users_telcell = ?,
                  users_email = ?,
                  users_jobtitle = ?,
                  users_numofice = ?,
                  users_status = ?
                WHERE users_id = '". $values['users_id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
                  $values['group_id'],
                  $values['structureorganization_id'],
                  $values['users_name'],
                  $values['users_password'],
                  $values['users_passclean'],
                  $values['users_fio'],
                  $values['users_note'],
                  $values['users_tel'],
                  $values['users_telint'],
                  $values['users_telcell'],
                  $values['users_email'],
                  $values['users_jobtitle'],
                  $values['users_numofice'],
                  $values['users_status']
                );
      $sth =& $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      //SiteLibs::GetVarDump($sth);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['users_id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'users_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableUsers ." 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 = array()){
//    if(true === is_array($values) && 0 < count($values)){
      //$values['organiz_id'] = '';
//      if(true === isset($values['FrmAgreeOrganizCheckboxes']) && true === is_array($values['FrmAgreeOrganizCheckboxes'])){
//        $values['organiz_id'] = current($values['FrmAgreeOrganizCheckboxes']);
//      }
      if (true === isset($values['users_status']) && 1 == $values['users_status']){
        $values['users_status'] = 1;
      }
      else{
        $values['users_status'] = 0;
      }
      if (true === isset($values['users_password']) && true === isset($values['users_password2']) && $values['users_password'] == $values['users_password2']){
        $values['users_passclean'] = $values['users_password'];
        $values['users_password'] = md5($values['users_password']);
      }
      if (true === isset($values['users_fio']) && '' != $values['users_fio']){
        $values['users_fio'] = SiteLibs::ConvertTextToSql($values['users_fio']);
      }
      if (true === isset($values['users_tel']) && '' != $values['users_tel']){
        $values['users_tel'] = SiteLibs::ConvertTextToSql($values['users_tel']);
      }
      if (true === isset($values['users_telint']) && '' != $values['users_telint']){
        $values['users_telint'] = SiteLibs::ConvertTextToSql($values['users_telint']);
      }
      if (true === isset($values['users_email']) && '' != $values['users_email']){
        $values['users_email'] = SiteLibs::ConvertTextToSql($values['users_email']);
        $values['users_name'] = SiteLibs::ConvertTextToSql($values['users_email']);
      }
      if (true === isset($values['users_jobtitle']) && '' != $values['users_jobtitle']){
        $values['users_jobtitle'] = SiteLibs::ConvertTextToSql($values['users_jobtitle']);
      }
      if (true === isset($values['users_note']) && '' != $values['users_note']){
        $values['users_note'] = SiteLibs::ConvertTextToSql($values['users_note']);
      }
      if(true === isset($values['FrmAgreeStructureOrganizationCheckboxes']) && '' != current($values['FrmAgreeStructureOrganizationCheckboxes'])){
        $values['structureorganization_id'] = current($values['FrmAgreeStructureOrganizationCheckboxes']);
      }
      if (true === isset($values['users_numofice']) && '' != $values['users_numofice']){
        $values['users_numofice'] = SiteLibs::ConvertTextToSql($values['users_numofice']);
      }
      $values['group_id'] = 2;
      $values['users_lastip'] = $_SERVER['REMOTE_ADDR'];
      $values['users_lasttime'] = date('YmdHis');
      $values['users_previp'] = '';
      $values['users_prevtime'] = '';
//    }
    return $values;
  }
  
  function InsertSession($values){
		$_SESSION['SessionAuth']['id'] = $values['users_id'];
    $_SESSION['SessionAuth']['gid'] = @$values['group_id'];
		$_SESSION['SessionAuth']['uname'] = $values['users_fio'];
		$_SESSION['SessionAuth']['lreg'] = 1;
    $this->Update();
		$ObjWebServicesMod = new WebServicesMod($this->ArrMsSp);
		$ArrFromDbWebServices = $ObjWebServicesMod->GetFromDb('SqlAll', 'GetAll', "t2.users_id = '". $values['users_id'] ."'");
		if (true === is_array($ArrFromDbWebServices) && 0 < count($ArrFromDbWebServices)){
      foreach ($ArrFromDbWebServices AS $k => $v){
        $_SESSION['SessionAuth'][$v['webservices_session']] = '';
				if (true === isset($v['users_id']) && '' != $v['users_id']){
					$_SESSION['SessionAuth'][$v['webservices_session']]['reg'] = 1;
					$_SESSION['SessionAuth'][$v['webservices_session']]['WebservicesId'] = $v['webservices_id'];
					$_SESSION['SessionAuth']['webservices'][$v['webservices_id']] = $v['webservices_id'];
				}
				else {
					//$_SESSION['SessionAuth'][$v['webservices_session']]['WelcomePage'] = $v['href'];
					//$_SESSION['SessionAuth'][$v['webservices_session']]['WebservicesId'] = $v['webservices_id'];
					//$_SESSION['SessionAuth']['webservices'] = $v['webservices_id'];
				}
      }
		}
		return true;
	}

  function Update(){
    if(true === isset($_SESSION['SessionAuth']['id']) && true === is_numeric($_SESSION['SessionAuth']['id'])){
      $VarSql = "UPDATE ". $this->VarTableUsers ." SET 
                  users_previp = users_lastip,
                  users_prevtime = users_lasttime,
                  users_lastip = ?,
                  users_lasttime = ?
                WHERE users_id = '". $_SESSION['SessionAuth']['id'] ."'";
      $values = $this->PrepareData();
      $ArrData = array(
                  $values['users_lastip'] ,
                  $values['users_lasttime']
                );
      $sth =& $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      //SiteLibs::GetVarDump($sth);
      $VarQueryResult = $sth->execute($ArrData);
      if (PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $_SESSION['SessionAuth']['id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
  
}
?>