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