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