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