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