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