Svarc System
[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');
5f093236
TM
21set_include_path(DIR_LIB.PATH_SEPARATOR.get_include_path());
22
63747dad 23require_once('Sklad_Auth.class/common.php');
cdfce7c2 24require_once('HTTP_Auth.class.php');
326a9fc9 25require_once('Locale.class.php');
81ab8aef 26require_once('Barcode.class.php');
958f1e84 27require_once('Fortune.php');
cdfce7c2 28
0a027cc7
TM
29/**
30* Trida poskytuje vseobecne funkce pro generovani HTML kodu
31*
32* Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
33*
34* @package HTML
35* @author Tomas Mudrunka
36*/
37class HTML {
5a7b0bb6 38 function row($row,$type=false,$class=false,$parameters='',$colspan=array(),$rowspan=array(),$break_after=array()) {
933b760a 39 $html = '';
1b9304b8 40 $class = $class ? $class=" class='$class' " : '';
933b760a 41 if($type) $html.="<$type>";
5a7b0bb6
TM
42 $tr="<tr$class$parameters>";
43 $html.=$tr;
933b760a 44 $td = $type == 'thead' ? 'th' : 'td';
842cb193
TM
45 foreach($row as $id => $var) {
46 $tdclass = " class='cell_$id'";
0a027cc7 47 if(trim($var) == '') $var = '&nbsp;';
5a7b0bb6
TM
48 $rs = isset($rowspan[$id]) ? " rowspan='$rowspan[$id]'" : '';
49 $cs = isset($colspan[$id]) ? " colspan='$colspan[$id]'" : '';
50 $html.="<$td$rs$cs$tdclass>$var</$td>";
51 if(in_array($id,$break_after,true)) $html.='</tr>'.$tr;
0a027cc7
TM
52 }
53 $html.='</tr>';
933b760a 54 if($type) $html.="</$type>";
0a027cc7
TM
55 return $html;
56 }
57
ba30732d 58 function table(&$table,$colspan=array(),$rowspan=array(),$break_after=array(),$parity_class=array('tr_odd','tr_even'),$params='border=1',$row_classes_field='_row_classes') {
0a027cc7
TM
59 $html="<table $params>";
60 $header=true;
1b9304b8 61 $even=false;
0a027cc7 62 foreach($table as $row) {
ba30732d
TM
63 $params = isset($row[$row_classes_field]) ? $row[$row_classes_field] : '';
64 unset($row[$row_classes_field]);
0a027cc7 65 if($header) {
5a7b0bb6 66 $keys = array(); foreach($row as $key => $val) $keys[$key]=$key;
ba30732d 67 $html.=$this->row(T($keys),'thead',false,'',$colspan,$rowspan,$break_after);
0a027cc7
TM
68 $header=false;
69 }
1b9304b8 70 $class = $parity_class ? $parity_class[$even] : false;
ba30732d 71 $html.=$this->row($row,false,$class.$params,'',$colspan,$rowspan,$break_after);
1b9304b8 72 $even = !$even;
0a027cc7
TM
73 }
74 $html.='</table>';
75 return $html;
76 }
77
81ab8aef 78 function link($title='n/a', $link='#void', $internal=true, $translate=true) {
35916247 79 if($internal && (!isset($link[0]) || $link[0] != '#')) $link = $this->internal_url($link);
81ab8aef
TM
80 if($translate) $title = T($title);
81 return "<a href='$link'>".$title."</a>";
0a027cc7
TM
82 }
83
32e14fd3 84 function img($src='#void', $title='img', $options='height=64') {
81ab8aef
TM
85 $options = $options ? " $options" : '';
86 return "<img src='$src' alt='$title' title='$title'$options; />";
0a027cc7
TM
87 }
88
32e14fd3 89 function img_link($src, $link='#void', $title='img_link', $internal=true, $translate=true, $options='height=64') {
6f7943a0
TM
90 return $this->link($this->img($src,$title,$options),$link,$internal,$translate);
91 }
92
0f7fe034
TM
93 function textarea($name=false, $value='', $placeholder=false, $options=false, $prefix='') {
94 $html = T($prefix)."<textarea";
95 if($name) $html.= " name='$name'";
96 if($options) $html.= " $options";
97 if($placeholder) $html.= " placeholder='$placeholder'";
98 $html .= ">$value</textarea>";
99 return $html;
100 }
101
35916247 102 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') {
120e3b45 103 if($type == 'textarea') return $this->textarea($name, $value, $placeholder, $options, $prefix);
35916247 104 $html = T($prefix)."<input type='$type' ";
0a027cc7 105 if($name) $html.= "name='$name' ";
326a9fc9
TM
106 if(!is_bool($value)) {
107 if($type == 'submit') $value = T($value);
108 $html.= "value='$value' ";
109 }
0a027cc7
TM
110 if($options) $html.= "$options ";
111 if($placeholder) $html.= "placeholder='$placeholder' ";
112 $html .= '/>';
113 return $html;
114 }
115
35916247
TM
116 function form($action=false, $method=false, $inputs, $options=false) {
117 $action = $action ? " action='$action'" : '';
118 $method = $method ? " method='$method'" : '';
119 $options = $options ? " $options" : '';
120 $html = "<form$action$method$options>";
121 foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input);
122 $html .= "</form>";
123 return $html;
124 }
125
0a027cc7
TM
126 function select($name, $selectbox, $default=false) {
127 //echo('<pre>'); print_r($selectbox);
128 $html = "<select name='$name'>";
129
90638f10 130 if(!is_bool($default)) {
0a027cc7
TM
131 $value=$default; $title=$selectbox[$value];
132 $html .= "<option value='$value'>$value :: $title</option>";
133 unset($selectbox[$value]);
134 }
135 foreach($selectbox as $value => $title) {
136 $html .= "<option value='$value'>$value :: $title</option>";
137 }
138 $html .= "</select>";
139 return $html;
140 }
35916247
TM
141
142 function ul($items,$tag=ul,$head='',$class=false) {
143 $class = $class ? " class='$class'" : '';
144 $html = "$head<$tag$class>";
145 foreach($items as $key => $value) {
146 $html .= '<li>';
147 if(is_numeric($key)) {
148 $html .= $value;
149 } else {
150 $html .= $this->link($key,$value);
151 }
152 $html .= '</li>';
153 }
154 $html .= "</$tag>";
155 return $html;
156 }
157
158 function div($html, $options) {
159 $options = $options ? " $options" : '';
160 return "<div$options>$html</div>";
161 }
ec106ddf 162
25b3e809
TM
163 function favicon($url='/favicon.ico') {
164 return '<link rel="shortcut icon" href="'.$url.'" /><link href="'.$url.'" rel="icon" type="image/gif" />';
165
166 }
167
ec106ddf
TM
168 function head($title=false,$charset='UTF-8',$more='') {
169 $title = $title ? "\n<title>$title</title>" : '';
170 $html= '<head>';
171 $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more;
25b3e809 172 $html.= $this->favicon(dirname($_SERVER['SCRIPT_NAME']).'/favicon.ico');
ec106ddf
TM
173 $html.= '</head>';
174 return $html;
175 }
0a027cc7
TM
176}
177
78bf26a5
TM
178/**
179* Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
180*
fc5c5b8b
TM
181* Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
182*
78bf26a5
TM
183* @package Sklad_HTML
184* @author Tomas Mudrunka
185*/
35916247 186class Sklad_HTML extends HTML { //TODO: Split into few more methods
e9f6461f 187 function header($title='', $user=array()) {
cdfce7c2
TM
188 $home = URL_HOME;
189 $script = $_SERVER['SCRIPT_NAME'];
d9384cb5 190 $search = htmlspecialchars(@trim($_GET['q']));
a8bbdc31 191 $message = strip_tags(@trim($_GET['message']),'<a><b><u><i><br>');
958f1e84 192 $fortune = fortune();
ac2ed3a1 193 $instance = INSTANCE_ID != '' ? '/'.INSTANCE_ID : '';
e9f6461f
TM
194 $user_id = htmlspecialchars($user['id']);
195 $user_gid = htmlspecialchars($user['gid']);
196 $user_name = htmlspecialchars($user['name']);
40eef626 197 $time = date('r');
35916247 198 //$title = T($title); //TODO
ec106ddf
TM
199
200 $html = $this->head("SōkoMan$title");
201 $html .= <<<EOF
6d98215b 202<h1 style="display: inline;"><a href="$script/">SōkoMan</a><small>$instance$title</small></h1>
40eef626
TM
203<div style="float:right; text-align:right;">
204 Logged in as <b>$user_name</b> [UID: <b>$user_id</b>; GID: <b>$user_gid</b>]<br />
205 Page loaded at $time
206</div>
c9759229
TM
207
208<style type="text/css">
ac2ed3a1 209* { font-family: arial; }
933b760a
TM
210td,body { background-color: white; }
211table { background-color: orange; border: orange; }
d0e7939c 212a, a img { text-decoration:none; color: darkblue; border:none; }
933b760a 213li a, a:hover { text-decoration:underline; }
1b9304b8 214.tr_even td { background-color: lemonchiffon; }
7df1effb 215.item_status_stored td { font-weight:bold; }
80457ef3
TM
216.item_status_deleted td { text-decoration:line-through; }
217.item_status_destroyed td { font-style:italic; }
842cb193
TM
218/* table, table * { table-layout:fixed; width:100%; overflow:hidden; word-wrap:break-word; } */
219/* td { position:absolute; } */
5a7b0bb6 220/* .cell_model_name { float:left; } */
842cb193 221
ac2ed3a1 222
7bcd6b42 223.menu li {
c9759229 224 float: left;
7bcd6b42
TM
225 padding: 0.2em;
226}
227
228.menu * li {
229 float: none;
c9759229
TM
230}
231
232.menu * menu {
233 position: absolute;
7bcd6b42 234 padding: 0.2em;
c9759229
TM
235}
236
237.menu, .menu * menu {
238 list-style: none;
239}
240
241.menu * menu {
7bcd6b42 242 border: 1px solid orange;
c9759229
TM
243 display: none;
244 margin: 0;
245}
246
7bcd6b42 247.menu li:hover menu, .menu li:hover {
c9759229 248 display: block;
7bcd6b42 249 background-color: yellow;
c9759229 250}
7bcd6b42 251
c9759229
TM
252</style>
253
cdfce7c2 254<div>
35916247
TM
255EOF;
256
257 $assistants=array();
258 foreach(scandir(DIR_ASSISTANTS) as $item) {
259 if($item == '.' || $item == '..') continue;
de42ea13
TM
260 $item = preg_replace('/\.inc\.php$/','',$item,-1,$count);
261 if($count) $assistants[$item] = "assistant/$item";
35916247
TM
262 }
263
264 $tables=array('item','model','category','producer','vendor','room','status');
265
266 foreach($tables as $table) {
267 $listable[$table] = $table;
268 $insertable[$table] = "$table/new";
269 }
270
271 $html .= $this->ul(array(
35916247 272 'Home' => '',
ec106ddf 273 'Logout' => '?logout',
35916247 274 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')),
ec106ddf
TM
275 1 => $this->ul($insertable,'menu',$this->link('New','#')),
276 2 => $this->ul($listable,'menu',$this->link('List','#'))
35916247
TM
277 ),'menu', '', 'menu');
278
279 $html .= '<div style="float: right;">';
280
bf9a965c
TM
281 /*
282 //TODO: Do we really need this?
88c38b26 283 $html .= $this->form("$script/api/go", 'GET', array(
81ab8aef 284 array('q','','text','smart id...', 'autofocus'),
35916247
TM
285 array(false,'go','submit')
286 ), 'style="float: left;"');
bf9a965c 287 */
35916247
TM
288
289 $html .= $this->form('?', 'GET', array(
290 array('q',$search,'text','regexp...'),
291 array(false,'filter','submit')
292 ), 'style="float: left;"');
293
bf9a965c
TM
294 $html .= $this->form("$script/item", 'GET', array(
295 array('q',$search,'text','regexp...','autofocus'),
296 array(false,'search','submit')
297 ), 'style="float: left;"');
298
35916247
TM
299 $html .= '</div>';
300
301 $html .= <<<EOF
cdfce7c2 302</div>
c9759229 303<hr style="clear: both;" />
d9384cb5
TM
304<div style="background-color:#FFDDDD;">
305 <font color="red">$message</font>
306</div>
958f1e84 307<div style="text-align:right; color:darkgreen;">
41e57f9e
TM
308$fortune
309</div>
cdfce7c2 310EOF;
35916247
TM
311
312 return $html;
cdfce7c2
TM
313 }
314
d9384cb5
TM
315 function internal_url($link) {
316 return $_SERVER['SCRIPT_NAME'].'/'.$link;
317 }
318
cdfce7c2
TM
319 function table_add_images(&$table) {
320 $image = array('model_id');
321 foreach($table as $id => $row) {
322 foreach($image as $column) if(isset($table[$id][$column])) {
323 $type = @array_shift(preg_split('/_/', $column));
324 $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg';
6f7943a0 325 $table[$id][$type.'_image']=$this->img_link($src, $src, $table[$id][$column], false, false);
cdfce7c2
TM
326 }
327 }
328 }
329
81ab8aef 330 function render_barcode($barcode,$opts=false) {
6f7943a0 331 return $this->img_link($this->internal_url("barcode/$barcode"),$this->internal_url("barcode/$barcode"),$barcode,false,false,$opts);
81ab8aef
TM
332 }
333
334 function table_add_barcodes(&$table) {
335 $image = array('model_barcode', 'item_serial');
336 foreach($table as $id => $row) {
337 foreach($image as $column) if(isset($table[$id][$column])) {
338 $table[$id][$column]=$this->render_barcode($table[$id][$column]);
339 }
340 }
341 }
342
ba30732d 343 function table_add_row_classes(&$table, $class_col='_row_classes') {
7df1effb
TM
344 $image = array('status_name' => ' item_status_');
345 foreach($table as $id => $row) {
346 foreach($image as $column => $param) if(isset($table[$id][$column])) {
ba30732d 347 @$table[$id][$class_col] .= $param.$table[$id][$column];
7df1effb
TM
348 }
349 }
350 }
351
d6975011 352 function table_add_relations(&$table, $class, $suffix_relations='_relations') {
aaafc8b7 353 $where_url = '%d/?where[%c]==%v';
d6975011 354 $relations = array( //TODO: Autodetect???
aaafc8b7 355 'model' => array(
9c310625 356 'model_id' => array(array('item',$where_url),array('edit','model/%v/edit/')),
d516a31d 357 'model_barcode' => array(array('store','assistant/%d?barcode=%v')),
41e57f9e 358 'model_name' => array(array('google','http://google.com/search?q=%v',true)) //TODO: add manufacturer to google query
aaafc8b7 359 ),
4ece8e80 360 'item' => array(
d1686e10 361 'item_serial' => array(array('dispose','assistant/%d?serial=%v','in_stock'),array('sell','assistant/%d?serial=%v','in_stock')),
6265a8d4 362 'item_id' => array(array('edit','item/%v/edit/'))
4ece8e80 363 ),
d7d9ce39 364 'category' => array('category_id' => array(array('item',$where_url), array('model',$where_url))),
0646d9ef 365 'producer' => array('producer_id' => array(array('item',$where_url), array('model',$where_url))),
aaafc8b7
TM
366 'vendor' => array('vendor_id' => array(array('item',$where_url))),
367 'room' => array('room_id' => array(array('item',$where_url))),
368 'status' => array('status_id' => array(array('item',$where_url)))
d6975011 369 );
6265a8d4 370 $relations_conditions=array(
d1686e10
TM
371 'in_stock' => 'return(@$table[$id]["status_name"] == "stored");',
372 'not_sold' => 'return(@$table[$id]["status_name"] != "saled");',
373 'not_sold_or_disposed' => 'return(@$table[$id]["status_name"] != "saled" && @$table[$id]["status_name"] != "disposed");'
6265a8d4 374 );
d6975011
TM
375 foreach($table as $id => $row) {
376 foreach($row as $column => $value) {
377 if(isset($relations[$class][$column])) {
378 foreach($relations[$class][$column] as $destination) {
aaafc8b7
TM
379 $destination_url = str_replace(
380 array('%d','%c','%v'),
d516a31d 381 array(urlencode($destination[0]),urlencode($column),urlencode($value)),
aaafc8b7
TM
382 $destination[1]
383 );
9c310625 384 if(isset($destination[2]) && isset($relations_conditions[$destination[2]])) {
e9482e8a
TM
385 //$condition = $relations_conditions[$destination[2]]($table,$id);
386 if(!eval($relations_conditions[$destination[2]])) continue;
6265a8d4 387 }
2195f218 388 @$table[$id][$class.$suffix_relations] .= $this->link($destination[0], $destination_url).',';
d6975011
TM
389 }
390 }
391 }
392 }
393 }
394
cdfce7c2
TM
395 function table_collapse(&$table) {
396 $collapse = array(
397 'item_id' => 'item_id',
398 'model_id' => 'model_name',
399 'category_id' => 'category_name',
400 'producer_id' => 'producer_name',
401 'vendor_id' => 'vendor_name',
402 'room_id' => 'room_name',
403 'status_id' => 'status_name',
d7d9ce39 404 'item_author' => 'item_author_backend',
a278a425 405 'item_customer' => 'item_customer',
cdfce7c2 406 );
d7d9ce39 407
cdfce7c2
TM
408 foreach($table as $id => $row) {
409 foreach($collapse as $link => $title)
fff6ce40 410 if(isset($table[$id][$link]) && isset($row[$title])) {
cdfce7c2
TM
411 $type = @array_shift(preg_split('/_/', $link));
412 if($link != $title) unset($table[$id][$link]);
d7d9ce39
TM
413 switch($link) { //TODO: Move to array for easy configuration
414 case 'item_author':
a278a425
TM
415 case 'item_customer':
416 $table[$id][$title]=$this->link($row[$title], "?where[$link]==".$row[$link], false);
d7d9ce39
TM
417 break;
418 default:
419 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
420 break;
421 }
cdfce7c2
TM
422 }
423 }
424 }
425
426 function table_sort(&$table) {
9c310625 427 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name','item_quantity','item_price_in','item_price_out','model_price_in','model_price_out','item_relations','model_relations');
cdfce7c2
TM
428 $table_sorted = array();
429 foreach($table as $id => $row) {
430 $table_sorted[$id] = array();
431 foreach($precedence as $column) if(isset($table[$id][$column])) {
842cb193 432 $table_sorted[$id][$column]=$table[$id][$column];
cdfce7c2
TM
433 unset($table[$id][$column]);
434 }
842cb193
TM
435 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
436 //foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating
cdfce7c2
TM
437 }
438 $table = $table_sorted;
439 }
440
ecaeb1d6
TM
441 function table_hide_columns(&$table, $class) { //TODO: Move to build_query_select() !!! :-)))
442 $fields_hide = array(
443 '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')
444 );
445 //print_r($table); die();
446 if(isset($fields_hide[$class])) foreach($table as $id => $row) {
447 foreach($fields_hide[$class] as $field) unset($table[$id][$field]);
448 }
449 }
450
d6975011 451 function render_item_table($table,$class=false) {
5a7b0bb6
TM
452
453 $cellspan = array(
454 'break_after' => array(
455 'item' => array('model_name'),
456 'model'=> array('model_descript')
457 ),
458 'rowspan' => array(
459 'item' => array('model_image'=>2,'item_id'=>2),
460 'model'=> array('model_image'=>2)
461 ),
462 'colspan' => array(
463 'item' => array('model_name'=>'100%'),
464 'model'=> array('model_name'=>4,'model_descript'=>'100%')
465 )
466 );
467
468 foreach(array_keys($cellspan) as $vari)
469 $$vari = isset($cellspan[$vari][$class]) ? $cellspan[$vari][$class] : array();
470
8ca87ee3 471 if(empty($table)) return '<h3>'.T('holy primordial emptiness is all you can find here...').'</h3><br />';
ba30732d 472 $this->table_add_row_classes($table);
cdfce7c2 473 $this->table_add_images($table);
d6975011 474 if($class) $this->table_add_relations($table,$class);
aaafc8b7 475 $this->table_add_barcodes($table);
cdfce7c2 476 $this->table_collapse($table);
ecaeb1d6 477 if($class) $this->table_hide_columns($table,$class);
cdfce7c2 478 $this->table_sort($table);
5a7b0bb6 479 return $this->table($table,$colspan,$rowspan,$break_after);
cdfce7c2
TM
480 }
481
d0e7939c 482 function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
120e3b45
TM
483 $textarea = array(
484 'item' => array('item_note'),
485 'model' => array('model_descript')
486 );
d0e7939c 487 $html = '';
cdfce7c2 488 foreach($columns as $column) {
326a9fc9 489 $html.=T($class).':<b>'.T($column['Field']).'</b>: ';
7d20381f 490 $name="values[$class][".$column['Field'].'][]';
16261142 491 $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false;
cdfce7c2 492 switch(true) {
a084118b 493 case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)):
90638f10 494 if(is_bool($val) && !$val) $val = '';
fc5c5b8b
TM
495 $html.=$this->input($name, $val, 'hidden');
496 $html.=$val.'(AUTO)';
cdfce7c2
TM
497 break;
498 case isset($selectbox[$column['Field']]):
e9cb8cea 499 $html.=$this->select($name,$selectbox[$column['Field']],$val);
cdfce7c2 500 break;
120e3b45
TM
501 case isset($textarea[$class]) && in_array($column['Field'],$textarea[$class]):
502 $html.=$this->input($name, $val, 'textarea');
503 break;
504 default:
fc5c5b8b 505 $html.=$this->input($name, $val);
cdfce7c2
TM
506 break;
507 }
fc5c5b8b 508 $html.='<br />';
cdfce7c2 509 }
d0e7939c
TM
510 return $html;
511 }
cdfce7c2 512
d0e7939c
TM
513 function render_insert_form_multi($array) {
514 $html = '';
515 $head=false;
516
517 foreach($array as $key => $args) {
518 $parts=array('inputs');
519 if(!$head) { $head = true;
520 $parts[]='head';
521 }
522 if(!isset($array[$key+1])) {
523 $parts[]='foot';
5b0075fa
TM
524 $hr = '';
525 } else $hr = '<hr />';
526 //$args[] = false;
d0e7939c 527 $args[] = $parts;
f5baa075 528
d0e7939c 529 $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
5b0075fa 530 $html .= $hr;
d0e7939c
TM
531 }
532 return $html;
533 }
534
535 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
536 $html = '';
537 //print_r($parts);
538 //echo('<pre>'); print_r($selectbox);
539 //echo('<pre>'); print_r($current);
540 $update = false;
541 if(is_array($current)) {
542 $update = true;
543 $current = array_shift($current);
544 }
545
546 if(!is_array($hidecols)) $hidecols = array();
547 $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
548
549 if(!is_array($parts) || in_array('head', $parts)) {
550 $action = $action ? " action='$action'" : false;
551 $html.="<form$action method='POST'>"; //TODO: use $this->form()
5b0075fa 552 $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
cdfce7c2
TM
553 }
554
d0e7939c
TM
555 if(!is_array($parts) || in_array('inputs', $parts))
556 $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update);
557
558 if(!is_array($parts) || in_array('foot', $parts)) {
5b0075fa 559 $html .= '</div></span><br style="clear:both" />';
d0e7939c
TM
560 if($multi_insert) { //TODO, move to separate JS file
561 $html.=<<<EOF
d0e7939c
TM
562 <script>
563 function duplicate_element(what, where) {
564 var node = document.getElementsByName(what)[0];
565 node.parentNode.appendChild(node.cloneNode(true));
566 }
567 </script>
568 <a href='#' onClick="duplicate_element('input_set')">+</a>
569EOF;
570 }
571
572 $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
573 $html.=$this->input(false, $btn, 'submit');
574 $html.='</form>';
575 }
fc5c5b8b 576 return $html;
cdfce7c2
TM
577 }
578}
579
78bf26a5
TM
580/**
581* Trida poskytuje rozhrani k databazi skladu
582*
583* @package Sklad_DB
584* @author Tomas Mudrunka
585*/
cdfce7c2
TM
586class Sklad_DB extends PDO {
587 function __construct() {
63747dad 588 $this->auth = new Sklad_Auth();
cdfce7c2
TM
589
590 parent::__construct(
591 DB_DSN, DB_USER, DB_PASS,
592 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL
593 );
594 }
595
596 function escape($str) {
597 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
598 }
599
382324d3
TM
600 function quote_identifier($str) {
601 return '`'.$this->escape($str).'`'; //TODO HACK
602 }
603
cb8a6861 604 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
2bedfdac 605 //Configuration
cdfce7c2
TM
606 $join = array(
607 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
608 'model' => array('category', 'producer')
9ea191cb 609 ); //TODO Autodetect using foreign keys?
ecaeb1d6 610 $fields_search = array(
0646d9ef
TM
611 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name'),
612 'model' => array('model_id','model_name','model_barcode','model_descript','producer_name')
9ea191cb 613 ); //TODO Autodetect
2bedfdac 614
d9d47bd3
TM
615 //Init
616 if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
617
2bedfdac
TM
618 //Escaping
619 $class = $this->escape($class);
620
621 //SELECT
382324d3 622 $sql="SELECT * FROM `$class`\n";
2bedfdac 623 //JOIN
382324d3 624 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n";
2bedfdac 625 //WHERE/REGEXP
cdfce7c2
TM
626 if($search) {
627 $search = $this->quote($search);
ecaeb1d6 628 if(!isset($fields_search[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get
5895162b 629 $sql_search = '';
ecaeb1d6 630 foreach($fields_search[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
5895162b 631 $where[] = "FALSE $sql_search";
cb8a6861
TM
632 } elseif($id) $where[] = "$class$suffix_id = $id";
633 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
634
5895162b 635 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
117817be 636 //ORDER
fd479ff5 637 if(!$order) $order = $class.$suffix_id.' DESC';
117817be
TM
638 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
639 $sql .= "ORDER BY $order\n";
2bedfdac 640 //LIMIT/OFFSET
cdfce7c2
TM
641 if($limit) {
642 $limit = $this->escape((int)$limit);
643 $offset = $this->escape((int)$offset);
644 $sql .= "LIMIT $offset,$limit\n";
645 }
2bedfdac 646
cdfce7c2
TM
647 return $sql;
648 }
649
66b6f4d6 650 function safe_query($sql, $fatal=true) {
cdfce7c2
TM
651 $result = $this->query($sql);
652 if(!$result) {
2bedfdac 653 $error = $this->errorInfo();
66b6f4d6
TM
654 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>");
655 if($fatal) die();
cdfce7c2
TM
656 }
657 return $result;
658 }
659
d9601e5d 660 function translate_query_results(&$result) {
d1686e10 661 $translate_cols = array('item_valid_till'); //TODO: Hardcoded
326a9fc9
TM
662 foreach($result as $key => $row) {
663 foreach($translate_cols as $col) if(isset($result[$key][$col])){
664 $result[$key][$col] = T($result[$key][$col]);
665 }
666 }
d9601e5d
TM
667 }
668
d7d9ce39 669 function load_backend_data_to_query_results(&$result,$suffix_backend='_backend') {
d9601e5d
TM
670 $translate_cols = array(
671 'item_author' => 'return($this->auth->get_username_by_id($result[$key][$col]));'
672 ); //TODO: Hardcoded
673 foreach($result as $key => $row) {
674 foreach($translate_cols as $col => $backend) if(isset($result[$key][$col])){
d7d9ce39 675 $result[$key][$col.$suffix_backend] = eval($backend);
d9601e5d
TM
676 }
677 }
326a9fc9
TM
678 }
679
680 function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO::FETCH_ASSOC, $translate=true) {
681 $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags);
d9601e5d
TM
682 $this->load_backend_data_to_query_results($result);
683 if($translate) $this->translate_query_results($result);
326a9fc9
TM
684 return $result;
685 }
686
cb8a6861
TM
687 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
688 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
326a9fc9 689 $result = $this->safe_query_fetch($sql);
cdfce7c2
TM
690 if(!$result || !is_array($indexed)) return $result;
691
692 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
693 return $indexed;
694 }
695
d0e7939c 696 function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
cdfce7c2
TM
697 $class = $this->escape($class);
698 $sql = "SHOW COLUMNS FROM $class;";
d0e7939c
TM
699 $columns = $this->safe_query_fetch($sql);
700 /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
701 if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
702 }*/
703 return $columns;
cdfce7c2
TM
704 }
705
706 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
1c99d83b 707 $selectbox=array( //TODO: Hardcoded...
41e57f9e 708 'model_countable' => array(0 => 'no', 1 => 'yes'),
3e939604
TM
709 'model_eshop_hide' => array(0 => 'no', 1 => 'yes'),
710 'vendor_id' => array('COMPULSORY' => 'select...')
1c99d83b 711 );
cdfce7c2 712 foreach($columns as $column) {
66b6f4d6 713 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
714 if($class && $column['Field'] == $class.$suffix_id) continue;
715 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
716 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
66b6f4d6 717
117817be 718 $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : '';
b4dcae05 719 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
326a9fc9 720 $result = $this->safe_query_fetch($sql, false);
66b6f4d6 721 if(!$result) continue;
cdfce7c2
TM
722 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
723 }
724 //echo('<pre>'); print_r($selectbox);
66b6f4d6 725 return array_filter($selectbox, 'ksort');
9ea191cb
TM
726 }
727
d0e7939c 728 function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
16261142
TM
729 $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
730 $value=$this->quote($value);
731 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
326a9fc9 732 $result = $this->safe_query_fetch($sql);
d0e7939c 733 if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
16261142
TM
734 }
735
9ea191cb
TM
736 function contains_history($table) {
737 $history_tables = array('item'); //TODO Autodetect
738 return in_array($table, $history_tables);
cdfce7c2
TM
739 }
740
741 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
b66fadbb
TM
742 //Init
743 $history = $this->contains_history($table);
744
9ea191cb 745 //Escaping
cdfce7c2
TM
746 $table = $this->escape($table);
747
748 //Get list of POSTed columns
aa3fd0a8
TM
749 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
750 $columns = implode(',',$columns_array);
cdfce7c2 751
9fb856ba 752 //Build query
b66fadbb 753 $sql = '';
b66fadbb
TM
754 //echo('<pre>'); die(print_r($values));
755
756 if($history) {
757 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
758 if($history_update) {
759 $sql .= "UPDATE $table";
117817be
TM
760 $sql .= " SET ${table}_valid_till=NOW()";
761 $sql .= " WHERE ${table}_valid_till=0 AND (";
b66fadbb
TM
762 $or = '';
763 foreach($values as $row) {
5b0075fa 764 $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
b66fadbb
TM
765 $or = ' OR';
766 }
767 $sql .= " );\n\n";
768 $replace = false;
769 }
770 }
771
cdfce7c2 772 //Insert into table (columns)
aa3fd0a8 773 $sql .= "INSERT INTO $table ($columns) VALUES ";
cdfce7c2
TM
774
775 //Values (a,b,c),(d,e,f)
776 $comma='';
777 foreach($values as $row) {
9fb856ba
TM
778 $row_quoted = array_map(array($this,'quote'), $row); //Check
779 if($history) {
b66fadbb
TM
780 foreach($row as $column => $value) {
781 switch($column) {
782 case $table.'_valid_from':
783 $row_quoted[$column] = 'NOW()';
784 break;
785 case $table.'_valid_till':
786 $row_quoted[$column] = '0';
787 break;
9fb856ba 788 case $table.'_author':
e9f6461f
TM
789 $row_quoted[$column] = $this->auth->get_user_id();
790 //die($this->auth->get_user_id().'=USER');
b66fadbb 791 break;
b66fadbb
TM
792 }
793 }
794 }
795 $sql .= $comma.'('.implode(',',$row_quoted).')';
cdfce7c2
TM
796 $comma = ',';
797 }
798
aa3fd0a8
TM
799 //On duplicate key
800 if($replace) {
801 foreach($columns_array as $col) {
802 if($col == $table.'_id' || $col == $table.'_valid_till') continue;
803 $on_duplicate[] = "$col=VALUES($col)";
804 }
805 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
806 }
807
cdfce7c2
TM
808 //Terminate
809 $sql .= ';';
810 return $sql;
811 }
812
b4dcae05
TM
813 function insert_or_update($table, $values, $replace=true) {
814 $sql = $this->build_query_insert($table, $values, $replace);
cdfce7c2
TM
815 $this->safe_query($sql);
816 return $this->lastInsertId();
817 }
818
b4dcae05 819 function insert_or_update_multitab($values, $replace=true) {
371a86f4 820 $last=false;
b4dcae05 821 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
371a86f4
TM
822 return $last;
823 }
824
cdfce7c2 825 function delete($table, $id, $suffix_id='_id') {
64f31c54 826 if($this->contains_history($table)) return false;
cdfce7c2
TM
827 $key = $this->escape($table.$suffix_id);
828 $table = $this->escape($table);
829 $id = $this->quote($id);
830 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
831 }
832}
833
0a027cc7
TM
834/**
835* Trida poskytuje high-level rozhrani k databazi skladu
836*
837* @package Sklad_DB_Abstract
838* @author Tomas Mudrunka
839*/
840class Sklad_DB_Abstract extends Sklad_DB {
841 //TODO Code
842}
843
78bf26a5
TM
844/**
845* Trida implementuje uzivatelske rozhrani skladu
846*
847* Example usage:
848* $sklad = new Sklad_UI();
849* $sklad->process_http_request();
850*
851* @package Sklad_UI
852* @author Tomas Mudrunka
853*/
cdfce7c2
TM
854class Sklad_UI {
855 function __construct() {
856 $this->db = new Sklad_DB();
857 $this->html = new Sklad_HTML();
858 }
859
cb8a6861 860 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
d6975011 861 return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
cdfce7c2
TM
862 }
863
a25f85f8 864 function render_form_add($class) {
cdfce7c2
TM
865 $columns = $this->db->get_columns($class);
866 $selectbox = $this->db->columns_get_selectbox($columns, $class);
a25f85f8 867 return $this->html->render_insert_form($class, $columns, $selectbox);
cdfce7c2
TM
868 }
869
f5baa075 870 function render_form_edit($class, $id, $multi_insert) {
cdfce7c2
TM
871 $columns = $this->db->get_columns($class);
872 $selectbox = $this->db->columns_get_selectbox($columns, $class);
117817be 873 $current = $this->db->get_listing($class, $id, 1);
f5baa075 874 return $this->html->render_insert_form($class, $columns, $selectbox, $current, false, false, $multi_insert);
cdfce7c2
TM
875 }
876
a25f85f8 877 function render_single_record_details($class, $id) {
cdfce7c2
TM
878 $id_next = $id + 1;
879 $id_prev = $id - 1 > 0 ? $id - 1 : 0;
880 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
a25f85f8
TM
881 $html='';
882 $html.= $this->html->link('<<', "$class/$id_prev/");
883 $html.= '-';
884 $html.= $this->html->link('>>', "$class/$id_next/");
885 $html.= '<br />';
54694117 886 $html.='<span style="float:right;">'.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")).'</span>';
a25f85f8 887 $html.= $this->html->link('edit', "$class/$id/edit/");
9fb856ba 888 if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
a25f85f8 889 return $html;
cdfce7c2
TM
890 }
891
a25f85f8 892 function render_listing_navigation($class, $id, $limit, $offset) {
cdfce7c2
TM
893 $offset_next = $offset + $limit;
894 $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0;
895 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
a25f85f8
TM
896 $html='';
897 $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get");
898 $html.= '-';
899 $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get");
900 $html.= '<br />';
901 $html.= $this->html->link('new', "$class/new/$get");
902 return $html;
cdfce7c2
TM
903 }
904
a25f85f8
TM
905 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
906 $html='';
cdfce7c2 907 if(is_numeric($id)) {
a25f85f8 908 $html.=$this->render_single_record_details($class, $id);
cdfce7c2 909 } else {
a25f85f8 910 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
cdfce7c2
TM
911 }
912 if($edit) {
f5baa075 913 $html.= $this->render_form_edit($class, $id, false);
cdfce7c2 914 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
35916247
TM
915 $html.=$this->html->form($action,'POST',array(
916 array(false,'DELETE','submit'),
917 array('sure', false, 'checkbox', false, false, 'sure?')
918 ));
cdfce7c2 919 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
35916247
TM
920 $html.=$this->html->form($action,'POST',array(
921 array('image', false, 'file', false, 'size="30"'),
922 array(false, 'IMAGE', 'submit')
923 ), "enctype='multipart/form-data'");
cdfce7c2 924 }
a25f85f8 925 return $html;
cdfce7c2
TM
926 }
927
928 function check_auth() {
042a4988 929 new HTTP_Auth('WareHouse ['.BACKEND_AUTH.']', true, array($this->db->auth,'check_auth'));
cdfce7c2
TM
930 }
931
a8bbdc31
TM
932 function post_redirect_get($location, $message='', $error=false, $translate=true) {
933 $messaget = $translate ? T($message) : $message;
934 $url_args = $messaget != '' ? '?message='.urlencode($messaget) : '';
8acef003 935 $location = $this->html->internal_url($location).$url_args;
1f74892d 936 header('Location: '.$location);
64f31c54 937 if($error) trigger_error($message);
7efdf72a
TM
938 $location=htmlspecialchars($location);
939 die(
940 "<meta http-equiv='refresh' content='0; url=$location'>".
a8bbdc31 941 $messaget."<br />Location: <a href='$location'>$location</a>"
7efdf72a 942 );
cdfce7c2
TM
943 }
944
623c65e2 945 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
64f31c54 946 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
3e6c412e 947 $filename="$dir/$name$ext";
64f31c54 948 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
623c65e2 949 foreach($vars as $var => $val) $$var=$val;
3e6c412e
TM
950 ob_start();
951 include($filename);
952 $out=ob_get_contents();
953 ob_end_clean();
954 return $out;
955 }
956
a8bbdc31
TM
957 function check_input_validity($field, $value='', $ruleset=0) {
958 $rules = array(0 => array(
959 'model_barcode' => '/./',
3e939604
TM
960 'item_serial' => '/./',
961 'vendor_id' => '/^[0-9]*$/'
a8bbdc31
TM
962 ));
963 if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false;
964 return true;
965 }
966
bda4a4be 967 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
cdfce7c2 968 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
1f74892d 969 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
cdfce7c2
TM
970
971 //SephirPOST:
371a86f4
TM
972
973 /* Tenhle foreach() prekopiruje promenne
7d20381f 974 * z: $_POST['values'][$table][$column][$id];
371a86f4
TM
975 * do: $values[$table][$id][$column]
976 */
7d20381f
TM
977 if(isset($_POST['values'])) {
978 $values=array();
979 foreach($_POST['values'] as $table => $columns) {
980 foreach($columns as $column => $ids) {
a8bbdc31
TM
981 foreach($ids as $id => $val) {
982 $values[$table][$id][$column] = trim($val);
983 if(!$this->check_input_validity($column,$val)) {
984 $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS
985 $this->html->link('GO BACK', 'javascript:history.back()', false, false);
986 $this->post_redirect_get('', $message, false, false);
987 }
988 }
7d20381f 989 }
cdfce7c2 990 }
7d20381f 991 //die(print_r($values));
cdfce7c2
TM
992 }
993
994 if($action) switch($action) {
995 case 'new':
b4dcae05 996 $replace = false;
cdfce7c2 997 case 'edit':
b4dcae05 998 if(!isset($replace)) $replace = true;
64f31c54 999 $table = $class ? $class : 'item';
cdfce7c2 1000 //print_r($values); //debug
b4dcae05 1001 $last = $this->db->insert_or_update_multitab($values, $replace);
bda4a4be 1002 $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/";
d9384cb5 1003 $next = "$table/new/";
bda4a4be
TM
1004 $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.';
1005 $this->post_redirect_get($last, $message);
cdfce7c2
TM
1006 break;
1007 case 'delete':
64f31c54
TM
1008 if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
1009 $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
1f74892d 1010 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
cdfce7c2
TM
1011 break;
1012 case 'image':
1013 $image_classes = array('model'); //TODO, use this more widely across the code
64f31c54 1014 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
cdfce7c2 1015 $image_destination = DIR_IMAGES."/$class/$id.jpg";
326a9fc9 1016 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true);
cdfce7c2 1017 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
1f74892d 1018 chmod ($image_destination, 0664);
326a9fc9
TM
1019 $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)');
1020 } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true);
cdfce7c2
TM
1021 break;
1022 default:
326a9fc9 1023 $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*');
cdfce7c2
TM
1024 break;
1025 }
1026
1027 die('POSTed pyčo!');
1028 }
1029
1030 function process_http_request() {
1031 $this->check_auth();
1032
1033 @ini_set('magic_quotes_gpc' , 'off');
1034 if(get_magic_quotes_gpc()) {
1035 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
1036 }
1037
1038 $PATH_INFO=@trim($_SERVER[PATH_INFO]);
a5094502 1039 if($PATH_INFO == '' || $PATH_INFO == '/') $PATH_INFO = FRONTEND_PAGE_WELCOME;
cdfce7c2 1040 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
81ab8aef 1041 //Sephirot:
cdfce7c2 1042 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
88c38b26 1043 if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode' && $PATH_CHUNKS[1]!='api') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost...
e9f6461f 1044 echo $this->html->header($PATH_INFO,$this->db->auth->get_user());
81ab8aef 1045 switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
cdfce7c2
TM
1046 case 'test': //test
1047 die('Tell me why you cry');
1048 break;
88c38b26
TM
1049 case 'assistant': case 'api': //assistant|api
1050 $incdirs = array(
1051 'assistant' => DIR_ASSISTANTS,
1052 'api' => DIR_APIS
1053 );
de77377e
TM
1054 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
1055 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
1056 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
1057 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
5ef6c52f 1058 $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
88c38b26 1059 echo $this->safe_include($incdirs[$PATH_CHUNKS[1]],$PATH_CHUNKS[2],$assistant_vars);
3e6c412e 1060 break;
81ab8aef
TM
1061 case 'barcode': //barcode
1062 Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
1063 break;
cdfce7c2
TM
1064 default: //?
1065 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
1066 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
1067 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
1068 switch($PATH_CHUNKS[2]) {
1069 case 'new': //?/new
1070 $this->process_http_request_post($PATH_CHUNKS[2], $class);
a25f85f8 1071 echo $this->render_form_add($class);
cdfce7c2
TM
1072 break;
1073 default: //?/?
1074 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false);
1075 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
1076 $edit=false;
1077 switch($PATH_CHUNKS[3]) {
1078 case 'edit': //?/?/edit
9fb856ba
TM
1079 case 'image': //?/?/image
1080 case 'delete': //?/?/delete
cdfce7c2
TM
1081 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
1082 $edit=true;
1083 default: //?/?/?
9fb856ba 1084 $history = $PATH_CHUNKS[3] == 'history' ? true : false;
2415a365
TM
1085 $limit = is_numeric($PATH_CHUNKS[3]) ? (int) $PATH_CHUNKS[3] : FRONTEND_LISTING_LIMIT;
1086 $offset = isset($PATH_CHUNKS[4]) ? (int) $PATH_CHUNKS[4] : 0;
d9d47bd3 1087 $where = @is_array($_GET['where']) ? $_GET['where'] : false;
cb8a6861 1088 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
a25f85f8 1089 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
cdfce7c2
TM
1090 //print_r(array("<pre>",$_SERVER));
1091 break;
1092 }
1093 break;
1094 }
1095 break;
1096 }
1097 }
1098}
1099
1100$sklad = new Sklad_UI();
1101$sklad->process_http_request();
1102
54694117 1103echo('<br style="clear:both;" /><hr />');
This page took 1.395001 seconds and 4 git commands to generate.