<?php 
/**
 *  create_dbGrid()
 *  ======================================================================= //
 */
  function create_dbGrid( $dbgrid,
                          $dbgrid_Conf )
  {
    global $modo_RAD;
    
    $path  = ( $modo_RAD )? _CFG_ : _CFG_;
    $dataSet = '';
    $flagINI    = false; // indica se config.data.ini foi lido com sucesso
    $flagConect = false; // indica se conexão com a tabela foi feita com sucesso
    $flagQuery  = false; // indica se a query foi executada com sucesso

    if( empty($dbgrid_Conf['data_set'] ) )
    {
      $dataSet = 'table';
      //  inicializa o dbGrid vazio
      $ix_ = $dataSet . '_data';
      $arrINI[ $ix_ ]['datasource'] = '';
      $arrINI[ $ix_ ]['tabela']     = '';
      $arrINI[ $ix_ ]['pk']         = '';

      $arrINI[ $dataSet . '_field']['field1'] = 'coluna_1' ;

      $arrINI[ $dataSet . '_field1']['caption'] = " &nbsp;&nbsp; &nbsp;&nbsp;  ";
      $arrINI[ $dataSet . '_field1']['size'] = '100';
      $arrINI[ $dataSet . '_field1']['align'] = 'left';
      //echo"com data_set";//exit;
    }else
    {
      $dataSet = $dbgrid_Conf['data_set'];
      $nFile = $path . $dataSet . ".config.data.ini";
      if( file_exists( $nFile ) )
      {
        //echo "com data_set: ".$nFile;exit;
        $arrINI = readINIfile( $nFile );
        $flagINI = true;
        //echo('<pre>');print_r($arrINI);exit;
      }// End
//echo('teste');exit;
    }// End
    
    
//echo($nFile);exit;
    
    if( $flagINI )
    {
      /**
       *  Conexão com o Servidor MySql
       */
       
      $dts = $path . $arrINI[ $dataSet .'_data' ]['datasource'] . '.config.data.ini';

      if( file_exists( $dts ) )
      {
        $arrDTS = readINIfile( $dts );
        $host  = $arrDTS['db_conect']['host'];
        $user  = $arrDTS['db_conect']['user'];
        $pass  = $arrDTS['db_conect']['pass'];
        $dbase = $arrDTS['db_conect']['dbase'];

        $con = mysql_connect( $host, $user )
          or die('Erro mysql_connect.');
        $db = mysql_select_db( $dbase, $con)
          or die('Erro select DB.');

        $flagConect = true;

        if( empty($arrINI[$dataSet.'_data']['tabela']) )
        {
          //echo"sem tabela";
          $flagQuery = false;
        }else
        {
//echo("\n<pre>");print_r($arrINI);exit;
          //echo"tabela: ".$arrINI[$dataSet.'_data']['tabela'];
        // cria o SELECT de acordo com o config.data.ini
          $arrField = $arrINI[$dataSet.'_field'];
          $campos = '';
          $idx    = 1;
          $max    = count( $arrField );
          foreach( $arrField as $ind => $val )
          {
            if( $val == 'field' )
            {
              if( $idx > 1 )
                $campos .= ',';
              $ixField = $dataSet . '_' . $ind;
              $campos .= $arrINI[$ixField]['field'];
              $idx ++;
            }// End
          }
        //echo($idx.' : '.$max."\n");
          //echo($campos);exit;
          //$query = "SELECT " . $campos . " FROM " . $dbgrid_Conf['tabela'];
          $query = "SELECT " . $campos . " FROM " . $arrINI[$dataSet.'_data']['tabela'];
          
          $result = mysql_query($query);
          $flagQuery = true;
        }// End
      }else
      {
        echo('erro: '.$dts);
        $flagConect = false;
      }// End
      //exit;
    }// End

    
      
    $nrLin = $dbgrid_Conf['linhas'];
    $nrCol = $dbgrid_Conf['colunas'];
    
    writeTO( '  ' );
    writeTO( '  <table id="' . $dbgrid . '_table" cellpadding="2" cellspacing="1">' );
//    writeTO( '  <table id="' . $dbgrid . '_table" width="100%" cellpadding="2" cellspacing="1">' );
//    writeTO( '  <table id="' . $dbgrid . '_table">' );

/**
 *  Desenha a linha de Titulos
 *  ------------------------------------------------------------------------  //
 */
    writeTO( '  <thead>' );
    writeTO( '  <tr id="' . $dbgrid . '_tit">' );

    writeTO( '    <th id="' . $dbgrid . '_th" class="' . $dbgrid . '_tit">' );
    writeTO( '      <span id="' . $dbgrid . '_num"' );
    writeTO( '            class="' . $dbgrid . '_col"> &nbsp; &nbsp; </span>' );
    writeTO( '    </th>' );

    $arrCol = $arrINI[ $dataSet . '_field' ];
    $ix = 1;
    foreach( $arrCol as $ind => $nome_ )
    {
      if( $arrINI[ $dataSet . '_' . $ind ]['grid'] == 'visible' )
      {
        $nome = $arrINI[ $dataSet . '_' . $ind ]['caption'];

        writeTO( '    <th id="' . $dbgrid . '_th_' . $ix . '" class="' . $dbgrid . '_tit">' );
        writeTO( '      <input id="' . $dbgrid . '_tit_' . $ix . '" ' );
        writeTO( '             type="text" readOnly="true" ' );
        writeTO( '             value="' . $nome . '" ' );
        writeTO( '              class="' . $dbgrid . '_col_'.$ix.'">'  );
        writeTO( '    </th>' );
        $ix ++;
      }// End if( $arrINI[ $dataSet . '_' . $ind ]['grid'] == 'visible' )
    }// End foreach( $arrCol as $ind => $nome_ )
    writeTO( '  </tr>' );
    writeTO( '  </thead>' );
    writeTO( '  ' );


if( $flagConect)
{
    writeTO( '  <tbody>' );
/**
 *  Desenha as linhas da Grid
 *  ------------------------------------------------------------------------  //
 */
//    for( $lin_ = 1; $lin_ <= $nrLin; $lin_ ++ )

    if( $flagQuery )
    {
      $lin_ = 0;
      $tipo_dado = "texto";
      while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
      {
        $lin_ ++;
        writeTO( '    <tr id="' . $dbgrid . '_lin_'.$lin_.'">' );
        /**
         *  coluna da numeração
         */
        writeTO( '      <th id="' . $dbgrid . '_th_'.$lin_.'" class="' . $dbgrid . '_tit">' );
        writeTO( '        <span id="' . $dbgrid . '_num_'.$lin_.'"' );
        writeTO( '              class="' . $dbgrid . '_col">' . $lin_ . '</span>' );
        writeTO( '      </th>' );

        $ix = 1;
        foreach ($line as $col_name=>$col_value)
        {
          writeTO( '      <td id="' . $dbgrid . '_td_'.$lin_.'_' . $ix . '">' );
          writeTO( '        <input id="' . $dbgrid . '_col_' . $lin_ . '_' . $ix .  '"' );
          writeTO( '               type="text" readOnly="true" ' );
          writeTO( '               class="' . $dbgrid . '_col_' . $ix . '" ' );
          writeTO( '               colName="'.$col_name . '" ' );
          writeTO( '               value="'.$col_value . '" ' );
          writeTO( '               tipoDado="'.$tipo_dado . '" ' );
          writeTO( '               />' );
          writeTO( '      </td>' );
          $ix ++;
        }// End foreach ($line as $col_name=>$col_value)
      
        writeTO( '    </tr>' );
        writeTO( '  ' );
      }// End while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
      writeTO( '  </tbody>' );
    }// End
}// End
    writeTO( '  </table>' );
    writeTO( '  ' );
  }// End create_dbGrid() 
  
  
  /**
   *  create_dbGrid_css()
   *  ======================================================================= //
   */
  function create_dbGrid_css( $dbgrid,
                              $dbgrid_Global,
                              $dbgrid_Style,
                              $dbgrid_Conf,
                              $flag_dbGrid )
  {
    global $projeto,
           $esquema,
           $modo_RAD;
    $nrCol = $dbgrid_Conf['colunas'];
    $path  = ( $modo_RAD )? _CFG_ : _CFG_;
    $nFile = $path . $dbgrid . ".config.data.ini";

    if( $dbgrid_Global['visibility'] == 'visible' )
    {
      $visible = 'inherit';
    }else
    {
      $visible = 'hidden';
    }// End
    
    if( empty($dbgrid_Conf['data_set'] ) )
    {
      $dataSet = "table";
      //  inicializa o dbGrid vazio
      $ix_ = $dataSet . '_data';
      $arrINI[ $ix_ ]['datasource'] = '';
      $arrINI[ $ix_ ]['tabela']     = '';
      $arrINI[ $ix_ ]['pk']         = '';

      $arrINI[ $dataSet . '_field']['field1'] = 'coluna_1' ;

      $arrINI[ $dataSet . '_field1']['caption'] = " &nbsp;&nbsp; &nbsp;&nbsp;  ";
      $arrINI[ $dataSet . '_field1']['size'] = '100';
      $arrINI[ $dataSet . '_field1']['align'] = 'left';
      
      //echo"com data_set";//exit;
    }else
    {
      $dataSet = $dbgrid_Conf['data_set'];
      $nFile = $path . $dataSet . ".config.data.ini";
      //echo"com data_set";//exit;
      if( file_exists( $nFile ) )
      {
        $arrINI = readINIfile( $nFile );
        $flagINI = true;
        //echo('<pre>');print_r($arrINI);exit;
      }// End

    }// End

    
    writeTO( '  #' . $dbgrid . '{ '                           );
    writeTO( '    position:absolute; '                        );
    writeTO( '    top:' . $dbgrid_Global['top'] . 'px;'       );
    writeTO( '    left:' . $dbgrid_Global['left'] . 'px;'     );
    writeTO( '    '                                           );
    writeTO( '    z-index:100;'                               );
    writeTO( '    '                                           );
    writeTO( '    width:' . $dbgrid_Global['width'] . 'px;'   );
    writeTO( '    height:' . $dbgrid_Global['height'] . 'px;' );
    writeTO( '    '                                                   );
    writeTO( '    visibility:' . $visible . ';'   );
    writeTO( '    overflow:auto;'                                     );
    writeTO( '    padding:1px;'                                       );
    writeTO( '    padding-bottom:0px;'                                );
    writeTO( '    '                                                   );
    writeTO( '    background-color:' . $dbgrid_Style['BgColor'] . ';' );
    writeTO( '    '                                                   );
    writeTO( '    border-top:'   .$dbgrid_Style['boxTop'].';'         );
    writeTO( '    border-bottom:'.$dbgrid_Style['boxBottom'].';'      );
    writeTO( '    border-left:'  .$dbgrid_Style['boxLeft'].';'        );
    writeTO( '    border-right:' .$dbgrid_Style['boxRight'].';'       );
    writeTO( '    }'                                                  );
    writeTO( '    '                                                   );
    writeTO( '  #' . $dbgrid . '_table { '                            );
    writeTO( '    background-color:'.$dbgrid_Style['boxBgColor'].';'  );
//    writeTO( '    width:' . $dbgrid_Global['width'] . 'px;'   );
//    writeTO( '    height:' . $dbgrid_Global['height'] . 'px;' );
    writeTO( '    }'                                                  );
    writeTO( '  #' . $dbgrid . '_table th{ '                          );
    writeTO( '    padding:0px;'                                         );
    writeTO( '    font:' . $dbgrid_Global['fontFamily'] . ';'         );
    writeTO( '    font-size:' . $dbgrid_Global['fontSize'] . 'px;'    );
    writeTO( '    background-color:' . $dbgrid_Style['borderBgColor'] . ';' );
    writeTO( '    }'                                                        );
    writeTO( '    '                                                         );
    writeTO( '  #' . $dbgrid . '_table tr{ '                                );
    writeTO( '    font-size:' . $dbgrid_Global['fontSize'] . 'px;'          );
    writeTO( '    background-color:'.$dbgrid_Style['borderBgColor'].';'     );
    writeTO( '    }'                                                        );
    writeTO( '    '                                                         );
    writeTO( '  #' . $dbgrid . '_table td{ '                                );
//    writeTO( '    margin:10px;'                                             );
    writeTO( '    padding:0px;'                                         );
//    writeTO( '    padding-left:5px;'                                        );
//    writeTO( '    padding-bottom:1px;'                                      );
//    writeTO( '    padding-right:5px;'                                       );
    writeTO( '    }'                                                        );
    writeTO( '    '                                                         );

    /**
     *  declara a definição das colunas
     */
    writeTO( '  .' . $dbgrid . '_col{ '                                );
    writeTO( '    width:50px;'                                              );
    writeTO( '    text-align:right;'                                        );
    writeTO( '    overflow:hidden;'                                         );
    writeTO( '    }'                                                        );

    //for( $col_ = 1; $col_ <= $nrCol; $col_ ++ )
    $arrCol = $arrINI[ $dataSet . '_field' ];
    $ix = 1;
    foreach( $arrCol as $ind => $nome_ )
    {
      $len = $arrINI[ $dataSet . '_' . $ind ]['size'];
      $align = $arrINI[ $dataSet . '_' . $ind ]['align'];
      
      writeTO( '  .' . $dbgrid . '_col_' . $ix . '{ '                         );
      writeTO( '    width:' . $len . 'px;'                                    );
      writeTO( '    text-align:' . $align . ';'                               );
      writeTO( '    overflow:hidden;'                                         );
      writeTO( '    background-color:#FFFFFF;'                                );
      writeTO( '    padding:1px;'                                             );
      writeTO( '    padding-left:5px;'                                        );
      writeTO( '    padding-bottom:1px;'                                      );
      writeTO( '    padding-right:5px;'                                       );
      writeTO( '    border:0px;'                                              );
      writeTO( '    }'                                                        );
      writeTO( '    '                                                         );

      writeTO( '  #' . $dbgrid . '_tit_'.$ix.'{ '                                      );
      writeTO( '    height:15px;' );
      writeTO( '    color:#0000FF;'                                             );
      writeTO( '    text-align:center;'                                         );
      writeTO( '    background-color:'.$dbgrid_Style['borderBgColor'].';'     );

      writeTO( '    padding:0px;'                                         );
      writeTO( '    font:' . $dbgrid_Global['fontFamily'] . ';'         );
      writeTO( '    font-size:' . $dbgrid_Global['fontSize'] . 'px;'    );
      writeTO( '    font-weight:bold;'    );
      writeTO( '    }'                                                          );

      $ix ++;
    }// End
    
    writeTO( '  .' . $dbgrid . '_tit_{ '                                      );
    writeTO( '    color:#0000FF;'                                             );
    writeTO( '    text-align:center;'                                         );
    writeTO( '    }'                                                          );
    writeTO( '  .' . $dbgrid . '_tit{ '                                       );
    writeTO( '    border-top:'   .$dbgrid_Style['borderTop'].';'              );
    writeTO( '    border-bottom:'.$dbgrid_Style['borderBottom'].';'           );
    writeTO( '    border-left:'  .$dbgrid_Style['borderLeft'].';'             );
    writeTO( '    border-right:' .$dbgrid_Style['borderRight'].';'            );
    writeTO( '    }'                                                          );
  }// End create_dbGrid_css()
  

