pripraveno rozhrani pro redirecty po postu a zobrazovani (nejen) chybovych hlasek
[mirrors/SokoMan.git] / index.php
index b2474e362a2d44c483e4932e548aab571bf498a1..fa04f335198b64a3d021682a921ce6a3cc302fd7 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -33,7 +33,8 @@ class Sklad_HTML {
        function header($title='') {
                $home = URL_HOME;
                $script = $_SERVER['SCRIPT_NAME'];
-               $search = @trim($_GET['q']);
+               $search = htmlspecialchars(@trim($_GET['q']));
+               $message = htmlspecialchars(@trim($_GET['message']));
                return <<<EOF
 <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
@@ -53,6 +54,9 @@ class Sklad_HTML {
                <input type="submit" value="search items" />
        </form -->
 </div>
+<div style="background-color:#FFDDDD;">
+       <font color="red">$message</font>
+</div>
 EOF;
        }
 
@@ -80,8 +84,12 @@ EOF;
                return $html;
        }
 
+       function internal_url($link) {
+               return $_SERVER['SCRIPT_NAME'].'/'.$link;
+       }
+
        function link($title='n/a', $link='#void', $internal=true) {
-               if($internal) $link = $_SERVER['SCRIPT_NAME'].'/'.$link;
+               if($internal) $link = $this->internal_url($link);
                return "<a href='$link'>$title</a>";
        }
 
@@ -178,22 +186,21 @@ EOF;
 
                $html='<form method="POST">';
                if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">';
-               $html.=$this->input('table', $class, 'hidden');
+               //$html.=$this->input('table', $class, 'hidden');
                foreach($columns as $column)    {
-                       $html.=$column['Field'].': ';
-                       $name='value:'.$column['Field'].'[]';
+                       $html.=$class.':<b>'.$column['Field'].'</b>: ';
+                       $name="values[$class][".$column['Field'].'][]';
+                       $val = $update ? $current[$column['Field']] : false;
                        switch(true) {
                                case preg_match('/auto_increment/', $column['Extra']):
-                                       $val = $update ? $current[$column['Field']] : ''; //opakuje se (skoro) zbytecne
+                                       if(!$val) $val = '';
                                        $html.=$this->input($name, $val, 'hidden');
                                        $html.=$val.'(AUTO)';
                                        break;
                                case isset($selectbox[$column['Field']]):
-                                       $val = $update ? $current[$column['Field']] : false;
-                                       $html.=$this->select($name,$selectbox[$column['Field']],$val); //opakuje se
+                                       $html.=$this->select($name,$selectbox[$column['Field']],$val);
                                        break;
                                default:
-                                       $val = $update ? $current[$column['Field']] : false; //opakuje se
                                        $html.=$this->input($name, $val);
                                        break;
                        }
@@ -338,6 +345,12 @@ class Sklad_DB extends PDO {
                return $this->lastInsertId();
        }
 
+       function insert_or_update_multitab($values) {
+               $last=false;
+               foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows);
+               return $last;
+       }
+
        function delete($table, $id, $suffix_id='_id') {
                $key = $this->escape($table.$suffix_id);
                $table = $this->escape($table);
@@ -433,17 +446,17 @@ class Sklad_UI {
                new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth'));
        }
 
-       function post_redirect_get($last, $next) { //TODO prepracovat, tohle je uplna picovina...
+       function post_redirect_get($last, $next) {
                //header('Location: '.$_SERVER['REQUEST_URI']); //TODO redirect (need templating system or ob_start() first!!!)
-               echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html->link('zde', $last).'.<br />'.
-               'Další záznam přidáte '.$this->html->link('zde', $next).'.';
-               die();
+               header('Location: '.$this->html->internal_url($dest));
+               die("Redirect: $dest");
        }
 
-       function safe_include($dir,$name,$ext='.inc.php') {
+       function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
                if(preg_match('/[^a-zA-Z0-9-]/',$name)) die(trigger_error('SAFE INCLUDE: Securityfuck.'));
                $filename="$dir/$name$ext";
                if(!is_file($filename)) die(trigger_error('SAFE INCLUDE: Fuckfound.'));
+               foreach($vars as $var => $val) $$var=$val;
                ob_start();
                include($filename);
                $out=ob_get_contents();
@@ -456,31 +469,39 @@ class Sklad_UI {
                echo('<pre>'); //DEBUG (maybe todo remove)
 
                //SephirPOST:
-               $values=array();
-               foreach($_POST as $key => $value) {
-                       $name = preg_split('/:/',$key);
-                       if(isset($name[0])) switch($name[0]) {
-                               case 'value':
-                                       foreach($value as $id => $val) $values[$id][$name[1]]=$value[$id];
-                                       break;
-                               default:
-                                       break;
+
+               /* Tenhle foreach() prekopiruje promenne
+                * z:           $_POST['values'][$table][$column][$id];
+                * do:  $values[$table][$id][$column]
+                */
+               if(isset($_POST['values'])) {
+                       $values=array();
+                       foreach($_POST['values'] as $table => $columns) {
+                               foreach($columns as $column => $ids) {
+                                       foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
+                               }
                        }
+                       //die(print_r($values));
                }
 
                if($action) switch($action) {
                        case 'new':
                        case 'edit':
-                               if(!isset($_POST['table'])) die(trigger_error("Jest nutno specifikovat tabulku voe!"));
-                               $table=$_POST['table'];
+                               //if(!isset($_POST['table'])) die(trigger_error("Jest nutno specifikovat tabulku voe!"));
+                               //$table=$_POST['table'];
+                               $table='item';
                                //print_r($values); //debug
-                               $last = $this->db->insert_or_update($table, $values);
-                               $this->post_redirect_get("$table/$last/", "$table/new/");
+                               $last = $this->db->insert_or_update_multitab($values);
+                               $last = "$table/$last/";
+                               $next = "$table/new/";
+                               echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html->link('zde', $last).'.<br />'.
+                                       'Další záznam přidáte '.$this->html->link('zde', $next).'.';
+                               die();
                                break;
                        case 'delete':
                                if(!isset($_POST['sure']) || !$_POST['sure']) die(trigger_error('Sure user expected :-)'));
-                               //$this->db->delete($class, $id);
-                               die('Neco asi bylo smazano. Fnuk :\'-('); //TODO REDIRECT
+                               $this->db->delete($class, $id);
+                               die("Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); //TODO REDIRECT
                                break;
                        case 'image':
                                $image_classes = array('model'); //TODO, use this more widely across the code
@@ -520,7 +541,8 @@ class Sklad_UI {
                                die('Tell me why you cry');
                                break;
                        case 'assistant': //assistant
-                               echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2]);
+                               $assistant_vars['step'] = isset($PATH_CHUNKS[3]) && is_numeric($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
+                               echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars);
                                break;
                        default:        //?
                                $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
This page took 0.144347 seconds and 4 git commands to generate.