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'); | |
5f093236 TM |
21 | set_include_path(DIR_LIB.PATH_SEPARATOR.get_include_path()); |
22 | ||
63747dad | 23 | require_once('Sklad_Auth.class/common.php'); |
cdfce7c2 | 24 | require_once('HTTP_Auth.class.php'); |
326a9fc9 | 25 | require_once('Locale.class.php'); |
81ab8aef | 26 | require_once('Barcode.class.php'); |
cdfce7c2 | 27 | |
0a027cc7 TM |
28 | /** |
29 | * Trida poskytuje vseobecne funkce pro generovani HTML kodu | |
30 | * | |
31 | * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)! | |
32 | * | |
33 | * @package HTML | |
34 | * @author Tomas Mudrunka | |
35 | */ | |
36 | class HTML { | |
1b9304b8 | 37 | function row($row,$type=false,$class=false) { |
933b760a | 38 | $html = ''; |
1b9304b8 | 39 | $class = $class ? $class=" class='$class' " : ''; |
933b760a | 40 | if($type) $html.="<$type>"; |
1b9304b8 | 41 | $html.="<tr$class>"; |
933b760a | 42 | $td = $type == 'thead' ? 'th' : 'td'; |
0a027cc7 TM |
43 | foreach($row as $var) { |
44 | if(trim($var) == '') $var = ' '; | |
933b760a | 45 | $html.="<$td>$var</$td>"; |
0a027cc7 TM |
46 | } |
47 | $html.='</tr>'; | |
933b760a | 48 | if($type) $html.="</$type>"; |
0a027cc7 TM |
49 | return $html; |
50 | } | |
51 | ||
1b9304b8 | 52 | function table(&$table, $parity_class=array('tr_odd','tr_even'), $params='border=1') { |
0a027cc7 TM |
53 | $html="<table $params>"; |
54 | $header=true; | |
1b9304b8 | 55 | $even=false; |
0a027cc7 TM |
56 | foreach($table as $row) { |
57 | if($header) { | |
933b760a | 58 | $html.=$this->row(array_keys($row),'thead'); |
0a027cc7 TM |
59 | $header=false; |
60 | } | |
1b9304b8 TM |
61 | $class = $parity_class ? $parity_class[$even] : false; |
62 | $html.=$this->row($row,false,$class); | |
63 | $even = !$even; | |
0a027cc7 TM |
64 | } |
65 | $html.='</table>'; | |
66 | return $html; | |
67 | } | |
68 | ||
81ab8aef | 69 | function link($title='n/a', $link='#void', $internal=true, $translate=true) { |
35916247 | 70 | if($internal && (!isset($link[0]) || $link[0] != '#')) $link = $this->internal_url($link); |
81ab8aef TM |
71 | if($translate) $title = T($title); |
72 | return "<a href='$link'>".$title."</a>"; | |
0a027cc7 TM |
73 | } |
74 | ||
81ab8aef TM |
75 | function img($src='#void', $title='img', $options='width=64') { |
76 | $options = $options ? " $options" : ''; | |
77 | return "<img src='$src' alt='$title' title='$title'$options; />"; | |
0a027cc7 TM |
78 | } |
79 | ||
6f7943a0 TM |
80 | function img_link($src, $link='#void', $title='img_link', $internal=true, $translate=true, $options='width=64') { |
81 | return $this->link($this->img($src,$title,$options),$link,$internal,$translate); | |
82 | } | |
83 | ||
35916247 TM |
84 | function input($name=false, $value=false, $type='text', $placeholder=false, $options=false, $prefix='') { |
85 | $html = T($prefix)."<input type='$type' "; | |
0a027cc7 | 86 | if($name) $html.= "name='$name' "; |
326a9fc9 TM |
87 | if(!is_bool($value)) { |
88 | if($type == 'submit') $value = T($value); | |
89 | $html.= "value='$value' "; | |
90 | } | |
0a027cc7 TM |
91 | if($options) $html.= "$options "; |
92 | if($placeholder) $html.= "placeholder='$placeholder' "; | |
93 | $html .= '/>'; | |
94 | return $html; | |
95 | } | |
96 | ||
35916247 TM |
97 | function form($action=false, $method=false, $inputs, $options=false) { |
98 | $action = $action ? " action='$action'" : ''; | |
99 | $method = $method ? " method='$method'" : ''; | |
100 | $options = $options ? " $options" : ''; | |
101 | $html = "<form$action$method$options>"; | |
102 | foreach($inputs as $input) $html .= call_user_func_array(array($this,'input'), $input); | |
103 | $html .= "</form>"; | |
104 | return $html; | |
105 | } | |
106 | ||
0a027cc7 TM |
107 | function select($name, $selectbox, $default=false) { |
108 | //echo('<pre>'); print_r($selectbox); | |
109 | $html = "<select name='$name'>"; | |
110 | ||
111 | if($default) { | |
112 | $value=$default; $title=$selectbox[$value]; | |
113 | $html .= "<option value='$value'>$value :: $title</option>"; | |
114 | unset($selectbox[$value]); | |
115 | } | |
116 | foreach($selectbox as $value => $title) { | |
117 | $html .= "<option value='$value'>$value :: $title</option>"; | |
118 | } | |
119 | $html .= "</select>"; | |
120 | return $html; | |
121 | } | |
35916247 TM |
122 | |
123 | function ul($items,$tag=ul,$head='',$class=false) { | |
124 | $class = $class ? " class='$class'" : ''; | |
125 | $html = "$head<$tag$class>"; | |
126 | foreach($items as $key => $value) { | |
127 | $html .= '<li>'; | |
128 | if(is_numeric($key)) { | |
129 | $html .= $value; | |
130 | } else { | |
131 | $html .= $this->link($key,$value); | |
132 | } | |
133 | $html .= '</li>'; | |
134 | } | |
135 | $html .= "</$tag>"; | |
136 | return $html; | |
137 | } | |
138 | ||
139 | function div($html, $options) { | |
140 | $options = $options ? " $options" : ''; | |
141 | return "<div$options>$html</div>"; | |
142 | } | |
ec106ddf TM |
143 | |
144 | function head($title=false,$charset='UTF-8',$more='') { | |
145 | $title = $title ? "\n<title>$title</title>" : ''; | |
146 | $html= '<head>'; | |
147 | $html.= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />'.$title.$more; | |
148 | $html.= '</head>'; | |
149 | return $html; | |
150 | } | |
0a027cc7 TM |
151 | } |
152 | ||
78bf26a5 TM |
153 | /** |
154 | * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad | |
155 | * | |
fc5c5b8b TM |
156 | * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)! |
157 | * | |
78bf26a5 TM |
158 | * @package Sklad_HTML |
159 | * @author Tomas Mudrunka | |
160 | */ | |
35916247 | 161 | class Sklad_HTML extends HTML { //TODO: Split into few more methods |
e9f6461f | 162 | function header($title='', $user=array()) { |
cdfce7c2 TM |
163 | $home = URL_HOME; |
164 | $script = $_SERVER['SCRIPT_NAME']; | |
d9384cb5 | 165 | $search = htmlspecialchars(@trim($_GET['q'])); |
a8bbdc31 | 166 | $message = strip_tags(@trim($_GET['message']),'<a><b><u><i><br>'); |
41e57f9e | 167 | $fortune = 'test'; |
ac2ed3a1 | 168 | $instance = INSTANCE_ID != '' ? '/'.INSTANCE_ID : ''; |
e9f6461f TM |
169 | $user_id = htmlspecialchars($user['id']); |
170 | $user_gid = htmlspecialchars($user['gid']); | |
171 | $user_name = htmlspecialchars($user['name']); | |
40eef626 | 172 | $time = date('r'); |
35916247 | 173 | //$title = T($title); //TODO |
ec106ddf TM |
174 | |
175 | $html = $this->head("SōkoMan$title"); | |
176 | $html .= <<<EOF | |
6d98215b | 177 | <h1 style="display: inline;"><a href="$script/">SōkoMan</a><small>$instance$title</small></h1> |
40eef626 TM |
178 | <div style="float:right; text-align:right;"> |
179 | Logged in as <b>$user_name</b> [UID: <b>$user_id</b>; GID: <b>$user_gid</b>]<br /> | |
180 | Page loaded at $time | |
181 | </div> | |
c9759229 TM |
182 | |
183 | <style type="text/css"> | |
ac2ed3a1 | 184 | * { font-family: arial; } |
933b760a TM |
185 | td,body { background-color: white; } |
186 | table { background-color: orange; border: orange; } | |
d0e7939c | 187 | a, a img { text-decoration:none; color: darkblue; border:none; } |
933b760a | 188 | li a, a:hover { text-decoration:underline; } |
1b9304b8 | 189 | .tr_even td { background-color: lemonchiffon; } |
ac2ed3a1 | 190 | |
7bcd6b42 | 191 | .menu li { |
c9759229 | 192 | float: left; |
7bcd6b42 TM |
193 | padding: 0.2em; |
194 | } | |
195 | ||
196 | .menu * li { | |
197 | float: none; | |
c9759229 TM |
198 | } |
199 | ||
200 | .menu * menu { | |
201 | position: absolute; | |
7bcd6b42 | 202 | padding: 0.2em; |
c9759229 TM |
203 | } |
204 | ||
205 | .menu, .menu * menu { | |
206 | list-style: none; | |
207 | } | |
208 | ||
209 | .menu * menu { | |
7bcd6b42 | 210 | border: 1px solid orange; |
c9759229 TM |
211 | display: none; |
212 | margin: 0; | |
213 | } | |
214 | ||
7bcd6b42 | 215 | .menu li:hover menu, .menu li:hover { |
c9759229 | 216 | display: block; |
7bcd6b42 | 217 | background-color: yellow; |
c9759229 | 218 | } |
7bcd6b42 | 219 | |
c9759229 TM |
220 | </style> |
221 | ||
cdfce7c2 | 222 | <div> |
35916247 TM |
223 | EOF; |
224 | ||
225 | $assistants=array(); | |
226 | foreach(scandir(DIR_ASSISTANTS) as $item) { | |
227 | if($item == '.' || $item == '..') continue; | |
de42ea13 TM |
228 | $item = preg_replace('/\.inc\.php$/','',$item,-1,$count); |
229 | if($count) $assistants[$item] = "assistant/$item"; | |
35916247 TM |
230 | } |
231 | ||
232 | $tables=array('item','model','category','producer','vendor','room','status'); | |
233 | ||
234 | foreach($tables as $table) { | |
235 | $listable[$table] = $table; | |
236 | $insertable[$table] = "$table/new"; | |
237 | } | |
238 | ||
239 | $html .= $this->ul(array( | |
35916247 | 240 | 'Home' => '', |
ec106ddf | 241 | 'Logout' => '?logout', |
35916247 | 242 | 0 => $this->ul($assistants,'menu',$this->link('Assistants','#')), |
ec106ddf TM |
243 | 1 => $this->ul($insertable,'menu',$this->link('New','#')), |
244 | 2 => $this->ul($listable,'menu',$this->link('List','#')) | |
35916247 TM |
245 | ),'menu', '', 'menu'); |
246 | ||
247 | $html .= '<div style="float: right;">'; | |
248 | ||
88c38b26 | 249 | $html .= $this->form("$script/api/go", 'GET', array( |
81ab8aef | 250 | array('q','','text','smart id...', 'autofocus'), |
35916247 TM |
251 | array(false,'go','submit') |
252 | ), 'style="float: left;"'); | |
253 | ||
254 | $html .= $this->form('?', 'GET', array( | |
255 | array('q',$search,'text','regexp...'), | |
256 | array(false,'filter','submit') | |
257 | ), 'style="float: left;"'); | |
258 | ||
259 | $html .= '</div>'; | |
260 | ||
261 | $html .= <<<EOF | |
cdfce7c2 | 262 | </div> |
c9759229 | 263 | <hr style="clear: both;" /> |
d9384cb5 TM |
264 | <div style="background-color:#FFDDDD;"> |
265 | <font color="red">$message</font> | |
266 | </div> | |
41e57f9e TM |
267 | <div style="text-align:right;"> |
268 | $fortune | |
269 | </div> | |
cdfce7c2 | 270 | EOF; |
35916247 TM |
271 | |
272 | return $html; | |
cdfce7c2 TM |
273 | } |
274 | ||
d9384cb5 TM |
275 | function internal_url($link) { |
276 | return $_SERVER['SCRIPT_NAME'].'/'.$link; | |
277 | } | |
278 | ||
cdfce7c2 TM |
279 | function table_add_images(&$table) { |
280 | $image = array('model_id'); | |
281 | foreach($table as $id => $row) { | |
282 | foreach($image as $column) if(isset($table[$id][$column])) { | |
283 | $type = @array_shift(preg_split('/_/', $column)); | |
284 | $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg'; | |
6f7943a0 | 285 | $table[$id][$type.'_image']=$this->img_link($src, $src, $table[$id][$column], false, false); |
cdfce7c2 TM |
286 | } |
287 | } | |
288 | } | |
289 | ||
81ab8aef | 290 | function render_barcode($barcode,$opts=false) { |
6f7943a0 | 291 | return $this->img_link($this->internal_url("barcode/$barcode"),$this->internal_url("barcode/$barcode"),$barcode,false,false,$opts); |
81ab8aef TM |
292 | } |
293 | ||
294 | function table_add_barcodes(&$table) { | |
295 | $image = array('model_barcode', 'item_serial'); | |
296 | foreach($table as $id => $row) { | |
297 | foreach($image as $column) if(isset($table[$id][$column])) { | |
298 | $table[$id][$column]=$this->render_barcode($table[$id][$column]); | |
299 | } | |
300 | } | |
301 | } | |
302 | ||
d6975011 | 303 | function table_add_relations(&$table, $class, $suffix_relations='_relations') { |
aaafc8b7 | 304 | $where_url = '%d/?where[%c]==%v'; |
d6975011 | 305 | $relations = array( //TODO: Autodetect??? |
aaafc8b7 TM |
306 | 'model' => array( |
307 | 'model_id' => array(array('item',$where_url)), | |
d516a31d | 308 | 'model_barcode' => array(array('store','assistant/%d?barcode=%v')), |
41e57f9e | 309 | 'model_name' => array(array('google','http://google.com/search?q=%v',true)) //TODO: add manufacturer to google query |
aaafc8b7 | 310 | ), |
4ece8e80 TM |
311 | 'item' => array( |
312 | 'item_serial' => array(array('dispose','assistant/%d?serial=%v'),array('sell','assistant/%d?serial=%v')) | |
313 | ), | |
0646d9ef TM |
314 | 'category' => array('category_id' => array(array('item',$where_url), array('model',$where_url))), |
315 | 'producer' => array('producer_id' => array(array('item',$where_url), array('model',$where_url))), | |
aaafc8b7 TM |
316 | 'vendor' => array('vendor_id' => array(array('item',$where_url))), |
317 | 'room' => array('room_id' => array(array('item',$where_url))), | |
318 | 'status' => array('status_id' => array(array('item',$where_url))) | |
d6975011 TM |
319 | ); |
320 | foreach($table as $id => $row) { | |
321 | foreach($row as $column => $value) { | |
322 | if(isset($relations[$class][$column])) { | |
323 | foreach($relations[$class][$column] as $destination) { | |
aaafc8b7 TM |
324 | $destination_url = str_replace( |
325 | array('%d','%c','%v'), | |
d516a31d | 326 | array(urlencode($destination[0]),urlencode($column),urlencode($value)), |
aaafc8b7 TM |
327 | $destination[1] |
328 | ); | |
d516a31d | 329 | @$table[$id][$class.$suffix_relations] .= $this->link($destination[0], $destination_url, !isset($destination[2])).','; |
d6975011 TM |
330 | } |
331 | } | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
cdfce7c2 TM |
336 | function table_collapse(&$table) { |
337 | $collapse = array( | |
338 | 'item_id' => 'item_id', | |
339 | 'model_id' => 'model_name', | |
340 | 'category_id' => 'category_name', | |
341 | 'producer_id' => 'producer_name', | |
342 | 'vendor_id' => 'vendor_name', | |
343 | 'room_id' => 'room_name', | |
344 | 'status_id' => 'status_name', | |
345 | ); | |
346 | foreach($table as $id => $row) { | |
347 | foreach($collapse as $link => $title) | |
fff6ce40 | 348 | if(isset($table[$id][$link]) && isset($row[$title])) { |
cdfce7c2 TM |
349 | $type = @array_shift(preg_split('/_/', $link)); |
350 | if($link != $title) unset($table[$id][$link]); | |
351 | $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/'); | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
356 | function table_sort(&$table) { | |
357 | $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name'); | |
358 | $table_sorted = array(); | |
359 | foreach($table as $id => $row) { | |
360 | $table_sorted[$id] = array(); | |
361 | foreach($precedence as $column) if(isset($table[$id][$column])) { | |
326a9fc9 | 362 | $table_sorted[$id][T($column)]=$table[$id][$column]; |
cdfce7c2 TM |
363 | unset($table[$id][$column]); |
364 | } | |
326a9fc9 TM |
365 | //$table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]); |
366 | foreach($table[$id] as $key => $val) $table_sorted[$id][T($key)] = $val; //array_merge with T() translating | |
cdfce7c2 TM |
367 | } |
368 | $table = $table_sorted; | |
369 | } | |
370 | ||
d6975011 | 371 | function render_item_table($table,$class=false) { |
8ca87ee3 | 372 | if(empty($table)) return '<h3>'.T('holy primordial emptiness is all you can find here...').'</h3><br />'; |
cdfce7c2 | 373 | $this->table_add_images($table); |
d6975011 | 374 | if($class) $this->table_add_relations($table,$class); |
aaafc8b7 | 375 | $this->table_add_barcodes($table); |
cdfce7c2 TM |
376 | $this->table_collapse($table); |
377 | $this->table_sort($table); | |
fc5c5b8b | 378 | return $this->table($table); |
cdfce7c2 TM |
379 | } |
380 | ||
d0e7939c TM |
381 | function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) { |
382 | $html = ''; | |
cdfce7c2 | 383 | foreach($columns as $column) { |
326a9fc9 | 384 | $html.=T($class).':<b>'.T($column['Field']).'</b>: '; |
7d20381f | 385 | $name="values[$class][".$column['Field'].'][]'; |
16261142 | 386 | $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false; |
cdfce7c2 | 387 | switch(true) { |
a084118b | 388 | case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)): |
e9cb8cea | 389 | if(!$val) $val = ''; |
fc5c5b8b TM |
390 | $html.=$this->input($name, $val, 'hidden'); |
391 | $html.=$val.'(AUTO)'; | |
cdfce7c2 TM |
392 | break; |
393 | case isset($selectbox[$column['Field']]): | |
e9cb8cea | 394 | $html.=$this->select($name,$selectbox[$column['Field']],$val); |
cdfce7c2 TM |
395 | break; |
396 | default: | |
fc5c5b8b | 397 | $html.=$this->input($name, $val); |
cdfce7c2 TM |
398 | break; |
399 | } | |
fc5c5b8b | 400 | $html.='<br />'; |
cdfce7c2 | 401 | } |
d0e7939c TM |
402 | return $html; |
403 | } | |
cdfce7c2 | 404 | |
d0e7939c TM |
405 | function render_insert_form_multi($array) { |
406 | $html = ''; | |
407 | $head=false; | |
408 | ||
409 | foreach($array as $key => $args) { | |
410 | $parts=array('inputs'); | |
411 | if(!$head) { $head = true; | |
412 | $parts[]='head'; | |
413 | } | |
414 | if(!isset($array[$key+1])) { | |
415 | $parts[]='foot'; | |
5b0075fa TM |
416 | $hr = ''; |
417 | } else $hr = '<hr />'; | |
418 | //$args[] = false; | |
d0e7939c | 419 | $args[] = $parts; |
f5baa075 | 420 | |
d0e7939c | 421 | $html .= call_user_func_array(array($this, 'render_insert_form'), $args); |
5b0075fa | 422 | $html .= $hr; |
d0e7939c TM |
423 | } |
424 | return $html; | |
425 | } | |
426 | ||
427 | function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) { | |
428 | $html = ''; | |
429 | //print_r($parts); | |
430 | //echo('<pre>'); print_r($selectbox); | |
431 | //echo('<pre>'); print_r($current); | |
432 | $update = false; | |
433 | if(is_array($current)) { | |
434 | $update = true; | |
435 | $current = array_shift($current); | |
436 | } | |
437 | ||
438 | if(!is_array($hidecols)) $hidecols = array(); | |
439 | $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect | |
440 | ||
441 | if(!is_array($parts) || in_array('head', $parts)) { | |
442 | $action = $action ? " action='$action'" : false; | |
443 | $html.="<form$action method='POST'>"; //TODO: use $this->form() | |
5b0075fa | 444 | $html.='<span><div name="input_set" style="float:left; border:1px solid grey; padding: 1px; margin: 1px;">'; |
cdfce7c2 TM |
445 | } |
446 | ||
d0e7939c TM |
447 | if(!is_array($parts) || in_array('inputs', $parts)) |
448 | $html.=$this->render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update); | |
449 | ||
450 | if(!is_array($parts) || in_array('foot', $parts)) { | |
5b0075fa | 451 | $html .= '</div></span><br style="clear:both" />'; |
d0e7939c TM |
452 | if($multi_insert) { //TODO, move to separate JS file |
453 | $html.=<<<EOF | |
d0e7939c TM |
454 | <script> |
455 | function duplicate_element(what, where) { | |
456 | var node = document.getElementsByName(what)[0]; | |
457 | node.parentNode.appendChild(node.cloneNode(true)); | |
458 | } | |
459 | </script> | |
460 | <a href='#' onClick="duplicate_element('input_set')">+</a> | |
461 | EOF; | |
462 | } | |
463 | ||
464 | $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting... | |
465 | $html.=$this->input(false, $btn, 'submit'); | |
466 | $html.='</form>'; | |
467 | } | |
fc5c5b8b | 468 | return $html; |
cdfce7c2 TM |
469 | } |
470 | } | |
471 | ||
78bf26a5 TM |
472 | /** |
473 | * Trida poskytuje rozhrani k databazi skladu | |
474 | * | |
475 | * @package Sklad_DB | |
476 | * @author Tomas Mudrunka | |
477 | */ | |
cdfce7c2 TM |
478 | class Sklad_DB extends PDO { |
479 | function __construct() { | |
63747dad | 480 | $this->auth = new Sklad_Auth(); |
cdfce7c2 TM |
481 | |
482 | parent::__construct( | |
483 | DB_DSN, DB_USER, DB_PASS, | |
484 | array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL | |
485 | ); | |
486 | } | |
487 | ||
488 | function escape($str) { | |
489 | return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK | |
490 | } | |
491 | ||
382324d3 TM |
492 | function quote_identifier($str) { |
493 | return '`'.$this->escape($str).'`'; //TODO HACK | |
494 | } | |
495 | ||
cb8a6861 | 496 | function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') { |
2bedfdac | 497 | //Configuration |
cdfce7c2 TM |
498 | $join = array( |
499 | 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'), | |
500 | 'model' => array('category', 'producer') | |
9ea191cb | 501 | ); //TODO Autodetect using foreign keys? |
cdfce7c2 | 502 | $search_fields = array( |
0646d9ef TM |
503 | 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name'), |
504 | 'model' => array('model_id','model_name','model_barcode','model_descript','producer_name') | |
9ea191cb | 505 | ); //TODO Autodetect |
2bedfdac | 506 | |
d9d47bd3 TM |
507 | //Init |
508 | if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!! | |
509 | ||
2bedfdac TM |
510 | //Escaping |
511 | $class = $this->escape($class); | |
512 | ||
513 | //SELECT | |
382324d3 | 514 | $sql="SELECT * FROM `$class`\n"; |
2bedfdac | 515 | //JOIN |
382324d3 | 516 | if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN `$j` USING($j$suffix_id)\n"; |
2bedfdac | 517 | //WHERE/REGEXP |
cdfce7c2 TM |
518 | if($search) { |
519 | $search = $this->quote($search); | |
326a9fc9 | 520 | if(!isset($search_fields[$class])) die(trigger_error(T("Can't search in $class table yet :-("))); //TODO: post_redirect_get |
5895162b TM |
521 | $sql_search = ''; |
522 | foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search "; | |
523 | $where[] = "FALSE $sql_search"; | |
cb8a6861 TM |
524 | } elseif($id) $where[] = "$class$suffix_id = $id"; |
525 | if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0'; | |
526 | ||
5895162b | 527 | if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n"; |
117817be | 528 | //ORDER |
fd479ff5 | 529 | if(!$order) $order = $class.$suffix_id.' DESC'; |
117817be TM |
530 | if($this->contains_history($class)) $order .= ",${class}_valid_from DESC"; |
531 | $sql .= "ORDER BY $order\n"; | |
2bedfdac | 532 | //LIMIT/OFFSET |
cdfce7c2 TM |
533 | if($limit) { |
534 | $limit = $this->escape((int)$limit); | |
535 | $offset = $this->escape((int)$offset); | |
536 | $sql .= "LIMIT $offset,$limit\n"; | |
537 | } | |
2bedfdac | 538 | |
cdfce7c2 TM |
539 | return $sql; |
540 | } | |
541 | ||
66b6f4d6 | 542 | function safe_query($sql, $fatal=true) { |
cdfce7c2 TM |
543 | $result = $this->query($sql); |
544 | if(!$result) { | |
2bedfdac | 545 | $error = $this->errorInfo(); |
66b6f4d6 TM |
546 | 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>"); |
547 | if($fatal) die(); | |
cdfce7c2 TM |
548 | } |
549 | return $result; | |
550 | } | |
551 | ||
326a9fc9 TM |
552 | function translate_query_results($result) { |
553 | $translate_cols = array('status_name', 'item_valid_till'); //TODO: Hardcoded | |
554 | foreach($result as $key => $row) { | |
555 | foreach($translate_cols as $col) if(isset($result[$key][$col])){ | |
556 | $result[$key][$col] = T($result[$key][$col]); | |
557 | } | |
558 | } | |
559 | return $result; | |
560 | } | |
561 | ||
562 | function safe_query_fetch($sql, $fatal=true, $fetch_flags = PDO::FETCH_ASSOC, $translate=true) { | |
563 | $result = $this->safe_query($sql, $fatal)->fetchAll($fetch_flags); | |
564 | if($translate) $result = $this->translate_query_results($result); | |
565 | return $result; | |
566 | } | |
567 | ||
568 | ||
cb8a6861 TM |
569 | function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') { |
570 | $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history); | |
326a9fc9 | 571 | $result = $this->safe_query_fetch($sql); |
cdfce7c2 TM |
572 | if(!$result || !is_array($indexed)) return $result; |
573 | ||
574 | foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row; | |
575 | return $indexed; | |
576 | } | |
577 | ||
d0e7939c | 578 | function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs |
cdfce7c2 TM |
579 | $class = $this->escape($class); |
580 | $sql = "SHOW COLUMNS FROM $class;"; | |
d0e7939c TM |
581 | $columns = $this->safe_query_fetch($sql); |
582 | /*foreach($columns as $colk => $col) foreach($col as $key => $val) { | |
583 | if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment'; | |
584 | }*/ | |
585 | return $columns; | |
cdfce7c2 TM |
586 | } |
587 | ||
588 | function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') { | |
1c99d83b | 589 | $selectbox=array( //TODO: Hardcoded... |
41e57f9e | 590 | 'model_countable' => array(0 => 'no', 1 => 'yes'), |
3e939604 TM |
591 | 'model_eshop_hide' => array(0 => 'no', 1 => 'yes'), |
592 | 'vendor_id' => array('COMPULSORY' => 'select...') | |
1c99d83b | 593 | ); |
cdfce7c2 | 594 | foreach($columns as $column) { |
66b6f4d6 | 595 | 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 |
596 | if($class && $column['Field'] == $class.$suffix_id) continue; |
597 | if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue; | |
598 | $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']); | |
66b6f4d6 | 599 | |
117817be | 600 | $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : ''; |
b4dcae05 | 601 | $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!! |
326a9fc9 | 602 | $result = $this->safe_query_fetch($sql, false); |
66b6f4d6 | 603 | if(!$result) continue; |
cdfce7c2 TM |
604 | foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name]; |
605 | } | |
606 | //echo('<pre>'); print_r($selectbox); | |
66b6f4d6 | 607 | return array_filter($selectbox, 'ksort'); |
9ea191cb TM |
608 | } |
609 | ||
d0e7939c | 610 | function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed |
16261142 TM |
611 | $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : ''; |
612 | $value=$this->quote($value); | |
613 | $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!! | |
326a9fc9 | 614 | $result = $this->safe_query_fetch($sql); |
d0e7939c | 615 | if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get... |
16261142 TM |
616 | } |
617 | ||
9ea191cb TM |
618 | function contains_history($table) { |
619 | $history_tables = array('item'); //TODO Autodetect | |
620 | return in_array($table, $history_tables); | |
cdfce7c2 TM |
621 | } |
622 | ||
623 | function build_query_insert($table, $values, $replace=true, $suffix_id='_id') { | |
b66fadbb TM |
624 | //Init |
625 | $history = $this->contains_history($table); | |
626 | ||
9ea191cb | 627 | //Escaping |
cdfce7c2 TM |
628 | $table = $this->escape($table); |
629 | ||
630 | //Get list of POSTed columns | |
aa3fd0a8 TM |
631 | $columns_array = array_map(array($this,'escape'), array_keys($values[0])); |
632 | $columns = implode(',',$columns_array); | |
cdfce7c2 | 633 | |
9fb856ba | 634 | //Build query |
b66fadbb | 635 | $sql = ''; |
b66fadbb TM |
636 | //echo('<pre>'); die(print_r($values)); |
637 | ||
638 | if($history) { | |
639 | $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true; | |
640 | if($history_update) { | |
641 | $sql .= "UPDATE $table"; | |
117817be TM |
642 | $sql .= " SET ${table}_valid_till=NOW()"; |
643 | $sql .= " WHERE ${table}_valid_till=0 AND ("; | |
b66fadbb TM |
644 | $or = ''; |
645 | foreach($values as $row) { | |
5b0075fa | 646 | $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']); |
b66fadbb TM |
647 | $or = ' OR'; |
648 | } | |
649 | $sql .= " );\n\n"; | |
650 | $replace = false; | |
651 | } | |
652 | } | |
653 | ||
cdfce7c2 | 654 | //Insert into table (columns) |
aa3fd0a8 | 655 | $sql .= "INSERT INTO $table ($columns) VALUES "; |
cdfce7c2 TM |
656 | |
657 | //Values (a,b,c),(d,e,f) | |
658 | $comma=''; | |
659 | foreach($values as $row) { | |
9fb856ba TM |
660 | $row_quoted = array_map(array($this,'quote'), $row); //Check |
661 | if($history) { | |
b66fadbb TM |
662 | foreach($row as $column => $value) { |
663 | switch($column) { | |
664 | case $table.'_valid_from': | |
665 | $row_quoted[$column] = 'NOW()'; | |
666 | break; | |
667 | case $table.'_valid_till': | |
668 | $row_quoted[$column] = '0'; | |
669 | break; | |
9fb856ba | 670 | case $table.'_author': |
e9f6461f TM |
671 | $row_quoted[$column] = $this->auth->get_user_id(); |
672 | //die($this->auth->get_user_id().'=USER'); | |
b66fadbb | 673 | break; |
b66fadbb TM |
674 | } |
675 | } | |
676 | } | |
677 | $sql .= $comma.'('.implode(',',$row_quoted).')'; | |
cdfce7c2 TM |
678 | $comma = ','; |
679 | } | |
680 | ||
aa3fd0a8 TM |
681 | //On duplicate key |
682 | if($replace) { | |
683 | foreach($columns_array as $col) { | |
684 | if($col == $table.'_id' || $col == $table.'_valid_till') continue; | |
685 | $on_duplicate[] = "$col=VALUES($col)"; | |
686 | } | |
687 | $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate); | |
688 | } | |
689 | ||
cdfce7c2 TM |
690 | //Terminate |
691 | $sql .= ';'; | |
692 | return $sql; | |
693 | } | |
694 | ||
b4dcae05 TM |
695 | function insert_or_update($table, $values, $replace=true) { |
696 | $sql = $this->build_query_insert($table, $values, $replace); | |
cdfce7c2 TM |
697 | $this->safe_query($sql); |
698 | return $this->lastInsertId(); | |
699 | } | |
700 | ||
b4dcae05 | 701 | function insert_or_update_multitab($values, $replace=true) { |
371a86f4 | 702 | $last=false; |
b4dcae05 | 703 | foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace); |
371a86f4 TM |
704 | return $last; |
705 | } | |
706 | ||
cdfce7c2 | 707 | function delete($table, $id, $suffix_id='_id') { |
64f31c54 | 708 | if($this->contains_history($table)) return false; |
cdfce7c2 TM |
709 | $key = $this->escape($table.$suffix_id); |
710 | $table = $this->escape($table); | |
711 | $id = $this->quote($id); | |
712 | return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;"); | |
713 | } | |
714 | } | |
715 | ||
0a027cc7 TM |
716 | /** |
717 | * Trida poskytuje high-level rozhrani k databazi skladu | |
718 | * | |
719 | * @package Sklad_DB_Abstract | |
720 | * @author Tomas Mudrunka | |
721 | */ | |
722 | class Sklad_DB_Abstract extends Sklad_DB { | |
723 | //TODO Code | |
724 | } | |
725 | ||
78bf26a5 TM |
726 | /** |
727 | * Trida implementuje uzivatelske rozhrani skladu | |
728 | * | |
729 | * Example usage: | |
730 | * $sklad = new Sklad_UI(); | |
731 | * $sklad->process_http_request(); | |
732 | * | |
733 | * @package Sklad_UI | |
734 | * @author Tomas Mudrunka | |
735 | */ | |
cdfce7c2 TM |
736 | class Sklad_UI { |
737 | function __construct() { | |
738 | $this->db = new Sklad_DB(); | |
739 | $this->html = new Sklad_HTML(); | |
740 | } | |
741 | ||
cb8a6861 | 742 | function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) { |
d6975011 | 743 | return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class); |
cdfce7c2 TM |
744 | } |
745 | ||
a25f85f8 | 746 | function render_form_add($class) { |
cdfce7c2 TM |
747 | $columns = $this->db->get_columns($class); |
748 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
a25f85f8 | 749 | return $this->html->render_insert_form($class, $columns, $selectbox); |
cdfce7c2 TM |
750 | } |
751 | ||
f5baa075 | 752 | function render_form_edit($class, $id, $multi_insert) { |
cdfce7c2 TM |
753 | $columns = $this->db->get_columns($class); |
754 | $selectbox = $this->db->columns_get_selectbox($columns, $class); | |
117817be | 755 | $current = $this->db->get_listing($class, $id, 1); |
f5baa075 | 756 | return $this->html->render_insert_form($class, $columns, $selectbox, $current, false, false, $multi_insert); |
cdfce7c2 TM |
757 | } |
758 | ||
a25f85f8 | 759 | function render_single_record_details($class, $id) { |
cdfce7c2 TM |
760 | $id_next = $id + 1; |
761 | $id_prev = $id - 1 > 0 ? $id - 1 : 0; | |
762 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
763 | $html=''; |
764 | $html.= $this->html->link('<<', "$class/$id_prev/"); | |
765 | $html.= '-'; | |
766 | $html.= $this->html->link('>>', "$class/$id_next/"); | |
767 | $html.= '<br />'; | |
54694117 | 768 | $html.='<span style="float:right;">'.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")).'</span>'; |
a25f85f8 | 769 | $html.= $this->html->link('edit', "$class/$id/edit/"); |
9fb856ba | 770 | if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/"); |
a25f85f8 | 771 | return $html; |
cdfce7c2 TM |
772 | } |
773 | ||
a25f85f8 | 774 | function render_listing_navigation($class, $id, $limit, $offset) { |
cdfce7c2 TM |
775 | $offset_next = $offset + $limit; |
776 | $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0; | |
777 | $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : ''; | |
a25f85f8 TM |
778 | $html=''; |
779 | $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get"); | |
780 | $html.= '-'; | |
781 | $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get"); | |
782 | $html.= '<br />'; | |
783 | $html.= $this->html->link('new', "$class/new/$get"); | |
784 | return $html; | |
cdfce7c2 TM |
785 | } |
786 | ||
a25f85f8 TM |
787 | function render_listing_extensions($class, $id, $limit, $offset, $edit=false) { |
788 | $html=''; | |
cdfce7c2 | 789 | if(is_numeric($id)) { |
a25f85f8 | 790 | $html.=$this->render_single_record_details($class, $id); |
cdfce7c2 | 791 | } else { |
a25f85f8 | 792 | $html.=$this->render_listing_navigation($class, '*', $limit, $offset); |
cdfce7c2 TM |
793 | } |
794 | if($edit) { | |
f5baa075 | 795 | $html.= $this->render_form_edit($class, $id, false); |
cdfce7c2 | 796 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete"; |
35916247 TM |
797 | $html.=$this->html->form($action,'POST',array( |
798 | array(false,'DELETE','submit'), | |
799 | array('sure', false, 'checkbox', false, false, 'sure?') | |
800 | )); | |
cdfce7c2 | 801 | $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image"; |
35916247 TM |
802 | $html.=$this->html->form($action,'POST',array( |
803 | array('image', false, 'file', false, 'size="30"'), | |
804 | array(false, 'IMAGE', 'submit') | |
805 | ), "enctype='multipart/form-data'"); | |
cdfce7c2 | 806 | } |
a25f85f8 | 807 | return $html; |
cdfce7c2 TM |
808 | } |
809 | ||
810 | function check_auth() { | |
63747dad | 811 | new HTTP_Auth('SkladovejSystem', true, array($this->db->auth,'check_auth')); |
cdfce7c2 TM |
812 | } |
813 | ||
a8bbdc31 TM |
814 | function post_redirect_get($location, $message='', $error=false, $translate=true) { |
815 | $messaget = $translate ? T($message) : $message; | |
816 | $url_args = $messaget != '' ? '?message='.urlencode($messaget) : ''; | |
8acef003 | 817 | $location = $this->html->internal_url($location).$url_args; |
1f74892d | 818 | header('Location: '.$location); |
64f31c54 | 819 | if($error) trigger_error($message); |
7efdf72a TM |
820 | $location=htmlspecialchars($location); |
821 | die( | |
822 | "<meta http-equiv='refresh' content='0; url=$location'>". | |
a8bbdc31 | 823 | $messaget."<br />Location: <a href='$location'>$location</a>" |
7efdf72a | 824 | ); |
cdfce7c2 TM |
825 | } |
826 | ||
623c65e2 | 827 | function safe_include($dir,$name,$vars=array(),$ext='.inc.php') { |
64f31c54 | 828 | if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true); |
3e6c412e | 829 | $filename="$dir/$name$ext"; |
64f31c54 | 830 | if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true); |
623c65e2 | 831 | foreach($vars as $var => $val) $$var=$val; |
3e6c412e TM |
832 | ob_start(); |
833 | include($filename); | |
834 | $out=ob_get_contents(); | |
835 | ob_end_clean(); | |
836 | return $out; | |
837 | } | |
838 | ||
a8bbdc31 TM |
839 | function check_input_validity($field, $value='', $ruleset=0) { |
840 | $rules = array(0 => array( | |
841 | 'model_barcode' => '/./', | |
3e939604 TM |
842 | 'item_serial' => '/./', |
843 | 'vendor_id' => '/^[0-9]*$/' | |
a8bbdc31 TM |
844 | )); |
845 | if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false; | |
846 | return true; | |
847 | } | |
848 | ||
bda4a4be | 849 | function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) { |
cdfce7c2 | 850 | if($_SERVER['REQUEST_METHOD'] != 'POST') return; |
1f74892d | 851 | //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!! |
cdfce7c2 TM |
852 | |
853 | //SephirPOST: | |
371a86f4 TM |
854 | |
855 | /* Tenhle foreach() prekopiruje promenne | |
7d20381f | 856 | * z: $_POST['values'][$table][$column][$id]; |
371a86f4 TM |
857 | * do: $values[$table][$id][$column] |
858 | */ | |
7d20381f TM |
859 | if(isset($_POST['values'])) { |
860 | $values=array(); | |
861 | foreach($_POST['values'] as $table => $columns) { | |
862 | foreach($columns as $column => $ids) { | |
a8bbdc31 TM |
863 | foreach($ids as $id => $val) { |
864 | $values[$table][$id][$column] = trim($val); | |
865 | if(!$this->check_input_validity($column,$val)) { | |
866 | $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS | |
867 | $this->html->link('GO BACK', 'javascript:history.back()', false, false); | |
868 | $this->post_redirect_get('', $message, false, false); | |
869 | } | |
870 | } | |
7d20381f | 871 | } |
cdfce7c2 | 872 | } |
7d20381f | 873 | //die(print_r($values)); |
cdfce7c2 TM |
874 | } |
875 | ||
876 | if($action) switch($action) { | |
877 | case 'new': | |
b4dcae05 | 878 | $replace = false; |
cdfce7c2 | 879 | case 'edit': |
b4dcae05 | 880 | if(!isset($replace)) $replace = true; |
64f31c54 | 881 | $table = $class ? $class : 'item'; |
cdfce7c2 | 882 | //print_r($values); //debug |
b4dcae05 | 883 | $last = $this->db->insert_or_update_multitab($values, $replace); |
bda4a4be | 884 | $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/"; |
d9384cb5 | 885 | $next = "$table/new/"; |
bda4a4be TM |
886 | $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.'; |
887 | $this->post_redirect_get($last, $message); | |
cdfce7c2 TM |
888 | break; |
889 | case 'delete': | |
64f31c54 TM |
890 | if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)'); |
891 | $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P"); | |
1f74892d | 892 | $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); |
cdfce7c2 TM |
893 | break; |
894 | case 'image': | |
895 | $image_classes = array('model'); //TODO, use this more widely across the code | |
64f31c54 | 896 | if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!"); |
cdfce7c2 | 897 | $image_destination = DIR_IMAGES."/$class/$id.jpg"; |
326a9fc9 | 898 | if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Everything has to be called somehow!', true); |
cdfce7c2 | 899 | if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) { |
1f74892d | 900 | chmod ($image_destination, 0664); |
326a9fc9 TM |
901 | $this->post_redirect_get("$class/$id", 'Image has been upbloated successfully :)'); |
902 | } else $this->post_redirect_get("$class/$id/edit", 'File upload failed :(', true); | |
cdfce7c2 TM |
903 | break; |
904 | default: | |
326a9fc9 | 905 | $this->post_redirect_get('', 'Nothin\' to do here my cutie :-*'); |
cdfce7c2 TM |
906 | break; |
907 | } | |
908 | ||
909 | die('POSTed pyčo!'); | |
910 | } | |
911 | ||
912 | function process_http_request() { | |
913 | $this->check_auth(); | |
914 | ||
915 | @ini_set('magic_quotes_gpc' , 'off'); | |
916 | if(get_magic_quotes_gpc()) { | |
917 | die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!")); | |
918 | } | |
919 | ||
920 | $PATH_INFO=@trim($_SERVER[PATH_INFO]); | |
a5094502 | 921 | if($PATH_INFO == '' || $PATH_INFO == '/') $PATH_INFO = FRONTEND_PAGE_WELCOME; |
cdfce7c2 | 922 | $PATH_CHUNKS = preg_split('/\//', $PATH_INFO); |
81ab8aef | 923 | //Sephirot: |
cdfce7c2 | 924 | if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]=''; |
88c38b26 | 925 | if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode' && $PATH_CHUNKS[1]!='api') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost... |
e9f6461f | 926 | echo $this->html->header($PATH_INFO,$this->db->auth->get_user()); |
81ab8aef | 927 | switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible |
cdfce7c2 TM |
928 | case 'test': //test |
929 | die('Tell me why you cry'); | |
930 | break; | |
88c38b26 TM |
931 | case 'assistant': case 'api': //assistant|api |
932 | $incdirs = array( | |
933 | 'assistant' => DIR_ASSISTANTS, | |
934 | 'api' => DIR_APIS | |
935 | ); | |
de77377e TM |
936 | $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false; |
937 | $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3); | |
938 | $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2]; | |
939 | $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL']; | |
5ef6c52f | 940 | $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2]; |
88c38b26 | 941 | echo $this->safe_include($incdirs[$PATH_CHUNKS[1]],$PATH_CHUNKS[2],$assistant_vars); |
3e6c412e | 942 | break; |
81ab8aef TM |
943 | case 'barcode': //barcode |
944 | Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2))); | |
945 | break; | |
cdfce7c2 TM |
946 | default: //? |
947 | $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false; | |
948 | $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item'; | |
949 | if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]=''; | |
950 | switch($PATH_CHUNKS[2]) { | |
951 | case 'new': //?/new | |
952 | $this->process_http_request_post($PATH_CHUNKS[2], $class); | |
a25f85f8 | 953 | echo $this->render_form_add($class); |
cdfce7c2 TM |
954 | break; |
955 | default: //?/? | |
956 | $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false); | |
957 | if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]=''; | |
958 | $edit=false; | |
959 | switch($PATH_CHUNKS[3]) { | |
960 | case 'edit': //?/?/edit | |
9fb856ba TM |
961 | case 'image': //?/?/image |
962 | case 'delete': //?/?/delete | |
cdfce7c2 TM |
963 | $this->process_http_request_post($PATH_CHUNKS[3], $class, $id); |
964 | $edit=true; | |
965 | default: //?/?/? | |
9fb856ba | 966 | $history = $PATH_CHUNKS[3] == 'history' ? true : false; |
2415a365 TM |
967 | $limit = is_numeric($PATH_CHUNKS[3]) ? (int) $PATH_CHUNKS[3] : FRONTEND_LISTING_LIMIT; |
968 | $offset = isset($PATH_CHUNKS[4]) ? (int) $PATH_CHUNKS[4] : 0; | |
d9d47bd3 | 969 | $where = @is_array($_GET['where']) ? $_GET['where'] : false; |
cb8a6861 | 970 | echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history); |
a25f85f8 | 971 | echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit); |
cdfce7c2 TM |
972 | //print_r(array("<pre>",$_SERVER)); |
973 | break; | |
974 | } | |
975 | break; | |
976 | } | |
977 | break; | |
978 | } | |
979 | } | |
980 | } | |
981 | ||
982 | $sklad = new Sklad_UI(); | |
983 | $sklad->process_http_request(); | |
984 | ||
54694117 | 985 | echo('<br style="clear:both;" /><hr />'); |