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() |
089455c4 | 330 | if($multi_insert) $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">'; |
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 | ||
089455c4 | 352 | if($multi_insert) { //TODO, move to separate JS file |
fc5c5b8b | 353 | $html.=<<<EOF |
089455c4 TM |
354 | </div></span> |
355 | <br style="clear:both" /> | |
cdfce7c2 TM |
356 | <script> |
357 | function duplicate_element(what, where) { | |
089455c4 TM |
358 | var node = document.getElementsByName(what)[0]; |
359 | node.parentNode.appendChild(node.cloneNode(true)); | |
cdfce7c2 TM |
360 | } |
361 | </script> | |
089455c4 | 362 | <a href='#' onClick="duplicate_element('input_set')">+</a> |
cdfce7c2 TM |
363 | EOF; |
364 | } | |
365 | ||
bda4a4be | 366 | $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting... |
fc5c5b8b TM |
367 | $html.=$this->input(false, $btn, 'submit'); |
368 | $html.='</form>'; | |
369 | return $html; | |
cdfce7c2 TM |
370 | } |
371 | } | |
372 | ||
78bf26a5 TM |
373 | /** |
374 | * Trida poskytuje rozhrani k databazi skladu | |
375 | * | |
376 | * @package Sklad_DB | |
377 | * @author Tomas Mudrunka | |
378 | */ | |
cdfce7c2 TM |
379 | class Sklad_DB extends PDO { |
380 | function __construct() { | |
381 | $this->lms = new Sklad_LMS(); | |
382 | ||
383 | parent::__construct( | |
384 | DB_DSN, DB_USER, DB_PASS, | |
385 | array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL | |
386 | ); | |
387 | } | |
388 | ||
389 | function escape($str) { | |
390 | return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK | |
391 | } | |
392 | ||
cb8a6861 | 393 | function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') { |
2bedfdac | 394 | //Configuration |
cdfce7c2 TM |
395 | $join = array( |
396 | 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'), | |
397 | 'model' => array('category', 'producer') | |
9ea191cb | 398 | ); //TODO Autodetect using foreign keys? |
cdfce7c2 | 399 | $search_fields = array( |
df4079a6 | 400 | 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name') |
9ea191cb | 401 | ); //TODO Autodetect |
2bedfdac TM |
402 | |
403 | //Escaping | |
404 | $class = $this->escape($class); | |
405 | ||
406 | //SELECT | |
cdfce7c2 | 407 | $sql="SELECT * FROM $class\n"; |
2bedfdac | 408 | //JOIN |
8fd613d6 | 409 | if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n"; |
2bedfdac | 410 | //WHERE/REGEXP |
cdfce7c2 TM |
411 | if($search) { |
412 | $search = $this->quote($search); | |
326a9fc9 | 413 | if(!isset($search_fields[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get |
5895162b TM |
414 | $sql_search = ''; |
415 | foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search "; | |
416 | $where[] = "FALSE $sql_search"; | |
cb8a6861 TM |
417 | } elseif($id) $where[] = "$class$suffix_id = $id"; |
418 | if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0'; | |
419 | ||
5895162b | 420 | if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n"; |
117817be TM |
421 | //ORDER |
422 | if(!$order) $order = $class.$suffix_id; | |
423 | if($this->contains_history($class)) $order .= ",${class}_valid_from DESC"; | |
424 | $sql .= "ORDER BY $order\n"; | |
2bedfdac | 425 | //LIMIT/OFFSET |
cdfce7c2 TM |
426 | if($limit) { |
427 | $limit = $this->escape((int)$limit); | |
428 | $offset = $this->escape((int)$offset); | |
429 | $sql .= "LIMIT $offset,$limit\n"; | |
430 | } | |
2bedfdac | 431 | |
cdfce7c2 TM |
432 | return $sql; |
433 | } | |
434 | ||
66b6f4d6 | 435 | function safe_query($sql, $fatal=true) { |
cdfce7c2 TM |
436 | $result = $this->query($sql); |
437 | if(!$result) { | |
2bedfdac | 438 | $error = $this->errorInfo(); |
66b6f4d6 TM |
439 | 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>"); |
440 | if($fatal) die(); | |
cdfce7c2 TM |
441 | } |
442 | return $result; | |
443 | } | |
444 | ||
326a9fc9 TM |
445 | function translate_query_results($result) { |
446 | $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded | |
447 | foreach($result as $key => $row) { | |
448 | foreach($translate_cols as $col) if(isset($result[$key][$col])){ | |
449 | $result[$key][$col] = T($result[$key][$col]); | |
450 | } | |
451 | } | |
452 | return $result; | |
453 | } | |
454 | ||
455 | function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO::FETCH_ASSOC, $translate=true) { | |
456 | $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags); | |
457 | if($translate) $result = $this->translate_query_results($result); | |
458 | return $result; | |
459 | } | |
460 | ||
461 | ||
cb8a6861 TM |
462 | function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') { |
463 | $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history); | |
326a9fc9 | 464 | $result = $this->safe_query_fetch($sql); |
cdfce7c2 TM |
465 | if(!$result || !is_array($indexed)) return $result; |
466 | ||
467 | foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row; | |
468 | return $indexed; | |
469 | } | |
470 | ||
471 | function get_columns($class) { | |
472 | $class = $this->escape($class); | |
473 | $sql = "SHOW COLUMNS FROM $class;"; | |
326a9fc9 | 474 | return $this->safe_query_fetch($sql); |
cdfce7c2 TM |
475 | } |
476 | ||
477 | function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') { | |
478 | $selectbox=array(); | |
479 | foreach($columns as $column) { | |
66b6f4d6 | 480 | 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 |
481 | if($class && $column['Field'] == $class.$suffix_id) continue; |
482 | if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue; | |
483 | $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']); | |
66b6f4d6 | 484 | |
117817be | 485 | $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : ''; |
b4dcae05 | 486 | $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!! |
326a9fc9 | 487 | $result = $this->safe_query_fetch($sql, false); |
66b6f4d6 | 488 | if(!$result) continue; |
cdfce7c2 TM |
489 | foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name]; |
490 | } | |
491 | //echo('<pre>'); print_r($selectbox); | |
66b6f4d6 | 492 | return array_filter($selectbox, 'ksort'); |
9ea191cb TM |
493 | } |
494 | ||
16261142 TM |
495 | function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed |
496 | $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : ''; | |
497 | $value=$this->quote($value); | |
498 | $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!! | |
326a9fc9 TM |
499 | $result = $this->safe_query_fetch($sql); |
500 | if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error(T('Record not found!'))); //TODO post_redirect_get... | |
16261142 TM |
501 | } |
502 | ||
9ea191cb TM |
503 | function contains_history($table) { |
504 | $history_tables = array('item'); //TODO Autodetect | |
505 | return in_array($table, $history_tables); | |
cdfce7c2 TM |
506 | } |
507 | ||
508 | function build_query_insert($table, $values, $replace=true, $suffix_id='_id') { | |
b66fadbb TM |
509 | //Init |
510 | $history = $this->contains_history($table); | |
511 | ||
9ea191cb | 512 | //Escaping |
cdfce7c2 TM |
513 | $table = $this->escape($table); |
514 | ||
515 | //Get list of POSTed columns | |
aa3fd0a8 TM |
516 | $columns_array = array_map(array($this,'escape'), array_keys($values[0])); |
517 | $columns = implode(',',$columns_array); | |
cdfce7c2 | 518 | |
9fb856ba | 519 | //Build query |
b66fadbb | 520 | $sql = ''; |
b66fadbb TM |
521 | //echo('<pre>'); die(print_r($values)); |
522 | ||
523 | if($history) { | |
524 | $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true; | |
525 | if($history_update) { | |
526 | $sql .= "UPDATE $table"; | |
117817be TM |
527 | $sql .= " SET ${table}_valid_till=NOW()"; |
528 | $sql .= " WHERE ${table}_valid_till=0 AND ("; | |
b66fadbb TM |
529 | $or = ''; |
530 | foreach($values as $row) { | |
531 | $sql .= $or.' '.$table.'_id='.$row[$table.'_id']; | |
532 | $or = ' OR'; | |
533 | } | |
534 | $sql .= " );\n\n"; | |
535 | $replace = false; | |
536 | } | |
537 | } | |
538 | ||
cdfce7c2 | 539 | //Insert into table (columns) |
aa3fd0a8 | 540 | $sql .= "INSERT INTO $table ($columns) VALUES "; |
cdfce7c2 TM |
541 | |
542 | //Values (a,b,c),(d,e,f) | |
543 | $comma=''; | |
544 | foreach($values as $row) { | |
9fb856ba TM |
545 | $row_quoted = array_map(array($this,'quote'), $row); //Check |
546 | if($history) { | |
b66fadbb TM |
547 | foreach($row as $column => $value) { |
548 | switch($column) { | |
549 | case $table.'_valid_from': | |
550 | $row_quoted[$column] = 'NOW()'; | |
551 | break; | |
552 | case $table.'_valid_till': | |
553 | $row_quoted[$column] = '0'; | |
554 | break; | |
9fb856ba TM |
555 | case $table.'_author': |
556 | $row_quoted[$column] = $this->lms->get_authorized_user_id(); | |
b66fadbb TM |
557 | //die($this->lms->get_authorized_user_id().'=USER'); |
558 | break; | |
b66fadbb TM |
559 | } |
560 | } | |
561 | } | |
562 | $sql .= $comma.'('.implode(',',$row_quoted).')'; | |
cdfce7c2 TM |
563 | $comma = ','; |
564 | } | |
565 | ||
aa3fd0a8 TM |
566 | //On duplicate key |
567 | if($replace) { | |
568 | foreach($columns_array as $col) { | |
569 | if($col == $table.'_id' || $col == $table.'_valid_till') continue; | |
570 | $on_duplicate[] = "$col=VALUES($col)"; | |
571 | } | |
572 | $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate); | |
573 | } | |
574 | ||
cdfce7c2 TM |
575 | //Terminate |
576 | $sql .= ';'; | |
577 | return $sql; | |
578 | } | |
579 | ||
b4dcae05 TM |
580 | function insert_or_update($table, $values, $replace=true) { |
581 | $sql = $this->build_query_insert($table, $values, $replace); | |
cdfce7c2 TM |
582 | $this->safe_query($sql); |
583 | return $this->lastInsertId(); | |
584 | } | |
585 | ||
b4dcae05 | 586 | function insert_or_update_multitab($values, $replace=true) { |
371a86f4 | 587 | $last=false; |
b4dcae05 | 588 | foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace); |
371a86f4 TM |
589 | return $last; |
590 | } | |
591 | ||
cdfce7c2 | 592 | function delete($table, $id, $suffix_id='_id') { |
64f31c54 | 593 | if($this->contains_history($table)) return false; |
cdfce7c2 TM |
594 | $key = $this->escape($table.$suffix_id); |
595 | $table = $this->escape($table); | |
596 | $id = $this->quote($id); | |
597 | return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;"); | |
598 | } | |
599 | } | |
600 | ||
0a027cc7 TM |
601 | /** |
602 | * Trida poskytuje high-level rozhrani k databazi skladu | |
603 | * | |
604 | * @package Sklad_DB_Abstract | |
605 | * @author Tomas Mudrunka | |
606 | */ | |
607 | class Sklad_DB_Abstract extends Sklad_DB { | |
608 | //TODO Code | |
609 | } | |
610 | ||
78bf26a5 TM |
611 | /** |
612 | * Trida implementuje uzivatelske rozhrani skladu | |
613 | * | |
614 | * Example usage: | |
615 | * $sklad = new Sklad_UI(); | |
616 | * $sklad->process_http_request(); | |
617 | * | |
618 | * @package Sklad_UI | |
619 | * @author Tomas Mudrunka | |
620 | */ | |
cdfce7c2 TM |
621 | class Sklad_UI { |
622 | function __construct() { | |
623 | $this->db = new Sklad_DB(); | |
624 | $this->html = new Sklad_HTML(); | |
625 | } | |
626 | ||
cb8a6861 TM |
627 | function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) { |
628 | return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false)); | |
cdfce7c2 TM |
629 | } |
630 | ||
a25f85f8 | 631 | function render_form_add($class) { |
cdfce7c2 TM |
632 | $columns = $this->db->get_columns($class); |
633 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
a25f85f8 | 634 | return $this->html->render_insert_form($class, $columns, $selectbox); |
cdfce7c2 TM |
635 | } |
636 | ||
a25f85f8 | 637 | function render_form_edit($class, $id) { |
cdfce7c2 TM |
638 | $columns = $this->db->get_columns($class); |
639 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
117817be | 640 | $current = $this->db->get_listing($class, $id, 1); |
a25f85f8 | 641 | return $this->html->render_insert_form($class, $columns, $selectbox, $current); |
cdfce7c2 TM |
642 | } |
643 | ||
a25f85f8 | 644 | function render_single_record_details($class, $id) { |
cdfce7c2 TM |
645 | $id_next = $id + 1; |
646 | $id_prev = $id - 1 > 0 ? $id - 1 : 0; | |
647 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
648 | $html=''; |
649 | $html.= $this->html->link('<<', "$class/$id_prev/"); | |
650 | $html.= '-'; | |
651 | $html.= $this->html->link('>>', "$class/$id_next/"); | |
652 | $html.= '<br />'; | |
54694117 | 653 | $html.='<span style="float:right;">'.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")).'</span>'; |
a25f85f8 | 654 | $html.= $this->html->link('edit', "$class/$id/edit/"); |
9fb856ba | 655 | if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/"); |
a25f85f8 | 656 | return $html; |
cdfce7c2 TM |
657 | } |
658 | ||
a25f85f8 | 659 | function render_listing_navigation($class, $id, $limit, $offset) { |
cdfce7c2 TM |
660 | $offset_next = $offset + $limit; |
661 | $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0; | |
662 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
663 | $html=''; |
664 | $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get"); | |
665 | $html.= '-'; | |
666 | $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get"); | |
667 | $html.= '<br />'; | |
668 | $html.= $this->html->link('new', "$class/new/$get"); | |
669 | return $html; | |
cdfce7c2 TM |
670 | } |
671 | ||
a25f85f8 TM |
672 | function render_listing_extensions($class, $id, $limit, $offset, $edit=false) { |
673 | $html=''; | |
cdfce7c2 | 674 | if(is_numeric($id)) { |
a25f85f8 | 675 | $html.=$this->render_single_record_details($class, $id); |
cdfce7c2 | 676 | } else { |
a25f85f8 | 677 | $html.=$this->render_listing_navigation($class, '*', $limit, $offset); |
cdfce7c2 TM |
678 | } |
679 | if($edit) { | |
a25f85f8 | 680 | $html.= $this->render_form_edit($class, $id); |
cdfce7c2 | 681 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete"; |
35916247 TM |
682 | $html.=$this->html->form($action,'POST',array( |
683 | array(false,'DELETE','submit'), | |
684 | array('sure', false, 'checkbox', false, false, 'sure?') | |
685 | )); | |
cdfce7c2 | 686 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image"; |
35916247 TM |
687 | $html.=$this->html->form($action,'POST',array( |
688 | array('image', false, 'file', false, 'size="30"'), | |
689 | array(false, 'IMAGE', 'submit') | |
690 | ), "enctype='multipart/form-data'"); | |
cdfce7c2 | 691 | } |
a25f85f8 | 692 | return $html; |
cdfce7c2 TM |
693 | } |
694 | ||
695 | function check_auth() { | |
696 | new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth')); | |
697 | } | |
698 | ||
64f31c54 | 699 | function post_redirect_get($location, $message='', $error=false) { |
326a9fc9 | 700 | $url_args = $message != '' ? '?message='.urlencode(T($message)) : ''; |
8acef003 | 701 | $location = $this->html->internal_url($location).$url_args; |
1f74892d | 702 | header('Location: '.$location); |
64f31c54 | 703 | if($error) trigger_error($message); |
7efdf72a TM |
704 | $location=htmlspecialchars($location); |
705 | die( | |
706 | "<meta http-equiv='refresh' content='0; url=$location'>". | |
326a9fc9 | 707 | T($message)."<br />Location: <a href='$location'>$location</a>" |
7efdf72a | 708 | ); |
cdfce7c2 TM |
709 | } |
710 | ||
623c65e2 | 711 | function safe_include($dir,$name,$vars=array(),$ext='.inc.php') { |
64f31c54 | 712 | if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true); |
3e6c412e | 713 | $filename="$dir/$name$ext"; |
64f31c54 | 714 | if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true); |
623c65e2 | 715 | foreach($vars as $var => $val) $$var=$val; |
3e6c412e TM |
716 | ob_start(); |
717 | include($filename); | |
718 | $out=ob_get_contents(); | |
719 | ob_end_clean(); | |
720 | return $out; | |
721 | } | |
722 | ||
bda4a4be | 723 | function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) { |
cdfce7c2 | 724 | if($_SERVER['REQUEST_METHOD'] != 'POST') return; |
1f74892d | 725 | //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!! |
cdfce7c2 TM |
726 | |
727 | //SephirPOST: | |
371a86f4 TM |
728 | |
729 | /* Tenhle foreach() prekopiruje promenne | |
7d20381f | 730 | * z: $_POST['values'][$table][$column][$id]; |
371a86f4 TM |
731 | * do: $values[$table][$id][$column] |
732 | */ | |
7d20381f TM |
733 | if(isset($_POST['values'])) { |
734 | $values=array(); | |
735 | foreach($_POST['values'] as $table => $columns) { | |
736 | foreach($columns as $column => $ids) { | |
737 | foreach($ids as $id => $val) $values[$table][$id][$column] = $val; | |
738 | } | |
cdfce7c2 | 739 | } |
7d20381f | 740 | //die(print_r($values)); |
cdfce7c2 TM |
741 | } |
742 | ||
743 | if($action) switch($action) { | |
744 | case 'new': | |
b4dcae05 | 745 | $replace = false; |
cdfce7c2 | 746 | case 'edit': |
b4dcae05 | 747 | if(!isset($replace)) $replace = true; |
64f31c54 | 748 | $table = $class ? $class : 'item'; |
cdfce7c2 | 749 | //print_r($values); //debug |
b4dcae05 | 750 | $last = $this->db->insert_or_update_multitab($values, $replace); |
bda4a4be | 751 | $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/"; |
d9384cb5 | 752 | $next = "$table/new/"; |
bda4a4be TM |
753 | $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.'; |
754 | $this->post_redirect_get($last, $message); | |
cdfce7c2 TM |
755 | break; |
756 | case 'delete': | |
64f31c54 TM |
757 | if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)'); |
758 | $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P"); | |
1f74892d | 759 | $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); |
cdfce7c2 TM |
760 | break; |
761 | case 'image': | |
762 | $image_classes = array('model'); //TODO, use this more widely across the code | |
64f31c54 | 763 | if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!"); |
cdfce7c2 | 764 | $image_destination = DIR_IMAGES."/$class/$id.jpg"; |
326a9fc9 | 765 | if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true); |
cdfce7c2 | 766 | if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) { |
1f74892d | 767 | chmod ($image_destination, 0664); |
326a9fc9 TM |
768 | $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)'); |
769 | } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true); | |
cdfce7c2 TM |
770 | break; |
771 | default: | |
326a9fc9 | 772 | $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*'); |
cdfce7c2 TM |
773 | break; |
774 | } | |
775 | ||
776 | die('POSTed pyčo!'); | |
777 | } | |
778 | ||
779 | function process_http_request() { | |
780 | $this->check_auth(); | |
781 | ||
782 | @ini_set('magic_quotes_gpc' , 'off'); | |
783 | if(get_magic_quotes_gpc()) { | |
784 | die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!")); | |
785 | } | |
786 | ||
787 | $PATH_INFO=@trim($_SERVER[PATH_INFO]); | |
cdfce7c2 | 788 | $PATH_CHUNKS = preg_split('/\//', $PATH_INFO); |
81ab8aef | 789 | //Sephirot: |
cdfce7c2 | 790 | if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]=''; |
81ab8aef TM |
791 | 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... |
792 | switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible | |
cdfce7c2 TM |
793 | case 'test': //test |
794 | die('Tell me why you cry'); | |
795 | break; | |
3e6c412e | 796 | case 'assistant': //assistant |
de77377e TM |
797 | $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false; |
798 | $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3); | |
799 | $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2]; | |
800 | $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL']; | |
623c65e2 | 801 | echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars); |
3e6c412e | 802 | break; |
81ab8aef TM |
803 | case 'barcode': //barcode |
804 | Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2))); | |
805 | break; | |
cdfce7c2 TM |
806 | default: //? |
807 | $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false; | |
808 | $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item'; | |
809 | if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]=''; | |
810 | switch($PATH_CHUNKS[2]) { | |
811 | case 'new': //?/new | |
812 | $this->process_http_request_post($PATH_CHUNKS[2], $class); | |
a25f85f8 | 813 | echo $this->render_form_add($class); |
cdfce7c2 TM |
814 | break; |
815 | default: //?/? | |
816 | $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false); | |
817 | if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]=''; | |
818 | $edit=false; | |
819 | switch($PATH_CHUNKS[3]) { | |
820 | case 'edit': //?/?/edit | |
9fb856ba TM |
821 | case 'image': //?/?/image |
822 | case 'delete': //?/?/delete | |
cdfce7c2 TM |
823 | $this->process_http_request_post($PATH_CHUNKS[3], $class, $id); |
824 | $edit=true; | |
825 | default: //?/?/? | |
9fb856ba | 826 | $history = $PATH_CHUNKS[3] == 'history' ? true : false; |
cdfce7c2 TM |
827 | $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0'); |
828 | $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0'); | |
cb8a6861 TM |
829 | $where = false; //TODO get from URL |
830 | echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history); | |
a25f85f8 | 831 | echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit); |
cdfce7c2 TM |
832 | //print_r(array("<pre>",$_SERVER)); |
833 | break; | |
834 | } | |
835 | break; | |
836 | } | |
837 | break; | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
842 | $sklad = new Sklad_UI(); | |
843 | $sklad->process_http_request(); | |
844 | ||
54694117 | 845 | echo('<br style="clear:both;" /><hr />'); |