<?php

/**
* Clase Dispacher para TipoTesis
*
* @package IM
* @subpackage TipoTesisDP
* @author Willy
* @version SVN: : TipoTesisDP.class.php,v 1.1 2008/11/17 20:39:13 Exp $
*/
class TipoTesisDP implements interfaceDP {

  protected $o_Bean;

  /**
  * Constructor donde asigna a un atributo el objeto TipoTesisBean
  *
  * @param TipoTesisBean $o_TipoTesisBean
  */
  public function __construct(TipoTesisBean $o_TipoTesisBean){
  
    $this->o_Bean = $o_TipoTesisBean;
    
  }
  
  /**
  * Retorna el objeto TipoTesisBean
  *
  * @return Object
  */
  protected function getObjetoBean(){
  
    if(!$this->o_Bean){
        $this->o_Bean = new TipoTesisBean();
    }        
    return $this->o_Bean;
    
  }
  
  /**
  * Ingresa un nuevo registro
  *
  * @return IdGenerado
  */
  public function save(){

    $con = Conexion::getConexion();
    $sql = "SELECT IF(MAX(TT_ID),(MAX(TT_ID)+1), 1) AS Maximo FROM OAITT;";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $row = mysql_fetch_array($result);
    $this->getObjetoBean()->setTipoTesisId($row[0]);

    if($this->getObjetoBean()->getTipoTesisId()){

      $a_Fields = array();

      if($this->getObjetoBean()->getTipoTesisId()) : $a_Fields['TT_ID'] = ($this->getObjetoBean()->getTipoTesisId()); endif;
      if($this->getObjetoBean()->getTipoTesisDescripcion()) : $a_Fields['TT_DESCRIPCION'] = ($this->getObjetoBean()->getTipoTesisDescripcion()); endif;
      
      $sStringField = arrayClass::implode_keys(',', $a_Fields);
      $sStringValue = "'".implode("','", $a_Fields)."'";

      //$con = Conexion::getConexion();
      $sql = "INSERT INTO OAITT (".$sStringField.")
                      VALUES (".$sStringValue.");";
      $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      mysql_close($con);

      $iResultSave = $result;
      if($iResultSave > 0){
        return $iResultSave;
      }else{
        return false;
      }

    }else{
      return false;
    }
  }
  
  /**
  * Modifica un nuevo registro
  *
  * @return IdGenerado
  */
  public function update() {

    if($this->getObjetoBean()->getTipoTesisId()){
      
      if($this->getObjetoBean()->getTipoTesisId()) : $a_Fields['TT_ID'] = ($this->getObjetoBean()->getTipoTesisId()); endif;
      if($this->getObjetoBean()->getTipoTesisDescripcion()) : $a_Fields['TT_DESCRIPCION'] = ($this->getObjetoBean()->getTipoTesisDescripcion()); endif;
      
      $sString = arrayClass::implode_key_value(",", $a_Fields);

      $sql = "UPDATE OAITT SET
                  ".$sString."
            WHERE 1 AND TT_ID = '".$this->getObjetoBean()->getTipoTesisId()."' ";
      $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      mysql_close($con);

      $iResultSave = $result;
      if($iResultSave > 0){
        return $iResultSave;
      }else{
        return false;
      }

    }else{
      return false;
    }

  }
  
  /**
  * Elimina registro
  *
  * @return Boolean
  */
  public function delete(){
    /*
      $o_TipoTesis = new TipoTesis();
      
      if($this->getObjetoBean()->getTipoTesisId()){
          $o_TipoTesis->setTipoTesisId($this->getObjetoBean()->getTipoTesisId());
      }
      $iResultDelete = $o_TipoTesis->delete();
      if($iResultDelete > 0){
          return true;
      }else{
          return false;
      }    
    */
  }
  /**
  * Selecciona un nuevo registro
  *
  * @return IdGenerado
  */
  public function select(){
      
    $where = "";
    
    if($this->getObjetoBean()->getTipoTesisId()){
       $where .= " AND TT_ID LIKE '%".$this->getObjetoBean()->getTipoTesisId()."%'";
    }
    
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM OAITT WHERE 1 ".$where;
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $num_rows = mysql_num_rows($result);

    $a_tmp = array();

    if($num_rows>0){
      while ($o_valor = mysql_fetch_object($result)) {
      $a_tmp[] = $this->insertarObjetoBean($o_valor);
      }
    }

    mysql_close($con);
   
    return count($a_tmp)>0?$a_tmp:null;
      
  }
  
  /**
  * Retorna un objeto TipoTesisBean
  *
  * @return Object
  */
  public function selectByPk(){
  
    $con = Conexion::getConexion();
    $sql = "SELECT * FROM OAITT WHERE 1 AND TT_ID = '".$this->getObjetoBean()->getTipoTesisId()."' ";
    $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
    $o_Propel = $result ? mysql_fetch_object($result) : null;
    mysql_close($con);

    return $o_Propel?$this->insertarObjetoBean($o_Propel):null;
  
  }
  
  /**
  * Reasigna valores retornados del propel a un objeto TipoTesisBean
  *
  * @param Object $o_Propel
  * @return TipoTesisBean
  */
  private function insertarObjetoBean($o_Propel){

    $o_TipoTesisBean = new TipoTesisBean();
    
    $o_TipoTesisBean->setTipoTesisId($o_Propel->TT_ID);
    $o_TipoTesisBean->setTipoTesisDescripcion($o_Propel->TT_DESCRIPCION);
         
    return $o_TipoTesisBean;    
    
  }
    
}

