Commit | Line | Data |
---|---|---|
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 | ||
20 | require_once('sklad.conf.php'); | |
5f093236 TM |
21 | set_include_path(DIR_LIB.PATH_SEPARATOR.get_include_path()); |
22 | ||
cdfce7c2 TM |
23 | require_once('Sklad_LMS-fake.class.php'); |
24 | require_once('HTTP_Auth.class.php'); | |
326a9fc9 | 25 | require_once('Locale.class.php'); |
81ab8aef | 26 | require_once('Barcode.class.php'); |
cdfce7c2 | 27 | |
0a027cc7 TM |
28 | /** |
29 | * Trida poskytuje vseobecne funkce pro generovani HTML kodu | |
30 | * | |
31 | * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)! | |
32 | * | |
33 | * @package HTML | |
34 | * @author Tomas Mudrunka | |
35 | */ | |
36 | class HTML { | |
37 | function row($row) { | |
38 | $html='<tr>'; | |
39 | foreach($row as $var) { | |
40 | if(trim($var) == '') $var = ' '; | |
41 | $html.="<td>$var</td>"; | |
42 | } | |
43 | $html.='</tr>'; | |
44 | return $html; | |
45 | } | |
46 | ||
47 | function table(&$table, $params='border=1') { | |
48 | $html="<table $params>"; | |
49 | $header=true; | |
50 | foreach($table as $row) { | |
51 | if($header) { | |
52 | $html.=$this->row(array_keys($row)); | |
53 | $header=false; | |
54 | } | |
55 | $html.=$this->row($row); | |
56 | } | |
57 | $html.='</table>'; | |
58 | return $html; | |
59 | } | |
60 | ||
81ab8aef | 61 | function link($title='n/a', $link='#void', $internal=true, $translate=true) { |
35916247 | 62 | if($internal && (!isset($link[0]) || $link[0] != '#')) $link = $this->internal_url($link); |
81ab8aef TM |
63 | if($translate) $title = T($title); |
64 | return "<a href='$link'>".$title."</a>"; | |
0a027cc7 TM |
65 | } |
66 | ||
81ab8aef TM |
67 | function img($src='#void', $title='img', $options='width=64') { |
68 | $options = $options ? " $options" : ''; | |
69 | return "<img src='$src' alt='$title' title='$title'$options; />"; | |
0a027cc7 TM |
70 | } |
71 | ||
35916247 TM |
72 | function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') { |
73 | $html = T($prefix)."<input type='$type' "; | |
0a027cc7 | 74 | if($name) $html.= "name='$name' "; |
326a9fc9 TM |
75 | if(!is_bool($value)) { |
76 | if($type == 'submit') $value = T($value); | |
77 | $html.= "value='$value' "; | |
78 | } | |
0a027cc7 TM |
79 | if($options) $html.= "$options "; |
80 | if($placeholder) $html.= "placeholder='$placeholder' "; | |
81 | $html .= '/>'; | |
82 | return $html; | |
83 | } | |
84 | ||
35916247 TM |
85 | function form($action=false, $method=false, $inputs, $options=false) { |
86 | $action = $action ? " action='$action'" : ''; | |
87 | $method = $method ? " method='$method'" : ''; | |
88 | $options = $options ? " $options" : ''; | |
89 | $html = "<form$action$method$options>"; | |
90 | foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input); | |
91 | $html .= "</form>"; | |
92 | return $html; | |
93 | } | |
94 | ||
0a027cc7 TM |
95 | function select($name, $selectbox, $default=false) { |
96 | //echo('<pre>'); print_r($selectbox); | |
97 | $html = "<select name='$name'>"; | |
98 | ||
99 | if($default) { | |
100 | $value=$default; $title=$selectbox[$value]; | |
101 | $html .= "<option value='$value'>$value :: $title</option>"; | |
102 | unset($selectbox[$value]); | |
103 | } | |
104 | foreach($selectbox as $value => $title) { | |
105 | $html .= "<option value='$value'>$value :: $title</option>"; | |
106 | } | |
107 | $html .= "</select>"; | |
108 | return $html; | |
109 | } | |
35916247 TM |
110 | |
111 | function ul($items,$tag=ul,$head='',$class=false) { | |
112 | $class = $class ? " class='$class'" : ''; | |
113 | $html = "$head<$tag$class>"; | |
114 | foreach($items as $key => $value) { | |
115 | $html .= '<li>'; | |
116 | if(is_numeric($key)) { | |
117 | $html .= $value; | |
118 | } else { | |
119 | $html .= $this->link($key,$value); | |
120 | } | |
121 | $html .= '</li>'; | |
122 | } | |
123 | $html .= "</$tag>"; | |
124 | return $html; | |
125 | } | |
126 | ||
127 | function div($html, $options) { | |
128 | $options = $options ? " $options" : ''; | |
129 | return "<div$options>$html</div>"; | |
130 | } | |
ec106ddf TM |
131 | |
132 | function head($title=false,$charset='UTF-8',$more='') { | |
133 | $title = $title ? "\n<title>$title</title>" : ''; | |
134 | $html= '<head>'; | |
135 | $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more; | |
136 | $html.= '</head>'; | |
137 | return $html; | |
138 | } | |
0a027cc7 TM |
139 | } |
140 | ||
78bf26a5 TM |
141 | /** |
142 | * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad | |
143 | * | |
fc5c5b8b TM |
144 | * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)! |
145 | * | |
78bf26a5 TM |
146 | * @package Sklad_HTML |
147 | * @author Tomas Mudrunka | |
148 | */ | |
35916247 | 149 | class Sklad_HTML extends HTML { //TODO: Split into few more methods |
fc5c5b8b | 150 | function header($title='') { |
cdfce7c2 TM |
151 | $home = URL_HOME; |
152 | $script = $_SERVER['SCRIPT_NAME']; | |
d9384cb5 | 153 | $search = htmlspecialchars(@trim($_GET['q'])); |
64f31c54 | 154 | $message = strip_tags(@trim($_GET['message']),'<a><b><u><i>'); |
ac2ed3a1 | 155 | $instance = INSTANCE_ID != '' ? '/'.INSTANCE_ID : ''; |
35916247 | 156 | //$title = T($title); //TODO |
ec106ddf TM |
157 | |
158 | $html = $this->head("SōkoMan$title"); | |
159 | $html .= <<<EOF | |
ac2ed3a1 | 160 | <h1><a href="$script/">SōkoMan</a><small>$instance$title</small></h1> |
c9759229 TM |
161 | |
162 | <style type="text/css"> | |
ac2ed3a1 TM |
163 | * { font-family: arial; } |
164 | ||
7bcd6b42 | 165 | .menu li { |
c9759229 | 166 | float: left; |
7bcd6b42 TM |
167 | padding: 0.2em; |
168 | } | |
169 | ||
170 | .menu * li { | |
171 | float: none; | |
c9759229 TM |
172 | } |
173 | ||
174 | .menu * menu { | |
175 | position: absolute; | |
7bcd6b42 | 176 | padding: 0.2em; |
c9759229 TM |
177 | } |
178 | ||
179 | .menu, .menu * menu { | |
180 | list-style: none; | |
181 | } | |
182 | ||
183 | .menu * menu { | |
7bcd6b42 | 184 | border: 1px solid orange; |
c9759229 TM |
185 | display: none; |
186 | margin: 0; | |
187 | } | |
188 | ||
7bcd6b42 | 189 | .menu li:hover menu, .menu li:hover { |
c9759229 | 190 | display: block; |
7bcd6b42 | 191 | background-color: yellow; |
c9759229 | 192 | } |
7bcd6b42 | 193 | |
c9759229 TM |
194 | </style> |
195 | ||
cdfce7c2 | 196 | <div> |
35916247 TM |
197 | EOF; |
198 | ||
199 | $assistants=array(); | |
200 | foreach(scandir(DIR_ASSISTANTS) as $item) { | |
201 | if($item == '.' || $item == '..') continue; | |
202 | $item = preg_replace('/\.inc\.php$/','',$item); | |
203 | $assistants[$item] = "assistant/$item"; | |
204 | } | |
205 | ||
206 | $tables=array('item','model','category','producer','vendor','room','status'); | |
207 | ||
208 | foreach($tables as $table) { | |
209 | $listable[$table] = $table; | |
210 | $insertable[$table] = "$table/new"; | |
211 | } | |
212 | ||
213 | $html .= $this->ul(array( | |
35916247 | 214 | 'Home' => '', |
ec106ddf | 215 | 'Logout' => '?logout', |
35916247 | 216 | 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')), |
ec106ddf TM |
217 | 1 => $this->ul($insertable,'menu',$this->link('New','#')), |
218 | 2 => $this->ul($listable,'menu',$this->link('List','#')) | |
35916247 TM |
219 | ),'menu', '', 'menu'); |
220 | ||
221 | $html .= '<div style="float: right;">'; | |
222 | ||
223 | $html .= $this->form("$script/assistant/go", 'GET', array( | |
81ab8aef | 224 | array('q','','text','smart id...', 'autofocus'), |
35916247 TM |
225 | array(false,'go','submit') |
226 | ), 'style="float: left;"'); | |
227 | ||
228 | $html .= $this->form('?', 'GET', array( | |
229 | array('q',$search,'text','regexp...'), | |
230 | array(false,'filter','submit') | |
231 | ), 'style="float: left;"'); | |
232 | ||
233 | $html .= '</div>'; | |
234 | ||
235 | $html .= <<<EOF | |
cdfce7c2 | 236 | </div> |
c9759229 | 237 | <hr style="clear: both;" /> |
d9384cb5 TM |
238 | <div style="background-color:#FFDDDD;"> |
239 | <font color="red">$message</font> | |
240 | </div> | |
cdfce7c2 | 241 | EOF; |
35916247 TM |
242 | |
243 | return $html; | |
cdfce7c2 TM |
244 | } |
245 | ||
d9384cb5 TM |
246 | function internal_url($link) { |
247 | return $_SERVER['SCRIPT_NAME'].'/'.$link; | |
248 | } | |
249 | ||
cdfce7c2 TM |
250 | function table_add_images(&$table) { |
251 | $image = array('model_id'); | |
252 | foreach($table as $id => $row) { | |
253 | foreach($image as $column) if(isset($table[$id][$column])) { | |
254 | $type = @array_shift(preg_split('/_/', $column)); | |
255 | $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg'; | |
256 | $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]); | |
257 | } | |
258 | } | |
259 | } | |
260 | ||
81ab8aef TM |
261 | function render_barcode($barcode,$opts=false) { |
262 | return $this->link($this->img($this->internal_url("barcode/$barcode"),$barcode,$opts),"barcode/$barcode",true,false); | |
263 | } | |
264 | ||
265 | function table_add_barcodes(&$table) { | |
266 | $image = array('model_barcode', 'item_serial'); | |
267 | foreach($table as $id => $row) { | |
268 | foreach($image as $column) if(isset($table[$id][$column])) { | |
269 | $table[$id][$column]=$this->render_barcode($table[$id][$column]); | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
cdfce7c2 TM |
274 | function table_collapse(&$table) { |
275 | $collapse = array( | |
276 | 'item_id' => 'item_id', | |
277 | 'model_id' => 'model_name', | |
278 | 'category_id' => 'category_name', | |
279 | 'producer_id' => 'producer_name', | |
280 | 'vendor_id' => 'vendor_name', | |
281 | 'room_id' => 'room_name', | |
282 | 'status_id' => 'status_name', | |
283 | ); | |
284 | foreach($table as $id => $row) { | |
285 | foreach($collapse as $link => $title) | |
fff6ce40 | 286 | if(isset($table[$id][$link]) && isset($row[$title])) { |
cdfce7c2 TM |
287 | $type = @array_shift(preg_split('/_/', $link)); |
288 | if($link != $title) unset($table[$id][$link]); | |
289 | $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/'); | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | function table_sort(&$table) { | |
295 | $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name'); | |
296 | $table_sorted = array(); | |
297 | foreach($table as $id => $row) { | |
298 | $table_sorted[$id] = array(); | |
299 | foreach($precedence as $column) if(isset($table[$id][$column])) { | |
326a9fc9 | 300 | $table_sorted[$id][T($column)]=$table[$id][$column]; |
cdfce7c2 TM |
301 | unset($table[$id][$column]); |
302 | } | |
326a9fc9 TM |
303 | //$table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]); |
304 | foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating | |
cdfce7c2 TM |
305 | } |
306 | $table = $table_sorted; | |
307 | } | |
308 | ||
fc5c5b8b | 309 | function render_item_table($table) { |
cdfce7c2 | 310 | $this->table_add_images($table); |
81ab8aef | 311 | $this->table_add_barcodes($table); |
cdfce7c2 TM |
312 | $this->table_collapse($table); |
313 | $this->table_sort($table); | |
fc5c5b8b | 314 | return $this->table($table); |
cdfce7c2 TM |
315 | } |
316 | ||
16261142 | 317 | function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true) { |
cdfce7c2 TM |
318 | //echo('<pre>'); print_r($selectbox); |
319 | //echo('<pre>'); print_r($current); | |
320 | $update = false; | |
321 | if(is_array($current)) { | |
322 | $update = true; | |
323 | $current = array_shift($current); | |
324 | } | |
325 | ||
a084118b TM |
326 | if(!is_array($hidecols)) $hidecols = array('item_author', 'item_valid_from', 'item_valid_till'); //TODO Autodetect |
327 | ||
16261142 | 328 | $action = $action ? " action='$action'" : false; |
35916247 | 329 | $html="<form$action method='POST'>"; //TODO: use $this->form() |
fc5c5b8b | 330 | if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">'; |
371a86f4 | 331 | //$html.=$this->input('table', $class, 'hidden'); |
cdfce7c2 | 332 | foreach($columns as $column) { |
326a9fc9 | 333 | $html.=T($class).':<b>'.T($column['Field']).'</b>: '; |
7d20381f | 334 | $name="values[$class][".$column['Field'].'][]'; |
16261142 | 335 | $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false; |
cdfce7c2 | 336 | switch(true) { |
a084118b | 337 | case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)): |
e9cb8cea | 338 | if(!$val) $val = ''; |
fc5c5b8b TM |
339 | $html.=$this->input($name, $val, 'hidden'); |
340 | $html.=$val.'(AUTO)'; | |
cdfce7c2 TM |
341 | break; |
342 | case isset($selectbox[$column['Field']]): | |
e9cb8cea | 343 | $html.=$this->select($name,$selectbox[$column['Field']],$val); |
cdfce7c2 TM |
344 | break; |
345 | default: | |
fc5c5b8b | 346 | $html.=$this->input($name, $val); |
cdfce7c2 TM |
347 | break; |
348 | } | |
fc5c5b8b | 349 | $html.='<br />'; |
cdfce7c2 TM |
350 | } |
351 | ||
352 | if($multi_insert) { | |
353 | //TODO, move to separate JS file | |
fc5c5b8b | 354 | $html.=<<<EOF |
cdfce7c2 TM |
355 | </div> |
356 | <span name="input_set_next"></span><br style="clear:both" /> | |
357 | <script> | |
358 | function duplicate_element(what, where) { | |
359 | document.getElementsByName(where)[0].outerHTML = | |
360 | document.getElementsByName(what)[0].outerHTML | |
361 | + document.getElementsByName(where)[0].outerHTML; | |
362 | } | |
363 | </script> | |
364 | <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a> | |
365 | EOF; | |
366 | } | |
367 | ||
bda4a4be | 368 | $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting... |
fc5c5b8b TM |
369 | $html.=$this->input(false, $btn, 'submit'); |
370 | $html.='</form>'; | |
371 | return $html; | |
cdfce7c2 TM |
372 | } |
373 | } | |
374 | ||
78bf26a5 TM |
375 | /** |
376 | * Trida poskytuje rozhrani k databazi skladu | |
377 | * | |
378 | * @package Sklad_DB | |
379 | * @author Tomas Mudrunka | |
380 | */ | |
cdfce7c2 TM |
381 | class Sklad_DB extends PDO { |
382 | function __construct() { | |
383 | $this->lms = new Sklad_LMS(); | |
384 | ||
385 | parent::__construct( | |
386 | DB_DSN, DB_USER, DB_PASS, | |
387 | array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL | |
388 | ); | |
389 | } | |
390 | ||
391 | function escape($str) { | |
392 | return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK | |
393 | } | |
394 | ||
cb8a6861 | 395 | function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') { |
2bedfdac | 396 | //Configuration |
cdfce7c2 TM |
397 | $join = array( |
398 | 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'), | |
399 | 'model' => array('category', 'producer') | |
9ea191cb | 400 | ); //TODO Autodetect using foreign keys? |
cdfce7c2 | 401 | $search_fields = array( |
df4079a6 | 402 | 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name') |
9ea191cb | 403 | ); //TODO Autodetect |
2bedfdac TM |
404 | |
405 | //Escaping | |
406 | $class = $this->escape($class); | |
407 | ||
408 | //SELECT | |
cdfce7c2 | 409 | $sql="SELECT * FROM $class\n"; |
2bedfdac | 410 | //JOIN |
8fd613d6 | 411 | if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n"; |
2bedfdac | 412 | //WHERE/REGEXP |
cdfce7c2 TM |
413 | if($search) { |
414 | $search = $this->quote($search); | |
326a9fc9 | 415 | if(!isset($search_fields[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get |
5895162b TM |
416 | $sql_search = ''; |
417 | foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search "; | |
418 | $where[] = "FALSE $sql_search"; | |
cb8a6861 TM |
419 | } elseif($id) $where[] = "$class$suffix_id = $id"; |
420 | if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0'; | |
421 | ||
5895162b | 422 | if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n"; |
117817be TM |
423 | //ORDER |
424 | if(!$order) $order = $class.$suffix_id; | |
425 | if($this->contains_history($class)) $order .= ",${class}_valid_from DESC"; | |
426 | $sql .= "ORDER BY $order\n"; | |
2bedfdac | 427 | //LIMIT/OFFSET |
cdfce7c2 TM |
428 | if($limit) { |
429 | $limit = $this->escape((int)$limit); | |
430 | $offset = $this->escape((int)$offset); | |
431 | $sql .= "LIMIT $offset,$limit\n"; | |
432 | } | |
2bedfdac | 433 | |
cdfce7c2 TM |
434 | return $sql; |
435 | } | |
436 | ||
66b6f4d6 | 437 | function safe_query($sql, $fatal=true) { |
cdfce7c2 TM |
438 | $result = $this->query($sql); |
439 | if(!$result) { | |
2bedfdac | 440 | $error = $this->errorInfo(); |
66b6f4d6 TM |
441 | 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>"); |
442 | if($fatal) die(); | |
cdfce7c2 TM |
443 | } |
444 | return $result; | |
445 | } | |
446 | ||
326a9fc9 TM |
447 | function translate_query_results($result) { |
448 | $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded | |
449 | foreach($result as $key => $row) { | |
450 | foreach($translate_cols as $col) if(isset($result[$key][$col])){ | |
451 | $result[$key][$col] = T($result[$key][$col]); | |
452 | } | |
453 | } | |
454 | return $result; | |
455 | } | |
456 | ||
457 | function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO::FETCH_ASSOC, $translate=true) { | |
458 | $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags); | |
459 | if($translate) $result = $this->translate_query_results($result); | |
460 | return $result; | |
461 | } | |
462 | ||
463 | ||
cb8a6861 TM |
464 | function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') { |
465 | $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history); | |
326a9fc9 | 466 | $result = $this->safe_query_fetch($sql); |
cdfce7c2 TM |
467 | if(!$result || !is_array($indexed)) return $result; |
468 | ||
469 | foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row; | |
470 | return $indexed; | |
471 | } | |
472 | ||
473 | function get_columns($class) { | |
474 | $class = $this->escape($class); | |
475 | $sql = "SHOW COLUMNS FROM $class;"; | |
326a9fc9 | 476 | return $this->safe_query_fetch($sql); |
cdfce7c2 TM |
477 | } |
478 | ||
479 | function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') { | |
480 | $selectbox=array(); | |
481 | foreach($columns as $column) { | |
66b6f4d6 | 482 | 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 |
483 | if($class && $column['Field'] == $class.$suffix_id) continue; |
484 | if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue; | |
485 | $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']); | |
66b6f4d6 | 486 | |
117817be | 487 | $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : ''; |
b4dcae05 | 488 | $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!! |
326a9fc9 | 489 | $result = $this->safe_query_fetch($sql, false); |
66b6f4d6 | 490 | if(!$result) continue; |
cdfce7c2 TM |
491 | foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name]; |
492 | } | |
493 | //echo('<pre>'); print_r($selectbox); | |
66b6f4d6 | 494 | return array_filter($selectbox, 'ksort'); |
9ea191cb TM |
495 | } |
496 | ||
16261142 TM |
497 | function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed |
498 | $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : ''; | |
499 | $value=$this->quote($value); | |
500 | $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!! | |
326a9fc9 TM |
501 | $result = $this->safe_query_fetch($sql); |
502 | if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error(T('Record not found!'))); //TODO post_redirect_get... | |
16261142 TM |
503 | } |
504 | ||
9ea191cb TM |
505 | function contains_history($table) { |
506 | $history_tables = array('item'); //TODO Autodetect | |
507 | return in_array($table, $history_tables); | |
cdfce7c2 TM |
508 | } |
509 | ||
510 | function build_query_insert($table, $values, $replace=true, $suffix_id='_id') { | |
b66fadbb TM |
511 | //Init |
512 | $history = $this->contains_history($table); | |
513 | ||
9ea191cb | 514 | //Escaping |
cdfce7c2 TM |
515 | $table = $this->escape($table); |
516 | ||
517 | //Get list of POSTed columns | |
aa3fd0a8 TM |
518 | $columns_array = array_map(array($this,'escape'), array_keys($values[0])); |
519 | $columns = implode(',',$columns_array); | |
cdfce7c2 | 520 | |
9fb856ba | 521 | //Build query |
b66fadbb | 522 | $sql = ''; |
b66fadbb TM |
523 | //echo('<pre>'); die(print_r($values)); |
524 | ||
525 | if($history) { | |
526 | $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true; | |
527 | if($history_update) { | |
528 | $sql .= "UPDATE $table"; | |
117817be TM |
529 | $sql .= " SET ${table}_valid_till=NOW()"; |
530 | $sql .= " WHERE ${table}_valid_till=0 AND ("; | |
b66fadbb TM |
531 | $or = ''; |
532 | foreach($values as $row) { | |
533 | $sql .= $or.' '.$table.'_id='.$row[$table.'_id']; | |
534 | $or = ' OR'; | |
535 | } | |
536 | $sql .= " );\n\n"; | |
537 | $replace = false; | |
538 | } | |
539 | } | |
540 | ||
cdfce7c2 | 541 | //Insert into table (columns) |
aa3fd0a8 | 542 | $sql .= "INSERT INTO $table ($columns) VALUES "; |
cdfce7c2 TM |
543 | |
544 | //Values (a,b,c),(d,e,f) | |
545 | $comma=''; | |
546 | foreach($values as $row) { | |
9fb856ba TM |
547 | $row_quoted = array_map(array($this,'quote'), $row); //Check |
548 | if($history) { | |
b66fadbb TM |
549 | foreach($row as $column => $value) { |
550 | switch($column) { | |
551 | case $table.'_valid_from': | |
552 | $row_quoted[$column] = 'NOW()'; | |
553 | break; | |
554 | case $table.'_valid_till': | |
555 | $row_quoted[$column] = '0'; | |
556 | break; | |
9fb856ba TM |
557 | case $table.'_author': |
558 | $row_quoted[$column] = $this->lms->get_authorized_user_id(); | |
b66fadbb TM |
559 | //die($this->lms->get_authorized_user_id().'=USER'); |
560 | break; | |
b66fadbb TM |
561 | } |
562 | } | |
563 | } | |
564 | $sql .= $comma.'('.implode(',',$row_quoted).')'; | |
cdfce7c2 TM |
565 | $comma = ','; |
566 | } | |
567 | ||
aa3fd0a8 TM |
568 | //On duplicate key |
569 | if($replace) { | |
570 | foreach($columns_array as $col) { | |
571 | if($col == $table.'_id' || $col == $table.'_valid_till') continue; | |
572 | $on_duplicate[] = "$col=VALUES($col)"; | |
573 | } | |
574 | $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate); | |
575 | } | |
576 | ||
cdfce7c2 TM |
577 | //Terminate |
578 | $sql .= ';'; | |
579 | return $sql; | |
580 | } | |
581 | ||
b4dcae05 TM |
582 | function insert_or_update($table, $values, $replace=true) { |
583 | $sql = $this->build_query_insert($table, $values, $replace); | |
cdfce7c2 TM |
584 | $this->safe_query($sql); |
585 | return $this->lastInsertId(); | |
586 | } | |
587 | ||
b4dcae05 | 588 | function insert_or_update_multitab($values, $replace=true) { |
371a86f4 | 589 | $last=false; |
b4dcae05 | 590 | foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace); |
371a86f4 TM |
591 | return $last; |
592 | } | |
593 | ||
cdfce7c2 | 594 | function delete($table, $id, $suffix_id='_id') { |
64f31c54 | 595 | if($this->contains_history($table)) return false; |
cdfce7c2 TM |
596 | $key = $this->escape($table.$suffix_id); |
597 | $table = $this->escape($table); | |
598 | $id = $this->quote($id); | |
599 | return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;"); | |
600 | } | |
601 | } | |
602 | ||
0a027cc7 TM |
603 | /** |
604 | * Trida poskytuje high-level rozhrani k databazi skladu | |
605 | * | |
606 | * @package Sklad_DB_Abstract | |
607 | * @author Tomas Mudrunka | |
608 | */ | |
609 | class Sklad_DB_Abstract extends Sklad_DB { | |
610 | //TODO Code | |
611 | } | |
612 | ||
78bf26a5 TM |
613 | /** |
614 | * Trida implementuje uzivatelske rozhrani skladu | |
615 | * | |
616 | * Example usage: | |
617 | * $sklad = new Sklad_UI(); | |
618 | * $sklad->process_http_request(); | |
619 | * | |
620 | * @package Sklad_UI | |
621 | * @author Tomas Mudrunka | |
622 | */ | |
cdfce7c2 TM |
623 | class Sklad_UI { |
624 | function __construct() { | |
625 | $this->db = new Sklad_DB(); | |
626 | $this->html = new Sklad_HTML(); | |
627 | } | |
628 | ||
cb8a6861 TM |
629 | function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) { |
630 | return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false)); | |
cdfce7c2 TM |
631 | } |
632 | ||
a25f85f8 | 633 | function render_form_add($class) { |
cdfce7c2 TM |
634 | $columns = $this->db->get_columns($class); |
635 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
a25f85f8 | 636 | return $this->html->render_insert_form($class, $columns, $selectbox); |
cdfce7c2 TM |
637 | } |
638 | ||
a25f85f8 | 639 | function render_form_edit($class, $id) { |
cdfce7c2 TM |
640 | $columns = $this->db->get_columns($class); |
641 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
117817be | 642 | $current = $this->db->get_listing($class, $id, 1); |
a25f85f8 | 643 | return $this->html->render_insert_form($class, $columns, $selectbox, $current); |
cdfce7c2 TM |
644 | } |
645 | ||
a25f85f8 | 646 | function render_single_record_details($class, $id) { |
cdfce7c2 TM |
647 | $id_next = $id + 1; |
648 | $id_prev = $id - 1 > 0 ? $id - 1 : 0; | |
649 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
650 | $html=''; |
651 | $html.= $this->html->link('<<', "$class/$id_prev/"); | |
652 | $html.= '-'; | |
653 | $html.= $this->html->link('>>', "$class/$id_next/"); | |
654 | $html.= '<br />'; | |
655 | $html.= $this->html->link('edit', "$class/$id/edit/"); | |
9fb856ba | 656 | if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/"); |
81ab8aef | 657 | $html.='<br />'.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")); |
a25f85f8 | 658 | return $html; |
cdfce7c2 TM |
659 | } |
660 | ||
a25f85f8 | 661 | function render_listing_navigation($class, $id, $limit, $offset) { |
cdfce7c2 TM |
662 | $offset_next = $offset + $limit; |
663 | $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0; | |
664 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
665 | $html=''; |
666 | $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get"); | |
667 | $html.= '-'; | |
668 | $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get"); | |
669 | $html.= '<br />'; | |
670 | $html.= $this->html->link('new', "$class/new/$get"); | |
671 | return $html; | |
cdfce7c2 TM |
672 | } |
673 | ||
a25f85f8 TM |
674 | function render_listing_extensions($class, $id, $limit, $offset, $edit=false) { |
675 | $html=''; | |
cdfce7c2 | 676 | if(is_numeric($id)) { |
a25f85f8 | 677 | $html.=$this->render_single_record_details($class, $id); |
cdfce7c2 | 678 | } else { |
a25f85f8 | 679 | $html.=$this->render_listing_navigation($class, '*', $limit, $offset); |
cdfce7c2 TM |
680 | } |
681 | if($edit) { | |
a25f85f8 | 682 | $html.= $this->render_form_edit($class, $id); |
cdfce7c2 | 683 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete"; |
35916247 TM |
684 | $html.=$this->html->form($action,'POST',array( |
685 | array(false,'DELETE','submit'), | |
686 | array('sure', false, 'checkbox', false, false, 'sure?') | |
687 | )); | |
cdfce7c2 | 688 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image"; |
35916247 TM |
689 | $html.=$this->html->form($action,'POST',array( |
690 | array('image', false, 'file', false, 'size="30"'), | |
691 | array(false, 'IMAGE', 'submit') | |
692 | ), "enctype='multipart/form-data'"); | |
cdfce7c2 | 693 | } |
a25f85f8 | 694 | return $html; |
cdfce7c2 TM |
695 | } |
696 | ||
697 | function check_auth() { | |
698 | new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth')); | |
699 | } | |
700 | ||
64f31c54 | 701 | function post_redirect_get($location, $message='', $error=false) { |
326a9fc9 | 702 | $url_args = $message != '' ? '?message='.urlencode(T($message)) : ''; |
8acef003 | 703 | $location = $this->html->internal_url($location).$url_args; |
1f74892d | 704 | header('Location: '.$location); |
64f31c54 | 705 | if($error) trigger_error($message); |
7efdf72a TM |
706 | $location=htmlspecialchars($location); |
707 | die( | |
708 | "<meta http-equiv='refresh' content='0; url=$location'>". | |
326a9fc9 | 709 | T($message)."<br />Location: <a href='$location'>$location</a>" |
7efdf72a | 710 | ); |
cdfce7c2 TM |
711 | } |
712 | ||
623c65e2 | 713 | function safe_include($dir,$name,$vars=array(),$ext='.inc.php') { |
64f31c54 | 714 | if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true); |
3e6c412e | 715 | $filename="$dir/$name$ext"; |
64f31c54 | 716 | if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true); |
623c65e2 | 717 | foreach($vars as $var => $val) $$var=$val; |
3e6c412e TM |
718 | ob_start(); |
719 | include($filename); | |
720 | $out=ob_get_contents(); | |
721 | ob_end_clean(); | |
722 | return $out; | |
723 | } | |
724 | ||
bda4a4be | 725 | function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) { |
cdfce7c2 | 726 | if($_SERVER['REQUEST_METHOD'] != 'POST') return; |
1f74892d | 727 | //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!! |
cdfce7c2 TM |
728 | |
729 | //SephirPOST: | |
371a86f4 TM |
730 | |
731 | /* Tenhle foreach() prekopiruje promenne | |
7d20381f | 732 | * z: $_POST['values'][$table][$column][$id]; |
371a86f4 TM |
733 | * do: $values[$table][$id][$column] |
734 | */ | |
7d20381f TM |
735 | if(isset($_POST['values'])) { |
736 | $values=array(); | |
737 | foreach($_POST['values'] as $table => $columns) { | |
738 | foreach($columns as $column => $ids) { | |
739 | foreach($ids as $id => $val) $values[$table][$id][$column] = $val; | |
740 | } | |
cdfce7c2 | 741 | } |
7d20381f | 742 | //die(print_r($values)); |
cdfce7c2 TM |
743 | } |
744 | ||
745 | if($action) switch($action) { | |
746 | case 'new': | |
b4dcae05 | 747 | $replace = false; |
cdfce7c2 | 748 | case 'edit': |
b4dcae05 | 749 | if(!isset($replace)) $replace = true; |
64f31c54 | 750 | $table = $class ? $class : 'item'; |
cdfce7c2 | 751 | //print_r($values); //debug |
b4dcae05 | 752 | $last = $this->db->insert_or_update_multitab($values, $replace); |
bda4a4be | 753 | $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/"; |
d9384cb5 | 754 | $next = "$table/new/"; |
bda4a4be TM |
755 | $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.'; |
756 | $this->post_redirect_get($last, $message); | |
cdfce7c2 TM |
757 | break; |
758 | case 'delete': | |
64f31c54 TM |
759 | if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)'); |
760 | $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P"); | |
1f74892d | 761 | $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); |
cdfce7c2 TM |
762 | break; |
763 | case 'image': | |
764 | $image_classes = array('model'); //TODO, use this more widely across the code | |
64f31c54 | 765 | if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!"); |
cdfce7c2 | 766 | $image_destination = DIR_IMAGES."/$class/$id.jpg"; |
326a9fc9 | 767 | if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true); |
cdfce7c2 | 768 | if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) { |
1f74892d | 769 | chmod ($image_destination, 0664); |
326a9fc9 TM |
770 | $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)'); |
771 | } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true); | |
cdfce7c2 TM |
772 | break; |
773 | default: | |
326a9fc9 | 774 | $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*'); |
cdfce7c2 TM |
775 | break; |
776 | } | |
777 | ||
778 | die('POSTed pyčo!'); | |
779 | } | |
780 | ||
781 | function process_http_request() { | |
782 | $this->check_auth(); | |
783 | ||
784 | @ini_set('magic_quotes_gpc' , 'off'); | |
785 | if(get_magic_quotes_gpc()) { | |
786 | die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!")); | |
787 | } | |
788 | ||
789 | $PATH_INFO=@trim($_SERVER[PATH_INFO]); | |
cdfce7c2 | 790 | $PATH_CHUNKS = preg_split('/\//', $PATH_INFO); |
81ab8aef | 791 | //Sephirot: |
cdfce7c2 | 792 | if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]=''; |
81ab8aef TM |
793 | if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode') echo $this->html->header($PATH_INFO); //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost... |
794 | switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible | |
cdfce7c2 TM |
795 | case 'test': //test |
796 | die('Tell me why you cry'); | |
797 | break; | |
3e6c412e | 798 | case 'assistant': //assistant |
de77377e TM |
799 | $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false; |
800 | $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3); | |
801 | $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2]; | |
802 | $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL']; | |
623c65e2 | 803 | echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars); |
3e6c412e | 804 | break; |
81ab8aef TM |
805 | case 'barcode': //barcode |
806 | Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2))); | |
807 | break; | |
cdfce7c2 TM |
808 | default: //? |
809 | $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false; | |
810 | $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item'; | |
811 | if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]=''; | |
812 | switch($PATH_CHUNKS[2]) { | |
813 | case 'new': //?/new | |
814 | $this->process_http_request_post($PATH_CHUNKS[2], $class); | |
a25f85f8 | 815 | echo $this->render_form_add($class); |
cdfce7c2 TM |
816 | break; |
817 | default: //?/? | |
818 | $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false); | |
819 | if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]=''; | |
820 | $edit=false; | |
821 | switch($PATH_CHUNKS[3]) { | |
822 | case 'edit': //?/?/edit | |
9fb856ba TM |
823 | case 'image': //?/?/image |
824 | case 'delete': //?/?/delete | |
cdfce7c2 TM |
825 | $this->process_http_request_post($PATH_CHUNKS[3], $class, $id); |
826 | $edit=true; | |
827 | default: //?/?/? | |
9fb856ba | 828 | $history = $PATH_CHUNKS[3] == 'history' ? true : false; |
cdfce7c2 TM |
829 | $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0'); |
830 | $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0'); | |
cb8a6861 TM |
831 | $where = false; //TODO get from URL |
832 | echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history); | |
a25f85f8 | 833 | echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit); |
cdfce7c2 TM |
834 | //print_r(array("<pre>",$_SERVER)); |
835 | break; | |
836 | } | |
837 | break; | |
838 | } | |
839 | break; | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | $sklad = new Sklad_UI(); | |
845 | $sklad->process_http_request(); | |
846 | ||
847 | echo("<hr/>"); |