Do tabulky item pridan sloupec na poznamky (umisteni, etc...)
[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">
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
cdfce7c2 138<div>
c9759229 139 <menu class="menu">
cdfce7c2
TM
140 <li><a href="?logout">Logout</a></li>
141 <li><a href="$script/">Home</a></li>
c9759229
TM
142 <li><a href="#">Assistants</A>
143 <menu>
b4dcae05 144 <li><a href="$script/assistant/store">store</a></li>
c9759229
TM
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>
cdfce7c2 170 </menu>
c9759229
TM
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>
cdfce7c2 182</div>
c9759229 183<hr style="clear: both;" />
d9384cb5
TM
184<div style="background-color:#FFDDDD;">
185 <font color="red">$message</font>
186</div>
cdfce7c2
TM
187EOF;
188 }
189
d9384cb5
TM
190 function internal_url($link) {
191 return $_SERVER['SCRIPT_NAME'].'/'.$link;
192 }
193
cdfce7c2
TM
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
fc5c5b8b 239 function render_item_table($table) {
cdfce7c2
TM
240 $this->table_add_images($table);
241 $this->table_collapse($table);
242 $this->table_sort($table);
fc5c5b8b 243 return $this->table($table);
cdfce7c2
TM
244 }
245
16261142 246 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true) {
cdfce7c2
TM
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
a084118b
TM
255 if(!is_array($hidecols)) $hidecols = array('item_author', 'item_valid_from', 'item_valid_till'); //TODO Autodetect
256
16261142
TM
257 $action = $action ? " action='$action'" : false;
258 $html="<form$action method='POST'>";
fc5c5b8b 259 if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">';
371a86f4 260 //$html.=$this->input('table', $class, 'hidden');
cdfce7c2 261 foreach($columns as $column) {
371a86f4 262 $html.=$class.':<b>'.$column['Field'].'</b>: ';
7d20381f 263 $name="values[$class][".$column['Field'].'][]';
16261142 264 $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false;
cdfce7c2 265 switch(true) {
a084118b 266 case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)):
e9cb8cea 267 if(!$val) $val = '';
fc5c5b8b
TM
268 $html.=$this->input($name, $val, 'hidden');
269 $html.=$val.'(AUTO)';
cdfce7c2
TM
270 break;
271 case isset($selectbox[$column['Field']]):
e9cb8cea 272 $html.=$this->select($name,$selectbox[$column['Field']],$val);
cdfce7c2
TM
273 break;
274 default:
fc5c5b8b 275 $html.=$this->input($name, $val);
cdfce7c2
TM
276 break;
277 }
fc5c5b8b 278 $html.='<br />';
cdfce7c2
TM
279 }
280
281 if($multi_insert) {
282 //TODO, move to separate JS file
fc5c5b8b 283 $html.=<<<EOF
cdfce7c2
TM
284 </div>
285 <span name="input_set_next"></span><br style="clear:both" />
286 <script>
287 function duplicate_element(what, where) {
288 document.getElementsByName(where)[0].outerHTML =
289 document.getElementsByName(what)[0].outerHTML
290 + document.getElementsByName(where)[0].outerHTML;
291 }
292 </script>
293 <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a>
294EOF;
295 }
296
297 $btn = is_array($current) ? 'UPDATE' : 'INSERT';
fc5c5b8b
TM
298 $html.=$this->input(false, $btn, 'submit');
299 $html.='</form>';
300 return $html;
cdfce7c2
TM
301 }
302}
303
78bf26a5
TM
304/**
305* Trida poskytuje rozhrani k databazi skladu
306*
307* @package Sklad_DB
308* @author Tomas Mudrunka
309*/
cdfce7c2
TM
310class Sklad_DB extends PDO {
311 function __construct() {
312 $this->lms = new Sklad_LMS();
313
314 parent::__construct(
315 DB_DSN, DB_USER, DB_PASS,
316 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL
317 );
318 }
319
320 function escape($str) {
321 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
322 }
323
cb8a6861 324 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
2bedfdac 325 //Configuration
cdfce7c2
TM
326 $join = array(
327 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
328 'model' => array('category', 'producer')
9ea191cb 329 ); //TODO Autodetect using foreign keys?
cdfce7c2 330 $search_fields = array(
df4079a6 331 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name')
9ea191cb 332 ); //TODO Autodetect
2bedfdac
TM
333
334 //Escaping
335 $class = $this->escape($class);
336
337 //SELECT
cdfce7c2 338 $sql="SELECT * FROM $class\n";
2bedfdac 339 //JOIN
8fd613d6 340 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n";
2bedfdac 341 //WHERE/REGEXP
cdfce7c2
TM
342 if($search) {
343 $search = $this->quote($search);
cb8a6861 344 if(!isset($search_fields[$class])) $this->post_redirect_get($class, "Ve tride $class zatim vyhledavat nemozno :-(");
5895162b
TM
345 $sql_search = '';
346 foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
347 $where[] = "FALSE $sql_search";
cb8a6861
TM
348 } elseif($id) $where[] = "$class$suffix_id = $id";
349 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
350
5895162b 351 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
117817be
TM
352 //ORDER
353 if(!$order) $order = $class.$suffix_id;
354 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
355 $sql .= "ORDER BY $order\n";
2bedfdac 356 //LIMIT/OFFSET
cdfce7c2
TM
357 if($limit) {
358 $limit = $this->escape((int)$limit);
359 $offset = $this->escape((int)$offset);
360 $sql .= "LIMIT $offset,$limit\n";
361 }
2bedfdac 362
cdfce7c2
TM
363 return $sql;
364 }
365
66b6f4d6 366 function safe_query($sql, $fatal=true) {
cdfce7c2
TM
367 $result = $this->query($sql);
368 if(!$result) {
2bedfdac 369 $error = $this->errorInfo();
66b6f4d6
TM
370 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>");
371 if($fatal) die();
cdfce7c2
TM
372 }
373 return $result;
374 }
375
cb8a6861
TM
376 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
377 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
cdfce7c2
TM
378 $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
379 if(!$result || !is_array($indexed)) return $result;
380
381 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
382 return $indexed;
383 }
384
385 function get_columns($class) {
386 $class = $this->escape($class);
387 $sql = "SHOW COLUMNS FROM $class;";
388 return $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
389 }
390
391 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
392 $selectbox=array();
393 foreach($columns as $column) {
66b6f4d6 394 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
395 if($class && $column['Field'] == $class.$suffix_id) continue;
396 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
397 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
66b6f4d6 398
117817be 399 $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : '';
b4dcae05 400 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
66b6f4d6
TM
401 $result = $this->safe_query($sql, false);
402 if(!$result) continue;
403 $result = $result->fetchAll(PDO::FETCH_ASSOC);
cdfce7c2
TM
404 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
405 }
406 //echo('<pre>'); print_r($selectbox);
66b6f4d6 407 return array_filter($selectbox, 'ksort');
9ea191cb
TM
408 }
409
16261142
TM
410 function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed
411 $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
412 $value=$this->quote($value);
413 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
414 $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
415 if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error('Položka nenalezena!')); //TODO post_redirect_get...
416 }
417
9ea191cb
TM
418 function contains_history($table) {
419 $history_tables = array('item'); //TODO Autodetect
420 return in_array($table, $history_tables);
cdfce7c2
TM
421 }
422
423 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
b66fadbb
TM
424 //Init
425 $history = $this->contains_history($table);
426
9ea191cb 427 //Escaping
cdfce7c2
TM
428 $table = $this->escape($table);
429
430 //Get list of POSTed columns
aa3fd0a8
TM
431 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
432 $columns = implode(',',$columns_array);
cdfce7c2 433
9fb856ba 434 //Build query
b66fadbb 435 $sql = '';
b66fadbb
TM
436 //echo('<pre>'); die(print_r($values));
437
438 if($history) {
439 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
440 if($history_update) {
441 $sql .= "UPDATE $table";
117817be
TM
442 $sql .= " SET ${table}_valid_till=NOW()";
443 $sql .= " WHERE ${table}_valid_till=0 AND (";
b66fadbb
TM
444 $or = '';
445 foreach($values as $row) {
446 $sql .= $or.' '.$table.'_id='.$row[$table.'_id'];
447 $or = ' OR';
448 }
449 $sql .= " );\n\n";
450 $replace = false;
451 }
452 }
453
cdfce7c2 454 //Insert into table (columns)
aa3fd0a8 455 $sql .= "INSERT INTO $table ($columns) VALUES ";
cdfce7c2
TM
456
457 //Values (a,b,c),(d,e,f)
458 $comma='';
459 foreach($values as $row) {
9fb856ba
TM
460 $row_quoted = array_map(array($this,'quote'), $row); //Check
461 if($history) {
b66fadbb
TM
462 foreach($row as $column => $value) {
463 switch($column) {
464 case $table.'_valid_from':
465 $row_quoted[$column] = 'NOW()';
466 break;
467 case $table.'_valid_till':
468 $row_quoted[$column] = '0';
469 break;
9fb856ba
TM
470 case $table.'_author':
471 $row_quoted[$column] = $this->lms->get_authorized_user_id();
b66fadbb
TM
472 //die($this->lms->get_authorized_user_id().'=USER');
473 break;
b66fadbb
TM
474 }
475 }
476 }
477 $sql .= $comma.'('.implode(',',$row_quoted).')';
cdfce7c2
TM
478 $comma = ',';
479 }
480
aa3fd0a8
TM
481 //On duplicate key
482 if($replace) {
483 foreach($columns_array as $col) {
484 if($col == $table.'_id' || $col == $table.'_valid_till') continue;
485 $on_duplicate[] = "$col=VALUES($col)";
486 }
487 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
488 }
489
cdfce7c2
TM
490 //Terminate
491 $sql .= ';';
492 return $sql;
493 }
494
b4dcae05
TM
495 function insert_or_update($table, $values, $replace=true) {
496 $sql = $this->build_query_insert($table, $values, $replace);
cdfce7c2
TM
497 $this->safe_query($sql);
498 return $this->lastInsertId();
499 }
500
b4dcae05 501 function insert_or_update_multitab($values, $replace=true) {
371a86f4 502 $last=false;
b4dcae05 503 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
371a86f4
TM
504 return $last;
505 }
506
cdfce7c2 507 function delete($table, $id, $suffix_id='_id') {
64f31c54 508 if($this->contains_history($table)) return false;
cdfce7c2
TM
509 $key = $this->escape($table.$suffix_id);
510 $table = $this->escape($table);
511 $id = $this->quote($id);
512 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
513 }
514}
515
0a027cc7
TM
516/**
517* Trida poskytuje high-level rozhrani k databazi skladu
518*
519* @package Sklad_DB_Abstract
520* @author Tomas Mudrunka
521*/
522class Sklad_DB_Abstract extends Sklad_DB {
523 //TODO Code
524}
525
78bf26a5
TM
526/**
527* Trida implementuje uzivatelske rozhrani skladu
528*
529* Example usage:
530* $sklad = new Sklad_UI();
531* $sklad->process_http_request();
532*
533* @package Sklad_UI
534* @author Tomas Mudrunka
535*/
cdfce7c2
TM
536class Sklad_UI {
537 function __construct() {
538 $this->db = new Sklad_DB();
539 $this->html = new Sklad_HTML();
540 }
541
cb8a6861
TM
542 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
543 return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false));
cdfce7c2
TM
544 }
545
a25f85f8 546 function render_form_add($class) {
cdfce7c2
TM
547 $columns = $this->db->get_columns($class);
548 $selectbox = $this->db->columns_get_selectbox($columns, $class);
a25f85f8 549 return $this->html->render_insert_form($class, $columns, $selectbox);
cdfce7c2
TM
550 }
551
a25f85f8 552 function render_form_edit($class, $id) {
cdfce7c2
TM
553 $columns = $this->db->get_columns($class);
554 $selectbox = $this->db->columns_get_selectbox($columns, $class);
117817be 555 $current = $this->db->get_listing($class, $id, 1);
a25f85f8 556 return $this->html->render_insert_form($class, $columns, $selectbox, $current);
cdfce7c2
TM
557 }
558
a25f85f8 559 function render_single_record_details($class, $id) {
cdfce7c2
TM
560 $id_next = $id + 1;
561 $id_prev = $id - 1 > 0 ? $id - 1 : 0;
562 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
a25f85f8
TM
563 $html='';
564 $html.= $this->html->link('<<', "$class/$id_prev/");
565 $html.= '-';
566 $html.= $this->html->link('>>', "$class/$id_next/");
567 $html.= '<br />';
568 $html.= $this->html->link('edit', "$class/$id/edit/");
9fb856ba 569 if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
a25f85f8 570 return $html;
cdfce7c2
TM
571 }
572
a25f85f8 573 function render_listing_navigation($class, $id, $limit, $offset) {
cdfce7c2
TM
574 $offset_next = $offset + $limit;
575 $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0;
576 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
a25f85f8
TM
577 $html='';
578 $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get");
579 $html.= '-';
580 $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get");
581 $html.= '<br />';
582 $html.= $this->html->link('new', "$class/new/$get");
583 return $html;
cdfce7c2
TM
584 }
585
a25f85f8
TM
586 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
587 $html='';
cdfce7c2 588 if(is_numeric($id)) {
a25f85f8 589 $html.=$this->render_single_record_details($class, $id);
cdfce7c2 590 } else {
a25f85f8 591 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
cdfce7c2
TM
592 }
593 if($edit) {
a25f85f8 594 $html.= $this->render_form_edit($class, $id);
cdfce7c2 595 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
a25f85f8
TM
596 $html.= "<form action='$action' method='POST'>";
597 $html.= $this->html->input(false, 'DELETE', 'submit');
598 $html.= 'sure?'.$this->html->input('sure', false, 'checkbox');
599 $html.= '</form>';
cdfce7c2 600 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
a25f85f8
TM
601 $html.= "<form action='$action' method='POST' enctype='multipart/form-data'>";
602 $html.= $this->html->input('image', false, 'file', false, 'size="30"');
603 $html.= $this->html->input(false, 'IMAGE', 'submit');
604 $html.='</form>';
cdfce7c2 605 }
a25f85f8 606 return $html;
cdfce7c2
TM
607 }
608
609 function check_auth() {
610 new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth'));
611 }
612
64f31c54 613 function post_redirect_get($location, $message='', $error=false) {
1f74892d
TM
614 $location = $this->html->internal_url($location).'?message='.urlencode($message);
615 header('Location: '.$location);
64f31c54
TM
616 if($error) trigger_error($message);
617 die("Location: <a href='$location'>$location</a>");
cdfce7c2
TM
618 }
619
623c65e2 620 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
64f31c54 621 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
3e6c412e 622 $filename="$dir/$name$ext";
64f31c54 623 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
623c65e2 624 foreach($vars as $var => $val) $$var=$val;
3e6c412e
TM
625 ob_start();
626 include($filename);
627 $out=ob_get_contents();
628 ob_end_clean();
629 return $out;
630 }
631
cdfce7c2
TM
632 function process_http_request_post($action=false, $class=false, $id=false) {
633 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
1f74892d 634 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
cdfce7c2
TM
635
636 //SephirPOST:
371a86f4
TM
637
638 /* Tenhle foreach() prekopiruje promenne
7d20381f 639 * z: $_POST['values'][$table][$column][$id];
371a86f4
TM
640 * do: $values[$table][$id][$column]
641 */
7d20381f
TM
642 if(isset($_POST['values'])) {
643 $values=array();
644 foreach($_POST['values'] as $table => $columns) {
645 foreach($columns as $column => $ids) {
646 foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
647 }
cdfce7c2 648 }
7d20381f 649 //die(print_r($values));
cdfce7c2
TM
650 }
651
652 if($action) switch($action) {
653 case 'new':
b4dcae05 654 $replace = false;
cdfce7c2 655 case 'edit':
b4dcae05 656 if(!isset($replace)) $replace = true;
64f31c54 657 $table = $class ? $class : 'item';
cdfce7c2 658 //print_r($values); //debug
b4dcae05 659 $last = $this->db->insert_or_update_multitab($values, $replace);
d9384cb5
TM
660 $last = "$table/$last/";
661 $next = "$table/new/";
64f31c54 662 $this->post_redirect_get($last, 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.');
cdfce7c2
TM
663 break;
664 case 'delete':
64f31c54
TM
665 if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
666 $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
1f74892d 667 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
cdfce7c2
TM
668 break;
669 case 'image':
670 $image_classes = array('model'); //TODO, use this more widely across the code
64f31c54 671 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
cdfce7c2 672 $image_destination = DIR_IMAGES."/$class/$id.jpg";
64f31c54 673 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Kazde neco se musi nejak jmenovat!', true);
cdfce7c2 674 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
1f74892d
TM
675 chmod ($image_destination, 0664);
676 $this->post_redirect_get("$class/$id", 'Obrazek se naladoval :)');
64f31c54 677 } else $this->post_redirect_get("$class/$id/edit", 'Soubor se nenahral :(', true);
cdfce7c2
TM
678 break;
679 default:
680 trigger_error('Nothin\' to do here my cutie :-*');
681 break;
682 }
683
684 die('POSTed pyčo!');
685 }
686
687 function process_http_request() {
688 $this->check_auth();
689
690 @ini_set('magic_quotes_gpc' , 'off');
691 if(get_magic_quotes_gpc()) {
692 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
693 }
694
695 $PATH_INFO=@trim($_SERVER[PATH_INFO]);
8fd613d6 696 if($_SERVER['REQUEST_METHOD'] != 'POST') echo $this->html->header($PATH_INFO); //TODO tahle podminka naznacuje ze je v navrhu nejaka drobna nedomyslenost...
cdfce7c2
TM
697
698
699 //Sephirot:
700 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
701 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
702 switch($PATH_CHUNKS[1]) {
703 case 'test': //test
704 die('Tell me why you cry');
705 break;
3e6c412e 706 case 'assistant': //assistant
623c65e2
TM
707 $assistant_vars['step'] = isset($PATH_CHUNKS[3]) && is_numeric($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
708 echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars);
3e6c412e 709 break;
cdfce7c2
TM
710 default: //?
711 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
712 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
713 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
714 switch($PATH_CHUNKS[2]) {
715 case 'new': //?/new
716 $this->process_http_request_post($PATH_CHUNKS[2], $class);
a25f85f8 717 echo $this->render_form_add($class);
cdfce7c2
TM
718 break;
719 default: //?/?
720 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false);
721 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
722 $edit=false;
723 switch($PATH_CHUNKS[3]) {
724 case 'edit': //?/?/edit
9fb856ba
TM
725 case 'image': //?/?/image
726 case 'delete': //?/?/delete
cdfce7c2
TM
727 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
728 $edit=true;
729 default: //?/?/?
9fb856ba 730 $history = $PATH_CHUNKS[3] == 'history' ? true : false;
cdfce7c2
TM
731 $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0');
732 $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0');
cb8a6861
TM
733 $where = false; //TODO get from URL
734 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
a25f85f8 735 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
cdfce7c2
TM
736 //print_r(array("<pre>",$_SERVER));
737 break;
738 }
739 break;
740 }
741 break;
742 }
743 }
744}
745
746$sklad = new Sklad_UI();
747$sklad->process_http_request();
748
749echo("<hr/>");
This page took 0.699076 seconds and 4 git commands to generate.