Fixnuty bug s prepisovanim stavajicich zaznamu pri pridavani noveho itemu
[mirrors/SokoMan.git] / index.php
1 <?php
2 /*
3 * SkladovySystem - Storage management system compatible with LMS
4 * Copyright (C) 2011 Tomas Mudrunka
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 require_once('sklad.conf.php');
21 require_once('Sklad_LMS-fake.class.php');
22 require_once('HTTP_Auth.class.php');
23
24 /**
25 * Trida poskytuje vseobecne funkce pro generovani HTML kodu
26 *
27 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
28 *
29 * @package HTML
30 * @author Tomas Mudrunka
31 */
32 class HTML {
33 function row($row) {
34 $html='<tr>';
35 foreach($row as $var) {
36 if(trim($var) == '') $var = '&nbsp;';
37 $html.="<td>$var</td>";
38 }
39 $html.='</tr>';
40 return $html;
41 }
42
43 function table(&$table, $params='border=1') {
44 $html="<table $params>";
45 $header=true;
46 foreach($table as $row) {
47 if($header) {
48 $html.=$this->row(array_keys($row));
49 $header=false;
50 }
51 $html.=$this->row($row);
52 }
53 $html.='</table>';
54 return $html;
55 }
56
57 function link($title='n/a', $link='#void', $internal=true) {
58 if($internal) $link = $this->internal_url($link);
59 return "<a href='$link'>$title</a>";
60 }
61
62 function img($src='#void', $title='img') {
63 return "<img src='$src' alt='$title' title='$title' width=64 />";
64 }
65
66 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false) {
67 $html = "<input type='$type' ";
68 if($name) $html.= "name='$name' ";
69 if(!is_bool($value)) $html.= "value='$value' ";
70 if($options) $html.= "$options ";
71 if($placeholder) $html.= "placeholder='$placeholder' ";
72 $html .= '/>';
73 return $html;
74 }
75
76 function select($name, $selectbox, $default=false) {
77 //echo('<pre>'); print_r($selectbox);
78 $html = "<select name='$name'>";
79
80 if($default) {
81 $value=$default; $title=$selectbox[$value];
82 $html .= "<option value='$value'>$value :: $title</option>";
83 unset($selectbox[$value]);
84 }
85 foreach($selectbox as $value => $title) {
86 $html .= "<option value='$value'>$value :: $title</option>";
87 }
88 $html .= "</select>";
89 return $html;
90 }
91 }
92
93 /**
94 * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
95 *
96 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
97 *
98 * @package Sklad_HTML
99 * @author Tomas Mudrunka
100 */
101 class Sklad_HTML extends HTML {
102 function header($title='') {
103 $home = URL_HOME;
104 $script = $_SERVER['SCRIPT_NAME'];
105 $search = htmlspecialchars(@trim($_GET['q']));
106 $message = strip_tags(@trim($_GET['message']),'<a><b><u><i>');
107 return <<<EOF
108 <head>
109 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
110 </head>
111 <h1><a href="$script/">SystémSklad</a><small>$title</small></h1>
112
113 <style type="text/css">
114 .menu li, .menu * li {
115 float: left;
116 margin: 0.2em;
117 }
118
119 .menu * menu {
120 position: absolute;
121 background-color: yellow;
122 }
123
124 .menu, .menu * menu {
125 list-style: none;
126 }
127
128 .menu * menu {
129 display: none;
130 margin: 0;
131 }
132
133 .menu li:hover menu {
134 display: block;
135 }
136 </style>
137
138 <div>
139 <menu class="menu">
140 <li><a href="?logout">Logout</a></li>
141 <li><a href="$script/">Home</a></li>
142 <li><a href="#">Assistants</A>
143 <menu>
144 <li><a href="$script/assistant/store">store</a></li>
145 <li><a href="$script/assistant/new-item">new-item</a></li>
146 </menu>
147 </li>
148 <li><a href="#">List</a>
149 <menu>
150 <li><a href="$script/item">item</a></li>
151 <li><a href="$script/model">model</a></li>
152 <li><a href="$script/category">category</a></li>
153 <li><a href="$script/producer">producer</a></li>
154 <li><a href="$script/vendor">vendor</a></li>
155 <li><a href="$script/room">room</a></li>
156 <li><a href="$script/status">status</a></li>
157 </menu>
158 </li>
159 <li><a href="#">New</a>
160 <menu>
161 <li><a href="$script/item/new">item</a></li>
162 <li><a href="$script/model/new">model</a></li>
163 <li><a href="$script/category/new">category</a></li>
164 <li><a href="$script/producer/new">producer</a></li>
165 <li><a href="$script/vendor/new">vendor</a></li>
166 <li><a href="$script/room/new">room</a></li>
167 <li><a href="$script/status/new">status</a></li>
168 </menu>
169 </li>
170 </menu>
171
172 <div style="float: right;">
173 <form action="?" method="GET">
174 <input type="text" name="q" placeholder="regexp..." value="$search" />
175 <input type="submit" value="filter" />
176 </form>
177 <!-- form action="$script/" method="GET">
178 <input type="text" name="q" placeholder="regexp..." value="$search" />
179 <input type="submit" value="search items" />
180 </form -->
181 </div>
182 </div>
183 <hr style="clear: both;" />
184 <div style="background-color:#FFDDDD;">
185 <font color="red">$message</font>
186 </div>
187 EOF;
188 }
189
190 function internal_url($link) {
191 return $_SERVER['SCRIPT_NAME'].'/'.$link;
192 }
193
194 function table_add_images(&$table) {
195 $image = array('model_id');
196 foreach($table as $id => $row) {
197 foreach($image as $column) if(isset($table[$id][$column])) {
198 $type = @array_shift(preg_split('/_/', $column));
199 $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg';
200 $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]);
201 }
202 }
203 }
204
205 function table_collapse(&$table) {
206 $collapse = array(
207 'item_id' => 'item_id',
208 'model_id' => 'model_name',
209 'category_id' => 'category_name',
210 'producer_id' => 'producer_name',
211 'vendor_id' => 'vendor_name',
212 'room_id' => 'room_name',
213 'status_id' => 'status_name',
214 );
215 foreach($table as $id => $row) {
216 foreach($collapse as $link => $title)
217 if(isset($table[$id][$link])) {
218 $type = @array_shift(preg_split('/_/', $link));
219 if($link != $title) unset($table[$id][$link]);
220 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
221 }
222 }
223 }
224
225 function table_sort(&$table) {
226 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
227 $table_sorted = array();
228 foreach($table as $id => $row) {
229 $table_sorted[$id] = array();
230 foreach($precedence as $column) if(isset($table[$id][$column])) {
231 $table_sorted[$id][$column]=$table[$id][$column];
232 unset($table[$id][$column]);
233 }
234 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
235 }
236 $table = $table_sorted;
237 }
238
239 function render_item_table($table) {
240 $this->table_add_images($table);
241 $this->table_collapse($table);
242 $this->table_sort($table);
243 return $this->table($table);
244 }
245
246 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $multi_insert=true) {
247 //echo('<pre>'); print_r($selectbox);
248 //echo('<pre>'); print_r($current);
249 $update = false;
250 if(is_array($current)) {
251 $update = true;
252 $current = array_shift($current);
253 }
254
255 if(!is_array($hidecols)) $hidecols = array('item_author', 'item_valid_from', 'item_valid_till'); //TODO Autodetect
256
257 $html='<form method="POST">';
258 if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">';
259 //$html.=$this->input('table', $class, 'hidden');
260 foreach($columns as $column) {
261 $html.=$class.':<b>'.$column['Field'].'</b>: ';
262 $name="values[$class][".$column['Field'].'][]';
263 $val = $update ? $current[$column['Field']] : false;
264 switch(true) {
265 case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)):
266 if(!$val) $val = '';
267 $html.=$this->input($name, $val, 'hidden');
268 $html.=$val.'(AUTO)';
269 break;
270 case isset($selectbox[$column['Field']]):
271 $html.=$this->select($name,$selectbox[$column['Field']],$val);
272 break;
273 default:
274 $html.=$this->input($name, $val);
275 break;
276 }
277 $html.='<br />';
278 }
279
280 if($multi_insert) {
281 //TODO, move to separate JS file
282 $html.=<<<EOF
283 </div>
284 <span name="input_set_next"></span><br style="clear:both" />
285 <script>
286 function duplicate_element(what, where) {
287 document.getElementsByName(where)[0].outerHTML =
288 document.getElementsByName(what)[0].outerHTML
289 + document.getElementsByName(where)[0].outerHTML;
290 }
291 </script>
292 <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a>
293 EOF;
294 }
295
296 $btn = is_array($current) ? 'UPDATE' : 'INSERT';
297 $html.=$this->input(false, $btn, 'submit');
298 $html.='</form>';
299 return $html;
300 }
301 }
302
303 /**
304 * Trida poskytuje rozhrani k databazi skladu
305 *
306 * @package Sklad_DB
307 * @author Tomas Mudrunka
308 */
309 class Sklad_DB extends PDO {
310 function __construct() {
311 $this->lms = new Sklad_LMS();
312
313 parent::__construct(
314 DB_DSN, DB_USER, DB_PASS,
315 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL
316 );
317 }
318
319 function escape($str) {
320 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
321 }
322
323 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
324 //Configuration
325 $join = array(
326 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
327 'model' => array('category', 'producer')
328 ); //TODO Autodetect using foreign keys?
329 $search_fields = array(
330 'item' => array('item_id','model_name','model_barcode','model_descript','producer_name','vendor_name')
331 ); //TODO Autodetect
332
333 //Escaping
334 $class = $this->escape($class);
335
336 //SELECT
337 $sql="SELECT * FROM $class\n";
338 //JOIN
339 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n";
340 //WHERE/REGEXP
341 if($search) {
342 $search = $this->quote($search);
343 if(!isset($search_fields[$class])) $this->post_redirect_get($class, "Ve tride $class zatim vyhledavat nemozno :-(");
344 $search = '';
345 foreach($search_fields[$class] as $column) $search .= "OR $column REGEXP $search ";
346 $where[] = "FALSE $search";
347 } elseif($id) $where[] = "$class$suffix_id = $id";
348 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
349
350 if($where) $sql .= 'WHERE '.implode(' AND ', $where)."\n";
351 //ORDER
352 if(!$order) $order = $class.$suffix_id;
353 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
354 $sql .= "ORDER BY $order\n";
355 //LIMIT/OFFSET
356 if($limit) {
357 $limit = $this->escape((int)$limit);
358 $offset = $this->escape((int)$offset);
359 $sql .= "LIMIT $offset,$limit\n";
360 }
361
362 return $sql;
363 }
364
365 function safe_query($sql, $fatal=true) {
366 $result = $this->query($sql);
367 if(!$result) {
368 $error = $this->errorInfo();
369 trigger_error("<font color=red><b>QUERY FAILED ($error[0],$error[1]): </b>$error[2]<br /><br /><b>QUERY:</b>\n<pre>$sql</pre></font>");
370 if($fatal) die();
371 }
372 return $result;
373 }
374
375 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
376 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
377 $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
378 if(!$result || !is_array($indexed)) return $result;
379
380 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
381 return $indexed;
382 }
383
384 function get_columns($class) {
385 $class = $this->escape($class);
386 $sql = "SHOW COLUMNS FROM $class;";
387 return $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
388 }
389
390 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
391 $selectbox=array();
392 foreach($columns as $column) {
393 if($column['Field'] == 'user_id') continue; //TODO HACK Blacklist: tabulka nemusi obsahovat *_name!!! momentalne se to tyka jen tabulky user (a item - u ty to nevadi)!
394 if($class && $column['Field'] == $class.$suffix_id) continue;
395 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
396 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
397
398 $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : '';
399 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
400 $result = $this->safe_query($sql, false);
401 if(!$result) continue;
402 $result = $result->fetchAll(PDO::FETCH_ASSOC);
403 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
404 }
405 //echo('<pre>'); print_r($selectbox);
406 return array_filter($selectbox, 'ksort');
407 }
408
409 function contains_history($table) {
410 $history_tables = array('item'); //TODO Autodetect
411 return in_array($table, $history_tables);
412 }
413
414 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
415 //Init
416 $history = $this->contains_history($table);
417
418 //Escaping
419 $table = $this->escape($table);
420
421 //Get list of POSTed columns
422 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
423 $columns = implode(',',$columns_array);
424
425 //Build query
426 $sql = '';
427 //echo('<pre>'); die(print_r($values));
428
429 if($history) {
430 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
431 if($history_update) {
432 $sql .= "UPDATE $table";
433 $sql .= " SET ${table}_valid_till=NOW()";
434 $sql .= " WHERE ${table}_valid_till=0 AND (";
435 $or = '';
436 foreach($values as $row) {
437 $sql .= $or.' '.$table.'_id='.$row[$table.'_id'];
438 $or = ' OR';
439 }
440 $sql .= " );\n\n";
441 $replace = false;
442 }
443 }
444
445 //Insert into table (columns)
446 $sql .= "INSERT INTO $table ($columns) VALUES ";
447
448 //Values (a,b,c),(d,e,f)
449 $comma='';
450 foreach($values as $row) {
451 $row_quoted = array_map(array($this,'quote'), $row); //Check
452 if($history) {
453 foreach($row as $column => $value) {
454 switch($column) {
455 case $table.'_valid_from':
456 $row_quoted[$column] = 'NOW()';
457 break;
458 case $table.'_valid_till':
459 $row_quoted[$column] = '0';
460 break;
461 case $table.'_author':
462 $row_quoted[$column] = $this->lms->get_authorized_user_id();
463 //die($this->lms->get_authorized_user_id().'=USER');
464 break;
465 }
466 }
467 }
468 $sql .= $comma.'('.implode(',',$row_quoted).')';
469 $comma = ',';
470 }
471
472 //On duplicate key
473 if($replace) {
474 foreach($columns_array as $col) {
475 if($col == $table.'_id' || $col == $table.'_valid_till') continue;
476 $on_duplicate[] = "$col=VALUES($col)";
477 }
478 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
479 }
480
481 //Terminate
482 $sql .= ';';
483 return $sql;
484 }
485
486 function insert_or_update($table, $values, $replace=true) {
487 $sql = $this->build_query_insert($table, $values, $replace);
488 $this->safe_query($sql);
489 return $this->lastInsertId();
490 }
491
492 function insert_or_update_multitab($values, $replace=true) {
493 $last=false;
494 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
495 return $last;
496 }
497
498 function delete($table, $id, $suffix_id='_id') {
499 if($this->contains_history($table)) return false;
500 $key = $this->escape($table.$suffix_id);
501 $table = $this->escape($table);
502 $id = $this->quote($id);
503 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
504 }
505 }
506
507 /**
508 * Trida poskytuje high-level rozhrani k databazi skladu
509 *
510 * @package Sklad_DB_Abstract
511 * @author Tomas Mudrunka
512 */
513 class Sklad_DB_Abstract extends Sklad_DB {
514 //TODO Code
515 }
516
517 /**
518 * Trida implementuje uzivatelske rozhrani skladu
519 *
520 * Example usage:
521 * $sklad = new Sklad_UI();
522 * $sklad->process_http_request();
523 *
524 * @package Sklad_UI
525 * @author Tomas Mudrunka
526 */
527 class Sklad_UI {
528 function __construct() {
529 $this->db = new Sklad_DB();
530 $this->html = new Sklad_HTML();
531 }
532
533 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
534 return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false));
535 }
536
537 function render_form_add($class) {
538 $columns = $this->db->get_columns($class);
539 $selectbox = $this->db->columns_get_selectbox($columns, $class);
540 return $this->html->render_insert_form($class, $columns, $selectbox);
541 }
542
543 function render_form_edit($class, $id) {
544 $columns = $this->db->get_columns($class);
545 $selectbox = $this->db->columns_get_selectbox($columns, $class);
546 $current = $this->db->get_listing($class, $id, 1);
547 return $this->html->render_insert_form($class, $columns, $selectbox, $current);
548 }
549
550 function render_single_record_details($class, $id) {
551 $id_next = $id + 1;
552 $id_prev = $id - 1 > 0 ? $id - 1 : 0;
553 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
554 $html='';
555 $html.= $this->html->link('<<', "$class/$id_prev/");
556 $html.= '-';
557 $html.= $this->html->link('>>', "$class/$id_next/");
558 $html.= '<br />';
559 $html.= $this->html->link('edit', "$class/$id/edit/");
560 if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
561 return $html;
562 }
563
564 function render_listing_navigation($class, $id, $limit, $offset) {
565 $offset_next = $offset + $limit;
566 $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0;
567 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
568 $html='';
569 $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get");
570 $html.= '-';
571 $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get");
572 $html.= '<br />';
573 $html.= $this->html->link('new', "$class/new/$get");
574 return $html;
575 }
576
577 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
578 $html='';
579 if(is_numeric($id)) {
580 $html.=$this->render_single_record_details($class, $id);
581 } else {
582 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
583 }
584 if($edit) {
585 $html.= $this->render_form_edit($class, $id);
586 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
587 $html.= "<form action='$action' method='POST'>";
588 $html.= $this->html->input(false, 'DELETE', 'submit');
589 $html.= 'sure?'.$this->html->input('sure', false, 'checkbox');
590 $html.= '</form>';
591 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
592 $html.= "<form action='$action' method='POST' enctype='multipart/form-data'>";
593 $html.= $this->html->input('image', false, 'file', false, 'size="30"');
594 $html.= $this->html->input(false, 'IMAGE', 'submit');
595 $html.='</form>';
596 }
597 return $html;
598 }
599
600 function check_auth() {
601 new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth'));
602 }
603
604 function post_redirect_get($location, $message='', $error=false) {
605 $location = $this->html->internal_url($location).'?message='.urlencode($message);
606 header('Location: '.$location);
607 if($error) trigger_error($message);
608 die("Location: <a href='$location'>$location</a>");
609 }
610
611 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
612 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
613 $filename="$dir/$name$ext";
614 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
615 foreach($vars as $var => $val) $$var=$val;
616 ob_start();
617 include($filename);
618 $out=ob_get_contents();
619 ob_end_clean();
620 return $out;
621 }
622
623 function process_http_request_post($action=false, $class=false, $id=false) {
624 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
625 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
626
627 //SephirPOST:
628
629 /* Tenhle foreach() prekopiruje promenne
630 * z: $_POST['values'][$table][$column][$id];
631 * do: $values[$table][$id][$column]
632 */
633 if(isset($_POST['values'])) {
634 $values=array();
635 foreach($_POST['values'] as $table => $columns) {
636 foreach($columns as $column => $ids) {
637 foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
638 }
639 }
640 //die(print_r($values));
641 }
642
643 if($action) switch($action) {
644 case 'new':
645 $replace = false;
646 case 'edit':
647 if(!isset($replace)) $replace = true;
648 $table = $class ? $class : 'item';
649 //print_r($values); //debug
650 $last = $this->db->insert_or_update_multitab($values, $replace);
651 $last = "$table/$last/";
652 $next = "$table/new/";
653 $this->post_redirect_get($last, 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.');
654 break;
655 case 'delete':
656 if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
657 $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
658 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
659 break;
660 case 'image':
661 $image_classes = array('model'); //TODO, use this more widely across the code
662 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
663 $image_destination = DIR_IMAGES."/$class/$id.jpg";
664 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Kazde neco se musi nejak jmenovat!', true);
665 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
666 chmod ($image_destination, 0664);
667 $this->post_redirect_get("$class/$id", 'Obrazek se naladoval :)');
668 } else $this->post_redirect_get("$class/$id/edit", 'Soubor se nenahral :(', true);
669 break;
670 default:
671 trigger_error('Nothin\' to do here my cutie :-*');
672 break;
673 }
674
675 die('POSTed pyčo!');
676 }
677
678 function process_http_request() {
679 $this->check_auth();
680
681 @ini_set('magic_quotes_gpc' , 'off');
682 if(get_magic_quotes_gpc()) {
683 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
684 }
685
686 $PATH_INFO=@trim($_SERVER[PATH_INFO]);
687 if($_SERVER['REQUEST_METHOD'] != 'POST') echo $this->html->header($PATH_INFO); //TODO tahle podminka naznacuje ze je v navrhu nejaka drobna nedomyslenost...
688
689
690 //Sephirot:
691 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
692 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
693 switch($PATH_CHUNKS[1]) {
694 case 'test': //test
695 die('Tell me why you cry');
696 break;
697 case 'assistant': //assistant
698 $assistant_vars['step'] = isset($PATH_CHUNKS[3]) && is_numeric($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
699 echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars);
700 break;
701 default: //?
702 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
703 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
704 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
705 switch($PATH_CHUNKS[2]) {
706 case 'new': //?/new
707 $this->process_http_request_post($PATH_CHUNKS[2], $class);
708 echo $this->render_form_add($class);
709 break;
710 default: //?/?
711 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false);
712 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
713 $edit=false;
714 switch($PATH_CHUNKS[3]) {
715 case 'edit': //?/?/edit
716 case 'image': //?/?/image
717 case 'delete': //?/?/delete
718 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
719 $edit=true;
720 default: //?/?/?
721 $history = $PATH_CHUNKS[3] == 'history' ? true : false;
722 $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0');
723 $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0');
724 $where = false; //TODO get from URL
725 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
726 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
727 //print_r(array("<pre>",$_SERVER));
728 break;
729 }
730 break;
731 }
732 break;
733 }
734 }
735 }
736
737 $sklad = new Sklad_UI();
738 $sklad->process_http_request();
739
740 echo("<hr/>");
This page took 1.149259 seconds and 4 git commands to generate.