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