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