lepsi hlasky v add relations (wolfuv pozadavek)
[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(
5480af75 365 'model_id' => array(array('item',$where_url),array('barcode',$where_url),array('edit','model/%v/edit/'),array('barcode',$insert_url,false,'add barcode')),
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 }
5480af75
TM
407 $destination_title = isset($destination[3]) ? $destination[3] : $destination[0];
408 @$table[$id][$class.$suffix_relations] .= $this->link($destination_title, $destination_url, !preg_match('/http/', $destination_url) ).',';
d6975011
TM
409 }
410 }
411 }
412 }
413 }
414
cdfce7c2
TM
415 function table_collapse(&$table) {
416 $collapse = array(
417 'item_id' => 'item_id',
418 'model_id' => 'model_name',
419 'category_id' => 'category_name',
420 'producer_id' => 'producer_name',
421 'vendor_id' => 'vendor_name',
422 'room_id' => 'room_name',
928bb5a6 423 'location_id' => 'location_name',
cdfce7c2 424 'status_id' => 'status_name',
d7d9ce39 425 'item_author' => 'item_author_backend',
a278a425 426 'item_customer' => 'item_customer',
7f1203c4 427 'lock_id' => 'lock_name',
cdfce7c2 428 );
d7d9ce39 429
cdfce7c2
TM
430 foreach($table as $id => $row) {
431 foreach($collapse as $link => $title)
fff6ce40 432 if(isset($table[$id][$link]) && isset($row[$title])) {
cdfce7c2
TM
433 $type = @array_shift(preg_split('/_/', $link));
434 if($link != $title) unset($table[$id][$link]);
d7d9ce39
TM
435 switch($link) { //TODO: Move to array for easy configuration
436 case 'item_author':
a278a425
TM
437 case 'item_customer':
438 $table[$id][$title]=$this->link($row[$title], "?where[$link]==".$row[$link], false);
d7d9ce39
TM
439 break;
440 default:
441 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
442 break;
443 }
cdfce7c2
TM
444 }
445 }
446 }
447
448 function table_sort(&$table) {
9c310625 449 $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
450 $table_sorted = array();
451 foreach($table as $id => $row) {
452 $table_sorted[$id] = array();
453 foreach($precedence as $column) if(isset($table[$id][$column])) {
842cb193 454 $table_sorted[$id][$column]=$table[$id][$column];
cdfce7c2
TM
455 unset($table[$id][$column]);
456 }
842cb193
TM
457 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
458 //foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating
cdfce7c2
TM
459 }
460 $table = $table_sorted;
461 }
462
ecaeb1d6
TM
463 function table_hide_columns(&$table, $class) { //TODO: Move to build_query_select() !!! :-)))
464 $fields_hide = array(
7661315f
TM
465 'model' => array('barcode_name'),
466 'barcode' => array('model_price_in','model_price_out','model_reserve','producer_name','producer_note','model_eshop_hide','category_name','model_countable','model_descript'),
ea921a95 467 '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
468 );
469 //print_r($table); die();
470 if(isset($fields_hide[$class])) foreach($table as $id => $row) {
471 foreach($fields_hide[$class] as $field) unset($table[$id][$field]);
472 }
473 }
474
d6975011 475 function render_item_table($table,$class=false) {
5a7b0bb6
TM
476
477 $cellspan = array(
478 'break_after' => array(
1f7c4407 479 'item' => array('category_name'),
5a7b0bb6
TM
480 'model'=> array('model_descript')
481 ),
482 'rowspan' => array(
483 'item' => array('model_image'=>2,'item_id'=>2),
484 'model'=> array('model_image'=>2)
485 ),
486 'colspan' => array(
1f7c4407 487 'item' => array('model_name'=>6,'category_name'=>'100%'),
5a7b0bb6
TM
488 'model'=> array('model_name'=>4,'model_descript'=>'100%')
489 )
490 );
491
492 foreach(array_keys($cellspan) as $vari)
493 $$vari = isset($cellspan[$vari][$class]) ? $cellspan[$vari][$class] : array();
494
8ca87ee3 495 if(empty($table)) return '<h3>'.T('holy primordial emptiness is all you can find here...').'</h3><br />';
ba30732d 496 $this->table_add_row_classes($table);
cdfce7c2 497 $this->table_add_images($table);
d6975011 498 if($class) $this->table_add_relations($table,$class);
aaafc8b7 499 $this->table_add_barcodes($table);
cdfce7c2 500 $this->table_collapse($table);
ecaeb1d6 501 if($class) $this->table_hide_columns($table,$class);
cdfce7c2 502 $this->table_sort($table);
5a7b0bb6 503 return $this->table($table,$colspan,$rowspan,$break_after);
cdfce7c2
TM
504 }
505
d0e7939c 506 function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
120e3b45
TM
507 $textarea = array(
508 'item' => array('item_note'),
509 'model' => array('model_descript')
510 );
3634575a 511 $html = '<table>';
1f52346f 512 $even=false;
cdfce7c2 513 foreach($columns as $column) {
1f52346f
TM
514 $html.='<tr class="'. ($even ? 'tr_even' : 'tr_odd') .'"><td>'.T($class).':<b>'.T($column['Field']).'</b>:&nbsp;</td><td>';
515 $even = !$even;
7d20381f 516 $name="values[$class][".$column['Field'].'][]';
16261142 517 $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false;
cdfce7c2 518 switch(true) {
a084118b 519 case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)):
90638f10 520 if(is_bool($val) && !$val) $val = '';
fc5c5b8b 521 $html.=$this->input($name, $val, 'hidden');
725edff7 522 $html.='<span class="disabled"><i>[AUTO]</i> '.$val.'</span>';
cdfce7c2
TM
523 break;
524 case isset($selectbox[$column['Field']]):
e9cb8cea 525 $html.=$this->select($name,$selectbox[$column['Field']],$val);
cdfce7c2 526 break;
120e3b45
TM
527 case isset($textarea[$class]) && in_array($column['Field'],$textarea[$class]):
528 $html.=$this->input($name, $val, 'textarea');
529 break;
530 default:
fc5c5b8b 531 $html.=$this->input($name, $val);
cdfce7c2
TM
532 break;
533 }
3634575a 534 $html.='</td></tr>';
cdfce7c2 535 }
3634575a 536 $html.='</table>';
d0e7939c
TM
537 return $html;
538 }
cdfce7c2 539
d0e7939c
TM
540 function render_insert_form_multi($array) {
541 $html = '';
542 $head=false;
543
544 foreach($array as $key => $args) {
545 $parts=array('inputs');
546 if(!$head) { $head = true;
547 $parts[]='head';
548 }
549 if(!isset($array[$key+1])) {
550 $parts[]='foot';
5b0075fa
TM
551 $hr = '';
552 } else $hr = '<hr />';
553 //$args[] = false;
d0e7939c 554 $args[] = $parts;
f5baa075 555
d0e7939c 556 $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
5b0075fa 557 $html .= $hr;
d0e7939c
TM
558 }
559 return $html;
560 }
561
562 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
563 $html = '';
564 //print_r($parts);
565 //echo('<pre>'); print_r($selectbox);
566 //echo('<pre>'); print_r($current);
567 $update = false;
dda9984c 568 $current_new=array();
d0e7939c
TM
569 if(is_array($current)) {
570 $update = true;
dda9984c
TM
571 $current_new = array_merge($current_new,array_shift($current));
572 }
573 if(isset($_GET['insert']) && is_array($_GET['insert'])) {
574 $update = true;
575 $current_new = array_merge($current_new,$_GET['insert']);
d0e7939c
TM
576 }
577
578 if(!is_array($hidecols)) $hidecols = array();
579 $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
580
581 if(!is_array($parts) || in_array('head', $parts)) {
582 $action = $action ? " action='$action'" : false;
583 $html.="<form$action method='POST'>"; //TODO: use $this->form()
5b0075fa 584 $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">';
cdfce7c2
TM
585 }
586
d0e7939c 587 if(!is_array($parts) || in_array('inputs', $parts))
dda9984c 588 $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current_new,$hidecols,$update);
d0e7939c
TM
589
590 if(!is_array($parts) || in_array('foot', $parts)) {
5b0075fa 591 $html .= '</div></span><br style="clear:both" />';
d0e7939c
TM
592 if($multi_insert) { //TODO, move to separate JS file
593 $html.=<<<EOF
d0e7939c
TM
594 <script>
595 function duplicate_element(what, where) {
596 var node = document.getElementsByName(what)[0];
597 node.parentNode.appendChild(node.cloneNode(true));
598 }
599 </script>
600 <a href='#' onClick="duplicate_element('input_set')">+</a>
601EOF;
602 }
603
dda9984c 604 $btn = count($current_new)>0 ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
d0e7939c
TM
605 $html.=$this->input(false, $btn, 'submit');
606 $html.='</form>';
607 }
fc5c5b8b 608 return $html;
cdfce7c2
TM
609 }
610}
611
78bf26a5
TM
612/**
613* Trida poskytuje rozhrani k databazi skladu
614*
615* @package Sklad_DB
616* @author Tomas Mudrunka
617*/
cdfce7c2
TM
618class Sklad_DB extends PDO {
619 function __construct() {
63747dad 620 $this->auth = new Sklad_Auth();
cdfce7c2
TM
621
622 parent::__construct(
623 DB_DSN, DB_USER, DB_PASS,
624 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL
625 );
626 }
627
628 function escape($str) {
629 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
630 }
631
382324d3
TM
632 function quote_identifier($str) {
633 return '`'.$this->escape($str).'`'; //TODO HACK
634 }
635
cb8a6861 636 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
2bedfdac 637 //Configuration
cdfce7c2 638 $join = array(
ea921a95
TM
639 'barcode' => array('model', 'category', 'producer'),
640 'item' => array('barcode', 'model', 'category', 'producer', 'vendor', 'room', 'location', 'status'),
cdfce7c2 641 'model' => array('category', 'producer')
9ea191cb 642 ); //TODO Autodetect using foreign keys?
67360e50
TM
643 $join2 = array(
644 'model' => array('barcode'=>'model_id')
645 );
ecaeb1d6 646 $fields_search = array(
f2a3e8b0
TM
647 'item' => array('item_id','item_serial','model_name','barcode_name','model_descript','producer_name','vendor_name'),
648 'model' => array('model_id','model_name','barcode_name','model_descript','producer_name')
9ea191cb 649 ); //TODO Autodetect
67360e50
TM
650 $group_concat = array(
651 'model' => array('barcode_name'=>'model_id')
652 );
2bedfdac 653
d9d47bd3
TM
654 //Init
655 if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
656
2bedfdac
TM
657 //Escaping
658 $class = $this->escape($class);
659
67360e50
TM
660 //GROUP_CONCAT
661 $group_concat_query = '';
662 $group_by = '';
663 if(isset($group_concat[$class])) foreach($group_concat[$class] as $gc => $gb) {
664 $group_concat_query .= ",group_concat($gc separator ', ')";
665 $group_by .= "GROUP BY $gb\n";
666 }
667
2bedfdac 668 //SELECT
67360e50 669 $sql="SELECT *$group_concat_query FROM `$class`\n";
2bedfdac 670 //JOIN
382324d3 671 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n";
67360e50 672 if(isset($join2[$class])) foreach($join2[$class] as $j => $c) $sql .= "LEFT JOIN `$j` USING($c)\n";
2bedfdac 673 //WHERE/REGEXP
cdfce7c2
TM
674 if($search) {
675 $search = $this->quote($search);
ecaeb1d6 676 if(!isset($fields_search[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get
5895162b 677 $sql_search = '';
ecaeb1d6 678 foreach($fields_search[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
5895162b 679 $where[] = "FALSE $sql_search";
cb8a6861
TM
680 } elseif($id) $where[] = "$class$suffix_id = $id";
681 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
682
5895162b 683 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
67360e50
TM
684
685 //GROUP BY
686 $sql.=$group_by;
687
117817be 688 //ORDER
fd479ff5 689 if(!$order) $order = $class.$suffix_id.' DESC';
117817be 690 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
1fb6317f 691 $sql .= "ORDER BY $order\n";
2bedfdac 692 //LIMIT/OFFSET
cdfce7c2
TM
693 if($limit) {
694 $limit = $this->escape((int)$limit);
695 $offset = $this->escape((int)$offset);
696 $sql .= "LIMIT $offset,$limit\n";
697 }
2bedfdac 698
cdfce7c2
TM
699 return $sql;
700 }
701
66b6f4d6 702 function safe_query($sql, $fatal=true) {
cdfce7c2
TM
703 $result = $this->query($sql);
704 if(!$result) {
2bedfdac 705 $error = $this->errorInfo();
66b6f4d6
TM
706 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>");
707 if($fatal) die();
cdfce7c2
TM
708 }
709 return $result;
710 }
711
d9601e5d 712 function translate_query_results(&$result) {
d1686e10 713 $translate_cols = array('item_valid_till'); //TODO: Hardcoded
326a9fc9
TM
714 foreach($result as $key => $row) {
715 foreach($translate_cols as $col) if(isset($result[$key][$col])){
716 $result[$key][$col] = T($result[$key][$col]);
717 }
718 }
d9601e5d
TM
719 }
720
d7d9ce39 721 function load_backend_data_to_query_results(&$result,$suffix_backend='_backend') {
d9601e5d
TM
722 $translate_cols = array(
723 'item_author' => 'return($this->auth->get_username_by_id($result[$key][$col]));'
724 ); //TODO: Hardcoded
725 foreach($result as $key => $row) {
726 foreach($translate_cols as $col => $backend) if(isset($result[$key][$col])){
d7d9ce39 727 $result[$key][$col.$suffix_backend] = eval($backend);
d9601e5d
TM
728 }
729 }
326a9fc9
TM
730 }
731
732 function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO::FETCH_ASSOC, $translate=true) {
a1545cbd
TM
733 $result = $this->safe_query($sql, $fatal);
734 if(!$fatal && !$result) return $result;
735 $result = $result->fetchAll($fetch_flags);
d9601e5d
TM
736 $this->load_backend_data_to_query_results($result);
737 if($translate) $this->translate_query_results($result);
326a9fc9
TM
738 return $result;
739 }
740
cb8a6861
TM
741 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
742 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
326a9fc9 743 $result = $this->safe_query_fetch($sql);
cdfce7c2
TM
744 if(!$result || !is_array($indexed)) return $result;
745
746 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
747 return $indexed;
748 }
749
d0e7939c 750 function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
cdfce7c2 751 $class = $this->escape($class);
37595945 752 $sql = "SHOW COLUMNS FROM `$class`;";
d0e7939c
TM
753 $columns = $this->safe_query_fetch($sql);
754 /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
755 if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
756 }*/
757 return $columns;
cdfce7c2
TM
758 }
759
760 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
1c99d83b 761 $selectbox=array( //TODO: Hardcoded...
41e57f9e 762 'model_countable' => array(0 => 'no', 1 => 'yes'),
3e939604
TM
763 'model_eshop_hide' => array(0 => 'no', 1 => 'yes'),
764 'vendor_id' => array('COMPULSORY' => 'select...')
1c99d83b 765 );
cdfce7c2 766 foreach($columns as $column) {
66b6f4d6 767 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
768 if($class && $column['Field'] == $class.$suffix_id) continue;
769 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
770 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
66b6f4d6 771
117817be 772 $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : '';
b4dcae05 773 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
326a9fc9 774 $result = $this->safe_query_fetch($sql, false);
66b6f4d6 775 if(!$result) continue;
cdfce7c2
TM
776 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
777 }
778 //echo('<pre>'); print_r($selectbox);
8972e501 779 //return $selectbox;
3634575a 780 //return array_filter($selectbox, 'ksort');
725edff7
TM
781 return array_filter($selectbox, 'natcasesort');
782 //array_multisort($selectbox); return $selectbox;
9ea191cb
TM
783 }
784
d0e7939c 785 function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
16261142
TM
786 $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
787 $value=$this->quote($value);
788 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
326a9fc9 789 $result = $this->safe_query_fetch($sql);
d0e7939c 790 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
791 }
792
9ea191cb
TM
793 function contains_history($table) {
794 $history_tables = array('item'); //TODO Autodetect
795 return in_array($table, $history_tables);
cdfce7c2
TM
796 }
797
798 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
b66fadbb
TM
799 //Init
800 $history = $this->contains_history($table);
801
9ea191cb 802 //Escaping
cdfce7c2
TM
803 $table = $this->escape($table);
804
805 //Get list of POSTed columns
aa3fd0a8
TM
806 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
807 $columns = implode(',',$columns_array);
cdfce7c2 808
9fb856ba 809 //Build query
b66fadbb 810 $sql = '';
b66fadbb
TM
811 //echo('<pre>'); die(print_r($values));
812
813 if($history) {
814 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
815 if($history_update) {
816 $sql .= "UPDATE $table";
117817be
TM
817 $sql .= " SET ${table}_valid_till=NOW()";
818 $sql .= " WHERE ${table}_valid_till=0 AND (";
b66fadbb
TM
819 $or = '';
820 foreach($values as $row) {
5b0075fa 821 $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
b66fadbb
TM
822 $or = ' OR';
823 }
824 $sql .= " );\n\n";
825 $replace = false;
826 }
827 }
828
cdfce7c2 829 //Insert into table (columns)
aa3fd0a8 830 $sql .= "INSERT INTO $table ($columns) VALUES ";
cdfce7c2
TM
831
832 //Values (a,b,c),(d,e,f)
833 $comma='';
834 foreach($values as $row) {
9fb856ba
TM
835 $row_quoted = array_map(array($this,'quote'), $row); //Check
836 if($history) {
b66fadbb
TM
837 foreach($row as $column => $value) {
838 switch($column) {
839 case $table.'_valid_from':
840 $row_quoted[$column] = 'NOW()';
841 break;
842 case $table.'_valid_till':
843 $row_quoted[$column] = '0';
844 break;
9fb856ba 845 case $table.'_author':
e9f6461f
TM
846 $row_quoted[$column] = $this->auth->get_user_id();
847 //die($this->auth->get_user_id().'=USER');
b66fadbb 848 break;
b66fadbb
TM
849 }
850 }
851 }
852 $sql .= $comma.'('.implode(',',$row_quoted).')';
cdfce7c2
TM
853 $comma = ',';
854 }
855
aa3fd0a8
TM
856 //On duplicate key
857 if($replace) {
858 foreach($columns_array as $col) {
859 if($col == $table.'_id' || $col == $table.'_valid_till') continue;
860 $on_duplicate[] = "$col=VALUES($col)";
861 }
862 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
863 }
864
cdfce7c2
TM
865 //Terminate
866 $sql .= ';';
867 return $sql;
868 }
869
b4dcae05
TM
870 function insert_or_update($table, $values, $replace=true) {
871 $sql = $this->build_query_insert($table, $values, $replace);
cdfce7c2
TM
872 $this->safe_query($sql);
873 return $this->lastInsertId();
874 }
875
b4dcae05 876 function insert_or_update_multitab($values, $replace=true) {
371a86f4 877 $last=false;
b4dcae05 878 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
371a86f4
TM
879 return $last;
880 }
881
cdfce7c2 882 function delete($table, $id, $suffix_id='_id') {
64f31c54 883 if($this->contains_history($table)) return false;
cdfce7c2
TM
884 $key = $this->escape($table.$suffix_id);
885 $table = $this->escape($table);
886 $id = $this->quote($id);
887 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
888 }
889}
890
0a027cc7
TM
891/**
892* Trida poskytuje high-level rozhrani k databazi skladu
893*
894* @package Sklad_DB_Abstract
895* @author Tomas Mudrunka
896*/
897class Sklad_DB_Abstract extends Sklad_DB {
898 //TODO Code
899}
900
78bf26a5
TM
901/**
902* Trida implementuje uzivatelske rozhrani skladu
903*
904* Example usage:
905* $sklad = new Sklad_UI();
906* $sklad->process_http_request();
907*
908* @package Sklad_UI
909* @author Tomas Mudrunka
910*/
cdfce7c2
TM
911class Sklad_UI {
912 function __construct() {
913 $this->db = new Sklad_DB();
914 $this->html = new Sklad_HTML();
915 }
916
cb8a6861 917 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
d6975011 918 return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
cdfce7c2
TM
919 }
920
a25f85f8 921 function render_form_add($class) {
cdfce7c2
TM
922 $columns = $this->db->get_columns($class);
923 $selectbox = $this->db->columns_get_selectbox($columns, $class);
a25f85f8 924 return $this->html->render_insert_form($class, $columns, $selectbox);
cdfce7c2
TM
925 }
926
f5baa075 927 function render_form_edit($class, $id, $multi_insert) {
cdfce7c2
TM
928 $columns = $this->db->get_columns($class);
929 $selectbox = $this->db->columns_get_selectbox($columns, $class);
117817be 930 $current = $this->db->get_listing($class, $id, 1);
f5baa075 931 return $this->html->render_insert_form($class, $columns, $selectbox, $current, false, false, $multi_insert);
cdfce7c2
TM
932 }
933
8f451ba7 934 function render_single_record_details($class, $id, $barcode=true) {
cdfce7c2
TM
935 $id_next = $id + 1;
936 $id_prev = $id - 1 > 0 ? $id - 1 : 0;
937 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
a25f85f8
TM
938 $html='';
939 $html.= $this->html->link('<<', "$class/$id_prev/");
940 $html.= '-';
941 $html.= $this->html->link('>>', "$class/$id_next/");
8f451ba7 942 $html.= '&nbsp;&nbsp;&nbsp;';
60ec0b75 943 $barcode && $html.='<span style="float:right;" class="floating_barcode">'.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")).'</span>';
a25f85f8 944 $html.= $this->html->link('edit', "$class/$id/edit/");
9fb856ba 945 if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
a25f85f8 946 return $html;
cdfce7c2
TM
947 }
948
a25f85f8 949 function render_listing_navigation($class, $id, $limit, $offset) {
cdfce7c2
TM
950 $offset_next = $offset + $limit;
951 $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0;
952 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
e0f8e591 953 $moreget = isset($get[0]) ? '&' : '?';
a25f85f8
TM
954 $html='';
955 $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get");
956 $html.= '-';
e0f8e591 957 $html.= $this->html->link('[*]', "$class/$id/0/0/$get$moreget".'noimgs');
ef0fa7a4 958 $html.= '-';
a25f85f8 959 $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get");
a4dae920 960 $html.= '&nbsp;&nbsp;&nbsp;';
a25f85f8
TM
961 $html.= $this->html->link('new', "$class/new/$get");
962 return $html;
cdfce7c2
TM
963 }
964
8f451ba7 965 function render_listing_extensions($class, $id, $limit, $offset, $edit=false, $barcode=true) {
a25f85f8 966 $html='';
cdfce7c2 967 if(is_numeric($id)) {
8f451ba7 968 $html.=$this->render_single_record_details($class, $id, $barcode);
cdfce7c2 969 } else {
a25f85f8 970 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
cdfce7c2 971 }
60ec0b75 972 if($edit && $barcode) {
f5baa075 973 $html.= $this->render_form_edit($class, $id, false);
cdfce7c2 974 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
35916247
TM
975 $html.=$this->html->form($action,'POST',array(
976 array(false,'DELETE','submit'),
977 array('sure', false, 'checkbox', false, false, 'sure?')
978 ));
cdfce7c2 979 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
35916247
TM
980 $html.=$this->html->form($action,'POST',array(
981 array('image', false, 'file', false, 'size="30"'),
982 array(false, 'IMAGE', 'submit')
983 ), "enctype='multipart/form-data'");
cdfce7c2 984 }
a25f85f8 985 return $html;
cdfce7c2
TM
986 }
987
988 function check_auth() {
042a4988 989 new HTTP_Auth('WareHouse ['.BACKEND_AUTH.']', true, array($this->db->auth,'check_auth'));
cdfce7c2
TM
990 }
991
a8bbdc31
TM
992 function post_redirect_get($location, $message='', $error=false, $translate=true) {
993 $messaget = $translate ? T($message) : $message;
994 $url_args = $messaget != '' ? '?message='.urlencode($messaget) : '';
8acef003 995 $location = $this->html->internal_url($location).$url_args;
1f74892d 996 header('Location: '.$location);
64f31c54 997 if($error) trigger_error($message);
7efdf72a
TM
998 $location=htmlspecialchars($location);
999 die(
1000 "<meta http-equiv='refresh' content='0; url=$location'>".
a8bbdc31 1001 $messaget."<br />Location: <a href='$location'>$location</a>"
7efdf72a 1002 );
cdfce7c2
TM
1003 }
1004
623c65e2 1005 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
64f31c54 1006 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
3e6c412e 1007 $filename="$dir/$name$ext";
64f31c54 1008 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
623c65e2 1009 foreach($vars as $var => $val) $$var=$val;
3e6c412e
TM
1010 ob_start();
1011 include($filename);
1012 $out=ob_get_contents();
1013 ob_end_clean();
1014 return $out;
1015 }
1016
a8bbdc31
TM
1017 function check_input_validity($field, $value='', $ruleset=0) {
1018 $rules = array(0 => array(
1019 'model_barcode' => '/./',
ea921a95 1020 'barcode_name' => '/./',
3e939604
TM
1021 'item_serial' => '/./',
1022 'vendor_id' => '/^[0-9]*$/'
a8bbdc31
TM
1023 ));
1024 if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false;
1025 return true;
1026 }
1027
539ab3c1
TM
1028 function check_locks() {
1029 $result = $this->db->safe_query_fetch("SELECT * FROM `lock`;");
1030 if(!empty($result)) {
1031 echo T('There are locks:').' '.$this->html->render_item_table($result);
1032 $this->post_redirect_get('', 'There are locks!', true);
1033 }
1034 }
1035
bda4a4be 1036 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
cdfce7c2 1037 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
1f74892d 1038 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
cdfce7c2
TM
1039
1040 //SephirPOST:
371a86f4
TM
1041
1042 /* Tenhle foreach() prekopiruje promenne
7d20381f 1043 * z: $_POST['values'][$table][$column][$id];
371a86f4
TM
1044 * do: $values[$table][$id][$column]
1045 */
7d20381f 1046 if(isset($_POST['values'])) {
539ab3c1 1047 $this->check_locks();
7d20381f
TM
1048 $values=array();
1049 foreach($_POST['values'] as $table => $columns) {
1050 foreach($columns as $column => $ids) {
a8bbdc31
TM
1051 foreach($ids as $id => $val) {
1052 $values[$table][$id][$column] = trim($val);
1053 if(!$this->check_input_validity($column,$val)) {
1054 $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS
1055 $this->html->link('GO BACK', 'javascript:history.back()', false, false);
1056 $this->post_redirect_get('', $message, false, false);
1057 }
1058 }
7d20381f 1059 }
cdfce7c2 1060 }
7d20381f 1061 //die(print_r($values));
cdfce7c2
TM
1062 }
1063
1064 if($action) switch($action) {
1065 case 'new':
b4dcae05 1066 $replace = false;
cdfce7c2 1067 case 'edit':
539ab3c1 1068 $this->check_locks();
b4dcae05 1069 if(!isset($replace)) $replace = true;
64f31c54 1070 $table = $class ? $class : 'item';
cdfce7c2 1071 //print_r($values); //debug
b4dcae05 1072 $last = $this->db->insert_or_update_multitab($values, $replace);
bda4a4be 1073 $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/";
d9384cb5 1074 $next = "$table/new/";
bda4a4be
TM
1075 $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.';
1076 $this->post_redirect_get($last, $message);
cdfce7c2
TM
1077 break;
1078 case 'delete':
539ab3c1 1079 $this->check_locks();
64f31c54
TM
1080 if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
1081 $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
1f74892d 1082 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
cdfce7c2
TM
1083 break;
1084 case 'image':
1085 $image_classes = array('model'); //TODO, use this more widely across the code
64f31c54 1086 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
cdfce7c2 1087 $image_destination = DIR_IMAGES."/$class/$id.jpg";
326a9fc9 1088 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true);
cdfce7c2 1089 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
1f74892d 1090 chmod ($image_destination, 0664);
326a9fc9
TM
1091 $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)');
1092 } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true);
cdfce7c2
TM
1093 break;
1094 default:
326a9fc9 1095 $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*');
cdfce7c2
TM
1096 break;
1097 }
1098
1099 die('POSTed pyčo!');
1100 }
1101
1102 function process_http_request() {
fe5e0ee2
TM
1103 $listing_limit_classes = array('item','model');
1104
cdfce7c2
TM
1105 $this->check_auth();
1106
1107 @ini_set('magic_quotes_gpc' , 'off');
1108 if(get_magic_quotes_gpc()) {
1109 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
1110 }
1111
1112 $PATH_INFO=@trim($_SERVER[PATH_INFO]);
a5094502 1113 if($PATH_INFO == '' || $PATH_INFO == '/') $PATH_INFO = FRONTEND_PAGE_WELCOME;
cdfce7c2 1114 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
81ab8aef 1115 //Sephirot:
cdfce7c2 1116 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
cd65d0c4 1117 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
1118 $result = $this->db->safe_query_fetch("SELECT * FROM `lock`;");
1119 $headerhtml = !empty($result) ? T('There are locks:').' '.$this->html->render_item_table($result) : '';
cd65d0c4
TM
1120 echo $this->html->header($PATH_INFO,$this->db->auth->get_user(),$headerhtml);
1121 }
81ab8aef 1122 switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
cdfce7c2
TM
1123 case 'test': //test
1124 die('Tell me why you cry');
1125 break;
88c38b26
TM
1126 case 'assistant': case 'api': //assistant|api
1127 $incdirs = array(
1128 'assistant' => DIR_ASSISTANTS,
1129 'api' => DIR_APIS
1130 );
de77377e
TM
1131 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
1132 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
1133 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
1134 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
5ef6c52f 1135 $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
88c38b26 1136 echo $this->safe_include($incdirs[$PATH_CHUNKS[1]],$PATH_CHUNKS[2],$assistant_vars);
3e6c412e 1137 break;
ea921a95 1138 case 'barcodeimg': //barcode
81ab8aef
TM
1139 Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
1140 break;
cdfce7c2
TM
1141 default: //?
1142 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
1143 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
1144 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
1145 switch($PATH_CHUNKS[2]) {
1146 case 'new': //?/new
1147 $this->process_http_request_post($PATH_CHUNKS[2], $class);
a25f85f8 1148 echo $this->render_form_add($class);
cdfce7c2
TM
1149 break;
1150 default: //?/?
1151 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false);
1152 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
1153 $edit=false;
1154 switch($PATH_CHUNKS[3]) {
1155 case 'edit': //?/?/edit
9fb856ba
TM
1156 case 'image': //?/?/image
1157 case 'delete': //?/?/delete
cdfce7c2
TM
1158 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
1159 $edit=true;
1160 default: //?/?/?
9fb856ba 1161 $history = $PATH_CHUNKS[3] == 'history' ? true : false;
fe5e0ee2 1162 $limit = is_numeric($PATH_CHUNKS[3]) ? (int) $PATH_CHUNKS[3] : (in_array($class, $listing_limit_classes) ? FRONTEND_LISTING_LIMIT : 0);
2415a365 1163 $offset = isset($PATH_CHUNKS[4]) ? (int) $PATH_CHUNKS[4] : 0;
d9d47bd3 1164 $where = @is_array($_GET['where']) ? $_GET['where'] : false;
8f451ba7 1165 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit, false);
cb8a6861 1166 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
a25f85f8 1167 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
cdfce7c2
TM
1168 //print_r(array("<pre>",$_SERVER));
1169 break;
1170 }
1171 break;
1172 }
1173 break;
1174 }
1175 }
1176}
1177
1178$sklad = new Sklad_UI();
1179$sklad->process_http_request();
1180
54694117 1181echo('<br style="clear:both;" /><hr />');
This page took 1.185023 seconds and 4 git commands to generate.