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