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