<?php

/**
 * @class database
 * @brief Manage, edit, add, delete, test database
 * 
 * @author Benjamin Mercier
 * @data 14/08/2009
 * @version 0.1
*/
class database
{
  
  /**
   * @brief Add a database for a hosting
   * @param name : Name of the database
   * @param password : Password of the database
   * @return -1 : Quota of databases is reached
   * @return -2 : Syntax of database is forbidden
   * @return -3 : Complete name is too long
   * @return -4 : Database already existing
   * @return 1 : Databases added for creation
  */
  public function userAddDatabase( $name, $password, $comment = null )
  {
    $hosting_id = $_SESSION['hosting']->information_hosting->id;
    $mysql_service = $_SESSION['hosting']->information_hosting->mysql_id;
    
    // Quota
		if ( $_SESSION['hosting']->information_hosting->offer_domains_number >= 0 ) {
			$current_number_mysql = $this->countDatabases( $_SESSION['hosting']->information_hosting->id );
			if ( $current_number_mysql >= $_SESSION['hosting']->information_hosting->offer_domains_number ) return -1;
		}
		
		// Check syntax and with of db name
		if ( !textVerification::verifDatabaseName($name) ) return -2;
		if ( strlen($_SESSION['hosting']->information_hosting->id.'_'.$name)  > 16 ) return -3;
		
		// Check existence of database
		if ( $this->checkDatabaseExistence( $_SESSION['hosting']->information_hosting->id, $name ) ) return -4
		
		$action_id = $this->createDatabase( $mysql_service, $name )
		$this->createUserForDatabase( 
		    $mysql_service, 
		    $name, 
		    $password, 
		    $_SESSION['hosting']->information_hosting->id,
		    $name,
		    'normal',
		    $action_id 
		);
		
		// inserer en db
		 
  }
  
  public function userListDatabase()
  {
  }
  
  public function userDeleteDatabase()
  {
  }
  
  public function userChangePasswordForDatabase()
  {
  }
  
  /**
   * @brief Create user for db
   * @param service_id : Id of the mysql service
   * @param name : Name of the user
   * @param password : Password of the user
   * @param db_hosting: Hosting_id of the DB
   * @param db_name : Name of the database
   * @param (optionnal) level : Level of the user. "normal" is for customer
   * @param (optionnal) create_db_action : Id action of database creation if create_db in same time of create_user.
  */
  private function createUserForDatabase( $service_id, $name, $password, $db_hosting, $db_name, $level = 'normal', $create_db_action = null )
  {
    $data = array();
    $data['action'] = 'create_user';
    $data['name'] = $name;
    $data['password'] = $password;
    $data['db_hosting'] = $db_hosting;
    $data['db_name'] = $db_name;
    $data['level'] = $level;
    $action = action::userAddAction($service_id, $data, $create_db_action);
    return $action;
  }
  
  /**
   * @brief Create database
   * @param service_id : Id of the mysql service
   * @param name : Name of database 
  */
  private function createDatabase( $service_id, $name )
  {
    $data = array();
    $data['action'] = 'create_database';
    $data['name'] = $name;
    $action = action::userAddAction($service_id, $data);
    return $action;
  }
    
  private function checkDatabaseExistence()
  {
  }
  
  /**
   * @brief Count databases for one hosting
   * @param hosting_id : Id of the hosting
   * @return Number of databases
  */
  private function countDatabases( $hosting_id )
  {
    $hosting_id = $_SESSION['database']->clearString($hosting_id);
    $req = "SELECT COUNT(id) AS total FROM service_database WHERE hosting_id = '$hosting_id'";
    $query = $_SESSION['database']->fetchObject($req);
    return $query[0]->total;
  }
  
  
}
?>