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,$class=false) {
39 $class = $class ?
$class=" class='$class' " : '';
40 if($type) $html.="<$type>";
42 $td = $type == 'thead' ?
'th' : 'td';
43 foreach($row as $var) {
44 if(trim($var) == '') $var = ' ';
45 $html.="<$td>$var</$td>";
48 if($type) $html.="</$type>";
52 function table(&$table, $parity_class=array('tr_odd','tr_even'), $params='border=1') {
53 $html="<table $params>";
56 foreach($table as $row) {
58 $html.=$this->row(array_keys($row),'thead');
61 $class = $parity_class ?
$parity_class[$even] : false;
62 $html.=$this->row($row,false,$class);
69 function link($title='n/a', $link='#void', $internal=true, $translate=true) {
70 if($internal && (!isset($link[0]) ||
$link[0] != '#')) $link = $this->internal_url($link);
71 if($translate) $title = T($title);
72 return "<a href='$link'>".$title."</a>";
75 function img($src='#void', $title='img', $options='width=64') {
76 $options = $options ?
" $options" : '';
77 return "<img src='$src' alt='$title' title='$title'$options; />";
80 function img_link($src, $link='#void', $title='img_link', $internal=true, $translate=true, $options='width=64') {
81 return $this->link($this->img($src,$title,$options),$link,$internal,$translate);
84 function textarea($name=false, $value='', $placeholder=false, $options=false, $prefix='') {
85 $html = T($prefix)."<textarea";
86 if($name) $html.= " name='$name'";
87 if($options) $html.= " $options";
88 if($placeholder) $html.= " placeholder='$placeholder'";
89 $html .= ">$value</textarea>";
93 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') {
94 if($type == 'textarea') return $this->textarea($name, $value, $placeholder, $options, $prefix);
95 $html = T($prefix)."<input type='$type' ";
96 if($name) $html.= "name='$name' ";
97 if(!is_bool($value)) {
98 if($type == 'submit') $value = T($value);
99 $html.= "value='$value' ";
101 if($options) $html.= "$options ";
102 if($placeholder) $html.= "placeholder='$placeholder' ";
107 function form($action=false, $method=false, $inputs, $options=false) {
108 $action = $action ?
" action='$action'" : '';
109 $method = $method ?
" method='$method'" : '';
110 $options = $options ?
" $options" : '';
111 $html = "<form$action$method$options>";
112 foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input);
117 function select($name, $selectbox, $default=false) {
118 //echo('<pre>'); print_r($selectbox);
119 $html = "<select name='$name'>";
122 $value=$default; $title=$selectbox[$value];
123 $html .= "<option value='$value'>$value :: $title</option>";
124 unset($selectbox[$value]);
126 foreach($selectbox as $value => $title) {
127 $html .= "<option value='$value'>$value :: $title</option>";
129 $html .= "</select>";
133 function ul($items,$tag=ul
,$head='',$class=false) {
134 $class = $class ?
" class='$class'" : '';
135 $html = "$head<$tag$class>";
136 foreach($items as $key => $value) {
138 if(is_numeric($key)) {
141 $html .= $this->link($key,$value);
149 function div($html, $options) {
150 $options = $options ?
" $options" : '';
151 return "<div$options>$html</div>";
154 function favicon($url='/favicon.ico') {
155 return '<link rel="shortcut icon" href="'.$url.'" /><link href="'.$url.'" rel="icon" type="image/gif" />';
159 function head($title=false,$charset='UTF-8',$more='') {
160 $title = $title ?
"\n<title>$title</title>" : '';
162 $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more;
163 $html.= $this->favicon(dirname($_SERVER['SCRIPT_NAME']).'/favicon.ico');
170 * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
172 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
174 * @package Sklad_HTML
175 * @author Tomas Mudrunka
177 class Sklad_HTML
extends HTML
{ //TODO: Split into few more methods
178 function header($title='', $user=array()) {
180 $script = $_SERVER['SCRIPT_NAME'];
181 $search = htmlspecialchars(@trim
($_GET['q']));
182 $message = strip_tags(@trim
($_GET['message']),'<a><b><u><i><br>');
184 $instance = INSTANCE_ID
!= '' ?
'/'.INSTANCE_ID
: '';
185 $user_id = htmlspecialchars($user['id']);
186 $user_gid = htmlspecialchars($user['gid']);
187 $user_name = htmlspecialchars($user['name']);
189 //$title = T($title); //TODO
191 $html = $this->head("SōkoMan$title");
193 <h1 style="display: inline;"><a href="$script/">SōkoMan</a><small>$instance$title</small></h1>
194 <div style="float:right; text-align:right;">
195 Logged in as <b>$user_name</b> [UID: <b>$user_id</b>; GID: <b>$user_gid</b>]<br />
199 <style type="text/css">
200 * { font-family: arial; }
201 td,body { background-color: white; }
202 table { background-color: orange; border: orange; }
203 a, a img { text-decoration:none; color: darkblue; border:none; }
204 li a, a:hover { text-decoration:underline; }
205 .tr_even td { background-color: lemonchiffon; }
221 .menu, .menu * menu {
226 border: 1px solid orange;
231 .menu li:hover menu, .menu li:hover {
233 background-color: yellow;
242 foreach(scandir(DIR_ASSISTANTS
) as $item) {
243 if($item == '.' ||
$item == '..') continue;
244 $item = preg_replace('/\.inc\.php$/','',$item,-1,$count);
245 if($count) $assistants[$item] = "assistant/$item";
248 $tables=array('item','model','category','producer','vendor','room','status');
250 foreach($tables as $table) {
251 $listable[$table] = $table;
252 $insertable[$table] = "$table/new";
255 $html .= $this->ul(array(
257 'Logout' => '?logout',
258 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')),
259 1 => $this->ul($insertable,'menu',$this->link('New','#')),
260 2 => $this->ul($listable,'menu',$this->link('List','#'))
261 ),'menu', '', 'menu');
263 $html .= '<div style="float: right;">';
265 $html .= $this->form("$script/api/go", 'GET', array(
266 array('q','','text','smart id...', 'autofocus'),
267 array(false,'go','submit')
268 ), 'style="float: left;"');
270 $html .= $this->form('?', 'GET', array(
271 array('q',$search,'text','regexp...'),
272 array(false,'filter','submit')
273 ), 'style="float: left;"');
279 <hr style="clear: both;" />
280 <div style="background-color:#FFDDDD;">
281 <font color="red">$message</font>
283 <div style="text-align:right;">
291 function internal_url($link) {
292 return $_SERVER['SCRIPT_NAME'].'/'.$link;
295 function table_add_images(&$table) {
296 $image = array('model_id');
297 foreach($table as $id => $row) {
298 foreach($image as $column) if(isset($table[$id][$column])) {
299 $type = @array_shift
(preg_split('/_/', $column));
300 $src=URL_IMAGES
."/$type/".$table[$id][$column].'.jpg';
301 $table[$id][$type.'_image']=$this->img_link($src, $src, $table[$id][$column], false, false);
306 function render_barcode($barcode,$opts=false) {
307 return $this->img_link($this->internal_url("barcode/$barcode"),$this->internal_url("barcode/$barcode"),$barcode,false,false,$opts);
310 function table_add_barcodes(&$table) {
311 $image = array('model_barcode', 'item_serial');
312 foreach($table as $id => $row) {
313 foreach($image as $column) if(isset($table[$id][$column])) {
314 $table[$id][$column]=$this->render_barcode($table[$id][$column]);
319 function table_add_relations(&$table, $class, $suffix_relations='_relations') {
320 $where_url = '%d/?where[%c]==%v';
321 $relations = array( //TODO: Autodetect???
323 'model_id' => array(array('item',$where_url)),
324 'model_barcode' => array(array('store','assistant/%d?barcode=%v')),
325 'model_name' => array(array('google','http://google.com/search?q=%v',true)) //TODO: add manufacturer to google query
328 'item_serial' => array(array('dispose','assistant/%d?serial=%v'),array('sell','assistant/%d?serial=%v'))
330 'category' => array('category_id' => array(array('item',$where_url), array('model',$where_url))),
331 'producer' => array('producer_id' => array(array('item',$where_url), array('model',$where_url))),
332 'vendor' => array('vendor_id' => array(array('item',$where_url))),
333 'room' => array('room_id' => array(array('item',$where_url))),
334 'status' => array('status_id' => array(array('item',$where_url)))
336 foreach($table as $id => $row) {
337 foreach($row as $column => $value) {
338 if(isset($relations[$class][$column])) {
339 foreach($relations[$class][$column] as $destination) {
340 $destination_url = str_replace(
341 array('%d','%c','%v'),
342 array(urlencode($destination[0]),urlencode($column),urlencode($value)),
345 @$table[$id][$class.$suffix_relations] .= $this->link($destination[0], $destination_url, !isset($destination[2])).',';
352 function table_collapse(&$table) {
354 'item_id' => 'item_id',
355 'model_id' => 'model_name',
356 'category_id' => 'category_name',
357 'producer_id' => 'producer_name',
358 'vendor_id' => 'vendor_name',
359 'room_id' => 'room_name',
360 'status_id' => 'status_name',
362 foreach($table as $id => $row) {
363 foreach($collapse as $link => $title)
364 if(isset($table[$id][$link]) && isset($row[$title])) {
365 $type = @array_shift
(preg_split('/_/', $link));
366 if($link != $title) unset($table[$id][$link]);
367 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
372 function table_sort(&$table) {
373 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
374 $table_sorted = array();
375 foreach($table as $id => $row) {
376 $table_sorted[$id] = array();
377 foreach($precedence as $column) if(isset($table[$id][$column])) {
378 $table_sorted[$id][T($column)]=$table[$id][$column];
379 unset($table[$id][$column]);
381 //$table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
382 foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating
384 $table = $table_sorted;
387 function table_hide_columns(&$table, $class) { //TODO: Move to build_query_select() !!! :-)))
388 $fields_hide = array(
389 'item' => array('model_descript','model_price_in','model_price_out','model_barcode','model_countable','model_reserve','model_eshop_hide','room_descript','room_author','producer_name','producer_note','vendor_note')
391 //print_r($table); die();
392 if(isset($fields_hide[$class])) foreach($table as $id => $row) {
393 foreach($fields_hide[$class] as $field) unset($table[$id][$field]);
397 function render_item_table($table,$class=false) {
398 if(empty($table)) return '<h3>'.T('holy primordial emptiness is all you can find here...').'</h3><br />';
399 $this->table_add_images($table);
400 if($class) $this->table_add_relations($table,$class);
401 $this->table_add_barcodes($table);
402 $this->table_collapse($table);
403 if($class) $this->table_hide_columns($table,$class);
404 $this->table_sort($table);
405 return $this->table($table);
408 function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
410 'item' => array('item_note'),
411 'model' => array('model_descript')
414 foreach($columns as $column) {
415 $html.=T($class).':<b>'.T($column['Field']).'</b>: ';
416 $name="values[$class][".$column['Field'].'][]';
417 $val = $update && isset($current[$column['Field']]) ?
$current[$column['Field']] : false;
419 case (preg_match('/auto_increment/', $column['Extra']) ||
in_array($column['Field'], $hidecols)):
421 $html.=$this->input($name, $val, 'hidden');
422 $html.=$val.'(AUTO)';
424 case isset($selectbox[$column['Field']]):
425 $html.=$this->select($name,$selectbox[$column['Field']],$val);
427 case isset($textarea[$class]) && in_array($column['Field'],$textarea[$class]):
428 $html.=$this->input($name, $val, 'textarea');
431 $html.=$this->input($name, $val);
439 function render_insert_form_multi($array) {
443 foreach($array as $key => $args) {
444 $parts=array('inputs');
445 if(!$head) { $head = true;
448 if(!isset($array[$key+
1])) {
451 } else $hr = '<hr />';
455 $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
461 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
464 //echo('<pre>'); print_r($selectbox);
465 //echo('<pre>'); print_r($current);
467 if(is_array($current)) {
469 $current = array_shift($current);
472 if(!is_array($hidecols)) $hidecols = array();
473 $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
475 if(!is_array($parts) ||
in_array('head', $parts)) {
476 $action = $action ?
" action='$action'" : false;
477 $html.="<form$action method='POST'>"; //TODO: use $this->form()
478 $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
481 if(!is_array($parts) ||
in_array('inputs', $parts))
482 $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update);
484 if(!is_array($parts) ||
in_array('foot', $parts)) {
485 $html .= '</div></span><br style="clear:both" />';
486 if($multi_insert) { //TODO, move to separate JS file
489 function duplicate_element(what, where) {
490 var node = document.getElementsByName(what)[0];
491 node.parentNode.appendChild(node.cloneNode(true));
494 <a href='#' onClick="duplicate_element('input_set')">+</a>
498 $btn = is_array($current) ?
'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
499 $html.=$this->input(false, $btn, 'submit');
507 * Trida poskytuje rozhrani k databazi skladu
510 * @author Tomas Mudrunka
512 class Sklad_DB
extends PDO
{
513 function __construct() {
514 $this->auth
= new Sklad_Auth();
517 DB_DSN
, DB_USER
, DB_PASS
,
518 array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8") //Force UTF8 for MySQL
522 function escape($str) {
523 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
526 function quote_identifier($str) {
527 return '`'.$this->escape($str).'`'; //TODO HACK
530 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
533 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
534 'model' => array('category', 'producer')
535 ); //TODO Autodetect using foreign keys?
536 $fields_search = array(
537 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name'),
538 'model' => array('model_id','model_name','model_barcode','model_descript','producer_name')
542 if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
545 $class = $this->escape($class);
548 $sql="SELECT * FROM `$class`\n";
550 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n";
553 $search = $this->quote($search);
554 if(!isset($fields_search[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get
556 foreach($fields_search[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
557 $where[] = "FALSE $sql_search";
558 } elseif($id) $where[] = "$class$suffix_id = $id";
559 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
561 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
563 if(!$order) $order = $class.$suffix_id.' DESC';
564 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
565 $sql .= "ORDER BY $order\n";
568 $limit = $this->escape((int)$limit);
569 $offset = $this->escape((int)$offset);
570 $sql .= "LIMIT $offset,$limit\n";
576 function safe_query($sql, $fatal=true) {
577 $result = $this->query($sql);
579 $error = $this->errorInfo();
580 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>");
586 function translate_query_results($result) {
587 $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded
588 foreach($result as $key => $row) {
589 foreach($translate_cols as $col) if(isset($result[$key][$col])){
590 $result[$key][$col] = T($result[$key][$col]);
596 function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO
::FETCH_ASSOC
, $translate=true) {
597 $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags);
598 if($translate) $result = $this->translate_query_results($result);
603 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
604 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
605 $result = $this->safe_query_fetch($sql);
606 if(!$result ||
!is_array($indexed)) return $result;
608 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
612 function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
613 $class = $this->escape($class);
614 $sql = "SHOW COLUMNS FROM $class;";
615 $columns = $this->safe_query_fetch($sql);
616 /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
617 if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
622 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
623 $selectbox=array( //TODO: Hardcoded...
624 'model_countable' => array(0 => 'no', 1 => 'yes'),
625 'model_eshop_hide' => array(0 => 'no', 1 => 'yes'),
626 'vendor_id' => array('COMPULSORY' => 'select...')
628 foreach($columns as $column) {
629 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)!
630 if($class && $column['Field'] == $class.$suffix_id) continue;
631 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
632 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
634 $history = $this->contains_history($table) ?
" WHERE ${table}_valid_till=0" : '';
635 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
636 $result = $this->safe_query_fetch($sql, false);
637 if(!$result) continue;
638 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
640 //echo('<pre>'); print_r($selectbox);
641 return array_filter($selectbox, 'ksort');
644 function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
645 $history = $this->contains_history($table) ?
" AND ${table}_valid_till=0" : '';
646 $value=$this->quote($value);
647 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
648 $result = $this->safe_query_fetch($sql);
649 if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
652 function contains_history($table) {
653 $history_tables = array('item'); //TODO Autodetect
654 return in_array($table, $history_tables);
657 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
659 $history = $this->contains_history($table);
662 $table = $this->escape($table);
664 //Get list of POSTed columns
665 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
666 $columns = implode(',',$columns_array);
670 //echo('<pre>'); die(print_r($values));
673 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
674 if($history_update) {
675 $sql .= "UPDATE $table";
676 $sql .= " SET ${table}_valid_till=NOW()";
677 $sql .= " WHERE ${table}_valid_till=0 AND (";
679 foreach($values as $row) {
680 $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
688 //Insert into table (columns)
689 $sql .= "INSERT INTO $table ($columns) VALUES ";
691 //Values (a,b,c),(d,e,f)
693 foreach($values as $row) {
694 $row_quoted = array_map(array($this,'quote'), $row); //Check
696 foreach($row as $column => $value) {
698 case $table.'_valid_from':
699 $row_quoted[$column] = 'NOW()';
701 case $table.'_valid_till':
702 $row_quoted[$column] = '0';
704 case $table.'_author':
705 $row_quoted[$column] = $this->auth
->get_user_id();
706 //die($this->auth->get_user_id().'=USER');
711 $sql .= $comma.'('.implode(',',$row_quoted).')';
717 foreach($columns_array as $col) {
718 if($col == $table.'_id' ||
$col == $table.'_valid_till') continue;
719 $on_duplicate[] = "$col=VALUES($col)";
721 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
729 function insert_or_update($table, $values, $replace=true) {
730 $sql = $this->build_query_insert($table, $values, $replace);
731 $this->safe_query($sql);
732 return $this->lastInsertId();
735 function insert_or_update_multitab($values, $replace=true) {
737 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
741 function delete($table, $id, $suffix_id='_id') {
742 if($this->contains_history($table)) return false;
743 $key = $this->escape($table.$suffix_id);
744 $table = $this->escape($table);
745 $id = $this->quote($id);
746 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
751 * Trida poskytuje high-level rozhrani k databazi skladu
753 * @package Sklad_DB_Abstract
754 * @author Tomas Mudrunka
756 class Sklad_DB_Abstract
extends Sklad_DB
{
761 * Trida implementuje uzivatelske rozhrani skladu
764 * $sklad = new Sklad_UI();
765 * $sklad->process_http_request();
768 * @author Tomas Mudrunka
771 function __construct() {
772 $this->db
= new Sklad_DB();
773 $this->html
= new Sklad_HTML();
776 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
777 return $this->html
->render_item_table($this->db
->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
780 function render_form_add($class) {
781 $columns = $this->db
->get_columns($class);
782 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
783 return $this->html
->render_insert_form($class, $columns, $selectbox);
786 function render_form_edit($class, $id, $multi_insert) {
787 $columns = $this->db
->get_columns($class);
788 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
789 $current = $this->db
->get_listing($class, $id, 1);
790 return $this->html
->render_insert_form($class, $columns, $selectbox, $current, false, false, $multi_insert);
793 function render_single_record_details($class, $id) {
795 $id_prev = $id - 1 > 0 ?
$id - 1 : 0;
796 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
798 $html.= $this->html
->link('<<', "$class/$id_prev/");
800 $html.= $this->html
->link('>>', "$class/$id_next/");
802 $html.='<span style="float:right;">'.$this->html
->render_barcode(BARCODE_PREFIX
.strtoupper("$class/$id")).'</span>';
803 $html.= $this->html
->link('edit', "$class/$id/edit/");
804 if($this->db
->contains_history($class)) $html.= ' ][ '.$this->html
->link('history', "$class/$id/history/");
808 function render_listing_navigation($class, $id, $limit, $offset) {
809 $offset_next = $offset +
$limit;
810 $offset_prev = $offset - $limit > 0 ?
$offset - $limit : 0;
811 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
813 $html.= $this->html
->link('<<', "$class/$id/$limit/$offset_prev/$get");
815 $html.= $this->html
->link('>>', "$class/$id/$limit/$offset_next/$get");
817 $html.= $this->html
->link('new', "$class/new/$get");
821 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
823 if(is_numeric($id)) {
824 $html.=$this->render_single_record_details($class, $id);
826 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
829 $html.= $this->render_form_edit($class, $id, false);
830 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
831 $html.=$this->html
->form($action,'POST',array(
832 array(false,'DELETE','submit'),
833 array('sure', false, 'checkbox', false, false, 'sure?')
835 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
836 $html.=$this->html
->form($action,'POST',array(
837 array('image', false, 'file', false, 'size="30"'),
838 array(false, 'IMAGE', 'submit')
839 ), "enctype='multipart/form-data'");
844 function check_auth() {
845 new HTTP_Auth('WareHouse ['.BACKEND_AUTH
.']', true, array($this->db
->auth
,'check_auth'));
848 function post_redirect_get($location, $message='', $error=false, $translate=true) {
849 $messaget = $translate ?
T($message) : $message;
850 $url_args = $messaget != '' ?
'?message='.urlencode($messaget) : '';
851 $location = $this->html
->internal_url($location).$url_args;
852 header('Location: '.$location);
853 if($error) trigger_error($message);
854 $location=htmlspecialchars($location);
856 "<meta http-equiv='refresh' content='0; url=$location'>".
857 $messaget."<br />Location: <a href='$location'>$location</a>"
861 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
862 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
863 $filename="$dir/$name$ext";
864 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
865 foreach($vars as $var => $val) $
$var=$val;
868 $out=ob_get_contents();
873 function check_input_validity($field, $value='', $ruleset=0) {
874 $rules = array(0 => array(
875 'model_barcode' => '/./',
876 'item_serial' => '/./',
877 'vendor_id' => '/^[0-9]*$/'
879 if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false;
883 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
884 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
885 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
889 /* Tenhle foreach() prekopiruje promenne
890 * z: $_POST['values'][$table][$column][$id];
891 * do: $values[$table][$id][$column]
893 if(isset($_POST['values'])) {
895 foreach($_POST['values'] as $table => $columns) {
896 foreach($columns as $column => $ids) {
897 foreach($ids as $id => $val) {
898 $values[$table][$id][$column] = trim($val);
899 if(!$this->check_input_validity($column,$val)) {
900 $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS
901 $this->html
->link('GO BACK', 'javascript:history.back()', false, false);
902 $this->post_redirect_get('', $message, false, false);
907 //die(print_r($values));
910 if($action) switch($action) {
914 if(!isset($replace)) $replace = true;
915 $table = $class ?
$class : 'item';
916 //print_r($values); //debug
917 $last = $this->db
->insert_or_update_multitab($values, $replace);
918 $last = $force_redirect ?
$force_redirect."?last=$last" : "$table/$last/";
919 $next = "$table/new/";
920 $message = $force_redirect ?
'' : 'Hotovo. Další záznam přidáte '.$this->html
->link('zde', $next).'.';
921 $this->post_redirect_get($last, $message);
924 if(!isset($_POST['sure']) ||
!$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
925 $this->db
->delete($class, $id) ||
$this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
926 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
929 $image_classes = array('model'); //TODO, use this more widely across the code
930 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
931 $image_destination = DIR_IMAGES
."/$class/$id.jpg";
932 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true);
933 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
934 chmod ($image_destination, 0664);
935 $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)');
936 } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true);
939 $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*');
946 function process_http_request() {
949 @ini_set
('magic_quotes_gpc' , 'off');
950 if(get_magic_quotes_gpc()) {
951 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
954 $PATH_INFO=@trim
($_SERVER[PATH_INFO
]);
955 if($PATH_INFO == '' ||
$PATH_INFO == '/') $PATH_INFO = FRONTEND_PAGE_WELCOME
;
956 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
958 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
959 if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode' && $PATH_CHUNKS[1]!='api') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost...
960 echo $this->html
->header($PATH_INFO,$this->db
->auth
->get_user());
961 switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
963 die('Tell me why you cry');
965 case 'assistant': case 'api': //assistant|api
967 'assistant' => DIR_ASSISTANTS
,
970 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ?
trim($PATH_CHUNKS[3]) : false;
971 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
972 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
973 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
974 $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
975 echo $this->safe_include($incdirs[$PATH_CHUNKS[1]],$PATH_CHUNKS[2],$assistant_vars);
977 case 'barcode': //barcode
978 Barcode
::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
981 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ?
trim($_GET['q']) : false;
982 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ?
$PATH_CHUNKS[1] : 'item';
983 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
984 switch($PATH_CHUNKS[2]) {
986 $this->process_http_request_post($PATH_CHUNKS[2], $class);
987 echo $this->render_form_add($class);
990 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ?
(int) $PATH_CHUNKS[2] : false);
991 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
993 switch($PATH_CHUNKS[3]) {
994 case 'edit': //?/?/edit
995 case 'image': //?/?/image
996 case 'delete': //?/?/delete
997 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
1000 $history = $PATH_CHUNKS[3] == 'history' ?
true : false;
1001 $limit = is_numeric($PATH_CHUNKS[3]) ?
(int) $PATH_CHUNKS[3] : FRONTEND_LISTING_LIMIT
;
1002 $offset = isset($PATH_CHUNKS[4]) ?
(int) $PATH_CHUNKS[4] : 0;
1003 $where = @is_array
($_GET['where']) ?
$_GET['where'] : false;
1004 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
1005 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
1006 //print_r(array("<pre>",$_SERVER));
1016 $sklad = new Sklad_UI();
1017 $sklad->process_http_request();
1019 echo('<br style="clear:both;" /><hr />');
This page took 1.181225 seconds and 4 git commands to generate.