<?php
class gridEdit
{
  protected $dir;
  
  public $title;
  public $id              = 'g-edit';
  public $post_action     = null;
  public $form_html_src   = null;
  public $external_code   = null;
  public $params          = null;
  public $buttons         = null;
  public $id_param        = null;
  
  public $include_plugins = false;
  public $default_buttons = true;
  public $persist_edit    = false;
  public $form            = true;
  
  public $gdb_opt = array( 
                          'database_type' => 'mysql',
                          'database_name' => 'db_name',
                          'server'        => 'db_host',
                          'username'      => 'db_user',
                          'password'      => 'db_pass',
                          'port'          => 'db_port'
                        );
  
  function __construct() {
    $this -> dir   = dirname( __FILE__ );
  }
  
  public function connectToDb()
  {
    return new gdb( [
                    'database_type' => $this -> gdb_opt['database_type'],
                    'database_name' => $this -> gdb_opt['database_name'],
                    'server'        => $this -> gdb_opt['server'],
                    'username'      => $this -> gdb_opt['username'],
                    'password'      => $this -> gdb_opt['password'],
                    'port'          => $this -> gdb_opt['port'],
                    'charset'       => 'utf8'
                  ] );
  }
  
  public function  convertString( $string, $row )
  {
    $out = $string;

    preg_match_all( '/\[[a-zA-Z0-9_]*\]/', $string, $results_tmp1 );
    if ( is_array( $results_tmp1[0] ) ) foreach ( $results_tmp1[0] as $row_tmp1 )
    {
      preg_match_all( '/[a-zA-Z0-9_]*/', $row_tmp1, $results_tmp2 );
      if ( is_array( $results_tmp2[0] ) ) foreach ( $results_tmp2[0] as $row_tmp2 )
        if ( $row_tmp2 )
          $out = str_replace( '[' . $row_tmp2 . ']', $row[ $row_tmp2 ], $out );
    }
    return $out;
  }
  
  public function draw()
  {
    $_SESSION[ 'g-edit-' . $this -> id ] = $this;
    
    $values = get_object_vars( $this );
    
    $view = new gridView( $this -> dir . '/templates/' );
    $view -> values = $values;
    return $view -> render( 'edit-simple' );
  }
}