<?php
/**
 * Clase Base FN para BaseCrCovid19paramFN
 *
 * @package    
 * @subpackage BaseCrCovid19paramFN
 * @author     Willy
 * @version    SVN:: BaseCrCovid19paramFN.class.php,v 1.1 2020-04-15 14:49:00 william.taype Exp $
 */
class BaseCrCovid19paramFN extends CrCovid19paramBN
{
  private static $fieldNames = array (
    'Fechapara'=>'fechapara', 'Fechareinicio'=>'fechareinicio'  
  );

  public function select()
  {
    try
    {
      $tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;

      $where = $this->buildConsult();
      $orden = $this->buildOrderByField();

      $con = Conexion::getConexion();
      $sql = "SELECT fechapara, fechareinicio FROM cr_covid19param WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
        $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->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $con = Conexion::getConexion();
      $sql = "SELECT fechapara, fechareinicio FROM cr_covid19param WHERE 1 ".$where;
        $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getFechapara()){ $where .= " AND fechapara = '".$this->getFechapara()."'"; }
      if($this->getFechareinicio()){ $where .= " AND fechareinicio = '".$this->getFechareinicio()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Búsqueda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $con = Conexion::getConexion();
      $sql = "SELECT fechapara, fechareinicio FROM cr_covid19param WHERE 1 AND fechapara='".$this->getFechapara()."' AND fechareinicio='".$this->getFechareinicio()."'; ";
        $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con métodos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseCrCovid19paramFN = new BaseCrCovid19paramFN();
    $o_BaseCrCovid19paramFN->setFechapara($o_propel->fechapara);
    $o_BaseCrCovid19paramFN->setFechareinicio($o_propel->fechareinicio);
    return $o_BaseCrCovid19paramFN;
  }
}