3 * SkladovySystem - Storage management system compatible with LMS
4 * Copyright (C) 2011 Tomas Mudrunka
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.
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.
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/>.
20 require_once('sklad.conf.php');
21 set_include_path(DIR_LIB
.PATH_SEPARATOR
.get_include_path());
23 require_once('Sklad_Auth.class/common.php');
24 require_once('HTTP_Auth.class.php');
25 require_once('Locale.class.php');
26 require_once('Barcode.class.php');
29 * Trida poskytuje vseobecne funkce pro generovani HTML kodu
31 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
34 * @author Tomas Mudrunka
37 function row($row,$type=false) {
39 if($type) $html.="<$type>";
41 $td = $type == 'thead' ?
'th' : 'td';
42 foreach($row as $var) {
43 if(trim($var) == '') $var = ' ';
44 $html.="<$td>$var</$td>";
47 if($type) $html.="</$type>";
51 function table(&$table, $params='border=1') {
52 $html="<table $params>";
54 foreach($table as $row) {
56 $html.=$this->row(array_keys($row),'thead');
59 $html.=$this->row($row);
65 function link($title='n/a', $link='#void', $internal=true, $translate=true) {
66 if($internal && (!isset($link[0]) ||
$link[0] != '#')) $link = $this->internal_url($link);
67 if($translate) $title = T($title);
68 return "<a href='$link'>".$title."</a>";
71 function img($src='#void', $title='img', $options='width=64') {
72 $options = $options ?
" $options" : '';
73 return "<img src='$src' alt='$title' title='$title'$options; />";
76 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') {
77 $html = T($prefix)."<input type='$type' ";
78 if($name) $html.= "name='$name' ";
79 if(!is_bool($value)) {
80 if($type == 'submit') $value = T($value);
81 $html.= "value='$value' ";
83 if($options) $html.= "$options ";
84 if($placeholder) $html.= "placeholder='$placeholder' ";
89 function form($action=false, $method=false, $inputs, $options=false) {
90 $action = $action ?
" action='$action'" : '';
91 $method = $method ?
" method='$method'" : '';
92 $options = $options ?
" $options" : '';
93 $html = "<form$action$method$options>";
94 foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input);
99 function select($name, $selectbox, $default=false) {
100 //echo('<pre>'); print_r($selectbox);
101 $html = "<select name='$name'>";
104 $value=$default; $title=$selectbox[$value];
105 $html .= "<option value='$value'>$value :: $title</option>";
106 unset($selectbox[$value]);
108 foreach($selectbox as $value => $title) {
109 $html .= "<option value='$value'>$value :: $title</option>";
111 $html .= "</select>";
115 function ul($items,$tag=ul
,$head='',$class=false) {
116 $class = $class ?
" class='$class'" : '';
117 $html = "$head<$tag$class>";
118 foreach($items as $key => $value) {
120 if(is_numeric($key)) {
123 $html .= $this->link($key,$value);
131 function div($html, $options) {
132 $options = $options ?
" $options" : '';
133 return "<div$options>$html</div>";
136 function head($title=false,$charset='UTF-8',$more='') {
137 $title = $title ?
"\n<title>$title</title>" : '';
139 $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more;
146 * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
148 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
150 * @package Sklad_HTML
151 * @author Tomas Mudrunka
153 class Sklad_HTML
extends HTML
{ //TODO: Split into few more methods
154 function header($title='', $user=array()) {
156 $script = $_SERVER['SCRIPT_NAME'];
157 $search = htmlspecialchars(@trim
($_GET['q']));
158 $message = strip_tags(@trim
($_GET['message']),'<a><b><u><i>');
159 $instance = INSTANCE_ID
!= '' ?
'/'.INSTANCE_ID
: '';
160 $user_id = htmlspecialchars($user['id']);
161 $user_gid = htmlspecialchars($user['gid']);
162 $user_name = htmlspecialchars($user['name']);
164 //$title = T($title); //TODO
166 $html = $this->head("SōkoMan$title");
168 <h1 style="display: inline;"><a href="$script/">SōkoMan</a><small>$instance$title</small></h1>
169 <div style="float:right; text-align:right;">
170 Logged in as <b>$user_name</b> [UID: <b>$user_id</b>; GID: <b>$user_gid</b>]<br />
174 <style type="text/css">
175 * { font-family: arial; }
176 td,body { background-color: white; }
177 table { background-color: orange; border: orange; }
178 a, a img { text-decoration:none; color: darkblue; border:none; }
179 li a, a:hover { text-decoration:underline; }
195 .menu, .menu * menu {
200 border: 1px solid orange;
205 .menu li:hover menu, .menu li:hover {
207 background-color: yellow;
216 foreach(scandir(DIR_ASSISTANTS
) as $item) {
217 if($item == '.' ||
$item == '..') continue;
218 $item = preg_replace('/\.inc\.php$/','',$item);
219 $assistants[$item] = "assistant/$item";
222 $tables=array('item','model','category','producer','vendor','room','status');
224 foreach($tables as $table) {
225 $listable[$table] = $table;
226 $insertable[$table] = "$table/new";
229 $html .= $this->ul(array(
231 'Logout' => '?logout',
232 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')),
233 1 => $this->ul($insertable,'menu',$this->link('New','#')),
234 2 => $this->ul($listable,'menu',$this->link('List','#'))
235 ),'menu', '', 'menu');
237 $html .= '<div style="float: right;">';
239 $html .= $this->form("$script/assistant/go", 'GET', array(
240 array('q','','text','smart id...', 'autofocus'),
241 array(false,'go','submit')
242 ), 'style="float: left;"');
244 $html .= $this->form('?', 'GET', array(
245 array('q',$search,'text','regexp...'),
246 array(false,'filter','submit')
247 ), 'style="float: left;"');
253 <hr style="clear: both;" />
254 <div style="background-color:#FFDDDD;">
255 <font color="red">$message</font>
262 function internal_url($link) {
263 return $_SERVER['SCRIPT_NAME'].'/'.$link;
266 function table_add_images(&$table) {
267 $image = array('model_id');
268 foreach($table as $id => $row) {
269 foreach($image as $column) if(isset($table[$id][$column])) {
270 $type = @array_shift
(preg_split('/_/', $column));
271 $src=URL_IMAGES
."/$type/".$table[$id][$column].'.jpg';
272 $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]);
277 function render_barcode($barcode,$opts=false) {
278 return $this->link($this->img($this->internal_url("barcode/$barcode"),$barcode,$opts),"barcode/$barcode",true,false);
281 function table_add_barcodes(&$table) {
282 $image = array('model_barcode', 'item_serial');
283 foreach($table as $id => $row) {
284 foreach($image as $column) if(isset($table[$id][$column])) {
285 $table[$id][$column]=$this->render_barcode($table[$id][$column]);
290 function table_add_relations(&$table, $class, $suffix_relations='_relations') {
291 $relations = array( //TODO: Autodetect???
292 'model' => array('model_id' => array('item')),
293 'category' => array('category_id' => array('item')),
294 'producer' => array('producer_id' => array('item')),
295 'vendor' => array('vendor_id' => array('item')),
296 'room' => array('room_id' => array('item')),
297 'status' => array('status_id' => array('item')),
299 foreach($table as $id => $row) {
300 foreach($row as $column => $value) {
301 if(isset($relations[$class][$column])) {
302 foreach($relations[$class][$column] as $destination) {
303 @$table[$id][$class.$suffix_relations] .= $this->link($destination, "$destination/?where[$column]==$value").',';
310 function table_collapse(&$table) {
312 'item_id' => 'item_id',
313 'model_id' => 'model_name',
314 'category_id' => 'category_name',
315 'producer_id' => 'producer_name',
316 'vendor_id' => 'vendor_name',
317 'room_id' => 'room_name',
318 'status_id' => 'status_name',
320 foreach($table as $id => $row) {
321 foreach($collapse as $link => $title)
322 if(isset($table[$id][$link]) && isset($row[$title])) {
323 $type = @array_shift
(preg_split('/_/', $link));
324 if($link != $title) unset($table[$id][$link]);
325 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
330 function table_sort(&$table) {
331 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
332 $table_sorted = array();
333 foreach($table as $id => $row) {
334 $table_sorted[$id] = array();
335 foreach($precedence as $column) if(isset($table[$id][$column])) {
336 $table_sorted[$id][T($column)]=$table[$id][$column];
337 unset($table[$id][$column]);
339 //$table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
340 foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating
342 $table = $table_sorted;
345 function render_item_table($table,$class=false) {
346 $this->table_add_images($table);
347 $this->table_add_barcodes($table);
348 if($class) $this->table_add_relations($table,$class);
349 $this->table_collapse($table);
350 $this->table_sort($table);
351 return $this->table($table);
354 function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
356 foreach($columns as $column) {
357 $html.=T($class).':<b>'.T($column['Field']).'</b>: ';
358 $name="values[$class][".$column['Field'].'][]';
359 $val = $update && isset($current[$column['Field']]) ?
$current[$column['Field']] : false;
361 case (preg_match('/auto_increment/', $column['Extra']) ||
in_array($column['Field'], $hidecols)):
363 $html.=$this->input($name, $val, 'hidden');
364 $html.=$val.'(AUTO)';
366 case isset($selectbox[$column['Field']]):
367 $html.=$this->select($name,$selectbox[$column['Field']],$val);
370 $html.=$this->input($name, $val);
378 function render_insert_form_multi($array) {
382 foreach($array as $key => $args) {
383 $parts=array('inputs');
384 if(!$head) { $head = true;
387 if(!isset($array[$key+
1])) {
390 } else $hr = '<hr />';
393 $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
399 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
402 //echo('<pre>'); print_r($selectbox);
403 //echo('<pre>'); print_r($current);
405 if(is_array($current)) {
407 $current = array_shift($current);
410 if(!is_array($hidecols)) $hidecols = array();
411 $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
413 if(!is_array($parts) ||
in_array('head', $parts)) {
414 $action = $action ?
" action='$action'" : false;
415 $html.="<form$action method='POST'>"; //TODO: use $this->form()
416 $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
419 if(!is_array($parts) ||
in_array('inputs', $parts))
420 $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update);
422 if(!is_array($parts) ||
in_array('foot', $parts)) {
423 $html .= '</div></span><br style="clear:both" />';
424 if($multi_insert) { //TODO, move to separate JS file
427 function duplicate_element(what, where) {
428 var node = document.getElementsByName(what)[0];
429 node.parentNode.appendChild(node.cloneNode(true));
432 <a href='#' onClick="duplicate_element('input_set')">+</a>
436 $btn = is_array($current) ?
'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
437 $html.=$this->input(false, $btn, 'submit');
445 * Trida poskytuje rozhrani k databazi skladu
448 * @author Tomas Mudrunka
450 class Sklad_DB
extends PDO
{
451 function __construct() {
452 $this->auth
= new Sklad_Auth();
455 DB_DSN
, DB_USER
, DB_PASS
,
456 array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8") //Force UTF8 for MySQL
460 function escape($str) {
461 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
464 function quote_identifier($str) {
465 return '`'.$this->escape($str).'`'; //TODO HACK
468 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
471 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
472 'model' => array('category', 'producer')
473 ); //TODO Autodetect using foreign keys?
474 $search_fields = array(
475 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name')
479 if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
482 $class = $this->escape($class);
485 $sql="SELECT * FROM `$class`\n";
487 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n";
490 $search = $this->quote($search);
491 if(!isset($search_fields[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get
493 foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
494 $where[] = "FALSE $sql_search";
495 } elseif($id) $where[] = "$class$suffix_id = $id";
496 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
498 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
500 if(!$order) $order = $class.$suffix_id;
501 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
502 $sql .= "ORDER BY $order\n";
505 $limit = $this->escape((int)$limit);
506 $offset = $this->escape((int)$offset);
507 $sql .= "LIMIT $offset,$limit\n";
513 function safe_query($sql, $fatal=true) {
514 $result = $this->query($sql);
516 $error = $this->errorInfo();
517 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>");
523 function translate_query_results($result) {
524 $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded
525 foreach($result as $key => $row) {
526 foreach($translate_cols as $col) if(isset($result[$key][$col])){
527 $result[$key][$col] = T($result[$key][$col]);
533 function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO
::FETCH_ASSOC
, $translate=true) {
534 $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags);
535 if($translate) $result = $this->translate_query_results($result);
540 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
541 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
542 $result = $this->safe_query_fetch($sql);
543 if(!$result ||
!is_array($indexed)) return $result;
545 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
549 function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
550 $class = $this->escape($class);
551 $sql = "SHOW COLUMNS FROM $class;";
552 $columns = $this->safe_query_fetch($sql);
553 /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
554 if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
559 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
561 foreach($columns as $column) {
562 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)!
563 if($class && $column['Field'] == $class.$suffix_id) continue;
564 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
565 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
567 $history = $this->contains_history($table) ?
" WHERE ${table}_valid_till=0" : '';
568 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
569 $result = $this->safe_query_fetch($sql, false);
570 if(!$result) continue;
571 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
573 //echo('<pre>'); print_r($selectbox);
574 return array_filter($selectbox, 'ksort');
577 function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
578 $history = $this->contains_history($table) ?
" AND ${table}_valid_till=0" : '';
579 $value=$this->quote($value);
580 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
581 $result = $this->safe_query_fetch($sql);
582 if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
585 function contains_history($table) {
586 $history_tables = array('item'); //TODO Autodetect
587 return in_array($table, $history_tables);
590 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
592 $history = $this->contains_history($table);
595 $table = $this->escape($table);
597 //Get list of POSTed columns
598 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
599 $columns = implode(',',$columns_array);
603 //echo('<pre>'); die(print_r($values));
606 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
607 if($history_update) {
608 $sql .= "UPDATE $table";
609 $sql .= " SET ${table}_valid_till=NOW()";
610 $sql .= " WHERE ${table}_valid_till=0 AND (";
612 foreach($values as $row) {
613 $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
621 //Insert into table (columns)
622 $sql .= "INSERT INTO $table ($columns) VALUES ";
624 //Values (a,b,c),(d,e,f)
626 foreach($values as $row) {
627 $row_quoted = array_map(array($this,'quote'), $row); //Check
629 foreach($row as $column => $value) {
631 case $table.'_valid_from':
632 $row_quoted[$column] = 'NOW()';
634 case $table.'_valid_till':
635 $row_quoted[$column] = '0';
637 case $table.'_author':
638 $row_quoted[$column] = $this->auth
->get_user_id();
639 //die($this->auth->get_user_id().'=USER');
644 $sql .= $comma.'('.implode(',',$row_quoted).')';
650 foreach($columns_array as $col) {
651 if($col == $table.'_id' ||
$col == $table.'_valid_till') continue;
652 $on_duplicate[] = "$col=VALUES($col)";
654 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
662 function insert_or_update($table, $values, $replace=true) {
663 $sql = $this->build_query_insert($table, $values, $replace);
664 $this->safe_query($sql);
665 return $this->lastInsertId();
668 function insert_or_update_multitab($values, $replace=true) {
670 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
674 function delete($table, $id, $suffix_id='_id') {
675 if($this->contains_history($table)) return false;
676 $key = $this->escape($table.$suffix_id);
677 $table = $this->escape($table);
678 $id = $this->quote($id);
679 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
684 * Trida poskytuje high-level rozhrani k databazi skladu
686 * @package Sklad_DB_Abstract
687 * @author Tomas Mudrunka
689 class Sklad_DB_Abstract
extends Sklad_DB
{
694 * Trida implementuje uzivatelske rozhrani skladu
697 * $sklad = new Sklad_UI();
698 * $sklad->process_http_request();
701 * @author Tomas Mudrunka
704 function __construct() {
705 $this->db
= new Sklad_DB();
706 $this->html
= new Sklad_HTML();
709 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
710 return $this->html
->render_item_table($this->db
->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
713 function render_form_add($class) {
714 $columns = $this->db
->get_columns($class);
715 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
716 return $this->html
->render_insert_form($class, $columns, $selectbox);
719 function render_form_edit($class, $id) {
720 $columns = $this->db
->get_columns($class);
721 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
722 $current = $this->db
->get_listing($class, $id, 1);
723 return $this->html
->render_insert_form($class, $columns, $selectbox, $current);
726 function render_single_record_details($class, $id) {
728 $id_prev = $id - 1 > 0 ?
$id - 1 : 0;
729 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
731 $html.= $this->html
->link('<<', "$class/$id_prev/");
733 $html.= $this->html
->link('>>', "$class/$id_next/");
735 $html.='<span style="float:right;">'.$this->html
->render_barcode(BARCODE_PREFIX
.strtoupper("$class/$id")).'</span>';
736 $html.= $this->html
->link('edit', "$class/$id/edit/");
737 if($this->db
->contains_history($class)) $html.= ' ][ '.$this->html
->link('history', "$class/$id/history/");
741 function render_listing_navigation($class, $id, $limit, $offset) {
742 $offset_next = $offset +
$limit;
743 $offset_prev = $offset - $limit > 0 ?
$offset - $limit : 0;
744 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
746 $html.= $this->html
->link('<<', "$class/$id/$limit/$offset_prev/$get");
748 $html.= $this->html
->link('>>', "$class/$id/$limit/$offset_next/$get");
750 $html.= $this->html
->link('new', "$class/new/$get");
754 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
756 if(is_numeric($id)) {
757 $html.=$this->render_single_record_details($class, $id);
759 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
762 $html.= $this->render_form_edit($class, $id);
763 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
764 $html.=$this->html
->form($action,'POST',array(
765 array(false,'DELETE','submit'),
766 array('sure', false, 'checkbox', false, false, 'sure?')
768 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
769 $html.=$this->html
->form($action,'POST',array(
770 array('image', false, 'file', false, 'size="30"'),
771 array(false, 'IMAGE', 'submit')
772 ), "enctype='multipart/form-data'");
777 function check_auth() {
778 new HTTP_Auth('SkladovejSystem', true, array($this->db
->auth
,'check_auth'));
781 function post_redirect_get($location, $message='', $error=false) {
782 $url_args = $message != '' ?
'?message='.urlencode(T($message)) : '';
783 $location = $this->html
->internal_url($location).$url_args;
784 header('Location: '.$location);
785 if($error) trigger_error($message);
786 $location=htmlspecialchars($location);
788 "<meta http-equiv='refresh' content='0; url=$location'>".
789 T($message)."<br />Location: <a href='$location'>$location</a>"
793 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
794 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
795 $filename="$dir/$name$ext";
796 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
797 foreach($vars as $var => $val) $
$var=$val;
800 $out=ob_get_contents();
805 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
806 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
807 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
811 /* Tenhle foreach() prekopiruje promenne
812 * z: $_POST['values'][$table][$column][$id];
813 * do: $values[$table][$id][$column]
815 if(isset($_POST['values'])) {
817 foreach($_POST['values'] as $table => $columns) {
818 foreach($columns as $column => $ids) {
819 foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
822 //die(print_r($values));
825 if($action) switch($action) {
829 if(!isset($replace)) $replace = true;
830 $table = $class ?
$class : 'item';
831 //print_r($values); //debug
832 $last = $this->db
->insert_or_update_multitab($values, $replace);
833 $last = $force_redirect ?
$force_redirect."?last=$last" : "$table/$last/";
834 $next = "$table/new/";
835 $message = $force_redirect ?
'' : 'Hotovo. Další záznam přidáte '.$this->html
->link('zde', $next).'.';
836 $this->post_redirect_get($last, $message);
839 if(!isset($_POST['sure']) ||
!$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
840 $this->db
->delete($class, $id) ||
$this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
841 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
844 $image_classes = array('model'); //TODO, use this more widely across the code
845 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
846 $image_destination = DIR_IMAGES
."/$class/$id.jpg";
847 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true);
848 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
849 chmod ($image_destination, 0664);
850 $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)');
851 } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true);
854 $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*');
861 function process_http_request() {
864 @ini_set
('magic_quotes_gpc' , 'off');
865 if(get_magic_quotes_gpc()) {
866 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
869 $PATH_INFO=@trim
($_SERVER[PATH_INFO
]);
870 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
872 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
873 if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost...
874 echo $this->html
->header($PATH_INFO,$this->db
->auth
->get_user());
875 switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
877 die('Tell me why you cry');
879 case 'assistant': //assistant
880 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ?
trim($PATH_CHUNKS[3]) : false;
881 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
882 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
883 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
884 $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
885 echo $this->safe_include(DIR_ASSISTANTS
,$PATH_CHUNKS[2],$assistant_vars);
887 case 'barcode': //barcode
888 Barcode
::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
891 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ?
trim($_GET['q']) : false;
892 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ?
$PATH_CHUNKS[1] : 'item';
893 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
894 switch($PATH_CHUNKS[2]) {
896 $this->process_http_request_post($PATH_CHUNKS[2], $class);
897 echo $this->render_form_add($class);
900 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ?
(int) $PATH_CHUNKS[2] : false);
901 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
903 switch($PATH_CHUNKS[3]) {
904 case 'edit': //?/?/edit
905 case 'image': //?/?/image
906 case 'delete': //?/?/delete
907 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
910 $history = $PATH_CHUNKS[3] == 'history' ?
true : false;
911 $limit = (int) (isset($PATH_CHUNKS[3]) ?
$PATH_CHUNKS[3] : '0');
912 $offset = (int) (isset($PATH_CHUNKS[4]) ?
$PATH_CHUNKS[4] : '0');
913 $where = @is_array
($_GET['where']) ?
$_GET['where'] : false;
914 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
915 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
916 //print_r(array("<pre>",$_SERVER));
926 $sklad = new Sklad_UI();
927 $sklad->process_http_request();
929 echo('<br style="clear:both;" /><hr />');
This page took 1.043467 seconds and 5 git commands to generate.