menu link name]
* is_allowed_menu() - checks if User can execute this menu (in array main_menu)
* functions like NEW_RECORD, EDIT_RECORD, UPDATEDB etc. not in MENU_INIT
* history: [menu => ]
*
* [AJAX]
* print_js_ajax_callback
* js function definitions
* link_ajax( $name, $fun_name, $fun_args = array(), $attr = array() )
* create link to execute ajax function
* exec_ajax_request
* execute ajax request
* link_ajax_load_on_ready( $fun_name, $fun_args = array(), $attr = array() )
* load content from ajax request to function $fun_name
* TODO: ajax form creator
* TODO: ajax data table
*/
require_once dirname(__FILE__) . '/' . 'Lib.php';
Lib::loadClass('V');
class App {
/**
* Generate URL-encoded query string.
* @param $query - array or string
*/
public static function build_http_query( $query ) {
$query_arr = array();// like in parse_url @see http://pl.php.net/manual/pl/function.parse-url.php
$query_reserved_words = array('_scheme','_host','_user','_pass','_path','_query','_fragment');
if (is_array($query)) {
foreach ($query as $k => $v) {
if (in_array($k, $query_reserved_words)) {
$query_arr [substr($k, 1)] = $v;
} else if ($k == '#') {
$query_arr ['fragment'] = $v;
} else {
$query_arr ['query'][$k]= $v;
}
}//end foreach
}
else {
$query_arr = parse_url($query);
// 'http://uzytkownik:haslo@serwer/sciezka?arg=wartosc#kotwica'
// [scheme] => http
// [host] => serwer
// [user] => uzytkownik
// [pass] => haslo
// [path] => /sciezka
// [query] => arg=wartosc
// [fragment] => kotwica
if (!empty($query_arr['query'])) {
$query_arr_query = $query_arr['query'];
parse_str($query_arr_query, $query_arr['query']);
}
}
// [scheme] => http
// [host] => serwer // $_SERVER['HTTP_HOST'] or SERVER_NAME @see http://stackoverflow.com/questions/2297403/http-host-vs-server-name
// [user] => uzytkownik
// [pass] => haslo
// [path] => /sciezka
// [query] => Array( arg=>wartosc, ... )
// [fragment] => kotwica
// [scheme] -> require [host]
// [user] -> require [host]
// [pass] -> require [user],[host]
// [path] -> require [host]
// [query] ->
// [fragment] -> require [query]
// App add task always
if (!empty($query_arr['query'])) {
if (empty($query_arr['query']['task'])) {
if ($task = V::get('task', self::get_default_task(), $_REQUEST)) {
$query_arr['query']['task'] = $task;
}
}
}
$href = '';
if (!empty($query_arr['scheme'])) {
$href .= $query_arr['scheme'].'://';
// host is required if scheme
if (empty($query_arr['host'])) $query_arr['host'] = $_SERVER['HTTP_HOST'];
}
if (!empty($query_arr['user'])) {// user:pass@server
$href .= $query_arr['user'];
if (!empty($query_arr['pass'])) $href .= ':'.$query_arr['pass'];
$href .= '@';
// host is required if user
if (empty($query_arr['host'])) $query_arr['host'] = $_SERVER['HTTP_HOST'];
}
if (!empty($query_arr['host'])) $href .= $query_arr['host'];
//$href .= (!empty($query_arr['path']))? $query_arr['path'] : $query_arr['PHP_SELF'];
if (!empty($query_arr['path'])) $href .= $query_arr['path'];
if (!empty($query_arr['query'])) {
$tmp_query_arr = array();
foreach ($query_arr['query'] as $k => $v) {
if (is_array($v)) {
if (strlen($k) < 3 || substr($k, -2) != '[]') {
$k .= '[]';
}
foreach ($v as $v_arg_val) {
$tmp_query_arr []= '' . $k . '=' . $v_arg_val;
}//end foreach
} else {
$tmp_query_arr []= '' . $k . '=' . $v;
}
}//end foreach
$href .= "?".implode("&", $tmp_query_arr);
}
if (!empty($query_arr['fragment'])) $href .= '#'.$query_arr['fragment'];
return $href;
}
/**
* @returns html link tag ...
* @param $name
* @param $href
* @param $attr
*/
public static function link( $name, $href, $attr = array() ) {
$out_href = App::build_http_query( $href );
$txt = $name;
$ico = V::get('ico', '', $attr);
$ico_height = V::get('ico_height', 16, $attr, 'int');
if (isset($attr['ico_height'])) unset($attr['ico_height']);
if ($ico) {
if (count(explode('.', $ico)) == 1) {
$ico .= '.gif';// default ico format
}
if (file_exists( APP_PATH_ROOT . DS . 'icon' . DS . $ico )) {
$txt = '';
}
unset($attr['ico']);
}
$ico = V::get('ico_after_text', '', $attr);
if ($ico) {
if (count(explode('.', $ico)) == 1) {
$ico .= '.gif';
}
if (file_exists( APP_PATH_ROOT . DS . 'icon' . DS . $ico )) {
$txt .= '
';
}
unset($attr['ico_after_text']);
}
$p_attr = array();
if (!empty($attr)) {
$p_attr_arr = array();
foreach ($attr as $k => $v) {
$p_attr_arr []= ''.$k.'="'.$v.'"';
}//end foreach
$p_attr []= ' '.implode(' ',$p_attr_arr);
}
$p_attr = (!empty($p_attr))? ' '.implode(' ', $p_attr) : '';
return ''.$txt.'';
}
/**
* @param $out_type - 'show', 'search'
* @param $name -
* @param $sql_type -
* @param $value -
* @param $attr -
*/
public static function _field( $out_type, $name, $sql_type, $value = '', $attr = array() ) {
$ret = '';
//$type = $_SESSION[$table_column]['TYPE'][$field];
$type = $sql_type;
//$name = $field;//$_SESSION[$table_column]['DESC'][ $_SESSION[$table_column]['DESC_TO_KEY'][$field] ];
//if ($_SESSION['CURRENT_MENU']=='NEW_RECORD') $name = 'NEW_RECORD['.$name.']';
// html tag
$tag = new stdClass();
$tag->name = '';
$tag->attrs = '';
$tag->cnt = null;
// param attributes
if (!empty($attr)) {
foreach ($attr as $k_attr_name => $v_attr) {
// parse param $k_attr_name
switch ($k_attr_name) {
case 'class': {
$tag->attrs[$k_attr_name] []= $v_attr;
break;
}
case 'style': {
$attr_arr = array();
foreach ($v_attr as $k_style => $v_style) {
$attr_arr []= $k_style.'='.$v_style;
}//end foreach
$tag->attrs[$k_attr_name] []= $v_attr;
break;
}
// TODO: ajax, etc
default: {
$tag->attrs[$k_attr_name] = $v_attr;
}
}
}//end foreach
}
// size X -> style="width: Y px" : X=10 -> Y=75px, 1char=7,5px
// parse type
if (strstr($type, "enum")) {
$tag->name = 'select';
$tag->attrs['name'] = $name;
$tag->attrs['id'] = $name;
$tag->cnt = '';
$select_values = $type;
$select_values = explode("(", $select_values);
$select_values = end($select_values);
$select_values = explode(")", $select_values);
$select_values = reset($select_values);
$select_values = explode(",", $select_values);
sort($select_values);
foreach ($select_values as $v) {
$v = trim($v, "' ");//usuwa spacje i ' z poczatku i konca
$max_length = 26;// TODO: param in @attr
$sel = ($v == "$value")? ' selected="selected"' : '';
$tag->cnt .= '';
}
}
else if ((strstr($type, "char")) || (strstr($type, "int"))) {
$tag->name = 'input';
$max_length = $type;
$max_length = explode("(",$max_length);
$max_length = end($max_length);
$max_length = explode(")",$max_length);
$max_length = reset($max_length);
$min_length = (strstr($type, "int"))? 4 : 10;// min length if empty value
$tag->attrs['name'] = $name;
$tag->attrs['type'] = 'text';
$tag->attrs['id'] = $name;
$tag->attrs['value'] = htmlspecialchars($value);// TODO: htmlspecialchars
$tag->attrs['maxlength'] = $max_length;
if (empty($tag->attrs['size'])) {
$size = strlen($value) + 2;
if ($size < $min_length) $size = $min_length;
$tag->attrs['size'] = $size;
}
//$tag->attrs['style']['width'] = ceil($size * 7).'px';
}
else if (strstr($type, "datetime") || strstr($type, "date") || strstr($type, "timestamp")) {
$max_length = $type;
$max_length = end( explode("(", $max_length) );
$max_length = reset( explode(")", $max_length) );
$value = substr($value, 0, 10);
$tag->name = 'input';
$tag->attrs['name'] = $name;
$tag->attrs['type'] = 'text';
$tag->attrs['id'] = $name;
$tag->attrs['value'] = $value;// TODO: htmlspecialchars
$tag->attrs['maxlength'] = $max_length;
if (empty($tag->attrs['size'])) $tag->attrs['size'] = 10;
$tag->attrs['class'] []= 'date-pick';
}
else if (strstr($type, "double") || strstr($type, "float")) {
$tag->name = 'input';
$max_length = $type;
$max_length = end( explode("(", $max_length) );
$max_length = reset( explode(")", $max_length) );
$value = substr($value, 0, 10);
$tag->attrs['name'] = $name;
$tag->attrs['type'] = 'text';
$tag->attrs['id'] = $name;
$tag->attrs['value'] = $value;// TODO: htmlspecialchars
$tag->attrs['maxlength'] = $max_length;
if (empty($tag->attrs['size'])) $tag->attrs['size'] = 6;// TODO: param in @attr
}
else if (strstr($type, "text")) {
if ($out_type == 'search') {
$tag->name = 'input';
$tag->attrs['name'] = $name;
$tag->attrs['id'] = $name;
$tag->attrs['maxlength'] = 255;
$tag->attrs['value'] = $value;// TODO: htmlspecialchars
} else {
$tag->name = 'textarea';
$tag->attrs['name'] = $name;
$tag->attrs['id'] = $name;
if (empty($tag->attrs['rows'])) $tag->attrs['rows'] = 5;// TODO: param in @attr
if (empty($tag->attrs['cols'])) $tag->attrs['cols'] = 70;// TODO: param in @attr
$tag->cnt = $value;// TODO: htmlspecialchars
}
}
else if ('typespecial_' == substr($type, 0, 12)) {
if (!function_exists($type)) {
echo'
'."Typespecial function '".substr($type, 12)."' not exists!".'
'; } else { return $type( $name, $out_type, $value, $attr ); } } else { //$ret .= "UNKNOWN $type"; } $ret = ''; if ($tag->name) { // attributes with array values - like class if (empty($tag->attrs)) { $tag->attrs = ''; } else { $attr_arr = array(); foreach ($tag->attrs as $k_attr_name => $v_attr) { switch ($k_attr_name) { case 'class': { $v_attr = implode(' ', $v_attr); break; } case 'style': { $style_attr_arr = array(); foreach ($v_attr as $k_style => $v_style) { $style_attr_arr []= ''.$k_style.':'.$v_style; }//end foreach $v_attr = implode(';', $style_attr_arr); break; } default: { // } } $attr_arr []= $k_attr_name.'="'.$v_attr.'"'; }//end foreach $tag->attrs = ' '.implode(' ', $attr_arr); } // tag style if ($tag->cnt !== null) { $ret .= '<'.$tag->name.$tag->attrs.'>'.$tag->cnt.''.$tag->name.'>'; } else { $ret .= '<'.$tag->name.$tag->attrs.' />'; } } return $ret; } public static function field( $name, $sql_type, $value = '', $attr = array() ) { return self::_field( 'show', $name, $sql_type, $value, $attr ); } public static function field_search( $name, $sql_type, $value = '', $attr = array() ) { return self::_field( 'search', $name, $sql_type, $value, $attr ); } public static function set_function_init_output( $cnt ) { self::set_state('function_init_output', $cnt); } public static function get_function_init_output() { return self::get_state('function_init_output'); } public static function exec_function_init() { if (($fun_init = V::get('function_init', '', $_REQUEST)) != '') { ob_start(); if (function_exists( $fun_init )) { $fun_init(); } else { echo''."function_init not exists!".'
'; } $cnt = ob_get_contents(); ob_clean(); self::set_function_init_output( $cnt ); } } public static function quit() { App::set_state( 'quit', true ); } public static function is_quit() { return App::get_state( 'quit' ); } public static function set_default_task( $default_task ) { self::set_state('default_task', $default_task); } public static function get_default_task() { return self::get_state('default_task'); } public static function get_task() { $default_task = self::get_default_task(); $task = V::get('task', $default_task, $_GET); return $task; } public static function exec_task() { self::show_msgs(); echo "\n".''."Wrong task!".'
'; } } } public static function show_msgs() { $msgs_all = self::get_msgs(); if (!empty($msgs_all)) { foreach ($msgs_all as $msg_type => $msgs) { echo "\n".'
*LOGOWANIE* ";
session_destroy();
die();
}
}
if ( ! User::logged() ) {
echo "
by A.Binder arek@tx.pl