<?php

require_once('MDB2.php');

class GroupMod{
	var $ArrMsSp = '';
  var $VarTableGroupTypes = array('text', 'text', 'text', 'text', 'text', 'text', 'text');
	var $VarTableGroup = '';
  var $VarTableGos = '';

	function GroupMod($ArrMsSp){
		$this->ArrMsSp = $ArrMsSp;
    $this->VarTableGroup = $this->ArrMsSp->Sp_DbConf['db']['DbPref'] .'_'. $this->ArrMsSp->Sp_DbConf['db']['DbTbl_Group'];
		return true;
	}

	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    //SiteLibs::GetVarDump($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
 		//echo "here<br />";
    $db =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
    if (PEAR::isError($db)) {
      die($db->getMessage());
    }
    SiteLibs::DbSetCharset($db);
    //SiteLibs::GetVarDump($db);
    $db->setFetchMode(MDB2_FETCHMODE_ASSOC);
    $db->loadModule('Extended');
    /*** begin default values ***/
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableGroup .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    /*** end default values ***/
    ///
    $ArrSqlArray['SqlRow']['select'] = 'a.*';
    $ArrSqlArray['SqlRow']['from'] = $this->VarTableGroup .' AS a';
    $ArrSqlArray['SqlRow']['where'] = '';
    if('' != $VarWhere){
      $ArrSqlArray['SqlRow']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableGroup .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $sql = SiteLibs::BuildSqlFromArray($ArrSqlArray[$VarSqlName]);
    //echo $sql. "<br />";
    switch ($VarSqlGet) {
      case 'GetAssoc':
        $ArrResult =& $db->extended->getAssoc($sql);
      break;
      case 'GetOne':
        $ArrResult =& $db->extended->getOne($sql);
      break;
      case 'GetCol':
        $ArrResult =& $db->extended->getCol($sql);
      break;
      case 'GetRow':
        $ArrResult =& $db->extended->getRow($sql);
      break;
      case 'GetAll':
      default:
        $ArrResult =& $db->extended->getAll($sql);
      break;
    }
    if (PEAR::isError($ArrResult)) {
      die($ArrResult->getMessage());
    }
    $db->disconnect();
  	return $ArrResult;
	}

   function Add($values){
    if(true === is_array($values) && 0 < count($values)){
      $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($mdb2)) {
        die($mdb2->getMessage());
      }
      SiteLibs::DbSetCharset($mdb2);
      $VarSql = 'INSERT INTO '. $this->VarTableGroup .'
                (group_id, structureorganization_id, group_name, group_password, group_passclean, group_fio, group_note, group_tel, group_telint, group_telcell, group_email, group_jobtitle, group_status)
                VALUES (?, ?, ?, ?, ?, ?, ?)';
      $sth = $mdb2->prepare($VarSql, $this->VarTableGroupTypes, MDB2_PREPARE_MANIP);
      $data = array(
        1,
        SiteLibs::ConvertTextToSql($values['structureorganization_id']),
        SiteLibs::ConvertTextToSql($values['group_name']),
        SiteLibs::ConvertTextToSql($values['group_password']),
        SiteLibs::ConvertTextToSql($values['group_passclean']),
        SiteLibs::ConvertTextToSql($values['group_fio']),
        SiteLibs::ConvertTextToSql($values['group_note']),
        SiteLibs::ConvertTextToSql($values['group_tel']),
        SiteLibs::ConvertTextToSql($values['group_telint']),
        SiteLibs::ConvertTextToSql($values['group_telcell']),
        SiteLibs::ConvertTextToSql($values['group_email']),
        SiteLibs::ConvertTextToSql($values['group_jobtitle']),
        SiteLibs::ConvertTextToSql($values['group_status'])
      );
      //SiteLibs::GetVarDump($data);
      $res = $sth->execute($data);
      if (PEAR::isError($res)) {
        die($res->getMessage());
      }
      $mdb2->disconnect();
      return true;
    }
    else{
      $ArrResult['error'] = 'Неверный входной параметр!';
    }
  }

  function Edit($values){
    if(true === is_array($values) && 0 < count($values)){
      $mdb2 =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($mdb2)) {
        die($mdb2->getMessage());
      }
      SiteLibs::DbSetCharset($mdb2);
      //SiteLibs::GetVarDump($values);
      if (1 == $values['group_status']){
        $VarGroupStatus = $values['group_status'];
      }
      else{
        $VarGroupStatus = 0;
      }
       $VarSql = "UPDATE ". $this->VarTableGroup ." SET
                    group_id = '". SiteLibs::ConvertTextToSql($values['group_id']) ."',
                    structureorganization_id = '". SiteLibs::ConvertTextToSql($values['structureorganization_id']) ."',
                    group_name = '". SiteLibs::ConvertTextToSql($values['group_name']) ."',
                    group_password = '". md5(SiteLibs::ConvertTextToSql($values['group_password'])) ."',
                    group_passclean = '". SiteLibs::ConvertTextToSql($values['group_password']) ."',
                    group_fio = '". SiteLibs::ConvertTextToSql($values['group_fio']) ."',
                    group_note = '". SiteLibs::ConvertTextToSql($values['group_note']) ."',
                    group_tel = '". SiteLibs::ConvertTextToSql($values['group_tel']) ."',
                    group_telint = '". SiteLibs::ConvertTextToSql($values['group_telint']) ."',
                    group_telcell = '". SiteLibs::ConvertTextToSql($values['group_telcell']) ."',
                    group_email = '". SiteLibs::ConvertTextToSql($values['group_email']) ."',
                    group_jobtitle = '". SiteLibs::ConvertTextToSql($values['group_jobtitle']) ."',
                    group_status = '". $VarGroupStatus ."'
                  WHERE group_id ='".  $values['group_id'] ."'";
      //echo "VarSql: ". $VarSql ."<br />";
      $res =& $mdb2->query($VarSql);
      if (PEAR::isError($res)) {
          die($res->getMessage());
      }
      $mdb2->disconnect();
      return true;
    }
    else{
      $ArrResult['error'] = 'Неверный входной параметр!';
    }
  }

  function Del($VarId = '', $VarFiledName = 'group_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $db =& MDB2::connect($this->ArrMsSp->Sp_DbConf['db']['DbDsn']);
      if (PEAR::isError($db)) {
        die($db->getMessage());
      }
      SiteLibs::DbSetCharset($db);
      $sql = "DELETE FROM ". $this->VarTableGroup ." WHERE ". $VarFiledName ." ='". $VarId ."'";
      //echo "sql: ". $sql ."<br />";
      $VarResult =& $db->exec($sql);
      if (PEAR::isError($VarResult)) {
        die($VarResult->getMessage());
      }
      $db->disconnect();
      return true;
    }
    else {
      return array('error' => 'Неверный входной параметр');
    }
	}
}
?>