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