<?php
class ClassAdminUser extends ClassConfig {
  public function __construct(){}
  public function __destruct(){}
  
  private function _connectUserDB($db_host, $db_name, $db_user, $db_password){
    try {
      $oPDOLink = new PDO("pgsql:host=".$db_host.";dbname=".$db_name.";user=".$db_user.";password=".$db_password); //, array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8")
      return $oPDOLink;
    } catch( PDOException $Exception ) {
      // PHP Fatal Error. Second Argument Has To Be An Integer, But PDOException::getCode Returns A
      // String.
      //throw new MyDatabaseException( $Exception->getMessage( ) , (int)$Exception->getCode( ) );
      throw $Exception->getMessage();
    }
  }
  
  public function updateUserDB($file){
    $oPDOLink = $this->databaseConnect();
    $sql = "
    SELECT db_host, db_name, db_user, db_password
    FROM user_detail
    WHERE db_host IS NOT NULL
      AND db_name IS NOT NULL
      AND db_user IS NOT NULL
      AND db_password IS NOT NULL
    ;
    ";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array());
    $rows = $execSQL->fetchAll(PDO::FETCH_ASSOC);
    
    foreach($rows as $row){
      $oPDOUser = $this->_connectUserDB($row['db_host'], $row['db_name'], $row['db_user'], $row['db_password']);
      //$sql = file_get_contents(ClassConfig::getURLRoot('apps/install/'.$file['form_udpateUsersDB_file']['name']));
      $sql = file_get_contents($file['form_udpateUsersDB_file']['tmp_name']);
      $oPDOUser->exec($sql);
    }
    //TODO import data
    //$oPDOPrivate = new PDO("pgsql:host=localhost;dbname=".$db_name.";user=wefra;password=wefra");                                                   
    //$oPDOPrivate = new PDO("pgsql:host=localhost;dbname=".$db_name.";user=businessuseonly;password=1nrl5f6cug8n");          // PROD
    //$oPDOUser = new PDO("pgsql:host=localhost;dbname=businessuseonly_testimportfromsql;user=businessuseonly;password=1nrl5f6cug8n");               //
    return true;
  }
  
}
