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