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