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 img_link($src, $link='#void', $title='img_link', $internal=true, $translate=true, $options='width=64') {
77 return $this->link($this->img($src,$title,$options),$link,$internal,$translate);
80 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') {
81 $html = T($prefix)."<input type='$type' ";
82 if($name) $html.= "name='$name' ";
83 if(!is_bool($value)) {
84 if($type == 'submit') $value = T($value);
85 $html.= "value='$value' ";
87 if($options) $html.= "$options ";
88 if($placeholder) $html.= "placeholder='$placeholder' ";
93 function form($action=false, $method=false, $inputs, $options=false) {
94 $action = $action ?
" action='$action'" : '';
95 $method = $method ?
" method='$method'" : '';
96 $options = $options ?
" $options" : '';
97 $html = "<form$action$method$options>";
98 foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input);
103 function select($name, $selectbox, $default=false) {
104 //echo('<pre>'); print_r($selectbox);
105 $html = "<select name='$name'>";
108 $value=$default; $title=$selectbox[$value];
109 $html .= "<option value='$value'>$value :: $title</option>";
110 unset($selectbox[$value]);
112 foreach($selectbox as $value => $title) {
113 $html .= "<option value='$value'>$value :: $title</option>";
115 $html .= "</select>";
119 function ul($items,$tag=ul
,$head='',$class=false) {
120 $class = $class ?
" class='$class'" : '';
121 $html = "$head<$tag$class>";
122 foreach($items as $key => $value) {
124 if(is_numeric($key)) {
127 $html .= $this->link($key,$value);
135 function div($html, $options) {
136 $options = $options ?
" $options" : '';
137 return "<div$options>$html</div>";
140 function head($title=false,$charset='UTF-8',$more='') {
141 $title = $title ?
"\n<title>$title</title>" : '';
143 $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more;
150 * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
152 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
154 * @package Sklad_HTML
155 * @author Tomas Mudrunka
157 class Sklad_HTML
extends HTML
{ //TODO: Split into few more methods
158 function header($title='', $user=array()) {
160 $script = $_SERVER['SCRIPT_NAME'];
161 $search = htmlspecialchars(@trim
($_GET['q']));
162 $message = strip_tags(@trim
($_GET['message']),'<a><b><u><i><br>');
163 $instance = INSTANCE_ID
!= '' ?
'/'.INSTANCE_ID
: '';
164 $user_id = htmlspecialchars($user['id']);
165 $user_gid = htmlspecialchars($user['gid']);
166 $user_name = htmlspecialchars($user['name']);
168 //$title = T($title); //TODO
170 $html = $this->head("SōkoMan$title");
172 <h1 style="display: inline;"><a href="$script/">SōkoMan</a><small>$instance$title</small></h1>
173 <div style="float:right; text-align:right;">
174 Logged in as <b>$user_name</b> [UID: <b>$user_id</b>; GID: <b>$user_gid</b>]<br />
178 <style type="text/css">
179 * { font-family: arial; }
180 td,body { background-color: white; }
181 table { background-color: orange; border: orange; }
182 a, a img { text-decoration:none; color: darkblue; border:none; }
183 li a, a:hover { text-decoration:underline; }
199 .menu, .menu * menu {
204 border: 1px solid orange;
209 .menu li:hover menu, .menu li:hover {
211 background-color: yellow;
220 foreach(scandir(DIR_ASSISTANTS
) as $item) {
221 if($item == '.' ||
$item == '..') continue;
222 $item = preg_replace('/\.inc\.php$/','',$item);
223 $assistants[$item] = "assistant/$item";
226 $tables=array('item','model','category','producer','vendor','room','status');
228 foreach($tables as $table) {
229 $listable[$table] = $table;
230 $insertable[$table] = "$table/new";
233 $html .= $this->ul(array(
235 'Logout' => '?logout',
236 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')),
237 1 => $this->ul($insertable,'menu',$this->link('New','#')),
238 2 => $this->ul($listable,'menu',$this->link('List','#'))
239 ),'menu', '', 'menu');
241 $html .= '<div style="float: right;">';
243 $html .= $this->form("$script/assistant/go", 'GET', array(
244 array('q','','text','smart id...', 'autofocus'),
245 array(false,'go','submit')
246 ), 'style="float: left;"');
248 $html .= $this->form('?', 'GET', array(
249 array('q',$search,'text','regexp...'),
250 array(false,'filter','submit')
251 ), 'style="float: left;"');
257 <hr style="clear: both;" />
258 <div style="background-color:#FFDDDD;">
259 <font color="red">$message</font>
266 function internal_url($link) {
267 return $_SERVER['SCRIPT_NAME'].'/'.$link;
270 function table_add_images(&$table) {
271 $image = array('model_id');
272 foreach($table as $id => $row) {
273 foreach($image as $column) if(isset($table[$id][$column])) {
274 $type = @array_shift
(preg_split('/_/', $column));
275 $src=URL_IMAGES
."/$type/".$table[$id][$column].'.jpg';
276 $table[$id][$type.'_image']=$this->img_link($src, $src, $table[$id][$column], false, false);
281 function render_barcode($barcode,$opts=false) {
282 return $this->img_link($this->internal_url("barcode/$barcode"),$this->internal_url("barcode/$barcode"),$barcode,false,false,$opts);
285 function table_add_barcodes(&$table) {
286 $image = array('model_barcode', 'item_serial');
287 foreach($table as $id => $row) {
288 foreach($image as $column) if(isset($table[$id][$column])) {
289 $table[$id][$column]=$this->render_barcode($table[$id][$column]);
294 function table_add_relations(&$table, $class, $suffix_relations='_relations') {
295 $where_url = '%d/?where[%c]==%v';
296 $relations = array( //TODO: Autodetect???
298 'model_id' => array(array('item',$where_url)),
299 'model_barcode' => array(array('store','assistant/%d?barcode=%v')),
300 'model_name' => array(array('google','http://google.com/search?q=%v',true))
303 'item_serial' => array(array('dispose','assistant/%d?serial=%v'),array('sell','assistant/%d?serial=%v'))
305 'category' => array('category_id' => array(array('item',$where_url))),
306 'producer' => array('producer_id' => array(array('item',$where_url))),
307 'vendor' => array('vendor_id' => array(array('item',$where_url))),
308 'room' => array('room_id' => array(array('item',$where_url))),
309 'status' => array('status_id' => array(array('item',$where_url)))
311 foreach($table as $id => $row) {
312 foreach($row as $column => $value) {
313 if(isset($relations[$class][$column])) {
314 foreach($relations[$class][$column] as $destination) {
315 $destination_url = str_replace(
316 array('%d','%c','%v'),
317 array(urlencode($destination[0]),urlencode($column),urlencode($value)),
320 @$table[$id][$class.$suffix_relations] .= $this->link($destination[0], $destination_url, !isset($destination[2])).',';
327 function table_collapse(&$table) {
329 'item_id' => 'item_id',
330 'model_id' => 'model_name',
331 'category_id' => 'category_name',
332 'producer_id' => 'producer_name',
333 'vendor_id' => 'vendor_name',
334 'room_id' => 'room_name',
335 'status_id' => 'status_name',
337 foreach($table as $id => $row) {
338 foreach($collapse as $link => $title)
339 if(isset($table[$id][$link]) && isset($row[$title])) {
340 $type = @array_shift
(preg_split('/_/', $link));
341 if($link != $title) unset($table[$id][$link]);
342 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
347 function table_sort(&$table) {
348 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
349 $table_sorted = array();
350 foreach($table as $id => $row) {
351 $table_sorted[$id] = array();
352 foreach($precedence as $column) if(isset($table[$id][$column])) {
353 $table_sorted[$id][T($column)]=$table[$id][$column];
354 unset($table[$id][$column]);
356 //$table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
357 foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating
359 $table = $table_sorted;
362 function render_item_table($table,$class=false) {
363 $this->table_add_images($table);
364 if($class) $this->table_add_relations($table,$class);
365 $this->table_add_barcodes($table);
366 $this->table_collapse($table);
367 $this->table_sort($table);
368 return $this->table($table);
371 function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
373 foreach($columns as $column) {
374 $html.=T($class).':<b>'.T($column['Field']).'</b>: ';
375 $name="values[$class][".$column['Field'].'][]';
376 $val = $update && isset($current[$column['Field']]) ?
$current[$column['Field']] : false;
378 case (preg_match('/auto_increment/', $column['Extra']) ||
in_array($column['Field'], $hidecols)):
380 $html.=$this->input($name, $val, 'hidden');
381 $html.=$val.'(AUTO)';
383 case isset($selectbox[$column['Field']]):
384 $html.=$this->select($name,$selectbox[$column['Field']],$val);
387 $html.=$this->input($name, $val);
395 function render_insert_form_multi($array) {
399 foreach($array as $key => $args) {
400 $parts=array('inputs');
401 if(!$head) { $head = true;
404 if(!isset($array[$key+
1])) {
407 } else $hr = '<hr />';
410 $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
416 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
419 //echo('<pre>'); print_r($selectbox);
420 //echo('<pre>'); print_r($current);
422 if(is_array($current)) {
424 $current = array_shift($current);
427 if(!is_array($hidecols)) $hidecols = array();
428 $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
430 if(!is_array($parts) ||
in_array('head', $parts)) {
431 $action = $action ?
" action='$action'" : false;
432 $html.="<form$action method='POST'>"; //TODO: use $this->form()
433 $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
436 if(!is_array($parts) ||
in_array('inputs', $parts))
437 $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update);
439 if(!is_array($parts) ||
in_array('foot', $parts)) {
440 $html .= '</div></span><br style="clear:both" />';
441 if($multi_insert) { //TODO, move to separate JS file
444 function duplicate_element(what, where) {
445 var node = document.getElementsByName(what)[0];
446 node.parentNode.appendChild(node.cloneNode(true));
449 <a href='#' onClick="duplicate_element('input_set')">+</a>
453 $btn = is_array($current) ?
'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
454 $html.=$this->input(false, $btn, 'submit');
462 * Trida poskytuje rozhrani k databazi skladu
465 * @author Tomas Mudrunka
467 class Sklad_DB
extends PDO
{
468 function __construct() {
469 $this->auth
= new Sklad_Auth();
472 DB_DSN
, DB_USER
, DB_PASS
,
473 array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8") //Force UTF8 for MySQL
477 function escape($str) {
478 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
481 function quote_identifier($str) {
482 return '`'.$this->escape($str).'`'; //TODO HACK
485 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
488 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
489 'model' => array('category', 'producer')
490 ); //TODO Autodetect using foreign keys?
491 $search_fields = array(
492 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name')
496 if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
499 $class = $this->escape($class);
502 $sql="SELECT * FROM `$class`\n";
504 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n";
507 $search = $this->quote($search);
508 if(!isset($search_fields[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get
510 foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
511 $where[] = "FALSE $sql_search";
512 } elseif($id) $where[] = "$class$suffix_id = $id";
513 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
515 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
517 if(!$order) $order = $class.$suffix_id;
518 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
519 $sql .= "ORDER BY $order\n";
522 $limit = $this->escape((int)$limit);
523 $offset = $this->escape((int)$offset);
524 $sql .= "LIMIT $offset,$limit\n";
530 function safe_query($sql, $fatal=true) {
531 $result = $this->query($sql);
533 $error = $this->errorInfo();
534 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>");
540 function translate_query_results($result) {
541 $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded
542 foreach($result as $key => $row) {
543 foreach($translate_cols as $col) if(isset($result[$key][$col])){
544 $result[$key][$col] = T($result[$key][$col]);
550 function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO
::FETCH_ASSOC
, $translate=true) {
551 $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags);
552 if($translate) $result = $this->translate_query_results($result);
557 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
558 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
559 $result = $this->safe_query_fetch($sql);
560 if(!$result ||
!is_array($indexed)) return $result;
562 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
566 function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
567 $class = $this->escape($class);
568 $sql = "SHOW COLUMNS FROM $class;";
569 $columns = $this->safe_query_fetch($sql);
570 /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
571 if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
576 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
578 foreach($columns as $column) {
579 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)!
580 if($class && $column['Field'] == $class.$suffix_id) continue;
581 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
582 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
584 $history = $this->contains_history($table) ?
" WHERE ${table}_valid_till=0" : '';
585 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
586 $result = $this->safe_query_fetch($sql, false);
587 if(!$result) continue;
588 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
590 //echo('<pre>'); print_r($selectbox);
591 return array_filter($selectbox, 'ksort');
594 function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
595 $history = $this->contains_history($table) ?
" AND ${table}_valid_till=0" : '';
596 $value=$this->quote($value);
597 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
598 $result = $this->safe_query_fetch($sql);
599 if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
602 function contains_history($table) {
603 $history_tables = array('item'); //TODO Autodetect
604 return in_array($table, $history_tables);
607 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
609 $history = $this->contains_history($table);
612 $table = $this->escape($table);
614 //Get list of POSTed columns
615 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
616 $columns = implode(',',$columns_array);
620 //echo('<pre>'); die(print_r($values));
623 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
624 if($history_update) {
625 $sql .= "UPDATE $table";
626 $sql .= " SET ${table}_valid_till=NOW()";
627 $sql .= " WHERE ${table}_valid_till=0 AND (";
629 foreach($values as $row) {
630 $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
638 //Insert into table (columns)
639 $sql .= "INSERT INTO $table ($columns) VALUES ";
641 //Values (a,b,c),(d,e,f)
643 foreach($values as $row) {
644 $row_quoted = array_map(array($this,'quote'), $row); //Check
646 foreach($row as $column => $value) {
648 case $table.'_valid_from':
649 $row_quoted[$column] = 'NOW()';
651 case $table.'_valid_till':
652 $row_quoted[$column] = '0';
654 case $table.'_author':
655 $row_quoted[$column] = $this->auth
->get_user_id();
656 //die($this->auth->get_user_id().'=USER');
661 $sql .= $comma.'('.implode(',',$row_quoted).')';
667 foreach($columns_array as $col) {
668 if($col == $table.'_id' ||
$col == $table.'_valid_till') continue;
669 $on_duplicate[] = "$col=VALUES($col)";
671 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
679 function insert_or_update($table, $values, $replace=true) {
680 $sql = $this->build_query_insert($table, $values, $replace);
681 $this->safe_query($sql);
682 return $this->lastInsertId();
685 function insert_or_update_multitab($values, $replace=true) {
687 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
691 function delete($table, $id, $suffix_id='_id') {
692 if($this->contains_history($table)) return false;
693 $key = $this->escape($table.$suffix_id);
694 $table = $this->escape($table);
695 $id = $this->quote($id);
696 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
701 * Trida poskytuje high-level rozhrani k databazi skladu
703 * @package Sklad_DB_Abstract
704 * @author Tomas Mudrunka
706 class Sklad_DB_Abstract
extends Sklad_DB
{
711 * Trida implementuje uzivatelske rozhrani skladu
714 * $sklad = new Sklad_UI();
715 * $sklad->process_http_request();
718 * @author Tomas Mudrunka
721 function __construct() {
722 $this->db
= new Sklad_DB();
723 $this->html
= new Sklad_HTML();
726 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
727 return $this->html
->render_item_table($this->db
->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
730 function render_form_add($class) {
731 $columns = $this->db
->get_columns($class);
732 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
733 return $this->html
->render_insert_form($class, $columns, $selectbox);
736 function render_form_edit($class, $id) {
737 $columns = $this->db
->get_columns($class);
738 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
739 $current = $this->db
->get_listing($class, $id, 1);
740 return $this->html
->render_insert_form($class, $columns, $selectbox, $current);
743 function render_single_record_details($class, $id) {
745 $id_prev = $id - 1 > 0 ?
$id - 1 : 0;
746 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
748 $html.= $this->html
->link('<<', "$class/$id_prev/");
750 $html.= $this->html
->link('>>', "$class/$id_next/");
752 $html.='<span style="float:right;">'.$this->html
->render_barcode(BARCODE_PREFIX
.strtoupper("$class/$id")).'</span>';
753 $html.= $this->html
->link('edit', "$class/$id/edit/");
754 if($this->db
->contains_history($class)) $html.= ' ][ '.$this->html
->link('history', "$class/$id/history/");
758 function render_listing_navigation($class, $id, $limit, $offset) {
759 $offset_next = $offset +
$limit;
760 $offset_prev = $offset - $limit > 0 ?
$offset - $limit : 0;
761 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
763 $html.= $this->html
->link('<<', "$class/$id/$limit/$offset_prev/$get");
765 $html.= $this->html
->link('>>', "$class/$id/$limit/$offset_next/$get");
767 $html.= $this->html
->link('new', "$class/new/$get");
771 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
773 if(is_numeric($id)) {
774 $html.=$this->render_single_record_details($class, $id);
776 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
779 $html.= $this->render_form_edit($class, $id);
780 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
781 $html.=$this->html
->form($action,'POST',array(
782 array(false,'DELETE','submit'),
783 array('sure', false, 'checkbox', false, false, 'sure?')
785 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
786 $html.=$this->html
->form($action,'POST',array(
787 array('image', false, 'file', false, 'size="30"'),
788 array(false, 'IMAGE', 'submit')
789 ), "enctype='multipart/form-data'");
794 function check_auth() {
795 new HTTP_Auth('SkladovejSystem', true, array($this->db
->auth
,'check_auth'));
798 function post_redirect_get($location, $message='', $error=false, $translate=true) {
799 $messaget = $translate ?
T($message) : $message;
800 $url_args = $messaget != '' ?
'?message='.urlencode($messaget) : '';
801 $location = $this->html
->internal_url($location).$url_args;
802 header('Location: '.$location);
803 if($error) trigger_error($message);
804 $location=htmlspecialchars($location);
806 "<meta http-equiv='refresh' content='0; url=$location'>".
807 $messaget."<br />Location: <a href='$location'>$location</a>"
811 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
812 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
813 $filename="$dir/$name$ext";
814 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
815 foreach($vars as $var => $val) $
$var=$val;
818 $out=ob_get_contents();
823 function check_input_validity($field, $value='', $ruleset=0) {
824 $rules = array(0 => array(
825 'model_barcode' => '/./',
826 'item_serial' => '/./'
828 if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false;
832 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
833 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
834 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
838 /* Tenhle foreach() prekopiruje promenne
839 * z: $_POST['values'][$table][$column][$id];
840 * do: $values[$table][$id][$column]
842 if(isset($_POST['values'])) {
844 foreach($_POST['values'] as $table => $columns) {
845 foreach($columns as $column => $ids) {
846 foreach($ids as $id => $val) {
847 $values[$table][$id][$column] = trim($val);
848 if(!$this->check_input_validity($column,$val)) {
849 $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS
850 $this->html
->link('GO BACK', 'javascript:history.back()', false, false);
851 $this->post_redirect_get('', $message, false, false);
856 //die(print_r($values));
859 if($action) switch($action) {
863 if(!isset($replace)) $replace = true;
864 $table = $class ?
$class : 'item';
865 //print_r($values); //debug
866 $last = $this->db
->insert_or_update_multitab($values, $replace);
867 $last = $force_redirect ?
$force_redirect."?last=$last" : "$table/$last/";
868 $next = "$table/new/";
869 $message = $force_redirect ?
'' : 'Hotovo. Další záznam přidáte '.$this->html
->link('zde', $next).'.';
870 $this->post_redirect_get($last, $message);
873 if(!isset($_POST['sure']) ||
!$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
874 $this->db
->delete($class, $id) ||
$this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
875 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
878 $image_classes = array('model'); //TODO, use this more widely across the code
879 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
880 $image_destination = DIR_IMAGES
."/$class/$id.jpg";
881 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true);
882 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
883 chmod ($image_destination, 0664);
884 $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)');
885 } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true);
888 $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*');
895 function process_http_request() {
898 @ini_set
('magic_quotes_gpc' , 'off');
899 if(get_magic_quotes_gpc()) {
900 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
903 $PATH_INFO=@trim
($_SERVER[PATH_INFO
]);
904 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
906 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
907 if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost...
908 echo $this->html
->header($PATH_INFO,$this->db
->auth
->get_user());
909 switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
911 die('Tell me why you cry');
913 case 'assistant': //assistant
914 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ?
trim($PATH_CHUNKS[3]) : false;
915 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
916 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
917 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
918 $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
919 echo $this->safe_include(DIR_ASSISTANTS
,$PATH_CHUNKS[2],$assistant_vars);
921 case 'barcode': //barcode
922 Barcode
::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
925 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ?
trim($_GET['q']) : false;
926 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ?
$PATH_CHUNKS[1] : 'item';
927 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
928 switch($PATH_CHUNKS[2]) {
930 $this->process_http_request_post($PATH_CHUNKS[2], $class);
931 echo $this->render_form_add($class);
934 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ?
(int) $PATH_CHUNKS[2] : false);
935 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
937 switch($PATH_CHUNKS[3]) {
938 case 'edit': //?/?/edit
939 case 'image': //?/?/image
940 case 'delete': //?/?/delete
941 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
944 $history = $PATH_CHUNKS[3] == 'history' ?
true : false;
945 $limit = (int) (isset($PATH_CHUNKS[3]) ?
$PATH_CHUNKS[3] : '0');
946 $offset = (int) (isset($PATH_CHUNKS[4]) ?
$PATH_CHUNKS[4] : '0');
947 $where = @is_array
($_GET['where']) ?
$_GET['where'] : false;
948 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
949 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
950 //print_r(array("<pre>",$_SERVER));
960 $sklad = new Sklad_UI();
961 $sklad->process_http_request();
963 echo('<br style="clear:both;" /><hr />');
This page took 1.056226 seconds and 5 git commands to generate.