493c92bc6b42cf1c3b4d7541f88b72337c088631
3 * SkladovySystem - Storage management system compatible with LMS
4 * Copyright (C) 2011 Tomas 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 * Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
27 * Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
30 * @author Tomas Mudrunka
33 function header($title='') {
35 $script = $_SERVER['SCRIPT_NAME'];
36 $search = @trim
($_GET['q']);
39 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
41 <h1><a href="$script/">SystémSklad</a><small>$title</small></h1>
44 <li><a href="?logout">Logout</a></li>
45 <li><a href="$script/">Home</a></li>
47 <form action="?" method="GET">
48 <input type="text" name="q" placeholder="regexp..." value="$search" />
49 <input type="submit" value="filter" />
51 <!-- form action="$script/" method="GET">
52 <input type="text" name="q" placeholder="regexp..." value="$search" />
53 <input type="submit" value="search items" />
61 foreach($row as $var) {
62 if(trim($var) == '') $var = ' ';
63 $html.="<td>$var</td>";
69 function table(&$table, $params='border=1') {
70 $html="<table $params>";
72 foreach($table as $row) {
74 $html.=$this->row(array_keys($row));
77 $html.=$this->row($row);
83 function link($title='n/a', $link='#void', $internal=true) {
84 if($internal) $link = $_SERVER['SCRIPT_NAME'].'/'.$link;
85 return "<a href='$link'>$title</a>";
88 function img($src='#void', $title='img') {
89 return "<img src='$src' alt='$title' title='$title' width=64 />";
92 function table_add_images(&$table) {
93 $image = array('model_id');
94 foreach($table as $id => $row) {
95 foreach($image as $column) if(isset($table[$id][$column])) {
96 $type = @array_shift
(preg_split('/_/', $column));
97 $src=URL_IMAGES
."/$type/".$table[$id][$column].'.jpg';
98 $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]);
103 function table_collapse(&$table) {
105 'item_id' => 'item_id',
106 'model_id' => 'model_name',
107 'category_id' => 'category_name',
108 'producer_id' => 'producer_name',
109 'vendor_id' => 'vendor_name',
110 'room_id' => 'room_name',
111 'status_id' => 'status_name',
113 foreach($table as $id => $row) {
114 foreach($collapse as $link => $title)
115 if(isset($table[$id][$link])) {
116 $type = @array_shift
(preg_split('/_/', $link));
117 if($link != $title) unset($table[$id][$link]);
118 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
123 function table_sort(&$table) {
124 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
125 $table_sorted = array();
126 foreach($table as $id => $row) {
127 $table_sorted[$id] = array();
128 foreach($precedence as $column) if(isset($table[$id][$column])) {
129 $table_sorted[$id][$column]=$table[$id][$column];
130 unset($table[$id][$column]);
132 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
134 $table = $table_sorted;
137 function render_item_table($table) {
138 $this->table_add_images($table);
139 $this->table_collapse($table);
140 $this->table_sort($table);
141 return $this->table($table);
144 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false) {
145 $html = "<input type='$type' ";
146 if($name) $html.= "name='$name' ";
147 if(!is_bool($value)) $html.= "value='$value' ";
148 if($options) $html.= "$options ";
149 if($placeholder) $html.= "placeholder='$placeholder' ";
154 function select($name, $selectbox, $default=false) {
155 //echo('<pre>'); print_r($selectbox);
156 $html = "<select name='$name'>";
159 $value=$default; $title=$selectbox[$value];
160 $html .= "<option value='$value'>$value :: $title</option>";
161 unset($selectbox[$value]);
163 foreach($selectbox as $value => $title) {
164 $html .= "<option value='$value'>$value :: $title</option>";
166 $html .= "</select>";
170 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $multi_insert=true) {
171 //echo('<pre>'); print_r($selectbox);
172 //echo('<pre>'); print_r($current);
174 if(is_array($current)) {
176 $current = array_shift($current);
179 $html='<form method="POST">';
180 if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">';
181 $html.=$this->input('table', $class, 'hidden');
182 foreach($columns as $column) {
183 $html.=$column['Field'].': ';
184 $name='value:'.$column['Field'].'[]';
186 case preg_match('/auto_increment/', $column['Extra']):
187 $val = $update ?
$current[$column['Field']] : ''; //opakuje se (skoro) zbytecne
188 $html.=$this->input($name, $val, 'hidden');
189 $html.=$val.'(AUTO)';
191 case isset($selectbox[$column['Field']]):
192 $val = $update ?
$current[$column['Field']] : false;
193 $html.=$this->select($name,$selectbox[$column['Field']],$val); //opakuje se
196 $val = $update ?
$current[$column['Field']] : false; //opakuje se
197 $html.=$this->input($name, $val);
204 //TODO, move to separate JS file
207 <span name="input_set_next"></span><br style="clear:both" />
209 function duplicate_element(what, where) {
210 document.getElementsByName(where)[0].outerHTML =
211 document.getElementsByName(what)[0].outerHTML
212 + document.getElementsByName(where)[0].outerHTML;
215 <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a>
219 $btn = is_array($current) ?
'UPDATE' : 'INSERT';
220 $html.=$this->input(false, $btn, 'submit');
227 * Trida poskytuje rozhrani k databazi skladu
230 * @author Tomas Mudrunka
232 class Sklad_DB
extends PDO
{
233 function __construct() {
234 $this->lms
= new Sklad_LMS();
237 DB_DSN
, DB_USER
, DB_PASS
,
238 array(PDO
::MYSQL_ATTR_INIT_COMMAND
=> "SET NAMES utf8") //Force UTF8 for MySQL
242 function escape($str) {
243 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
246 function build_query_select($class, $id=false, $limit=false, $offset=0, $search=false, $id_suffix='_id') {
247 $class = $this->escape($class);
249 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
250 'model' => array('category', 'producer')
252 $search_fields = array(
253 'item' => array('item_id','model_name','model_barcode','model_descript','producer_name','vendor_name')
255 $sql="SELECT * FROM $class\n";
256 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$id_suffix)\n";
258 $search = $this->quote($search);
259 if(!isset($search_fields[$class])) {
260 trigger_error("Ve tride $class zatim vyhledavat nemozno :-(");
263 $sql .= 'WHERE FALSE ';
264 foreach($search_fields[$class] as $column) $sql .= "OR $column REGEXP $search ";
265 } elseif($id) $sql .= "WHERE $class$id_suffix = $id\n";
267 $limit = $this->escape((int)$limit);
268 $offset = $this->escape((int)$offset);
269 $sql .= "LIMIT $offset,$limit\n";
274 function safe_query($sql) {
275 $result = $this->query($sql);
277 trigger_error('<font color=red><b>QUERY FAILED:</b><pre>'.$sql.'</pre></font>');
283 function get_listing($class, $id=false, $limit=false, $offset=0, $search=false, $indexed=array(), $suffix_id='_id') {
284 $sql = $this->build_query_select($class, $id, $limit, $offset, $search);
285 $result = $this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
286 if(!$result ||
!is_array($indexed)) return $result;
288 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
292 function get_columns($class) {
293 $class = $this->escape($class);
294 $sql = "SHOW COLUMNS FROM $class;";
295 return $this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
298 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
300 foreach($columns as $column) {
301 if($class && $column['Field'] == $class.$suffix_id) continue;
302 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
303 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
304 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table;";
305 $result=$this->safe_query($sql)->fetchAll(PDO
::FETCH_ASSOC
);
306 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
308 //echo('<pre>'); print_r($selectbox);
312 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
313 $table = $this->escape($table);
315 //Get list of POSTed columns
316 $columns = implode(',',array_map(array($this,'escape'), array_keys($values[0])));
318 //Insert into table (columns)
320 if($replace) $sql = 'REPLACE';
321 $sql .= " INTO $table ($columns) VALUES ";
323 //Values (a,b,c),(d,e,f)
325 foreach($values as $row) {
326 $sql .= $comma.'('.implode(',',array_map(array($this,'quote'), $row)).')';
335 function insert_or_update($table, $values) {
336 $sql = $this->build_query_insert($table, $values);
337 $this->safe_query($sql);
338 return $this->lastInsertId();
341 function delete($table, $id, $suffix_id='_id') {
342 $key = $this->escape($table.$suffix_id);
343 $table = $this->escape($table);
344 $id = $this->quote($id);
345 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
350 * Trida implementuje uzivatelske rozhrani skladu
353 * $sklad = new Sklad_UI();
354 * $sklad->process_http_request();
357 * @author Tomas Mudrunka
360 function __construct() {
361 $this->db
= new Sklad_DB();
362 $this->html
= new Sklad_HTML();
365 function render_items($class, $id=false, $limit=false, $offset=0, $search=false) {
366 return $this->html
->render_item_table($this->db
->get_listing($class, $id, $limit, $offset, $search));
369 function render_form_add($class) {
370 $columns = $this->db
->get_columns($class);
371 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
372 return $this->html
->render_insert_form($class, $columns, $selectbox);
375 function render_form_edit($class, $id) {
376 $columns = $this->db
->get_columns($class);
377 $selectbox = $this->db
->columns_get_selectbox($columns, $class);
378 $current = $this->db
->get_listing($class, $id);
379 return $this->html
->render_insert_form($class, $columns, $selectbox, $current);
382 function render_single_record_details($class, $id) {
384 $id_prev = $id - 1 > 0 ?
$id - 1 : 0;
385 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
387 $html.= $this->html
->link('<<', "$class/$id_prev/");
389 $html.= $this->html
->link('>>', "$class/$id_next/");
391 $html.= $this->html
->link('edit', "$class/$id/edit/");
395 function render_listing_navigation($class, $id, $limit, $offset) {
396 $offset_next = $offset +
$limit;
397 $offset_prev = $offset - $limit > 0 ?
$offset - $limit : 0;
398 $get = $_SERVER['QUERY_STRING'] != '' ?
'?'.$_SERVER['QUERY_STRING'] : '';
400 $html.= $this->html
->link('<<', "$class/$id/$limit/$offset_prev/$get");
402 $html.= $this->html
->link('>>', "$class/$id/$limit/$offset_next/$get");
404 $html.= $this->html
->link('new', "$class/new/$get");
408 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
410 if(is_numeric($id)) {
411 $html.=$this->render_single_record_details($class, $id);
413 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
416 $html.='<br />TODO UPDATE FORM!<br />';
417 $html.= $this->render_form_edit($class, $id);
418 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
419 $html.= "<form action='$action' method='POST'>";
420 $html.= $this->html
->input(false, 'DELETE', 'submit');
421 $html.= 'sure?'.$this->html
->input('sure', false, 'checkbox');
423 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
424 $html.= "<form action='$action' method='POST' enctype='multipart/form-data'>";
425 $html.= $this->html
->input('image', false, 'file', false, 'size="30"');
426 $html.= $this->html
->input(false, 'IMAGE', 'submit');
432 function check_auth() {
433 new HTTP_Auth('SkladovejSystem', true, array($this->db
->lms
,'check_auth'));
436 function post_redirect_get($last, $next) { //TODO prepracovat, tohle je uplna picovina...
437 //header('Location: '.$_SERVER['REQUEST_URI']); //TODO redirect (need templating system or ob_start() first!!!)
438 echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html
->link('zde', $last).'.<br />'.
439 'Další záznam přidáte '.$this->html
->link('zde', $next).'.';
443 function process_http_request_post($action=false, $class=false, $id=false) {
444 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
445 echo('<pre>'); //DEBUG (maybe todo remove)
449 foreach($_POST as $key => $value) {
450 $name = preg_split('/:/',$key);
451 if(isset($name[0])) switch($name[0]) {
453 foreach($value as $id => $val) $values[$id][$name[1]]=$value[$id];
460 if($action) switch($action) {
463 if(!isset($_POST['table'])) die(trigger_error("Jest nutno specifikovat tabulku voe!"));
464 $table=$_POST['table'];
465 //print_r($values); //debug
466 $last = $this->db
->insert_or_update($table, $values);
467 $this->post_redirect_get("$table/$last/", "$table/new/");
470 if(!isset($_POST['sure']) ||
!$_POST['sure']) die(trigger_error('Sure user expected :-)'));
471 //$this->db->delete($class, $id);
472 die('Neco asi bylo smazano. Fnuk :\'-('); //TODO REDIRECT
475 $image_classes = array('model'); //TODO, use this more widely across the code
476 if(!in_array($class, $image_classes)) die(trigger_error("Nekdo nechce k DB Tride '$class' prirazovat obrazky!"));
477 $image_destination = DIR_IMAGES
."/$class/$id.jpg";
478 if($_FILES['image']['name'] == '') die(trigger_error('Kazde neco se musi nejak jmenovat!'));
479 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
480 chmod ($image_destination, 0664);
481 die('Obrazek se naladoval :)'); //TODO REDIRECT
482 } else die(trigger_error('Soubor se nenahral :('));
485 trigger_error('Nothin\' to do here my cutie :-*');
492 function process_http_request() {
495 @ini_set
('magic_quotes_gpc' , 'off');
496 if(get_magic_quotes_gpc()) {
497 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
500 $PATH_INFO=@trim
($_SERVER[PATH_INFO
]);
501 echo $this->html
->header($PATH_INFO);
505 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
506 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
507 switch($PATH_CHUNKS[1]) {
509 die('Tell me why you cry');
512 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ?
trim($_GET['q']) : false;
513 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ?
$PATH_CHUNKS[1] : 'item';
514 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
515 switch($PATH_CHUNKS[2]) {
517 $this->process_http_request_post($PATH_CHUNKS[2], $class);
518 echo $this->render_form_add($class);
521 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ?
(int) $PATH_CHUNKS[2] : false);
522 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
524 switch($PATH_CHUNKS[3]) {
525 case 'edit': //?/?/edit
526 case 'image': //?/image
527 case 'delete': //?/delete
528 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
531 $limit = (int) (isset($PATH_CHUNKS[3]) ?
$PATH_CHUNKS[3] : '0');
532 $offset = (int) (isset($PATH_CHUNKS[4]) ?
$PATH_CHUNKS[4] : '0');
533 echo $this->render_items($class, $id, $limit, $offset, $search);
534 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
535 //print_r(array("<pre>",$_SERVER));
545 $sklad = new Sklad_UI();
546 $sklad->process_http_request();
This page took 0.696273 seconds and 4 git commands to generate.