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