PageRenderTime 81ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/html_list.inc.php

https://bitbucket.org/cfraunholz/big-red-button
PHP | 509 lines | 431 code | 47 blank | 31 comment | 68 complexity | ff1e235f053fa296de0d83077873c654 MD5 | raw file
  1. <?php
  2. // The Big Red Red Button Source Code Generator
  3. // Dipl.-Ing. (FH) Christian K. Fraunholz (php10.de)
  4. // 2010
  5. // v 0.7.8
  6. $first = false;
  7. if (!$listMethod) $listMethod = 'row';
  8. if (WEBSITE == 'HROSE') {
  9. if (!$searchMethod) $searchMethod = '_VALID';
  10. if (!$searchDBMethod) $searchDBMethod = '_VALIDDB';
  11. } else {
  12. if (!$searchMethod) $searchMethod = '_SESSION';
  13. if (!$searchDBMethod) $searchDBMethod = '_SESSION';
  14. }
  15. /***** Init *****/
  16. $code = '$modul="'.$tableName.'";
  17. require("inc/req.php");
  18. /*** Rights ***/
  19. // Generally for people with the right to change ' . $tableName . '
  20. GRGR(1000);
  21. ';
  22. /*
  23. if ($_REQUEST['list_search']) {
  24. $code .= '
  25. session_start();';
  26. }
  27. if ($_REQUEST['list_sort']) {
  28. $code .= '
  29. define(\'DEFAULT_SORT\', \'' . varname($primary[0]) . '\');';
  30. }
  31. if ($_REQUEST['list_paginator']) {
  32. $code .= '
  33. define(\'ROWS_PER_PAGE\', \'50\');';
  34. }
  35. */
  36. $code .= $code ? '
  37. ' :'';
  38. if ($_REQUEST['list_search']) {
  39. if ($_REQUEST['list_ajax_??']) { $code .= '
  40. if (isset(' . varname('sort') . ')) {
  41. ' . varname('sort', $searchMethod) . ' = ' . varname('sort') . ';
  42. }';
  43. foreach ($array as $key => $value) {
  44. $code .= '
  45. if (isset(' . varname($value) . ')) {
  46. ' . varname($value, $searchMethod) . ' = ' . varname($value) . ';
  47. };';
  48. }
  49. }
  50. if ($_REQUEST['list_sort_alt']) {
  51. $code .= '
  52. if (isset(' . varname('sort') . ')) {
  53. ' . varname('sort', $searchMethod) . ' = ' . varname('sort') . ';
  54. }';
  55. }
  56. if ($_REQUEST['list_paginator']) {
  57. $code .= '
  58. if (isset(' . varname('page') . ')) {
  59. ' . varname('page', $searchMethod) . ' = ' . varname('page') . ';
  60. }
  61. ';
  62. } else {
  63. $code .= '
  64. /*** General Table variables **/
  65. // fill parameters from session
  66. if (!isset($_REQUEST[\'headless\']) && isset($_SESSION[$modul])) {
  67. foreach ($_SESSION[$modul] as $key => $value) {
  68. $_REQUEST[$key] = $value;
  69. }
  70. }';
  71. if ($_REQUEST['list_sort']) {
  72. if (in_array('name', $array)) {
  73. $sort = 'name';
  74. } else if (in_array('shortname',$array)) {
  75. $sort = 'shortname';
  76. } else {
  77. $sort = $array[0];
  78. }
  79. $code .= '
  80. if (isset($_REQUEST[\'sortcol\'])) {
  81. $_SESSION[$modul][\'sortcol\'] = $_REQUEST[\'sortcol\'];
  82. }
  83. if (isset($_REQUEST[\'sortdir\'])) {
  84. $_SESSION[$modul][\'sortdir\'] = $_REQUEST[\'sortdir\'];
  85. }
  86. validate(\'sortcol\', \'string\');
  87. validate(\'sortdir\', \'set\', array(\'ASC\',\'DESC\'));
  88. $orderBy = ($_VALID[\'sortcol\'])?$_VALID[\'sortcol\'] .(($_VALID[\'sortdir\'])?\' \' . $_VALID[\'sortdir\']:\'\'):\''.$sort.'\';';
  89. }
  90. $code .= '
  91. $headless = (isset($_REQUEST[\'headless\']))?true:false;
  92. $n4a[\''.$tableName.'_d.php\'] = \''.trans('Add '.ucfirst($tableName)).'\';
  93. if (!$headless) require("inc/header.inc.php");
  94. ';
  95. }
  96. }
  97. $listInitCode = $code;
  98. /***** DELETE statement ****/
  99. $code = '';
  100. $pkfirst = false;
  101. foreach ($primary as $key => $pk) {
  102. if ($pkfirst) $pkDelCode .= (($pkLast == 'int') ? ' . "' : '') . ' AND ';
  103. $pkDelCode .= varname($pk, 'blank') . ' = ';
  104. if (in_array($simpleType[$key], array('int', 'ckb'))) {
  105. $pkDelCode .= '" . (int) ' . varname($pk);
  106. $pkLast = 'int';
  107. } else {
  108. $pkDelCode .= '\'" . ' . varname($pk) . ' . "\'"';
  109. }
  110. $pkfirst = true;
  111. }
  112. $code .= '
  113. // delete
  114. if (isset(' . varname('delete','_REQUEST') . ')) {
  115. $sql = "DELETE FROM ' . $tableName . ' WHERE ' . $pkDelCode . ';
  116. mysql_query($sql) or error_log(mysql_error());
  117. }
  118. ';
  119. $listDeleteCode = $code;
  120. /***** Search ****/
  121. $code = '';
  122. if ($_REQUEST['list_sort_alt']) {
  123. $code .= '
  124. // Sort
  125. if (' . varname('sort', $searchMethod) . ') {
  126. $sortAr = explode(\' \', ' . varname('sort', $searchMethod) . ');
  127. $order_name = $sortAr[0];
  128. $order_dir = $sortAr[1];
  129. if (in_array($order_name, array(\'' . implode('\', \'', $array) . '\'))) {
  130. $orderBy = ((strtoupper($order_dir) == \'DESC\') ? \'DESC\' : \'ASC\';
  131. } else {
  132. $orderBy = DEFAULT_SORT;
  133. }
  134. }
  135. ';
  136. }
  137. if ($_REQUEST['list_paginator']) {
  138. $code .= '
  139. // Pagination
  140. $offset = (' . varname('page', $searchMethod) . '-1) * ROWS_PER_PAGE;
  141. ';
  142. }
  143. $code .= '
  144. // where condition';
  145. foreach ($array as $key => $value) {
  146. if ($simpleType[$key] == 'ckb') {
  147. $code .= '
  148. if (isset(' . varname($value, $searchMethod) . ')) {';
  149. } else {
  150. $code .= '
  151. if (' . varname($value, $searchMethod) . ') {';
  152. }
  153. switch ($simpleType[$key]) {
  154. case 'ckb':
  155. $code .= '
  156. $where[] = "' .$tableName . '.' . varname($value, 'blank') . ' = " . ((' . varname($value, $searchDBMethod) . ' == 1) ? 1 : 0);';
  157. break;
  158. case 'string':
  159. $code .= '
  160. $where[] = "' .$tableName . '.'. varname($value, 'blank') . ' LIKE \'%" . mysql_real_escape_string(' . varname($value, $searchMethod) . ') . "%\'";';
  161. break;
  162. case 'int':
  163. $code .= '
  164. $where[] = "' .$tableName . '.'. varname($value, 'blank') . ' = " . '.((WEBSITE == HROSE)?'':'(int) ') . varname($value, $searchDBMethod) . ';';
  165. break;
  166. default:
  167. $code .= '
  168. $where[] = "' .$tableName . '.'. varname($value, 'blank') . ' = \'" . mysql_real_escape_string(' . varname($value, $searchMethod) . ') . "\'";';
  169. }
  170. $code .= '
  171. }
  172. $_SESSION[$modul][\''.$value.'\'] = $_VALID[\''.$value.'\'];';
  173. }
  174. $code .= '
  175. $where = ($where) ? implode(" AND ", $where) : "1=1";';
  176. $listSearchCode = $code;
  177. /***** SELECT ****/
  178. $code = '';
  179. $code .= '
  180. $sql = "SELECT ' . (($_REQUEST['list_paginator']) ? 'SQL_CALC_FOUND_ROWS ' : '') .$tableName . '.' . implode(', '.$tableName.'.',$array).' FROM ' . $tableName . ' WHERE ' . (($_REQUEST['list_search']) ? '" . $where . "' : '1=1') . (($_REQUEST['list_sort']) ? ' ORDER BY " . $orderBy' : '') . (($_REQUEST['list_paginator']) ? ' . " LIMIT " . $offset . ", " . ROWS_PER_PAGE' : '') . ';';
  181. if ($_REQUEST['list_memcache']) {
  182. $code .= '
  183. /*** Store SQL in Session ***/
  184. $_SESSION[$modul][\'sql\'] = $sql;
  185. $listResult = getMemCache($sql);
  186. // refresh memcache if necessary
  187. $rl = isset($_SESSION[$modul][\'rl\']) || isset($_GET[\'rl\']);
  188. if (!$listResult || $rl) {
  189. $r = mysql_query($sql) or die(mysql_error());
  190. unset($listResult);
  191. while($row=mysql_fetch_array($r))
  192. $listResult[]=$row;
  193. if ($memcache) {
  194. setMemCache($sql, $listResult);
  195. }
  196. unset($_SESSION[$modul][\'rl\']);
  197. }';
  198. } else {
  199. $code .= '
  200. $listResult = mysql_query($sql);
  201. ';
  202. }
  203. $listSelectCode = $code;
  204. /***** The table ****/
  205. $code = '';
  206. if ($_REQUEST['list_ajax']) {
  207. $code = '
  208. if (!$headless) {
  209. ?>
  210. <div class="contentheadline"><?php echo '.trans(ucfirst($tableName),'php') . '?></div>
  211. <br>
  212. <div class="contenttext">
  213. <table cellspacing="0" cellpadding="0" class="bw">
  214. <?php
  215. }
  216. if (!$headless) {
  217. echo \'<tr class="head">\';';
  218. $i = 0;
  219. foreach ($array as $field) {
  220. $code .= '
  221. echo \'<th'.(($_REQUEST['list_odd_column'] AND (($i++ % 2)==0))?'':' class="grey"').'><a href="javascript:void(0)" onClick="changeSort(\\\''.$field.'\\\')">'.trans($field).'</a>&nbsp;&nbsp;
  222. <input class="search" name="'.$field.'" id="'.$field.'" value="\'.$_SESSION[$modul][\''.ucfirst($field).'\'].\'">
  223. </th>\';';
  224. }
  225. $code .= '
  226. echo \'<th>&nbsp;</th>\';
  227. echo \'</tr><tbody id="list_tbody">\';
  228. }';
  229. } else {
  230. $code = '
  231. <table>
  232. <tr>';
  233. if ($_REQUEST['dynamic_list_header']) {
  234. $code .= '
  235. <?php
  236. $header = array(\'' . implode(',', $array) . '\');
  237. foreach ($header as $key => $value) {';
  238. if ($_REQUEST['list_sort'] && !$_REQUEST['list_ajax']) {
  239. $code .= '
  240. if ($order_name == $value) {
  241. if ($order_dir == \'DESC\') {
  242. $order_dir = \'ASC\';
  243. $order_class = \'DESC\';
  244. } else {
  245. $order_dir = \'DESC\';
  246. $order_class = \'ASC\';
  247. }
  248. } else {
  249. $order_class = \'\';
  250. $order_dir = \'ASC\';
  251. }
  252. echo \'<th\' . (($order_class) ? \' class= "\' . $order_class . \'"\' : \'\') . \'><a href="?sort=\' . urlencode($value . \' \' . $order_dir) . \'">\' . ucfirst($value) . \'</a></th>\';';
  253. } else if(!$_REQUEST['list_sort']) {
  254. $code .= '
  255. echo \'<th>\' . ucfirst($value) . \'</th>\';
  256. ';
  257. } else {
  258. $code .= '
  259. echo \'<th href="javascript:void(0)" onClick="changeSort(\'.$value.\')">\' . ucfirst($value) . \'</th>\';
  260. ';
  261. }
  262. $code .= '
  263. }
  264. ?>';
  265. } else {
  266. foreach ($array as $key => $value) {
  267. $code .= '
  268. <th>' . ucfirst(varname($value, 'blank')) . '</th>';
  269. }
  270. }
  271. }
  272. if (!$_REQUEST['list_ajax']) $code .= '
  273. </tr>
  274. <?php';
  275. $code .= '
  276. if (!$listResult) {
  277. echo \'<tr><td colspan="3">\'.' . trans('No entries found', 'php') . '.\'</td></tr>\';
  278. } else {
  279. $i = 0;
  280. '.(($_REQUEST['list_memcache'])?'foreach($listResult as $index => $row) {':'while($row = mysql_fetch_array($listResult)) {').'
  281. echo \'<tr'.(($_REQUEST['list_odd_row'])?' class="dotted \' . ((($i++ % 2)==0) ? "tr_even":"tr_odd") . \'"':'').' id="tr_\'.$row[\''.$tableName.'_id\'].\'">\';';
  282. $i = 0;
  283. foreach ($array as $key => $value) {
  284. if ($i++ == 0) $mouseover = ((WEBSITE == HROSE)?' \'.$mouseover.\'':'').' onClick="location.href=\\\''.$tableName.'_d.php?i=\'.$index.\'&amp;'.$tableName.'_id=\'.$row[\''.$tableName.'_id\'].\'\\\'"';
  285. else $mouseover = '';
  286. switch ($simpleType[$key]) {
  287. case 'ckb':
  288. $code .= '
  289. echo \'<td'.$mouseover.' nowrap>\' . ((' . varname($value, $listMethod) . ' == \'1\') ? ' . trans('yes', 'php') . ' : ' . trans('no', 'php') . ') . \'</td>\';';
  290. break;
  291. case 'enum':
  292. $code .= '
  293. echo \'<td'.$mouseover.' nowrap>\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  294. break;
  295. case 'set':
  296. $code .= '
  297. echo \'<td'.$mouseover.' nowrap>\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  298. break;
  299. case 'integer':
  300. $code .= '
  301. echo \'<td'.$mouseover.' style="text-align:\'right\'" nowrap>\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  302. break;
  303. default:
  304. $code .= '
  305. echo \'<td'.$mouseover.' nowrap>\' . ' . varname($value, $listMethod) . ' . \'</td>\';';
  306. }
  307. }
  308. $pkfirst = false;
  309. $pkUrl = '';
  310. foreach ($primary as $key => $pk) {
  311. if ($pkfirst) $pkUrl .= '&amp;';
  312. switch ($simpleType[$key]) {
  313. case 'int':
  314. $escaped = '(int) ' . varname($pk, $listMethod);
  315. break;
  316. case 'ckb':
  317. $escaped = '(int) ' . varname($pk, $listMethod);
  318. break;
  319. default:
  320. $escaped = 'htmlspecialchars(' . varname($pk, $listMethod) . (($charset == 'UTF-8') ? ', ENT_QUOTES, \'UTF-8\'' : '') . ')';
  321. }
  322. $pkUrl .= $pk . '=\' . ' . $escaped . ' . \'';
  323. $pkfirst = true;
  324. }
  325. $code .= '
  326. echo \'<td nowrap><a href="'.$tableName.'_d.php?i='.$index.'&amp;' . $pkUrl . '">' . ((WEBSITE == HROSE)?'<img src="css/icon/pencil_icon&16.png" title="'.trans('Edit').'">':trans('Update')).'</a>\';';
  327. if ($_REQUEST['list_ajax'])
  328. $code .= "
  329. // people with right to delete see the delete button
  330. if (R(3))
  331. echo '&nbsp;&nbsp;<a href=\"#\" onclick=\"if (confirm(\'" . trans('Do you really want to delete the '.ucfirst($tableName).'?') . "\')) delRow('.\$row['".$tableName."_id'].');\">
  332. <img src=\"css/icon/delete_icon&16.png\" title=\"" . trans('Delete') . "\"></a>';
  333. echo '</td>';";
  334. else
  335. $code .= '
  336. echo \'&nbsp;&nbsp;<a href="?delete=1&amp;' . $pkUrl . '">' . trans('Delete') . '</a></td>\';';
  337. $code .= '
  338. echo \'</tr>\';
  339. }
  340. }';
  341. $code .= '
  342. '.(($_REQUEST['list_ajax'])?'if (!$headless) { ?>':'?>').'
  343. </table>
  344. </div>
  345. ';
  346. $tableCode = $code;
  347. /***** Pagination ****/
  348. $code = '';
  349. $code .= '
  350. <?php
  351. // Pagination
  352. $sql = "SELECT FOUND_ROWS() as rownum";
  353. $row = mysql_fetch_row(mysql_query($sql));
  354. $totalRows = $row[0];
  355. $pageNum = ceil($totalRows / ROWS_PER_PAGE);
  356. $paginator = "";
  357. for ($i = 1; $i <= $pageNum; $i++) {
  358. $paginator .= \'<a href="?page=\' . $i . \'">\' . (($i == ' . varname('page', $searchMethod) . ') ? \'<b>[\' . $i . \']</b>\' : $i) . \'</a>&nbsp;\';
  359. }
  360. echo $paginator;
  361. ';
  362. $paginationCode = $code;
  363. /***** jQuery ****/
  364. $code = '';
  365. $code .= "
  366. <script type=\"text/javascript\">";
  367. if ($_REQUEST['list_delete']) {
  368. $code .= "
  369. function delRow(pk) {
  370. $.ajax({
  371. url: 'a/".$tableName."_del.php?id='+pk
  372. });
  373. $('#tr_'+pk).hide();
  374. }
  375. ";
  376. }
  377. if ($_REQUEST['list_search'] || $_REQUEST['list_sort']) {
  378. $code .= "
  379. var sortcol = '".$sort."';
  380. var sortdir = '';
  381. var del = '';
  382. function updateList() {
  383. var url = '<?=\$_REQUEST['PHP_SELF']?>?headless&sortcol='+sortcol+'&sortdir='+sortdir+'&del='+del;
  384. var filterparams = '';
  385. ".(($_REQUEST['list_search'])?"
  386. // inputs
  387. var val = '';
  388. $('.search:input').each(function(index, obj) {
  389. val = $('#' + obj.name).val();
  390. if (val != '') filterparams += '&' + obj.name + '=' + $('#' + obj.name).val();
  391. });
  392. $('.bw select').each(function(index, obj) {
  393. val = $(\"#\" + obj.name).val();
  394. if (val != '') filterparams += '&' + obj.name + '=' + $('#' + obj.name).val();
  395. });":"")."
  396. url += filterparams;
  397. $.get(url, function(data) {
  398. $('#list_tbody').html(data);
  399. // also add the filterparam to the xls export
  400. $('#xlsbutton').attr('href',$('#xlsbutton').attr('href') + filterparams);
  401. });
  402. }";
  403. if ($_REQUEST['list_sort'])
  404. $code .= "
  405. function changeSort(col) {
  406. if (sortcol == col) {
  407. sortdir = (sortdir == 'DESC') ? 'ASC' : 'DESC';
  408. } else {
  409. sortdir = 'DESC';
  410. }
  411. sortcol = col;
  412. updateList();
  413. }";
  414. if ($_REQUEST['list_search'])
  415. $code .= "
  416. $('.search:input').keyup(function(index) {
  417. updateList();
  418. });
  419. $('.bw select').change(function() {
  420. updateList();
  421. });";
  422. }
  423. $code .= "
  424. </script>
  425. ";
  426. $jQueryCode = $code;
  427. /***** Pagination ****/
  428. $code = '';
  429. $code .= '<?php
  430. require("inc/footer.inc.php");
  431. '.(($_REQUEST['list_ajax'])?'}':'');
  432. $footerCode = $code;
  433. if ($_REQUEST['list_search']) {
  434. $htmlListCode =
  435. $listInitCode
  436. . $validateCode
  437. . $listDeleteCode
  438. . $listSearchCode
  439. . $listSelectCode
  440. . (($_REQUEST['list_ajax']) ? '' : $formCode)
  441. . $tableCode
  442. . (($_REQUEST['list_paginator']) ? $paginationCode : '')
  443. . (($_REQUEST['list_ajax']) ? $jQueryCode : '')
  444. . $footerCode;
  445. } else {
  446. $htmlListCode =
  447. $listInitCode
  448. . $validateCode
  449. . $listDeleteCode
  450. . $listSearchCode
  451. . $tableCode
  452. . (($_REQUEST['list_paginator']) ? $paginationCode : '')
  453. . (($_REQUEST['list_ajax']) ? $jQueryCode : '')
  454. . $footerCode;
  455. }