Commit | Line | Data |
---|---|---|
cdfce7c2 TM |
1 | <?php |
2 | /* | |
3 | * SkladovySystem - Storage management system compatible with LMS | |
4 | * Copyright (C) 2011 Thomas Mudrunka | |
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 | ||
24 | class Sklad_HTML { | |
25 | function header_print($title='') { | |
26 | $home = URL_HOME; | |
27 | $script = $_SERVER['SCRIPT_NAME']; | |
28 | $search = @trim($_GET['q']); | |
29 | echo <<<EOF | |
30 | <h1><a href="$script/">SystémSklad</a><small>$title</small></h1> | |
31 | <div> | |
32 | <menu> | |
33 | <li><a href="?logout">Logout</a></li> | |
34 | <li><a href="$script/">Home</a></li> | |
35 | </menu> | |
36 | <form action="?" method="GET"> | |
37 | <input type="text" name="q" placeholder="regexp..." value="$search" /> | |
38 | <input type="submit" value="filter" /> | |
39 | </form> | |
40 | <!-- form action="$script/" method="GET"> | |
41 | <input type="text" name="q" placeholder="regexp..." value="$search" /> | |
42 | <input type="submit" value="search items" /> | |
43 | </form --> | |
44 | </div> | |
45 | EOF; | |
46 | } | |
47 | ||
48 | function row_print($row) { | |
49 | echo('<tr>'); | |
50 | foreach($row as $var) { | |
51 | if(trim($var) == '') $var = ' '; | |
52 | echo("<td>$var</td>"); | |
53 | } | |
54 | echo('</tr>'); | |
55 | } | |
56 | ||
57 | function table_print(&$table, $params='border=1') { | |
58 | echo("<table $params>"); | |
59 | $header=true; | |
60 | foreach($table as $row) { | |
61 | if($header) { | |
62 | $this->row_print(array_keys($row)); | |
63 | $header=false; | |
64 | } | |
65 | $this->row_print($row); | |
66 | } | |
67 | echo('</table>'); | |
68 | } | |
69 | ||
70 | function link($title='n/a', $link='#void', $internal=true) { | |
71 | if($internal) $link = $_SERVER['SCRIPT_NAME'].'/'.$link; | |
72 | return "<a href='$link'>$title</a>"; | |
73 | } | |
74 | ||
75 | function img($src='#void', $title='img') { | |
76 | return "<img src='$src' alt='$title' title='$title' width=64 />"; | |
77 | } | |
78 | ||
79 | function table_add_images(&$table) { | |
80 | $image = array('model_id'); | |
81 | foreach($table as $id => $row) { | |
82 | foreach($image as $column) if(isset($table[$id][$column])) { | |
83 | $type = @array_shift(preg_split('/_/', $column)); | |
84 | $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg'; | |
85 | $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]); | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
90 | function table_collapse(&$table) { | |
91 | $collapse = array( | |
92 | 'item_id' => 'item_id', | |
93 | 'model_id' => 'model_name', | |
94 | 'category_id' => 'category_name', | |
95 | 'producer_id' => 'producer_name', | |
96 | 'vendor_id' => 'vendor_name', | |
97 | 'room_id' => 'room_name', | |
98 | 'status_id' => 'status_name', | |
99 | ); | |
100 | foreach($table as $id => $row) { | |
101 | foreach($collapse as $link => $title) | |
102 | if(isset($table[$id][$link])) { | |
103 | $type = @array_shift(preg_split('/_/', $link)); | |
104 | if($link != $title) unset($table[$id][$link]); | |
105 | $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/'); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | function table_sort(&$table) { | |
111 | $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name'); | |
112 | $table_sorted = array(); | |
113 | foreach($table as $id => $row) { | |
114 | $table_sorted[$id] = array(); | |
115 | foreach($precedence as $column) if(isset($table[$id][$column])) { | |
116 | $table_sorted[$id][$column]=$table[$id][$column]; | |
117 | unset($table[$id][$column]); | |
118 | } | |
119 | $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]); | |
120 | } | |
121 | $table = $table_sorted; | |
122 | } | |
123 | ||
124 | function print_item_table($table) { | |
125 | $this->table_add_images($table); | |
126 | $this->table_collapse($table); | |
127 | $this->table_sort($table); | |
128 | return $this->table_print($table); | |
129 | } | |
130 | ||
131 | function input($name=false, $value=false, $type='text', $placeholder=false, $options=false) { | |
132 | $html = "<input type='$type' "; | |
133 | if($name) $html.= "name='$name' "; | |
134 | if(!is_bool($value)) $html.= "value='$value' "; | |
135 | if($options) $html.= "$options "; | |
136 | if($placeholder) $html.= "placeholder='$placeholder' "; | |
137 | $html .= '/>'; | |
138 | return $html; | |
139 | } | |
140 | ||
141 | function select($name, $selectbox, $default=false) { | |
142 | //echo('<pre>'); print_r($selectbox); | |
143 | $html = "<select name='$name'>"; | |
144 | ||
145 | if($default) { | |
146 | $value=$default; $title=$selectbox[$value]; | |
147 | $html .= "<option value='$value'>$value :: $title</option>"; | |
148 | unset($selectbox[$value]); | |
149 | } | |
150 | foreach($selectbox as $value => $title) { | |
151 | $html .= "<option value='$value'>$value :: $title</option>"; | |
152 | } | |
153 | $html .= "</select>"; | |
154 | return $html; | |
155 | } | |
156 | ||
157 | function print_insert_form($class, $columns, $selectbox=array(), $current=false, $multi_insert=true) { | |
158 | //echo('<pre>'); print_r($selectbox); | |
159 | //echo('<pre>'); print_r($current); | |
160 | $update = false; | |
161 | if(is_array($current)) { | |
162 | $update = true; | |
163 | $current = array_shift($current); | |
164 | } | |
165 | ||
166 | echo('<form method="POST">'); | |
167 | if($multi_insert) echo('<div name="input_set" style="float:left; border:1px solid grey;">'); | |
168 | echo $this->input('table', $class, 'hidden'); | |
169 | foreach($columns as $column) { | |
170 | echo($column['Field'].': '); | |
171 | $name='value:'.$column['Field'].'[]'; | |
172 | switch(true) { | |
173 | case preg_match('/auto_increment/', $column['Extra']): | |
174 | $val = $update ? $current[$column['Field']] : ''; //opakuje se (skoro) zbytecne | |
175 | echo $this->input($name, $val, 'hidden'); | |
176 | echo($val.'(AUTO)'); | |
177 | break; | |
178 | case isset($selectbox[$column['Field']]): | |
179 | $val = $update ? $current[$column['Field']] : false; | |
180 | echo $this->select($name,$selectbox[$column['Field']],$val); //opakuje se | |
181 | break; | |
182 | default: | |
183 | $val = $update ? $current[$column['Field']] : false; //opakuje se | |
184 | echo $this->input($name, $val); | |
185 | break; | |
186 | } | |
187 | echo('<br />'); | |
188 | } | |
189 | ||
190 | if($multi_insert) { | |
191 | //TODO, move to separate JS file | |
192 | echo <<<EOF | |
193 | </div> | |
194 | <span name="input_set_next"></span><br style="clear:both" /> | |
195 | <script> | |
196 | function duplicate_element(what, where) { | |
197 | document.getElementsByName(where)[0].outerHTML = | |
198 | document.getElementsByName(what)[0].outerHTML | |
199 | + document.getElementsByName(where)[0].outerHTML; | |
200 | } | |
201 | </script> | |
202 | <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a> | |
203 | EOF; | |
204 | } | |
205 | ||
206 | $btn = is_array($current) ? 'UPDATE' : 'INSERT'; | |
207 | echo($this->input(false, $btn, 'submit')); | |
208 | echo('</form>'); | |
209 | } | |
210 | } | |
211 | ||
212 | class Sklad_DB extends PDO { | |
213 | function __construct() { | |
214 | $this->lms = new Sklad_LMS(); | |
215 | ||
216 | parent::__construct( | |
217 | DB_DSN, DB_USER, DB_PASS, | |
218 | array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL | |
219 | ); | |
220 | } | |
221 | ||
222 | function escape($str) { | |
223 | return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK | |
224 | } | |
225 | ||
226 | function build_query_select($class, $id=false, $limit=false, $offset=0, $search=false, $id_suffix='_id') { | |
227 | $class = $this->escape($class); | |
228 | $join = array( | |
229 | 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'), | |
230 | 'model' => array('category', 'producer') | |
231 | ); | |
232 | $search_fields = array( | |
233 | 'item' => array('item_id','model_name','model_barcode','model_descript','producer_name','vendor_name') | |
234 | ); | |
235 | $sql="SELECT * FROM $class\n"; | |
236 | if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$id_suffix)\n"; | |
237 | if($search) { | |
238 | $search = $this->quote($search); | |
239 | if(!isset($search_fields[$class])) { | |
240 | trigger_error("Ve tride $class zatim vyhledavat nemozno :-("); | |
241 | die(); | |
242 | } | |
243 | $sql .= 'WHERE FALSE '; | |
244 | foreach($search_fields[$class] as $column) $sql .= "OR $column REGEXP $search "; | |
245 | } elseif($id) $sql .= "WHERE $class$id_suffix = $id\n"; | |
246 | if($limit) { | |
247 | $limit = $this->escape((int)$limit); | |
248 | $offset = $this->escape((int)$offset); | |
249 | $sql .= "LIMIT $offset,$limit\n"; | |
250 | } | |
251 | return $sql; | |
252 | } | |
253 | ||
254 | function safe_query($sql) { | |
255 | $result = $this->query($sql); | |
256 | if(!$result) { | |
257 | trigger_error('<font color=red><b>QUERY FAILED:</b><pre>'.$sql.'</pre></font>'); | |
258 | die(); | |
259 | } | |
260 | return $result; | |
261 | } | |
262 | ||
263 | function get_listing($class, $id=false, $limit=false, $offset=0, $search=false, $indexed=array(), $suffix_id='_id') { | |
264 | $sql = $this->build_query_select($class, $id, $limit, $offset, $search); | |
265 | $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC); | |
266 | if(!$result || !is_array($indexed)) return $result; | |
267 | ||
268 | foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row; | |
269 | return $indexed; | |
270 | } | |
271 | ||
272 | function get_columns($class) { | |
273 | $class = $this->escape($class); | |
274 | $sql = "SHOW COLUMNS FROM $class;"; | |
275 | return $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC); | |
276 | } | |
277 | ||
278 | function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') { | |
279 | $selectbox=array(); | |
280 | foreach($columns as $column) { | |
281 | if($class && $column['Field'] == $class.$suffix_id) continue; | |
282 | if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue; | |
283 | $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']); | |
284 | $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table;"; | |
285 | $result=$this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC); | |
286 | foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name]; | |
287 | } | |
288 | //echo('<pre>'); print_r($selectbox); | |
289 | return $selectbox; | |
290 | } | |
291 | ||
292 | function build_query_insert($table, $values, $replace=true, $suffix_id='_id') { | |
293 | $table = $this->escape($table); | |
294 | ||
295 | //Get list of POSTed columns | |
296 | $columns = implode(',',array_map(array($this,'escape'), array_keys($values[0]))); | |
297 | ||
298 | //Insert into table (columns) | |
299 | $sql = 'INSERT'; | |
300 | if($replace) $sql = 'REPLACE'; | |
301 | $sql .= " INTO $table ($columns) VALUES "; | |
302 | ||
303 | //Values (a,b,c),(d,e,f) | |
304 | $comma=''; | |
305 | foreach($values as $row) { | |
306 | $sql .= $comma.'('.implode(',',array_map(array($this,'quote'), $row)).')'; | |
307 | $comma = ','; | |
308 | } | |
309 | ||
310 | //Terminate | |
311 | $sql .= ';'; | |
312 | return $sql; | |
313 | } | |
314 | ||
315 | function insert_or_update($table, $values) { | |
316 | $sql = $this->build_query_insert($table, $values); | |
317 | $this->safe_query($sql); | |
318 | return $this->lastInsertId(); | |
319 | } | |
320 | ||
321 | function delete($table, $id, $suffix_id='_id') { | |
322 | $key = $this->escape($table.$suffix_id); | |
323 | $table = $this->escape($table); | |
324 | $id = $this->quote($id); | |
325 | return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;"); | |
326 | } | |
327 | } | |
328 | ||
329 | class Sklad_UI { | |
330 | function __construct() { | |
331 | $this->db = new Sklad_DB(); | |
332 | $this->html = new Sklad_HTML(); | |
333 | } | |
334 | ||
335 | function show_items($class, $id=false, $limit=false, $offset=0, $search=false) { | |
336 | $this->html->print_item_table($this->db->get_listing($class, $id, $limit, $offset, $search)); | |
337 | } | |
338 | ||
339 | function show_form_add($class) { | |
340 | $columns = $this->db->get_columns($class); | |
341 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
342 | $this->html->print_insert_form($class, $columns, $selectbox); | |
343 | } | |
344 | ||
345 | function show_form_edit($class, $id) { | |
346 | $columns = $this->db->get_columns($class); | |
347 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
348 | $current = $this->db->get_listing($class, $id); | |
349 | $this->html->print_insert_form($class, $columns, $selectbox, $current); | |
350 | } | |
351 | ||
352 | function show_single_record_details($class, $id) { | |
353 | $id_next = $id + 1; | |
354 | $id_prev = $id - 1 > 0 ? $id - 1 : 0; | |
355 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
356 | echo $this->html->link('<<', "$class/$id_prev/"); | |
357 | echo '-'; | |
358 | echo $this->html->link('>>', "$class/$id_next/"); | |
359 | echo ('<br />'); | |
360 | echo $this->html->link('edit', "$class/$id/edit/"); | |
361 | } | |
362 | ||
363 | function show_listing_navigation($class, $id, $limit, $offset) { | |
364 | $offset_next = $offset + $limit; | |
365 | $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0; | |
366 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
367 | echo $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get"); | |
368 | echo '-'; | |
369 | echo $this->html->link('>>', "$class/$id/$limit/$offset_next/$get"); | |
370 | echo ('<br />'); | |
371 | echo $this->html->link('new', "$class/new/$get"); | |
372 | } | |
373 | ||
374 | function show_listing_extensions($class, $id, $limit, $offset, $edit=false) { | |
375 | if(is_numeric($id)) { | |
376 | $this->show_single_record_details($class, $id); | |
377 | } else { | |
378 | $this->show_listing_navigation($class, '*', $limit, $offset); | |
379 | } | |
380 | if($edit) { | |
381 | echo('<br />TODO UPDATE FORM!<br />'); | |
382 | $this->show_form_edit($class, $id); | |
383 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete"; | |
384 | echo("<form action='$action' method='POST'>"); | |
385 | echo $this->html->input(false, 'DELETE', 'submit'); | |
386 | echo 'sure?'.$this->html->input('sure', false, 'checkbox'); | |
387 | echo('</form>'); | |
388 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image"; | |
389 | echo("<form action='$action' method='POST' enctype='multipart/form-data'>"); | |
390 | echo $this->html->input('image', false, 'file', false, 'size="30"'); | |
391 | echo $this->html->input(false, 'IMAGE', 'submit'); | |
392 | echo('</form>'); | |
393 | } | |
394 | } | |
395 | ||
396 | function check_auth() { | |
397 | new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth')); | |
398 | } | |
399 | ||
400 | function post_redirect_get($last, $next) { //TODO prepracovat, tohle je uplna picovina... | |
401 | //header('Location: '.$_SERVER['REQUEST_URI']); //TODO redirect (need templating system or ob_start() first!!!) | |
402 | echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html->link('zde', $last).'.<br />'. | |
403 | 'Další záznam přidáte '.$this->html->link('zde', $next).'.'; | |
404 | die(); | |
405 | } | |
406 | ||
407 | function process_http_request_post($action=false, $class=false, $id=false) { | |
408 | if($_SERVER['REQUEST_METHOD'] != 'POST') return; | |
409 | echo('<pre>'); //DEBUG (maybe todo remove) | |
410 | ||
411 | //SephirPOST: | |
412 | $values=array(); | |
413 | foreach($_POST as $key => $value) { | |
414 | $name = preg_split('/:/',$key); | |
415 | if(isset($name[0])) switch($name[0]) { | |
416 | case 'value': | |
417 | foreach($value as $id => $val) $values[$id][$name[1]]=$value[$id]; | |
418 | break; | |
419 | default: | |
420 | break; | |
421 | } | |
422 | } | |
423 | ||
424 | if($action) switch($action) { | |
425 | case 'new': | |
426 | case 'edit': | |
427 | if(!isset($_POST['table'])) die(trigger_error("Jest nutno specifikovat tabulku voe!")); | |
428 | $table=$_POST['table']; | |
429 | //print_r($values); //debug | |
430 | $last = $this->db->insert_or_update($table, $values); | |
431 | $this->post_redirect_get("$table/$last/", "$table/new/"); | |
432 | break; | |
433 | case 'delete': | |
434 | if(!isset($_POST['sure']) || !$_POST['sure']) die(trigger_error('Sure user expected :-)')); | |
435 | //$this->db->delete($class, $id); | |
436 | die('Neco asi bylo smazano. Fnuk :\'-('); //TODO REDIRECT | |
437 | break; | |
438 | case 'image': | |
439 | $image_classes = array('model'); //TODO, use this more widely across the code | |
440 | if(!in_array($class, $image_classes)) die(trigger_error("Nekdo nechce k DB Tride '$class' prirazovat obrazky!")); | |
441 | $image_destination = DIR_IMAGES."/$class/$id.jpg"; | |
442 | if($_FILES['image']['name'] == '') die(trigger_error('Kazde neco se musi nejak jmenovat!')); | |
443 | if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) { | |
444 | chmod ($image_destination, 0664); | |
445 | die('Obrazek se naladoval :)'); //TODO REDIRECT | |
446 | } else die(trigger_error('Soubor se nenahral :(')); | |
447 | break; | |
448 | default: | |
449 | trigger_error('Nothin\' to do here my cutie :-*'); | |
450 | break; | |
451 | } | |
452 | ||
453 | die('POSTed pyčo!'); | |
454 | } | |
455 | ||
456 | function process_http_request() { | |
457 | $this->check_auth(); | |
458 | ||
459 | @ini_set('magic_quotes_gpc' , 'off'); | |
460 | if(get_magic_quotes_gpc()) { | |
461 | die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!")); | |
462 | } | |
463 | ||
464 | $PATH_INFO=@trim($_SERVER[PATH_INFO]); | |
465 | $this->html->header_print($PATH_INFO); | |
466 | ||
467 | ||
468 | //Sephirot: | |
469 | $PATH_CHUNKS = preg_split('/\//', $PATH_INFO); | |
470 | if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]=''; | |
471 | switch($PATH_CHUNKS[1]) { | |
472 | case 'test': //test | |
473 | die('Tell me why you cry'); | |
474 | break; | |
475 | default: //? | |
476 | $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false; | |
477 | $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item'; | |
478 | if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]=''; | |
479 | switch($PATH_CHUNKS[2]) { | |
480 | case 'new': //?/new | |
481 | $this->process_http_request_post($PATH_CHUNKS[2], $class); | |
482 | $this->show_form_add($class); | |
483 | break; | |
484 | default: //?/? | |
485 | $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false); | |
486 | if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]=''; | |
487 | $edit=false; | |
488 | switch($PATH_CHUNKS[3]) { | |
489 | case 'edit': //?/?/edit | |
490 | case 'image': //?/image | |
491 | case 'delete': //?/delete | |
492 | $this->process_http_request_post($PATH_CHUNKS[3], $class, $id); | |
493 | $edit=true; | |
494 | default: //?/?/? | |
495 | $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0'); | |
496 | $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0'); | |
497 | $this->show_items($class, $id, $limit, $offset, $search); | |
498 | $this->show_listing_extensions($class, $id, $limit, $offset, $edit); | |
499 | //print_r(array("<pre>",$_SERVER)); | |
500 | break; | |
501 | } | |
502 | break; | |
503 | } | |
504 | break; | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
509 | $sklad = new Sklad_UI(); | |
510 | $sklad->process_http_request(); | |
511 | ||
512 | echo("<hr/>"); |