/**
 *  create_dbGrid_control()
 *  ======================================================================= //
 */
function create_dbGrid_control( $modo_RAD )
{
    global $componentes_no_Prj,
           $projeto,
           $esquema;

    $path  = ( defined('STUDIO') )? _CFG_ : _CFG_;

    $nDest = $path . "dbgrid.control.js.php";
    $comp  = $componentes_no_Prj;

    $fp = @fopen( $nDest, 'wb' );
    foreach( $comp as $comp_ => $obj_ )
    {
      if( $comp_ == 'dbgrid' )
      {
        $nrObj  = 0;
        $arrObj = array();
        $arrConf = array();

        /**
         *  Lista das Propriedades
         */
        $dbgrid_colunas = array();

        /**
         *  conta o numero de objetos dbGrid no projeto
         *  e armazena os nomes de cada Objetos no
         *  array $arrObj
         */
        foreach( $obj_ as $i_ => $v_ )
        {
          $arrObj[ $nrObj ] = $i_;
          /**
           *  inclui o arquivo .config.def.php de cada Objeto
           */
          include( $path . $i_ . ".config.def.php" );
          $nConf_ = $i_ . "_Conf";
          $arrConf[$i_] = $$nConf_;

          $nFile = $path . $i_ . ".config.data.ini";
          if( file_exists( $nFile ) )
          {
            $arrINI = readINIfile( $nFile );
          }else
          {
            //  inicializa o dbGrid vazio
            $ix_ = $i_ . '_data';
            $arrINI[ $ix_ ]['datasource'] = '';
            $arrINI[ $ix_ ]['tabela']     = '';
            $arrINI[ $ix_ ]['query']      = '';
            $arrINI[ $ix_ ]['pk']         = '';

            $arrINI[ $i_ . '_field']['field1'] = 'coluna_1' ;

            $arrINI[ $i_ . '_field1']['caption'] = ' teste ';
            $arrINI[ $i_ . '_field1']['size'] = '200';
            $arrINI[ $i_ . '_field1']['align'] = 'left';
          }// End
          $dbgrid_colunas[ $i_ ] = count($arrINI[ $i_ . '_field']);
          $arrPK[ $i_ ] = $arrINI[ $i_ . '_data']['pk'];

          $nrObj ++;
        }// End

        writeTO( "dbgrid_obj = ". $nrObj .";",           $fp );
        writeTO( "dbgrid_enabled = new Array();",        $fp );
        writeTO( "dbgrid_edit = new Array();",           $fp );
        writeTO( "dbgrid_pk = new Array();",             $fp );
        writeTO( "dbgrid_linha_corrente = new Array();", $fp );
        writeTO( "dbgrid_modo_edit = new Array();",      $fp );
        writeTO( "dbgrid_nr_coluna = new Array();",      $fp );
        writeTO( " ", $fp );

        /**
         *  propriedade enable
         */
        $idx = 0;
        foreach( $arrConf as $ind => $val)
        {
          writeTO( 'dbgrid_enabled['.$idx . '] = '  . $val['enable'] .';', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

        /**
         *  propriedade edit
         */
        $idx = 0;
        foreach( $arrConf as $ind => $val)
        {
          writeTO( 'dbgrid_edit['.$idx . '] = '  . $val['edit'] .';', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

        /**
         *  propriedade PK (Primary Key)
         */
        $idx = 0;
        foreach( $arrPK as $ind => $val)
        {
          writeTO( 'dbgrid_pk['.$idx . '] = "'  . $val .'";', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

        /**
         *  numero da linha corrente
         *  =0 => indica sem linha corrente
         *  >0 => indica numero da linha
         */
        $idx = 0;
        foreach( $arrObj as $ind => $val)
        {
          writeTO( 'dbgrid_linha_corrente['.$idx . '] = 0;', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

        /**
         *  modo de edição
         *  =0 => indica modo list
         *  >0 => indica numero da coluna em edição
         */
        $idx = 0;
        foreach( $arrObj as $ind => $val)
        {
          writeTO( 'dbgrid_modo_edit['.$idx . '] = 0;', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

        /**
         *  numero de colunas na grid
         */

//echo("\n<pre>");print_r($dbgrid_colunas);exit;

        $idx = 0;
        foreach( $arrObj as $ind => $val)
        {
          writeTO( 'dbgrid_nr_coluna['.$idx . '] = ' . $dbgrid_colunas[$val] . ';', $fp );
          $idx ++;
        }// End foreach($arrConf as $ind => $val)
        writeTO( " ", $fp );

      }// End
    }// End

  fclose( $fp );

  return( $nDest );
}// End

?> 
