<?php

class StructureOrganizationMod extends DbMod{
  var $VarTableStructureOrganizationTypes = array('integer', 'integer', 'integer', 'text', 'text', 'text', 'date');
  
  function StructureOrganizationMod($ArrMsSp){
		parent::__construct($ArrMsSp);
		return true;
	}
	
	function GetFromDb($VarSqlName = 'SqlAll', $VarSqlGet = 'GetAll', $VarWhere = '', $VarOrder = '', $VarLimit = ''){
    $ArrSqlArray['SqlAll']['select'] = 'a.*';
    $ArrSqlArray['SqlAll']['from'] = $this->VarTableStructureOrganization .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlAll']['where'] = $VarWhere;
    }
    if('' != $VarOrder){
      $ArrSqlArray['SqlAll']['order'] = $VarOrder;
    }
    if('' != $VarLimit){
      $ArrSqlArray['SqlAll']['limit'] = $VarLimit;
    }
    ///
    $ArrSqlArray['SqlBuildTree']['select'] = 'a.*, COUNT(b.structureorganization_id) AS childcount';
    $ArrSqlArray['SqlBuildTree']['from'] = $this->VarTableStructureOrganization .' AS a';
    $ArrSqlArray['SqlBuildTree']['leftjoin'] = $this->VarTableStructureOrganization .' AS b ON a.structureorganization_id = b.structureorganization_parent_id';
    if('' != $VarWhere){
      $ArrSqlArray['SqlBuildTree']['where'] = $VarWhere;
    }
    $ArrSqlArray['SqlBuildTree']['group'] = 'a.structureorganization_id';
    if('' != $VarOrder){
      $ArrSqlArray['SqlBuildTree']['order'] = $VarOrder;
    }
    ///
    $ArrSqlArray['SqlCount']['select'] = 'count(*) AS vartotal';
    $ArrSqlArray['SqlCount']['from'] = $this->VarTableStructureOrganization .' AS a';
    if('' != $VarWhere){
      $ArrSqlArray['SqlCount']['where'] = $VarWhere;
    }
    ///
    $ArrSqlArray['SqlOne']['select'] = 'a.rn';
    $ArrSqlArray['SqlOne']['from'] = $this->VarTableStructureOrganization .' 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)){
      if ('' != $VarIdGlobal && true !== is_numeric($VarIdGlobal)){
        $VarIdGlobal = '';
      }
      $sth = $this->ArrMsSp->SP_DbConnect->prepare('INSERT INTO '. $this->VarTableStructureOrganization .' VALUES (?, ?, ?, ?, ?, ?, ?)', $this->VarTableStructureOrganizationTypes, MDB2_PREPARE_MANIP);
      $data = array('', $ArrValues['id'], $ArrValues['position'], $ArrValues['title'], $ArrValues['type'], $_SERVER['REMOTE_ADDR'], date('YmdHis'));
      $VarQueryResult = $sth->execute($data);
			if (PEAR::isError($VarQueryResult)) {
   			return array('error' => $VarQueryResult->getMessage());
			}
      $VarId = $this->ArrMsSp->SP_DbConnect->lastInsertID($this->VarTableStructureOrganization, 'structureorganization_id');
      if (PEAR::isError($VarId)) {
        return array('error' => $VarId->getMessage());
      }
      return array("status" => 1, "id" => $VarId);
    }
    else {
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
	}

   function Edit($values){
    if(true === is_array($values) && 0 < count($values) && true === isset($values['id']) && true === is_numeric($values['id'])){
      $VarSql = "UPDATE ". $this->VarTableStructureOrganization ." SET
                  structureorganization_title = ?,
                  structureorganization_userip = ?,
                  structureorganization_date = ?
                  WHERE structureorganization_id = '".  $values['id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['structureorganization_title'],
        $values['structureorganization_userip'],
        $values['structureorganization_date']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $values['id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Del($VarId = '', $VarFiledName = 'structureorganization_id'){
    if('' != $VarId && true === is_numeric($VarId)){
      $sql = "DELETE FROM ". $this->VarTableStructureOrganization ." 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 Copy($ArrValues){
    if(true === is_array($ArrValues) && 0 < count($ArrValues) && true === isset($ArrValues['id']) && true === is_numeric($ArrValues['id'])){
      $ArrFromDb = $this->GetFromDb('SqlAll', 'GetRow', "structureorganization_id = '". $ArrValues['id'] ."'");
      $ArrV = array('id' => $ArrValues['ref'], 'position' => $ArrValues['position'], 'title' => $ArrFromDb['structureorganization_title'], 'type' => $ArrFromDb['structureorganization_type']);
      return $this->Add($ArrV);
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }

  function Move($values){
    if(true === is_array($values) && 0 < count($values) && true === isset($values['id']) && true === is_numeric($values['id'])){
      $VarSql = "UPDATE ". $this->VarTableStructureOrganization ." SET
                  structureorganization_parent_id = ?,
                  structureorganization_position = ?
                  WHERE structureorganization_id = '".  $values['id'] ."'";
      $values = $this->PrepareData($values);
      $ArrData = array(
        $values['structureorganization_parent_id'],
        $values['structureorganization_position']
      );
      $sth = $this->ArrMsSp->SP_DbConnect->prepare($VarSql);
      $VarQueryResult = $sth->execute($ArrData);
      if(PEAR::isError($VarQueryResult)) {
        return array('error' => $VarQueryResult->getMessage());
      }
      return $ArrValues['id'];
    }
    else{
      return array('error' => $this->ArrMsSp->Sp_LangMessage['error']['ErrSettingWrong']);
    }
  }
  
  function PrepareData($values){
    if(true === is_array($values) && 0 < count($values)){
      if(true === isset($values['ref'])){
        $values['structureorganization_parent_id'] = $values['ref'];
      }
      if(true === isset($values['position'])){
        $values['structureorganization_position'] = $values['position'];
      }
      if(true === isset($values['title'])){
        $values['structureorganization_title'] = $values['title'];
      }
      $values['structureorganization_userip'] = $_SERVER['REMOTE_ADDR'];
      $values['structureorganization_date'] = date('YmdHis');
    }
    return $values;
  }
}
?>