3 * SkladovySystem - Storage management system compatible with LMS
4 * Copyright (C) 2011 Thomas Mudrunka
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.
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.
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/>.
20 require_once('sklad.conf.php');
21 require_once('Sklad_LMS-fake.class.php');
22 require_once('HTTP_Auth.class.php');
25 function header_print($title='') {
27 $script = $_SERVER['SCRIPT_NAME'];
28 $search = @trim
($_GET['q']);
30 <h1><a href="$script/">SystémSklad</a><small>$title</small></h1>
33 <li><a href="?logout">Logout</a></li>
34 <li><a href="$script/">Home</a></li>
36 <form action="?" method="GET">
37 <input type="text" name="q" placeholder="regexp..." value="$search" />
38 <input type="submit" value="filter" />
40 <!-- form action="$script/" method="GET">
41 <input type="text" name="q" placeholder="regexp..." value="$search" />
42 <input type="submit" value="search items" />
48 function row_print($row) {
50 foreach($row as $var) {
51 if(trim($var) == '') $var = ' ';
52 echo("<td>$var</td>");
57 function table_print(&$table, $params='border=1') {
58 echo("<table $params>");
60 foreach($table as $row) {
62 $this->row_print(array_keys($row));
65 $this->row_print($row);
70 function link($title='n/a', $link='#void', $internal=true) {
71 if($internal) $link = $_SERVER['SCRIPT_NAME'].'/'.$link;
72 return "<a href='$link'>$title</a>";
75 function img($src='#void', $title='img') {
76 return "<img src='$src' alt='$title' title='$title' width=64 />";
79 function table_add_images(&$table) {
80 $image = array('model_id');
81 foreach($table as $id => $row) {
82 foreach($image as $column) if(isset($table[$id][$column])) {
83 $type = @array_shift
(preg_split('/_/', $column));
84 $src=URL_IMAGES
."/$type/".$table[$id][$column].'.jpg';
85 $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]);
90 function table_collapse(&$table) {
92 'item_id' => 'item_id',
93 'model_id' => 'model_name',
94 'category_id' => 'category_name',
95 'producer_id' => 'producer_name',
96 'vendor_id' => 'vendor_name',
97 'room_id' => 'room_name',
98 'status_id' => 'status_name',
100 foreach($table as $id => $row) {
101 foreach($collapse as $link => $title)
102 if(isset($table[$id][$link])) {
103 $type = @array_shift
(preg_split('/_/', $link));
104 if($link != $title) unset($table[$id][$link]);
105 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
110 function table_sort(&$table) {
111 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
112 $table_sorted = array();
113 foreach($table as $id => $row) {
114 $table_sorted[$id] = array();
115 foreach($precedence as $column) if(isset($table[$id][$column])) {
116 $table_sorted[$id][$column]=$table[$id][$column];
117 unset($table[$id][$column]);
119 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
121 $table = $table_sorted;
124 function print_item_table($table) {
125 $this->table_add_images($table);
126 $this->table_collapse($table);
127 $this->table_sort($table);
128 return $this->table_print($table);
131 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false) {
132 $html = "<input type='$type' ";
133 if($name) $html.= "name='$name' ";
134 if(!is_bool($value)) $html.= "value='$value' ";
135 if($options) $html.= "$options ";
136 if($placeholder) $html.= "placeholder='$placeholder' ";
141 function select($name, $selectbox, $default=false) {
142 //echo('<pre>'); print_r($selectbox);
143 $html = "<select name='$name'>";
146 $value=$default; $title=$selectbox[$value];
147 $html .= "<option value='$value'>$value :: $title</option>";
148 unset($selectbox[$value]);
150 foreach($selectbox as $value => $title) {
151 $html .= "<option value='$value'>$value :: $title</option>";
153 $html .= "</select>";
157 function print_insert_form($class, $columns, $selectbox=array(), $current=false, $multi_insert=true) {
158 //echo('<pre>'); print_r($selectbox);
159 //echo('<pre>'); print_r($current);
161 if(is_array($current)) {
163 $current = array_shift($current);
166 echo('<form method="POST">');
167 if($multi_insert) echo('<div name="input_set" style="float:left; border:1px solid grey;">');
168 echo $this->input('table', $class, 'hidden');
169 foreach($columns as $column) {
170 echo($column['Field'].': ');
171 $name='value:'.$column['Field'].'[]';
173 case preg_match('/auto_increment/', $column['Extra']):
174 $val = $update ?
$current[$column['Field']] : ''; //opakuje se (skoro) zbytecne
175 echo $this->input($name, $val, 'hidden');
178 case isset($selectbox[$column['Field']]):
179 $val = $update ?
$current[$column['Field']] : false;
180 echo $this->select($name,$selectbox[$column['Field']],$val); //opakuje se
183 $val = $update ?
$current[$column['Field']] : false; //opakuje se
184 echo $this->input($name, $val);
191 //TODO, move to separate JS file
194 <span name="input_set_next"></span><br style="clear:both" />
196 function duplicate_element(what, where) {
197 document.getElementsByName(where)[0].outerHTML =
198 document.getElementsByName(what)[0].outerHTML
199 + document.getElementsByName(where)[0].outerHTML;
202 <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a>
206 $btn = is_array($current) ?
'UPDATE' : 'INSERT';
207 echo($this->input(false, $btn, 'submit'));
212 class Sklad_DB
extends PDO
{
213 function __construct() {
214 $this->lms
= new Sklad_LMS();
217 DB_DSN
, DB_USER
, DB_PASS
,
218 array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8") //Force UTF8 for MySQL
222 function escape($str) {
223 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
226 function build_query_select($class, $id=false, $limit=false, $offset=0, $search=false, $id_suffix='_id') {
227 $class = $this->escape($class);
229 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
230 'model' => array('category', 'producer')
232 $search_fields = array(
233 'item' => array('item_id','model_name','model_barcode','model_descript','producer_name','vendor_name')
235 $sql="SELECT * FROM $class\n";
236 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$id_suffix)\n";
238 $search = $this->quote($search);
239 if(!isset($search_fields[$class])) {
240 trigger_error("Ve tride $class zatim vyhledavat nemozno :-(");
243 $sql .= 'WHERE FALSE ';
244 foreach($search_fields[$class] as $column) $sql .= "OR $column REGEXP $search ";
245 } elseif($id) $sql .= "WHERE $class$id_suffix = $id\n";
247 $limit = $this->escape((int)$limit);
248 $offset = $this->escape((int)$offset);
249 $sql .= "LIMIT $offset,$limit\n";
254 function safe_query($sql) {
255 $result = $this->query($sql);
257 trigger_error('<font color=red><b>QUERY FAILED:</b><pre>'.$sql.'</pre></font>');
263 function get_listing($class, $id=false, $limit=false, $offset=0, $search=false, $indexed=array(), $suffix_id='_id') {
264 $sql = $this->build_query_select($class, $id, $limit, $offset, $search);
265 $result = $this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
266 if(!$result ||
!is_array($indexed)) return $result;
268 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
272 function get_columns($class) {
273 $class = $this->escape($class);
274 $sql = "SHOW COLUMNS FROM $class;";
275 return $this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
278 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
280 foreach($columns as $column) {
281 if($class && $column['Field'] == $class.$suffix_id) continue;
282 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
283 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
284 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table;";
285 $result=$this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
286 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
288 //echo('<pre>'); print_r($selectbox);
292 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
293 $table = $this->escape($table);
295 //Get list of POSTed columns
296 $columns = implode(',',array_map(array($this,'escape'), array_keys($values[0])));
298 //Insert into table (columns)
300 if($replace) $sql = 'REPLACE';
301 $sql .= " INTO $table ($columns) VALUES ";
303 //Values (a,b,c),(d,e,f)
305 foreach($values as $row) {
306 $sql .= $comma.'('.implode(',',array_map(array($this,'quote'), $row)).')';
315 function insert_or_update($table, $values) {
316 $sql = $this->build_query_insert($table, $values);
317 $this->safe_query($sql);
318 return $this->lastInsertId();
321 function delete($table, $id, $suffix_id='_id') {
322 $key = $this->escape($table.$suffix_id);
323 $table = $this->escape($table);
324 $id = $this->quote($id);
325 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
330 function __construct() {
331 $this->db
= new Sklad_DB();
332 $this->html
= new Sklad_HTML();
335 function show_items($class, $id=false, $limit=false, $offset=0, $search=false) {
336 $this->html
->print_item_table($this->db
->get_listing($class, $id, $limit, $offset, $search));
339 function show_form_add($class) {
340 $columns = $this->db
->get_columns($class);
341 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
342 $this->html
->print_insert_form($class, $columns, $selectbox);
345 function show_form_edit($class, $id) {
346 $columns = $this->db
->get_columns($class);
347 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
348 $current = $this->db
->get_listing($class, $id);
349 $this->html
->print_insert_form($class, $columns, $selectbox, $current);
352 function show_single_record_details($class, $id) {
354 $id_prev = $id - 1 > 0 ?
$id - 1 : 0;
355 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
356 echo $this->html
->link('<<', "$class/$id_prev/");
358 echo $this->html
->link('>>', "$class/$id_next/");
360 echo $this->html
->link('edit', "$class/$id/edit/");
363 function show_listing_navigation($class, $id, $limit, $offset) {
364 $offset_next = $offset +
$limit;
365 $offset_prev = $offset - $limit > 0 ?
$offset - $limit : 0;
366 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
367 echo $this->html
->link('<<', "$class/$id/$limit/$offset_prev/$get");
369 echo $this->html
->link('>>', "$class/$id/$limit/$offset_next/$get");
371 echo $this->html
->link('new', "$class/new/$get");
374 function show_listing_extensions($class, $id, $limit, $offset, $edit=false) {
375 if(is_numeric($id)) {
376 $this->show_single_record_details($class, $id);
378 $this->show_listing_navigation($class, '*', $limit, $offset);
381 echo('<br />TODO UPDATE FORM!<br />');
382 $this->show_form_edit($class, $id);
383 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
384 echo("<form action='$action' method='POST'>");
385 echo $this->html
->input(false, 'DELETE', 'submit');
386 echo 'sure?'.$this->html
->input('sure', false, 'checkbox');
388 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
389 echo("<form action='$action' method='POST' enctype='multipart/form-data'>");
390 echo $this->html
->input('image', false, 'file', false, 'size="30"');
391 echo $this->html
->input(false, 'IMAGE', 'submit');
396 function check_auth() {
397 new HTTP_Auth('SkladovejSystem', true, array($this->db
->lms
,'check_auth'));
400 function post_redirect_get($last, $next) { //TODO prepracovat, tohle je uplna picovina...
401 //header('Location: '.$_SERVER['REQUEST_URI']); //TODO redirect (need templating system or ob_start() first!!!)
402 echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html
->link('zde', $last).'.<br />'.
403 'Další záznam přidáte '.$this->html
->link('zde', $next).'.';
407 function process_http_request_post($action=false, $class=false, $id=false) {
408 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
409 echo('<pre>'); //DEBUG (maybe todo remove)
413 foreach($_POST as $key => $value) {
414 $name = preg_split('/:/',$key);
415 if(isset($name[0])) switch($name[0]) {
417 foreach($value as $id => $val) $values[$id][$name[1]]=$value[$id];
424 if($action) switch($action) {
427 if(!isset($_POST['table'])) die(trigger_error("Jest nutno specifikovat tabulku voe!"));
428 $table=$_POST['table'];
429 //print_r($values); //debug
430 $last = $this->db
->insert_or_update($table, $values);
431 $this->post_redirect_get("$table/$last/", "$table/new/");
434 if(!isset($_POST['sure']) ||
!$_POST['sure']) die(trigger_error('Sure user expected :-)'));
435 //$this->db->delete($class, $id);
436 die('Neco asi bylo smazano. Fnuk :\'-('); //TODO REDIRECT
439 $image_classes = array('model'); //TODO, use this more widely across the code
440 if(!in_array($class, $image_classes)) die(trigger_error("Nekdo nechce k DB Tride '$class' prirazovat obrazky!"));
441 $image_destination = DIR_IMAGES
."/$class/$id.jpg";
442 if($_FILES['image']['name'] == '') die(trigger_error('Kazde neco se musi nejak jmenovat!'));
443 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
444 chmod ($image_destination, 0664);
445 die('Obrazek se naladoval :)'); //TODO REDIRECT
446 } else die(trigger_error('Soubor se nenahral :('));
449 trigger_error('Nothin\' to do here my cutie :-*');
456 function process_http_request() {
459 @ini_set
('magic_quotes_gpc' , 'off');
460 if(get_magic_quotes_gpc()) {
461 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
464 $PATH_INFO=@trim
($_SERVER[PATH_INFO
]);
465 $this->html
->header_print($PATH_INFO);
469 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
470 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
471 switch($PATH_CHUNKS[1]) {
473 die('Tell me why you cry');
476 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ?
trim($_GET['q']) : false;
477 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ?
$PATH_CHUNKS[1] : 'item';
478 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
479 switch($PATH_CHUNKS[2]) {
481 $this->process_http_request_post($PATH_CHUNKS[2], $class);
482 $this->show_form_add($class);
485 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ?
(int) $PATH_CHUNKS[2] : false);
486 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
488 switch($PATH_CHUNKS[3]) {
489 case 'edit': //?/?/edit
490 case 'image': //?/image
491 case 'delete': //?/delete
492 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
495 $limit = (int) (isset($PATH_CHUNKS[3]) ?
$PATH_CHUNKS[3] : '0');
496 $offset = (int) (isset($PATH_CHUNKS[4]) ?
$PATH_CHUNKS[4] : '0');
497 $this->show_items($class, $id, $limit, $offset, $search);
498 $this->show_listing_extensions($class, $id, $limit, $offset, $edit);
499 //print_r(array("<pre>",$_SERVER));
509 $sklad = new Sklad_UI();
510 $sklad->process_http_request();
This page took 1.307655 seconds and 4 git commands to generate.