App.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. <?php
  2. /**
  3. * App:
  4. * main_menu = [menu safe name($_REQEST) => menu link name]
  5. * is_allowed_menu() - checks if User can execute this menu (in array main_menu)
  6. * functions like NEW_RECORD, EDIT_RECORD, UPDATEDB etc. not in MENU_INIT
  7. * history: [menu =>]
  8. *
  9. * [AJAX]
  10. * print_js_ajax_callback
  11. * js function definitions
  12. * link_ajax( $name, $fun_name, $fun_args = array(), $attr = array() )
  13. * create link to execute ajax function
  14. * exec_ajax_request
  15. * execute ajax request
  16. * link_ajax_load_on_ready( $fun_name, $fun_args = array(), $attr = array() )
  17. * load content from ajax request to function $fun_name
  18. * TODO: ajax form creator
  19. * TODO: ajax data table
  20. */
  21. Lib::loadClass('V');
  22. class App {
  23. /**
  24. * Generate URL-encoded query string.
  25. * @param $query - array or string
  26. */
  27. public static function build_http_query($query) {
  28. $query_arr = array();// like in parse_url @see http://pl.php.net/manual/pl/function.parse-url.php
  29. $query_reserved_words = array('_scheme','_host','_user','_pass','_path','_query','_fragment');
  30. if (is_array($query)) {
  31. foreach ($query as $k => $v) {
  32. if (in_array($k, $query_reserved_words)) {
  33. $query_arr [substr($k, 1)] = $v;
  34. } else if ($k == '#') {
  35. $query_arr['fragment'] = $v;
  36. } else {
  37. $query_arr['query'][$k]= $v;
  38. }
  39. }
  40. }
  41. else {
  42. $query_arr = parse_url($query);
  43. // 'http://uzytkownik:haslo@serwer/sciezka?arg=wartosc#kotwica'
  44. // [scheme] => http
  45. // [host] => serwer
  46. // [user] => uzytkownik
  47. // [pass] => haslo
  48. // [path] => /sciezka
  49. // [query] => arg=wartosc
  50. // [fragment] => kotwica
  51. if (!empty($query_arr['query'])) {
  52. $query_arr_query = $query_arr['query'];
  53. parse_str($query_arr_query, $query_arr['query']);
  54. }
  55. }
  56. // [scheme] => http
  57. // [host] => serwer // $_SERVER['HTTP_HOST'] or SERVER_NAME @see http://stackoverflow.com/questions/2297403/http-host-vs-server-name
  58. // [user] => uzytkownik
  59. // [pass] => haslo
  60. // [path] => /sciezka
  61. // [query] => Array( arg=>wartosc, ... )
  62. // [fragment] => kotwica
  63. // [scheme] -> require [host]
  64. // [user] -> require [host]
  65. // [pass] -> require [user],[host]
  66. // [path] -> require [host]
  67. // [query] ->
  68. // [fragment] -> require [query]
  69. // App add task always
  70. if (!empty($query_arr['query'])) {
  71. if (empty($query_arr['query']['task'])) {
  72. if ($task = V::get('task', self::get_default_task(), $_REQUEST)) {
  73. $query_arr['query']['task'] = $task;
  74. }
  75. }
  76. }
  77. $href = '';
  78. if (!empty($query_arr['scheme'])) {
  79. $href .= $query_arr['scheme'].'://';
  80. // host is required if scheme
  81. if (empty($query_arr['host'])) $query_arr['host'] = $_SERVER['HTTP_HOST'];
  82. }
  83. if (!empty($query_arr['user'])) {// user:pass@server
  84. $href .= $query_arr['user'];
  85. if (!empty($query_arr['pass'])) $href .= ':'.$query_arr['pass'];
  86. $href .= '@';
  87. // host is required if user
  88. if (empty($query_arr['host'])) $query_arr['host'] = $_SERVER['HTTP_HOST'];
  89. }
  90. if (!empty($query_arr['host'])) $href .= $query_arr['host'];
  91. //$href .= (!empty($query_arr['path']))? $query_arr['path'] : $query_arr['PHP_SELF'];
  92. if (!empty($query_arr['path'])) $href .= $query_arr['path'];
  93. if (!empty($query_arr['query'])) {
  94. $tmp_query_arr = array();
  95. foreach ($query_arr['query'] as $k => $v) {
  96. if (is_array($v)) {
  97. if (strlen($k) < 3 || substr($k, -2) != '[]') {
  98. $k .= '[]';
  99. }
  100. foreach ($v as $v_arg_val) {
  101. $tmp_query_arr[] = '' . $k . '=' . $v_arg_val;
  102. }
  103. } else {
  104. $tmp_query_arr[] = '' . $k . '=' . $v;
  105. }
  106. }
  107. $href .= "?".implode("&", $tmp_query_arr);
  108. }
  109. if (!empty($query_arr['fragment'])) $href .= '#'.$query_arr['fragment'];
  110. return $href;
  111. }
  112. /**
  113. * @param $out_type - 'show', 'search'
  114. * @param $name -
  115. * @param $sql_type -
  116. * @param $value -
  117. * @param $attr -
  118. */
  119. public static function _field($out_type, $name, $sql_type, $value = '', $attr = array()) {
  120. $ret = '';
  121. //$type = $_SESSION[$table_column]['TYPE'][$field];
  122. $type = $sql_type;
  123. //$name = $field;//$_SESSION[$table_column]['DESC'][$_SESSION[$table_column]['DESC_TO_KEY'][$field]];
  124. //if ($_SESSION['CURRENT_MENU']=='NEW_RECORD') $name = 'NEW_RECORD['.$name.']';
  125. // html tag
  126. $tag = new stdClass();
  127. $tag->name = '';
  128. $tag->attrs = '';
  129. $tag->cnt = null;
  130. // param attributes
  131. if (!empty($attr)) {
  132. foreach ($attr as $k_attr_name => $v_attr) {
  133. // parse param $k_attr_name
  134. switch ($k_attr_name) {
  135. case 'class': {
  136. $tag->attrs[$k_attr_name][] = $v_attr;
  137. break;
  138. }
  139. case 'style': {
  140. $attr_arr = array();
  141. foreach ($v_attr as $k_style => $v_style) {
  142. $attr_arr[] = $k_style.'='.$v_style;
  143. }
  144. $tag->attrs[$k_attr_name][] = $v_attr;
  145. break;
  146. }
  147. // TODO: ajax, etc
  148. default: {
  149. $tag->attrs[$k_attr_name] = $v_attr;
  150. }
  151. }
  152. }
  153. }
  154. // size X -> style="width: Y px" : X=10 -> Y=75px, 1char=7,5px
  155. // parse type
  156. if (strstr($type, "enum")) {
  157. $tag->name = 'select';
  158. $tag->attrs['name'] = $name;
  159. $tag->attrs['id'] = $name;
  160. $tag->cnt = '<option value="%">'." [ Wybierz ] ".'</option>';
  161. $select_values = $type;
  162. $select_values = explode("(", $select_values);
  163. $select_values = end($select_values);
  164. $select_values = explode(")", $select_values);
  165. $select_values = reset($select_values);
  166. $select_values = explode(",", $select_values);
  167. sort($select_values);
  168. foreach ($select_values as $v) {
  169. $v = trim($v, "' ");//usuwa spacje i ' z poczatku i konca
  170. $max_length = 26;// TODO: param in @attr
  171. $sel = ($v == "$value")? ' selected="selected"' : '';
  172. $tag->cnt .= '<option value="'.$v.'"'.$sel.'>'.substr($v, 0, $max_length).' </option>';
  173. }
  174. }
  175. else if ((strstr($type, "char")) || (strstr($type, "int"))) {
  176. $tag->name = 'input';
  177. $max_length = $type;
  178. $max_length = explode("(",$max_length);
  179. $max_length = end($max_length);
  180. $max_length = explode(")",$max_length);
  181. $max_length = reset($max_length);
  182. $min_length = (strstr($type, "int"))? 4 : 10;// min length if empty value
  183. $tag->attrs['name'] = $name;
  184. $tag->attrs['type'] = 'text';
  185. $tag->attrs['id'] = $name;
  186. $tag->attrs['value'] = htmlspecialchars($value);// TODO: htmlspecialchars
  187. $tag->attrs['maxlength'] = $max_length;
  188. if (empty($tag->attrs['size'])) {
  189. $size = strlen($value) + 2;
  190. if ($size < $min_length) $size = $min_length;
  191. $tag->attrs['size'] = $size;
  192. }
  193. //$tag->attrs['style']['width'] = ceil($size * 7).'px';
  194. }
  195. else if (strstr($type, "datetime") || strstr($type, "date") || strstr($type, "timestamp")) {
  196. $max_length = $type;
  197. $max_length = end( explode("(", $max_length) );
  198. $max_length = reset( explode(")", $max_length) );
  199. $value = substr($value, 0, 10);
  200. $tag->name = 'input';
  201. $tag->attrs['name'] = $name;
  202. $tag->attrs['type'] = 'text';
  203. $tag->attrs['id'] = $name;
  204. $tag->attrs['value'] = $value;// TODO: htmlspecialchars
  205. $tag->attrs['maxlength'] = $max_length;
  206. if (empty($tag->attrs['size'])) $tag->attrs['size'] = 10;
  207. $tag->attrs['class'][] = 'date-pick';
  208. }
  209. else if (strstr($type, "double") || strstr($type, "float")) {
  210. $tag->name = 'input';
  211. $max_length = $type;
  212. $max_length = end( explode("(", $max_length) );
  213. $max_length = reset( explode(")", $max_length) );
  214. $value = substr($value, 0, 10);
  215. $tag->attrs['name'] = $name;
  216. $tag->attrs['type'] = 'text';
  217. $tag->attrs['id'] = $name;
  218. $tag->attrs['value'] = $value;// TODO: htmlspecialchars
  219. $tag->attrs['maxlength'] = $max_length;
  220. if (empty($tag->attrs['size'])) $tag->attrs['size'] = 6;// TODO: param in @attr
  221. }
  222. else if (strstr($type, "text")) {
  223. if ($out_type == 'search') {
  224. $tag->name = 'input';
  225. $tag->attrs['name'] = $name;
  226. $tag->attrs['id'] = $name;
  227. $tag->attrs['maxlength'] = 255;
  228. $tag->attrs['value'] = $value;// TODO: htmlspecialchars
  229. } else {
  230. $tag->name = 'textarea';
  231. $tag->attrs['name'] = $name;
  232. $tag->attrs['id'] = $name;
  233. if (empty($tag->attrs['rows'])) $tag->attrs['rows'] = 5;// TODO: param in @attr
  234. if (empty($tag->attrs['cols'])) $tag->attrs['cols'] = 70;// TODO: param in @attr
  235. $tag->cnt = $value;// TODO: htmlspecialchars
  236. }
  237. }
  238. else if ('typespecial_' == substr($type, 0, 12)) {
  239. if (!function_exists($type)) {
  240. echo'<p class="red">'."Typespecial function '".substr($type, 12)."' not exists!".'</p>';
  241. } else {
  242. return $type( $name, $out_type, $value, $attr );
  243. }
  244. }
  245. else {
  246. //$ret .= "UNKNOWN $type";
  247. }
  248. $ret = '';
  249. if ($tag->name) {
  250. // attributes with array values - like class
  251. if (empty($tag->attrs)) {
  252. $tag->attrs = '';
  253. } else {
  254. $attr_arr = array();
  255. foreach ($tag->attrs as $k_attr_name => $v_attr) {
  256. switch ($k_attr_name) {
  257. case 'class': {
  258. $v_attr = implode(' ', $v_attr);
  259. break;
  260. }
  261. case 'style': {
  262. $style_attr_arr = array();
  263. foreach ($v_attr as $k_style => $v_style) {
  264. $style_attr_arr[] = ''.$k_style.':'.$v_style;
  265. }
  266. $v_attr = implode(';', $style_attr_arr);
  267. break;
  268. }
  269. default: {
  270. //
  271. }
  272. }
  273. $attr_arr[] = $k_attr_name.'="'.$v_attr.'"';
  274. }
  275. $tag->attrs = ' '.implode(' ', $attr_arr);
  276. }
  277. // tag style
  278. if ($tag->cnt !== null) {
  279. $ret .= '<'.$tag->name.$tag->attrs.'>'.$tag->cnt.'</'.$tag->name.'>';
  280. } else {
  281. $ret .= '<'.$tag->name.$tag->attrs.' />';
  282. }
  283. }
  284. return $ret;
  285. }
  286. public static function field($name, $sql_type, $value = '', $attr = array()) {
  287. return self::_field('show', $name, $sql_type, $value, $attr);
  288. }
  289. public static function field_search($name, $sql_type, $value = '', $attr = array()) {
  290. return self::_field('search', $name, $sql_type, $value, $attr);
  291. }
  292. public static function set_function_init_output($cnt) {
  293. self::set_state('function_init_output', $cnt);
  294. }
  295. public static function get_function_init_output() {
  296. return self::get_state('function_init_output');
  297. }
  298. public static function exec_function_init() {
  299. if (($fun_init = V::get('function_init', '', $_REQUEST)) != '') {
  300. ob_start();
  301. if (function_exists($fun_init)) {
  302. $fun_init();
  303. } else {
  304. echo'<p class="err">'."function_init not exists!".'</p>';
  305. }
  306. $cnt = ob_get_contents();
  307. ob_clean();
  308. self::set_function_init_output($cnt);
  309. }
  310. }
  311. public static function quit() {
  312. App::set_state('quit', true);
  313. }
  314. public static function is_quit() {
  315. return App::get_state('quit');
  316. }
  317. public static function set_default_task($default_task) {
  318. self::set_state('default_task', $default_task);
  319. }
  320. public static function get_default_task() {
  321. return self::get_state('default_task');
  322. }
  323. public static function get_task() {
  324. $default_task = self::get_default_task();
  325. $task = V::get('task', $default_task, $_GET);
  326. return $task;
  327. }
  328. public static function exec_task() {
  329. self::show_msgs();
  330. echo "\n".'<div class="function_init_output">'."\n";
  331. echo self::get_function_init_output();
  332. echo "\n".'</div>'."\n";
  333. if (self::is_quit()) {
  334. return;
  335. }
  336. $default_task = self::get_default_task();
  337. if ($task = V::get('task', $default_task, $_GET)) {
  338. $task_fun = 'task_'.$task;
  339. if (function_exists($task_fun)) {
  340. echo "\n".'<div id="body_content">'."\n";
  341. $task_fun();
  342. echo "\n".'</div>'."\n";
  343. } else {
  344. echo'<p class="err">'."Wrong task!".'</p>';
  345. }
  346. }
  347. }
  348. public static function show_msgs() {
  349. $msgs_all = self::get_msgs();
  350. if (!empty($msgs_all)) {
  351. foreach ($msgs_all as $msg_type => $msgs) {
  352. echo "\n".'<div class="app-msgs app-msg-type-'.$msg_type.'">'."\n";
  353. echo implode('<br />', $msgs);
  354. echo "\n".'</div>'."\n";
  355. }
  356. }
  357. }
  358. public static function print_js_ajax_callback() {
  359. static $_showed;
  360. if (!$_showed) {
  361. ?>
  362. <script>
  363. // linkPopup - jQuery plugin
  364. (function($){
  365. $.fn.linkPopup = function(event){
  366. function showPopup(trigger,popup,e){
  367. var et = e.pageY,
  368. el = e.pageX,
  369. //p = popup.height(),
  370. pw = 700,
  371. ph = 400,// ~ height auto
  372. ww = $(window).width(),
  373. wh = $(window).height(),
  374. ws = $(window).scrollTop(),
  375. // X center over mouse, Y 20 up
  376. pos_left = e.pageX - pw/2,
  377. pos_top = e.pageY - 20,
  378. popup_wrap=popup.parent();
  379. // show all popup
  380. if (pos_left + pw > ww) { pos_left = ww - pw - 20; }
  381. else if (pos_left < 0) { pos_left = 10; }
  382. if (pos_top + ph > ws + wh) { pos_top = ws + wh - ph; }
  383. else if (pos_top < ws) { pos_top = ws; }
  384. //pos_top = (et + p) > (s + wh) ? (s + wh - p) : et - 10,
  385. //if (popup.text() === '') {
  386. if (true) {
  387. // empty
  388. console.log('popup empty');
  389. console.log(trigger.attr('href'))
  390. jQuery.ajax({
  391. type: 'GET',
  392. url: trigger.attr('href'),
  393. success: function(data){
  394. //console.log(data);
  395. popup.html(data)
  396. popup_wrap.css({width: pw, top: pos_top, left: pos_left, height: ph});// width and height
  397. //popup_wrap.css({top: pos_top, left: pos_left});
  398. popup_wrap.fadeIn(100);
  399. }
  400. });
  401. }else{
  402. console.log('popup full');
  403. popup_wrap.css({width: pw, top: pos_top, left: pos_left, height: ph});// width and height
  404. //popup_wrap.css({top: pos_top, left: pos_left});
  405. popup_wrap.fadeIn(100);
  406. }
  407. }
  408. return this.each(function(){
  409. var trigger = $(this), timer, popup = null, popup_wrap=null;//$(this).next();
  410. popup = jQuery('#tip-popup-box');
  411. // create div if not exists ('#tip-popup-box')
  412. //if (!popup.hasClass('tip-popup-box'))
  413. if(popup.length==0){
  414. //console.log('Error popup not found, creating...')
  415. popup = jQuery('<div></div>')
  416. popup.attr('id', 'tip-popup-box')
  417. popup.text('')
  418. jQuery('body').append(popup);// popup.insertAfter(trigger);
  419. console.log('created')
  420. console.log('wrap popup...')
  421. popup.wrap('<div></div>')
  422. popup_wrap=popup.parent()
  423. popup_wrap.addClass('tip-popup-wrap')
  424. popup_wrap.css('display', 'none')
  425. jQuery('<div class=\"title\"><button class=\"btn_close\">X</button></div>').insertBefore(popup)
  426. console.log('wrap popup end')
  427. }else{
  428. popup_wrap=popup.parent()
  429. }
  430. //console.log(trigger.attr('id'));
  431. trigger.attr('onclick','return false;')
  432. trigger.click(function(e){
  433. clearTimeout(timer);
  434. timer = setTimeout(function(){ showPopup(trigger,popup,e); }, 500);
  435. });
  436. //popup.bind('mouseleave', function(){
  437. // trigger.removeClass('active');
  438. // clearTimeout(timer);
  439. // timer = setTimeout(function(){ popup.fadeOut(50); }, 500);
  440. //})
  441. // dont close after click on popup - TODO: X (close btn)
  442. popup_wrap.find('.btn_close').bind('click', function(){
  443. trigger.removeClass('active');
  444. clearTimeout(timer);
  445. timer = setTimeout(function(){ popup_wrap.fadeOut(50); }, 500);
  446. });
  447. // execute click on init:
  448. clearTimeout(timer);
  449. timer = setTimeout(function(){ showPopup(trigger,popup,event); }, 500);
  450. });
  451. };
  452. })(jQuery);
  453. function procesy_ajax_link(l, result_type, result){
  454. return procesy_ajax_run(l, l.href, result_type, result);
  455. }
  456. function insertAfter(parent, n, referenceNode) {
  457. parent.insertBefore(n, referenceNode.nextSibling);
  458. }
  459. function procesy_ajax_run(l, href, result_type, result){
  460. var img = jQuery('<img src=\"./stuff/i/loading.gif\" alt=\"loading\" />');
  461. l.parentNode.appendChild(img.get(0));//img.insertAfter(l);
  462. var res = null;
  463. if(result_type == 'id'){
  464. res = jQuery('#' + result).get(0);
  465. }else if(result_type == 'after'){
  466. res = document.createElement('div');
  467. insertAfter(l.parentNode, res, l);//res.insertAfter(l);
  468. }else if(result_type == 'override'){
  469. res = l;
  470. //insertAfter(l.parentNode, res, l);//res.insertAfter(l);
  471. }else if(result_type == 'override_parent'){
  472. res = l.parentNode;
  473. //insertAfter(l.parentNode, res, l);//res.insertAfter(l);
  474. }else if(result_type == 'popup'){
  475. // TODO: create or get windows object if already exists
  476. //var document.createElement('div');
  477. // TODO: add res to body at the end! popup z sew (SewView)
  478. //jQuery(l).linkPopup();
  479. try{
  480. jQuery(l).linkPopup(window.event);// create window with link href to put into window
  481. }catch(e){
  482. console.log('e...')
  483. console.log(e)
  484. }
  485. jQuery(img).remove();
  486. return false;
  487. }
  488. if(!res){
  489. console.log('error: no res!');
  490. return false;
  491. }
  492. jQuery.ajax({
  493. type: 'GET',
  494. url: href,
  495. success: function(data){
  496. //console.log(data);
  497. if(result_type == 'after') {
  498. //jQuery(data).insertAfter(l);
  499. res.innerHTML = data;
  500. }else if(result_type == 'override'){
  501. //jQuery(data).insertAfter(l);
  502. //res.innerHTML += data;
  503. //jQuery(res).append(data)
  504. jQuery(res).after(data);
  505. jQuery(l).remove();
  506. //res.parentNode.removeChild(l)
  507. }else if(result_type == 'override_parent'){
  508. //jQuery(data).insertAfter(l);
  509. //res.innerHTML += data;
  510. //jQuery(res).append(data)
  511. jQuery(res).after(data);
  512. jQuery(l.parentNode).remove();
  513. //res.parentNode.removeChild(l)
  514. }else if(result_type == 'popup'){
  515. //res.html(data)
  516. try{
  517. //var link_popup=jQuery(l).linkPopup();// create window
  518. var link_popup=jQuery(l).linkPopup(data);// create window and put data into window
  519. //link_popup.setData(data);// put data into window
  520. jQuery(l).click();
  521. }catch(e){
  522. console.log('e...')
  523. console.log(e)
  524. }
  525. }
  526. jQuery(img).remove();
  527. }
  528. });
  529. return false;
  530. }
  531. jQuery(document).ready(function(){
  532. jQuery('.ajax_load_on_ready').each(function(index, l){
  533. // html - replace contetn
  534. l.innerHTML += ' <img src=\"./stuff/i/loading.gif\" alt=\"loading\" />';
  535. // append
  536. jQuery.ajax({
  537. type: 'GET',
  538. url: l.href,
  539. success: function(data){
  540. jQuery(data).insertAfter(l);
  541. jQuery(l).remove();
  542. }
  543. });
  544. });
  545. });
  546. // lightbox, TODO: init after ajax
  547. jQuery(function() {
  548. $('a.lightbox').each(function(){
  549. jQuery(this).lightBox({
  550. overlayBgColor:'#000',
  551. overlayOpacity:0.7,
  552. imageLoading: 'stuff/i/loading.gif', // (string) Path and the name of the loading icon
  553. imageBtnPrev: 'stuff/i/prev.gif', // (string) Path and the name of the prev button image
  554. imageBtnNext: 'stuff/i/next.gif', // (string) Path and the name of the next button image
  555. imageBtnClose: 'stuff/i/del.png', // close.gif (string) Path and the name of the close btn
  556. imageBlank: 'stuff/i/blank.gif', // (string) Path and the name of a blank image (one pixel)
  557. containerResizeSpeed:350,
  558. showImageData:false, // show image data?
  559. txtImage:'Obrazek',
  560. txtOf:'z'
  561. });
  562. });
  563. });
  564. function scrollToProces(item_id) {
  565. var el=jQuery('#TREE'+item_id);
  566. el.parents('.close').removeClass('close');// open all closed elements
  567. if (!el.length) {
  568. alert(item_id + ' prawdopodbnie nie istnieje.');
  569. } else {
  570. el.scrollintoview({
  571. duration: 'fast',
  572. direction: 'vertical',
  573. complete: function() {
  574. el.addClass('active');
  575. }
  576. });
  577. }
  578. return false;
  579. }
  580. </script>
  581. <?php
  582. $_showed = true;
  583. }
  584. }
  585. /**
  586. * @returns html link tag <a ... > ... </a>
  587. * @param $name
  588. * @param $href
  589. * @param $attr
  590. */
  591. public static function link($name, $href, $attr = array()) {
  592. $out_href = App::build_http_query($href);
  593. $txt = $name;
  594. $ico = V::get('ico', '', $attr);
  595. $ico_height = V::get('ico_height', 16, $attr, 'int');
  596. if (isset($attr['ico_height'])) unset($attr['ico_height']);
  597. if ($ico) {
  598. if (count(explode('.', $ico)) == 1) {
  599. $ico .= '.gif';// default ico format
  600. }
  601. if (file_exists(APP_PATH_ROOT . DS . 'icon' . DS . $ico)) {
  602. $txt = '<img src="'."icon/".$ico.'" height="'.$ico_height.'" alt="'.$name.'" />';
  603. }
  604. unset($attr['ico']);
  605. }
  606. $ico = V::get('ico_after_text', '', $attr);
  607. if ($ico) {
  608. if (count(explode('.', $ico)) == 1) {
  609. $ico .= '.gif';
  610. }
  611. if (file_exists( APP_PATH_ROOT . DS . 'icon' . DS . $ico )) {
  612. $txt .= ' <img src="'."icon/".$ico.'" height="'.$ico_height.'" alt="'.$name.'" />';
  613. }
  614. unset($attr['ico_after_text']);
  615. }
  616. $iconBootstrap = V::get('iconBootstrap', '', $attr);
  617. if ($iconBootstrap) {
  618. $txt = '<i class="' . $iconBootstrap . '" style="margin:0"></i>';
  619. }
  620. $iconBootstrapAfterText = V::get('iconBootstrapAfterText', '', $attr);
  621. if ($iconBootstrapAfterText) {
  622. $txt .= '<i class="' . $iconBootstrapAfterText . '" style="margin:0"></i>';
  623. }
  624. $p_attr = array();
  625. if (!empty($attr)) {
  626. $p_attr_arr = array();
  627. foreach ($attr as $k => $v) {
  628. $p_attr_arr[] = ''.$k.'="'.$v.'"';
  629. }
  630. $p_attr[] = ' '.implode(' ',$p_attr_arr);
  631. }
  632. $p_attr = (!empty($p_attr))? ' '.implode(' ', $p_attr) : '';
  633. return '<a href="'.$out_href.'"'.$p_attr.'>'.$txt.'</a>';
  634. }
  635. /**
  636. * @returns html ajax link tag <a ... > ... </a>
  637. * @param $name
  638. * @param $href
  639. * @param $attr
  640. */
  641. public static function link_ajax($name, $fun_name, $fun_args = array(), $attr = array()) {
  642. // TODO: where to store ajax result: after, id, function
  643. // TODO: App::print_js_ajax_callback()
  644. $txt = $name;
  645. $ico = V::get('ico', '', $attr);
  646. $ico_height = V::get('ico_height', 16, $attr, 'int');
  647. if (isset($attr['ico_height'])) unset($attr['ico_height']);
  648. if ($ico) {
  649. if (count(explode('.', $ico)) == 1) {
  650. $ico .= '.gif';// default ico format
  651. }
  652. if (file_exists(APP_PATH_ROOT . DS . 'icon' . DS . $ico)) {
  653. $txt = '<img src="'."icon/".$ico.'" height="'.$ico_height.'" alt="'.$name.'" />';
  654. }
  655. unset($attr['ico']);
  656. }
  657. $ico = V::get('ico_after_text', '', $attr);
  658. if ($ico) {
  659. if (count(explode('.', $ico)) == 1) {
  660. $ico .= '.gif';
  661. }
  662. if (file_exists( APP_PATH_ROOT . DS . 'icon' . DS . $ico )) {
  663. $txt .= ' <img src="'."icon/".$ico.'" height="'.$ico_height.'" alt="'.$name.'" />';
  664. }
  665. unset($attr['ico_after_text']);
  666. }
  667. $iconBootstrap = V::get('iconBootstrap', '', $attr);
  668. if ($iconBootstrap) {
  669. $txt = '<i class="' . $iconBootstrap . '" style="margin:0"></i>';
  670. }
  671. $iconBootstrapAfterText = V::get('iconBootstrapAfterText', '', $attr);
  672. if ($iconBootstrapAfterText) {
  673. $txt .= '<i class="' . $iconBootstrapAfterText . '" style="margin:0"></i>';
  674. }
  675. $js_fun_attr = array();
  676. $js_fun_attr['result_type'] = V::get('js_result_type', 'node', $attr);
  677. $js_fun_attr['result'] = V::get('js_result', 'after', $attr);
  678. unset($attr['js_result_type']);
  679. unset($attr['js_result']);
  680. $js = "return procesy_ajax_link(this, '".$js_fun_attr['result_type']."', '".$js_fun_attr['result']."');";
  681. $attr['onclick'] = $js;
  682. $p_attr = array();
  683. if (!empty($attr)) {
  684. $p_attr_arr = array();
  685. foreach ($attr as $k => $v) {
  686. $p_attr_arr[] = ''.$k.'="'.$v.'"';
  687. }
  688. $p_attr[] = implode(' ',$p_attr_arr);
  689. }
  690. $p_attr = (!empty($p_attr))? ' '.implode(' ', $p_attr) : '';
  691. $href_args = array();
  692. $href_args[] = "_ajax_request=".$fun_name;
  693. foreach ($fun_args as $k => $v) {
  694. $href_args[] = "$k=$v";
  695. }
  696. $out_href = "?" . implode("&", $href_args);
  697. return '<a href="'.$out_href.'"'.$p_attr.'>'.$txt.'</a>';
  698. }
  699. /*
  700. * link_ajax_load_on_ready( $fun_name, $fun_args = array(), $attr = array() )
  701. * load content from ajax request to function $fun_name
  702. */
  703. public static function link_ajax_load_on_ready($name, $fun_name, $fun_args = array(), $attr = array()) {
  704. // TODO: where to store ajax result: after, id, function
  705. // TODO: App::print_js_ajax_callback()
  706. $js = "return procesy_ajax_link(this, 'override', 'node');";
  707. $attr['onclick'] = $js;
  708. $attr['class'] = 'ajax_load_on_ready';
  709. $p_attr = array();
  710. if (!empty($attr)) {
  711. $p_attr_arr = array();
  712. foreach ($attr as $k => $v) {
  713. $p_attr_arr[] = ''.$k.'="'.$v.'"';
  714. }
  715. $p_attr[] = implode(' ',$p_attr_arr);
  716. }
  717. $p_attr = (!empty($p_attr))? ' '.implode(' ', $p_attr) : '';
  718. $href_args = array();
  719. $href_args[] = "_ajax_request=".$fun_name;
  720. foreach ($fun_args as $k => $v) {
  721. $href_args[] = "$k=$v";
  722. }
  723. $out_href = "?" . implode("&", $href_args);
  724. $txt = $name;
  725. return '<a href="'.$out_href.'"'.$p_attr.'>'.$txt.'</a>';
  726. }
  727. // ?_ajax_request=FUNCTION
  728. public static function exec_ajax_request() {
  729. if (($ajax_request_fun = V::get('_ajax_request', '', $_REQUEST)) != '') {
  730. // TODO: SEF( $ajax_request_fun )
  731. if (function_exists($ajax_request_fun)) {
  732. //sleep(1);
  733. if ( User::logged() ) {
  734. $ajax_request_fun();
  735. } else {
  736. sleep(1);
  737. header("HTTP/1.0 401 Unauthorized");
  738. }
  739. } else {
  740. sleep(1);
  741. header("HTTP/1.0 404 Not Found");
  742. }
  743. die();
  744. }
  745. }
  746. public static function show_head_css() {
  747. ?>
  748. <!-- <link rel="stylesheet" type="text/css" href="stuff/jquery-ui-smoothness/jquery-ui-1.10.1.custom.min.css"> -->
  749. <style type="text/css">
  750. .tip-popup-wrap{display:none;padding:0;position:absolute;z-index:10000;background:#fff;border:4px solid #444;}
  751. .tip-popup-wrap #tip-popup-box{margin:0;padding:3px;background:#fff;color:#000;text-align:left;overflow:auto;}
  752. .tip-popup-wrap .title{margin:0;background:#444;color:#fff;line-height:20px;font-weight:bold;text-align:right;}
  753. .tip-popup-wrap .title h3{margin:0;padding:0;border-bottom:1px solid #ccc;background:#999;color:#fff;font-weight:bold;}
  754. .app-msgs{margin:3px 10px;padding:4px;border:1px solid #666;}
  755. .app-msg-type-msg{boder-color:#008000;background:#CEFFCE;}
  756. .app-msg-type-err{boder-color:#FF0000;background:#FFB8B8;}
  757. /* lightbox */
  758. #jquery-overlay { width:100%; height:500px; position:absolute; top:0; left:0; z-index:90; }
  759. #jquery-lightbox { width:100%; position:absolute; top:0; left:0; z-index:100; text-align:center; line-height:0; }
  760. #jquery-lightbox img { border:none; }
  761. #lightbox-container-image-box { width:250px; height:250px; margin:0 auto; position:relative; background-color:#fff; }
  762. #lightbox-container-image { padding:10px; }
  763. #lightbox-loading { position:absolute; top:40%; left:0%; height:25%; width:100%; text-align:center; line-height:0; }
  764. #lightbox-nav { position:absolute; top:0; left:0; height:100%; width:100%; z-index:10; }
  765. #lightbox-container-image-box > #lightbox-nav { left:0; }
  766. #lightbox-nav a { outline:none;}
  767. #lightbox-nav-btnPrev,
  768. #lightbox-nav-btnNext { display:block; width:49%; height:100%; zoom:1; }
  769. #lightbox-nav-btnPrev { float:left; left:0; }
  770. #lightbox-nav-btnNext { float:right; right:0; }
  771. #lightbox-container-image-data-box { width:100%; overflow:auto; margin:0 auto; padding:0 10px 0; font:10px Helvetica, sans-serif; background-color:#fff; line-height:1.4em; }
  772. #lightbox-container-image-data { padding:0 10px; color:#666; }
  773. #lightbox-container-image-data #lightbox-image-details { float:left; width:70%; text-align:left; }
  774. #lightbox-image-details-caption { font-weight:bold; }
  775. #lightbox-image-details-currentNumber { display:block; clear:left; padding-bottom:1.0em; }
  776. #lightbox-secNav-btnClose { float:right; width:66px; padding-bottom:0.7em; }
  777. .DEBUG_S {display:none;}
  778. body.show-DEBUG_S .DEBUG_S {display:block;}
  779. </style>
  780. <?php
  781. }
  782. public static function show_head_js() {
  783. ?>
  784. <!-- <script src="stuff/jquery-ui.custom.min.js"></script> -->
  785. <script src="stuff/jquery.lightbox-0.5.js"></script>
  786. <script src="stuff/jquery.scrollintoview.min.js"></script>
  787. <?php
  788. echo self::print_js_ajax_callback();
  789. }
  790. public static function ui_widget($type, $title, $msg = '') {
  791. $ico_types = array('check', 'info', 'alert');
  792. $ui_states = array('check' => 'highlight', 'info' => 'highlight', 'alert' => 'error');
  793. $ico = (in_array($type, $ico_types))? '<span class="ui-icon ui-icon-' . $type . '" style="float:left; margin-right:.3em;"></span>' : '';
  794. $ui_state = V::get($type, 'highlight', $ui_states);
  795. $out .= '<div class="ui-widget">';
  796. $out .= '<div class="ui-state-' . $ui_state . ' ui-corner-all" style="margin-top:20px; padding:0 .7em;">';
  797. $out .= '<p>' . $ico;
  798. if ($type == 'alert') {
  799. $out .= '<strong>' . "Error: " . '</strong>' . $title;
  800. } else {
  801. $out .= '<strong>' . $title . '</strong>';
  802. }
  803. echo '</p>';
  804. if (!empty($msg)) {
  805. $out .= '<p>' . $msg . '</p>';
  806. }
  807. $out .= '</div>';
  808. $out .= '</div>';
  809. return $out;
  810. }
  811. /**
  812. * object state
  813. */
  814. public static function get_state($k) {
  815. return self::state('get', $k);
  816. }
  817. public static function set_state($k, $v) {
  818. return self::state('set', $k, $v);
  819. }
  820. public static function state($task, $k, $v = null) {
  821. static $state;
  822. if (!$state) $state = array();
  823. if ($task == 'set') {
  824. $state [$k] = $v;
  825. } else if ($task == 'get') {
  826. if (array_key_exists($k, $state)) {
  827. return $state[$k];
  828. } else {
  829. $null = null;
  830. return $null;
  831. }
  832. } else {
  833. // TODO: err
  834. }
  835. }
  836. /**
  837. * Fetch all messages and remove from queue.
  838. */
  839. public static function get_msgs() {
  840. $ret = array();
  841. if (!empty($_SESSION['app-msgs'])) {
  842. foreach ($_SESSION['app-msgs'] as $msg_type => $msgs) {
  843. $ret[$msg_type] = $msgs;
  844. }
  845. $_SESSION['app-msgs'] = array();
  846. }
  847. return $ret;
  848. }
  849. /**
  850. * Add message to app message queue.
  851. * @see show_msgs
  852. */
  853. public static function add_error($msg = '') {
  854. self::_add_msg($msg, 'err');
  855. }
  856. public static function add_msg($msg = '') {
  857. self::_add_msg($msg, 'msg');
  858. }
  859. public static function _add_msg($msg = '', $type = 'msg') {
  860. if ($msg) {
  861. $_SESSION['app-msgs'][$type][] = $msg;
  862. }
  863. }
  864. public static function redirect($url, $msg = '') {
  865. if ($msg) {
  866. self::add_msg($msg);
  867. }
  868. if (!headers_sent()) {
  869. header('HTTP/1.1 303 See Other');
  870. header('Location: '.$url);
  871. } else {
  872. echo'<script type="text/javascript">'."
  873. window.location.href='{$url}';
  874. ".'</script>';
  875. echo "\n".'<noscript>';
  876. echo "\n".'<meta http-equiv="refresh" content="0;url='.$url.'" />';
  877. echo "\n".'</noscript>';
  878. echo'<p>'.'<a href="'.$url.'">'."dalej".'</a>'.'</p>';
  879. }
  880. exit;
  881. }
  882. public static function is_ajax_request() {
  883. if (array_key_exists('X_REQUESTED_WITH', $_SERVER)) {
  884. $index = 'X_REQUESTED_WITH';
  885. }
  886. else {
  887. $index = 'HTTP_X_REQUESTED_WITH';
  888. }
  889. $is_xml_http_request = array_key_exists($index, $_SERVER) && $_SERVER[$index] == 'XMLHttpRequest';
  890. return $is_xml_http_request;
  891. }
  892. }