<?php



    if(file_exists('_backend/internals/categories/l18n/'.$cfg['admin']['language'].'.php')) {
        require_once('_backend/internals/categories/l18n/'.$cfg['admin']['language'].'.php');
    }
    
 
    
    $table_config = array(
        'table' => 'categories',
        'multiSelect' => true,    
        'idField'=>'catId',
        'languages'=>true,
        'status'=>true
    );
    
    $table_fields = array(
        'catId' => array(
            'type' => 'id', 
            'default_list' => true,           
        ),
          
        'catTitle' => array(
            'type' => 'text',
            'size' => '300',
            'length' => '200',
            'default_list' => true,   
            'language' => true, 
            'formula' => 'make_slug({Slug},{Title})',                                    
        ),
        'catSlug' => array(
            'type' => 'text',
            'size' => '200',
            'length' => '100',
            'default_list' => true,
            'language' => true,    
            'formula' => 'make_slug({Slug},{Slug})'
        ),
        'catTemplate' => array(
            'type' => 'select_templates',
            'default_list' => true,          
        ),         
      
           
                
    );
  

    require_once('_backend/grid.php');                
                          
    if(file_exists(dirname(__FILE__).'/l18n/'.$cfg['admin']['language'].'.php')) {
        require_once(dirname(__FILE__).'/l18n/'.$cfg['admin']['language'].'.php');
    }
    
    
    function xprint_header() {
        global $_url, $cfg;
        if(!$_GET['modal_view']) echo '<h4>'._t('moduleName').'</h4>';
        if(!$_url[2]) $_url[2]='view';
        $active[$_url[2]]=' class="active"';
        echo '<ul class="nav nav-tabs">';
        if(!$_GET['modal_view']) {        
            echo '<li'.$active['view'].perm_display('categories','view').'><a href="/'.$_url[0].'/'.$_url[1].'/view"><span class="glyphicon glyphicon-eye-open"></span> Przeglądaj</a></li>
            <li'.$active['tree'].perm_display('categories','tree').'><a href="/'.$_url[0].'/'.$_url[1].'/tree"> Drzewo</a></li>
            <li'.$active['add'].perm_display('categories','add').'><a href="/'.$_url[0].'/'.$_url[1].'/add"><span class="glyphicon glyphicon-plus"></span> Dodaj</a></li>
            <li'.$active['structure'].perm_display('categories','structure').'><a href="/'.$_url[0].'/'.$_url[1].'/structure"><span class="glyphicon glyphicon-list"></span> Struktura</a></li>
            <li'.$active['adjust'].'><a href="/'.$_url[0].'/'.$_url[1].'/adjust"><i class="fa fa-adjust"></i> Widok</a></li>
            <li'.$active['adjust'].'><a href="/'.$_url[0].'/'.$_url[1].'/settings"><i class="fa fa-cog"></i> Ustawienia</a></li>                              
            <li'.$active['sql'].perm_display('categories','sql').'><a href="/'.$_url[0].'/'.$_url[1].'/sql"><span class="glyphicon glyphicon-cloud"></span> SQL</a></li>';
            
            if($_url[2]=='edit') echo '<li class="active"><a href="#"><span class="glyphicon glyphicon-edit"></span> Edycja #'.$_GET['id'].'</a></li>';
        
        } else {
            echo '<li><h4>'._t('moduleName').'</h4></li>';
        }  
        
        
        renderHeaderSearch('categories');    
        renderEditLanguage();
        if($_url[2]=='view') renderSorting('categories');     
        echo '</ul><div class="nice_bar"></div>';
   
    }    


    function print_header() {
        global $_url, $cfg, $table, $table_config;
        $_header=array('language'=>true, 'search'=>true, 'filters'=>true, 'sorting'=>true, 'custom'=>array(
            array('icon'=>'<span class="glyphicon glyphicon-tree-deciduous"></span>', 'action'=>'tree', 'title'=>'Drzewo')
        ));
        foreach((array)$_SESSION['search']['categories'] as $key=>$item) {
            echo '<div class="label label-warning filter-item" style="float: right;">'.$item.' <span class="glyphicon glyphicon glyphicon-remove-circle remove-filter-item" phrase="'.$key.'"></span></div>';
        }        
        require_once('_backend/templates/grid_header.php');
   
    }    
    



    function action_sql() {
        global $cfg, $_cat_tree, $db, $_url;
        if($_POST['action']=='set_sql') {
            mysqli_query($db, "UPDATE settings SET setValue='".$_POST['sql']."' WHERE setProperty='sqlCategories'");
            $alert[]=array('type'=>'success','message'=>'SQLQuerySaved');                           
        }
        $sql_query=mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM settings WHERE setProperty='sqlCategories'"));
        $default_sql="SELECT * FROM categories WHERE catStatus='1'";        
        require_once('_backend/templates/sql_edit.php');         
    }



    function action_tree() {  
        global $cfg, $_cat_tree, $db, $_url;
        
        $_files=null;
        

        $dir='';  
        /*
        if($_GET['ajax']) $dir.=$_GET['dir'];              
        if ($handle = opendir($dir)) {
        
    
            while (false !== ($entry = readdir($handle))) {
                    if(!is_dir($dir.'/'.$entry)) {
                    $_files[]=array(
                            'filename'=>$entry,
                            'size'=>filesize($dir.'/'.$entry),
                            'pathinfo'=>pathinfo($dir.'/'.$entry)
                        );
                    }
            }

            closedir($handle);
        } 
        */ 
        require_once('views/view.php');     
    }
    
    function action_index() {  
        global $table_config;          
        grid_view($table_config['table']);            
    }
    
    function action_view() {
        action_index();
         
    }    
        

    

    
    function action_sequence() {
        global $db;
        
        $lines=explode(';', $_GET['data']);
        foreach($lines as $key=>$item) {
            $spl=explode(',', $item);
            mysqli_query($db,"UPDATE categories SET catSequence='$spl[2]', catParent_catId='$spl[1]' WHERE catId='$spl[0]'");
        }
        echo 'OK';
        
        //echo $_GET['data']; 
  
            
        
    
    }
    
    function action_add() { 
        global $table_config;  
        grid_record($table_config['table'], false);
    }       
    


    
    function action_structure() {
        global $table_config;   
        grid_structure($table_config['table']);
          
    }     
    
    function action_edit() { 
        global $table_config;        
        grid_record($table_config['table'], true);
    }
    
    function action_settings() {
        global $table_config, $table_fields;
        grid_settings($table_config['table']);
    }    
    
    function action_adjust() {
        global $table_config, $table_fields;
        grid_adjust($table_config['table']);
    }
        


?>