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