<?php

    $_table = 'queries';

    if(file_exists('_backend/internals/queries/l18n/'.$cfg['admin']['language'].'.php')) {
        require_once('_backend/internals/queries/l18n/'.$cfg['admin']['language'].'.php');
    }
    $table_config = array(
        'table' => 'queries',
        'multiSelect' => true,    
        'idField'=>'queId',
        'status'=>true,
        'categories'=>true,
        'languages'=>true
    );
    $table_fields = array(
        'queId' => array(
            'type' => 'id', 
            'default_list' => true,           
        ),
        'queName' => array(
            'type' => 'text',                 
            'default_list' => true,
            //'language' => true        
        ),        
        'queTitle' => array(
            'type' => 'text',
            'size' => '300',
            'length' => '200',
            'default_list' => true,   
            'language' => true,                       
        ),
        'queSQLType' => array(
            'type' => 'select',
            'values' => array('select', 'insert', 'update', 'delete'),
            'labels' => array(_t('select'), _t('insert'), _t('update'), _t('delete')),
            'default_list' => true,
        ),
        'queSQL' => array(
            'type' => 'textarea',            
            'height' => '200',
            'mode' => 'code',
            //'language' => true        
        ),  
        'queRunIfGET'=>array(
            'type' => 'text',
            'size' => 200,
            'length' => 100,        
        ),
        'queRunNoPOST'=>array(
            'type' => 'checkbox'
        ),
        'queFieldName'=> array (
            'type' => 'text',
            'size' => 200,
            'length' => 100,
            'arrayField' => 'FieldFormat'            
        ),        
        
        'queFieldDecode'=> array (
            'type' => 'select',
            'values' => array('json', 'semicolon_separated'),
            'labels' => array(_t('json'), _t('semicolon_separated')),
            'arrayField' => 'FieldFormat'            
        ),
        
        
        
        'queFieldFormat' => array(      
            'type' => 'array_fields', 
            'fields' => array('queFieldName', 'queFieldDecode'),       
        ),             
        'quePageLimit' => array(
            'type' => 'int',                   
        ),   
        'quePageLimitVariable' => array(
            'type' => 'text',
            'length' => 60,
            'size'=>150,                   
        ),            
        
        
    
        
        
        'queValidKey'=> array (
            'type' => 'text',
            'size' => 150,
            'length' => 60,
            'arrayField' => 'Validators'
        ), 
        'queValidMode'=> array (
            'type' => 'select',
            'values' => array('any', 'email', 'url', 'int', 'float', 'password'),
            'labels' => array(_t('any'), _t('email'), _t('url'), _t('int'), _t('float'), _t('password')),
            'arrayField' => 'Validators'            
        ),
        'queValidAttributes'=> array (
            'type' => 'checkboxes',
            'values' => array('required', 'no_schars'),
            'labels' => array(_t('required'), _t('no_schars')),
            'arrayField' => 'Validators'            
        ),     
        'queValidMin'=> array (
            'type' => 'text',
            'size' => 50,
            'length' => 4,
            'arrayField' => 'Validators'
        ),  
        'queValidMax'=> array (
            'type' => 'text',
            'size' => 50,
            'length' => 4,
            'arrayField' => 'Validators'
        ),                     
                        
        'queValidators' => array(      
            'type' => 'array_fields', 
            'fields' => array('queValidKey', 'queValidMode', 'queValidMax', 'queValidAttributes', 'queValidMin'),       
        ),   
                 
      
                
    );

    require_once('_backend/grid.php');

    function print_header() {
        global $_url, $cfg;
        echo '<h4>'._t('moduleName').'</h4>';
        if(!$_url[2]) $_url[2]='view';
        $active[$_url[2]]=' class="active"';
        echo '<ul class="nav nav-tabs">';
        

        echo '<li'.$active['view'].perm_display('queries','view').'><a href="/'.$_url[0].'/'.$_url[1].'/view"><span class="glyphicon glyphicon-eye-open"></span> Przeglądaj</a></li>
        <li'.$active['add'].perm_display('queries','add').'><a href="/'.$_url[0].'/'.$_url[1].'/add"><span class="glyphicon glyphicon-plus"></span> Dodaj</a></li>';
        if($_url[2]=='edit') echo '<li class="active"><a href="#"><span class="glyphicon glyphicon-edit"></span> Edycja #'.$_GET['id'].'</a></li>';        


        renderHeaderSearch('queries');      
        echo '</ul><div class="nice_bar"></div>';

    }

    function action_index() {  
        global $table_config;          
        grid_view($table_config['table']);            
    }
    
    function action_view() {
        global $table_config;   
        grid_view($table_config['table']);   
         
    }  
    
    function action_get_fields() {
        global $db;
        $res=mysqli_query($db, "SHOW COLUMNS FROM $_GET[table]");
        $ret=null;
        while($rek=mysqli_fetch_assoc($res)) {
            $spl=explode('@', $rek['Field']);
            $ret[$spl[0]]=$spl[0];   
        }
        foreach($ret as $key=>$item) {
            $out.=$item.',';
        }
        echo substr($out,0,-1);
    }  
    
    function action_edit() { 
        global $table_config;  
        grid_record($table_config['table'], true);
    }
    

    function action_add() {
        global $table_config;  
        grid_record($table_config['table'], false);
    }
    
    function action_adjust() {
        global $table_config;  
        grid_adjust($table_config['table'], false);
    }    


?>