App.php 31 KB

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