<?php

    function action_cfi_sequence() {
        global $db;
        
        $row=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM customFields WHERE cfiTable='$_GET[table]' AND cfiSequence='$_GET[before]'"));        
        if(!$row || !$_GET['after']) { 
            echo 'ERROR'; exit;
        }
        
        if($_GET['before']>$_GET['after']) {
            _mysqli_query($db,"UPDATE customFields SET cfiSequence=cfiSequence+1 WHERE cfiTable='$_GET[table]' AND cfiSequence>='$_GET[after]' AND cfiSequence<'$_GET[before]'");
            _mysqli_query($db,"UPDATE customFields SET cfiSequence='$_GET[after]' WHERE cfiId='".$row['cfiId']."'");         
        } 
        
        if($_GET['before']<$_GET['after']) {
            _mysqli_query($db,"UPDATE customFields SET cfiSequence=cfiSequence-1 WHERE cfiTable='$_GET[table]' AND cfiSequence>'$_GET[before]' AND cfiSequence<=$_GET[after]");
            _mysqli_query($db,"UPDATE customFields SET cfiSequence='$_GET[after]' WHERE cfiId='".$row['cfiId']."'");         
        }
        echo 'OK';         
        
        //echo "SELECT * FROM custfomFields WHERE cfiTable='$_GET[table]' AND cfiSequence='$_GET[current]'";

    }
    
    
    function action_row_sequence() {
        global $db;
        $row=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM ".$_GET['table']." WHERE ".table2prefix($_GET['table'])."Sequence='$_GET[before]'"));      
        if(!$row || !$_GET['after']) { 
            echo 'ERROR'; exit;
        }
        //print_r($row);
        //exit;
        $sfield=table2prefix($_GET['table'])."Sequence";
        $ifield=table2prefix($_GET['table'])."Id";
        if($_GET['before']>$_GET['after']) {
            _mysqli_query($db,"UPDATE ".$_GET['table']." SET $sfield=$sfield+1 WHERE $sfield>='$_GET[after]' AND $sfield<'$_GET[before]'");
            _mysqli_query($db,"UPDATE ".$_GET['table']." SET $sfield='$_GET[after]' WHERE $ifield='".$row[$ifield]."'");         
        } 
        
        if($_GET['before']<$_GET['after']) {
            _mysqli_query($db,"UPDATE ".$_GET['table']." SET $sfield=$sfield-1 WHERE $sfield>'$_GET[before]' AND $sfield<=$_GET[after]");
            _mysqli_query($db,"UPDATE ".$_GET['table']." SET $sfield='$_GET[after]' WHERE $ifield='".$row[$ifield]."'");         
        }
        echo 'OK'; 
        
        exit;
        
        
    }

    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 get_modules_actions() {
        global $cfg;
        $ret=null;
        if ($handle = opendir('_modules')) {
            while (false !== ($entry = readdir($handle))) {
                if($entry!='.' && $entry!='..') {
                    
                    if(file_exists('_modules/'.$entry.'/l18n/'.$cfg['admin']['language'].'.php')) {
                        $inp=file_get_contents('_modules/'.$entry.'/l18n/'.$cfg['admin']['language'].'.php');
                        $spl=explode('//$', $inp);
                        $spl=explode("\r\n", $spl[1]);
                        $title=$spl[0];
                        $spl=explode('//#', $inp);
                        
                        if($spl[1]) {
                            $spl=explode("\r\n", $spl[1]);
                            $actions=$spl[0];
                            $actions=explode('#', $actions);
                            $ret[$entry]['title']=$title;                                            
                            foreach($actions as $key=>$item) {
                                $spl=explode(':', $item);
                                $ret[$entry]['actions'][$spl[0]]=$spl[1];                          
                            }
                        }                        
                        //echo $title.' '.$actions.'<br/>';
                    } else {
                        $title=$entry;
                    }
                }

            }                                        
            closedir($handle);
            return $ret;
        }      
    }

    function action_cfi_new() {
        $prefix=table2prefix($_GET['table']);
        global $cfg, $db;
        $res=mysqli_query($db, "SELECT * FROM customFields WHERE cfiTable='".$_GET['table']."' ORDER BY cfiSequence ASC");
        $cfields=null;
        while($rek=mysqli_fetch_assoc($res)) {
            $cfields[$rek['cfiName']]=$rek;
        }         
        $modules_actions=get_modules_actions();
      
        
        
        //print_R($_GET)
        require_once('templates/cfi_form.php');          
    }
    
    function action_cfi_edit() {
        $edit=true;
        global $cfg, $db;
        $cfi=mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM customFields WHERE cfiId='$_GET[id]'"));        
        $res=mysqli_query($db, "SELECT * FROM customFields WHERE cfiTable='".$cfi['cfiTable']."' ORDER BY cfiSequence ASC");
        $cfields=null;
        while($rek=mysqli_fetch_assoc($res)) {
            $cfields[$rek['cfiName']]=$rek;
        }        


        $uns=json_decode($cfi['cfiParameters'], true);

        if($uns) {
            foreach($uns as $key=>$item) {
                $cfi['cfi'.ucfirst($key)]=$item;
            }
        }

        if(!$cfi) {
            echo 'ERROR'; exit;
        }     
        $modules_actions=get_modules_actions();           
  
        require_once('templates/cfi_form.php');
    }
    
    
    
    function action_remove_record() {
        global $_url;    
        $prefix=table2prefix($_GET['table']);

        $table=$_GET['table'];
        $idField=$prefix.'Id';
        require_once('templates/dialog_remove.php');                        
    }
    
    function action_remove_cfi() {
        global $_url;
        //$prefix=table2prefix($_GET['table']);
        $table='customFields';
        $idField='cfiId';    
        require_once('templates/dialog_remove.php');                        
    }    
    
    function action_set_show_on_list() {
        global $cfg, $db;
        /*
        $columns=mysqli_fetch_assoc(_mysqli_query($db,"SELECT * FROM grid WHERE griAdmin_admId='".$_SESSION['admin']['admId']."' AND griTable='$_GET[table]'"));
        $columns=json_decode($columns['griFields'], true);
        $columns[urldecode($_GET['field'])]['showOnList']=$_GET['value'];
          */
        //_mysqli_query($db, "DELETE FROM grid WHERE griTable='".$_GET['table']."' AND griAdmin_admId='".$_SESSION['admin']['admId']."'");
        //echo "DELETE FROM grid WHERE griTable='".$_GET['table']."' AND griAdmin_admiId='".$_SESSION['admin']['admId']."'";
        //echo "DELETE FROM grid WHERE griTable='$table' AND griAdmin_admiId='".$_SESSION['admin']['admId']."'"; 
        //echo '<pre>';
       // print_r($columns);
       // exit;
      // print_r($columns);
        //_mysqli_query($db, "INSERT INTO grid (griAdmin_admId, griTable, griFields) VALUES ('".$_SESSION['admin']['admId']."', '".$_GET['table']."', '".json_encode($columns)."')");
        //_mysqli_query($db, 
       _mysqli_query($db, "UPDATE tables SET tabDefaultShowFields='".json_encode($_POST['field'])."' WHERE tabName='".$_POST['table']."'");
       echo 'OK';

        //print_r($columns);
    }
    
    function action_select_related() {
        global $db;
        $table_info = mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM tables WHERE tabName='$_GET[table]'"));
        if($table_info) {
            if($table_info['tabInternal']==1) {
                require_once('_backend/internals/categories/categories.php');
                action_view();
            }
        }
        exit;
    }
    
    function action_set_grid_limit() {
        $_SESSION['grid_limit']=$_GET['total'];
        echo 'OK';
    }
    
    function action_set_filter() {
        global $db;
        $table_data = mysqli_fetch_assoc(_mysqli_query($db, "SELECT * FROM tables WHERE tabName='$_GET[table]'"));
        require_once('templates/filter.php');
    }
    
    function action_inline_set() {
        global $db;
        mysqli_query($db, "UPDATE ".$_POST['table']." SET `".$_POST['field']."`='".$_POST['value']."' WHERE ".table2prefix($_POST['table'])."Id='".$_POST['id']."'");
        echo 'OK';
    }

    function action_get_relation_value() {
        global $db;
        //print_r($_GET);
        //echo "SELECT ".urldecode(base64_decode($_GET['field']))." AS val FROM ".$_GET['table']." WHERE ".table2prefix($_GET['table'])."Id='".$_GET['id']."'";
        //echo "SELECT `".urldecode(base64_decode($_GET['field']))."` AS val FROM ".$_GET['table']." WHERE ".table2prefix($_GET['table'])."Id='".$_GET['id']."'";
        
        $rek=@mysqli_fetch_assoc(mysqli_query($db, "SELECT `".urldecode(base64_decode($_GET['field']))."` AS val FROM ".$_GET['table']." WHERE ".table2prefix($_GET['table'])."Id='".$_GET['id']."'"));
        if(!$rek) {
          //print_R($_SESSION);
            $rek=@mysqli_fetch_assoc(mysqli_query($db, "SELECT `".urldecode(base64_decode($_GET['field']))."@".$_SESSION['edit_language']."` AS val FROM ".$_GET['table']." WHERE ".table2prefix($_GET['table'])."Id='".$_GET['id']."'"));
        
        }
        //print_r($rek);
        echo $rek['val'];
    }
    



?>