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