/hrose_list.inc.php

https://bitbucket.org/cfraunholz/big-red-button · PHP · 284 lines · 240 code · 34 blank · 10 comment · 25 complexity · cbc280d8709c10bd58f04f13dc07f5ea 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 (!$searchMethod) $searchMethod = '_SESSION';
  9. /***** Init *****/
  10. $code = '';
  11. if ($_REQUEST['list_search']) {
  12. $code .= '
  13. sesssion_start();';
  14. }
  15. if ($_REQUEST['list_sort']) {
  16. $code .= '
  17. define(\'DEFAULT_SORT\', \'' . varname($primary[0]) . '\');';
  18. }
  19. if ($_REQUEST['list_paginator']) {
  20. $code .= '
  21. define(\'ROWS_PER_PAGE\', \'50\');';
  22. }
  23. $code .= $code ? '
  24. ' :'';
  25. if ($_REQUEST['list_search']) {
  26. $code .= '
  27. if (isset(' . varname('sort') . ')) {
  28. ' . varname('sort', $searchMethod) . ' = ' . varname('sort') . ';
  29. }';
  30. foreach ($array as $key => $value) {
  31. $code .= '
  32. if (isset(' . varname($value) . ')) {
  33. ' . varname($value, $searchMethod) . ' = ' . varname($value) . ';
  34. };';
  35. }
  36. }
  37. if ($_REQUEST['list_sort']) {
  38. $code .= '
  39. if (isset(' . varname('sort') . ')) {
  40. ' . varname('sort', $searchMethod) . ' = ' . varname('sort') . ';
  41. }';
  42. }
  43. if ($_REQUEST['list_paginator']) {
  44. $code .= '
  45. if (isset(' . varname('page') . ')) {
  46. ' . varname('page', $searchMethod) . ' = ' . varname('page') . ';
  47. }
  48. ';
  49. }
  50. $listInitCode = $code;
  51. /***** DELETE statement ****/
  52. $code = '';
  53. $pkfirst = false;
  54. foreach ($primary as $key => $pk) {
  55. if ($pkfirst) $pkDelCode .= (($pkLast == 'int') ? ' . "' : '') . ' AND ';
  56. $pkDelCode .= varname($pk, 'blank') . ' = ';
  57. if (in_array($simpleType[$key], array('int', 'ckb'))) {
  58. $pkDelCode .= '" . (int) ' . varname($pk);
  59. $pkLast = 'int';
  60. } else {
  61. $pkDelCode .= '\'" . ' . varname($pk) . ' . "\'"';
  62. }
  63. $pkfirst = true;
  64. }
  65. $code .= '
  66. // delete
  67. if (isset(' . varname('delete') . ')) {
  68. $sql = "DELETE FROM ' . $tableName . ' WHERE ' . $pkDelCode . ';
  69. mysql_query($sql) or error_log(mysql_error());
  70. }
  71. ';
  72. $listDeleteCode = $code;
  73. /***** Search ****/
  74. $code = '';
  75. if ($_REQUEST['list_sort']) {
  76. $code .= '
  77. // Sort
  78. if (' . varname('sort', $searchMethod) . ') {
  79. $sortAr = explode(\' \', ' . varname('sort', $searchMethod) . ');
  80. $order_name = $sortAr[0];
  81. $order_dir = $sortAr[1];
  82. if (in_array($order_name, array(\'' . implode('\', \'', $array) . '\'))) {
  83. $orderby = ((strtoupper($order_dir) == \'DESC\') ? \'DESC\' : \'ASC\';
  84. } else {
  85. $orderby = DEFAULT_SORT;
  86. }
  87. }
  88. ';
  89. }
  90. if ($_REQUEST['list_paginator']) {
  91. $code .= '
  92. // Pagination
  93. $offset = (' . varname('page', $searchMethod) . '-1) * ROWS_PER_PAGE;
  94. ';
  95. }
  96. $code .= '
  97. // where condition';
  98. foreach ($array as $key => $value) {
  99. $code .= '
  100. if (' . varname($value, $searchMethod) . ') {';
  101. switch ($simpleType[$key]) {
  102. case 'ckb':
  103. $code .= '
  104. $where[] = "' . varname($value, 'blank') . ' = " . ((' . varname($value, $searchMethod) . ' == \'y\') ? 1 : 0);';
  105. break;
  106. case 'string':
  107. $code .= '
  108. $where[] = "' . varname($value, 'blank') . ' LIKE \'%" . mysql_real_escape_string(' . varname($value, $searchMethod) . ') . "%\'";';
  109. break;
  110. case 'int':
  111. $code .= '
  112. $where[] = "' . varname($value, 'blank') . ' = " . (int) ' . varname($value, $searchMethod) . ';';
  113. break;
  114. default:
  115. $code .= '
  116. $where[] = "' . varname($value, 'blank') . ' = \'" . mysql_real_escape_string(' . varname($value, $searchMethod) . ') . "\'";';
  117. }
  118. $code .= '
  119. }';
  120. }
  121. $code .= '
  122. $where = ($where) ? implode(" AND ", $where) : "1=1";';
  123. $listSearchCode = $code;
  124. /***** SELECT ****/
  125. $code = '';
  126. $code .= '
  127. $sql = "SELECT ' . (($_REQUEST['list_paginator']) ? 'SQL_CALC_FOUND_ROWS ' : '') .'* FROM ' . $tableName . ' WHERE ' . (($_REQUEST['list_search']) ? '" . $where' : '1=1') . (($_REQUEST['list_sort']) ? ' ORDER BY " . $orderby' : '') . (($_REQUEST['list_paginator']) ? ' . " LIMIT " . $offset . ", " . ROWS_PER_PAGE' : '') . ';
  128. $result = mysql_query($sql);
  129. ';
  130. $listSelectCode = $code;
  131. /***** The table ****/
  132. $code = '';
  133. $code = '
  134. <table>
  135. <tr>';
  136. if ($_REQUEST['dynamic_list_header']) {
  137. $code .= '
  138. <?php
  139. $header = array(\'' . implode(',', $array) . '\');
  140. foreach ($header as $key => $value) {';
  141. if ($_REQUEST['list_sort']) {
  142. $code .= '
  143. if ($order_name == $value) {
  144. if ($order_dir == \'DESC\') {
  145. $order_dir = \'ASC\';
  146. $order_class = \'DESC\';
  147. } else {
  148. $order_dir = \'DESC\';
  149. $order_class = \'ASC\';
  150. }
  151. } else {
  152. $order_class = \'\';
  153. $order_dir = \'ASC\';
  154. }
  155. echo \'<th\' . (($order_class) ? \' class= "\' . $order_class . \'"\' : \'\') . \'><a href="?sort=\' . urlencode($value . \' \' . $order_dir) . \'">\' . ucfirst($value) . \'</a></th>\';';
  156. } else {
  157. $code .= '
  158. echo \'<th>\' . ucfirst($value) . \'</th>\';
  159. ';
  160. }
  161. $code .= '
  162. }
  163. ?>';
  164. } else {
  165. foreach ($array as $key => $value) {
  166. $code .= '
  167. <th>' . ucfirst(varname($value, 'blank')) . '</th>';
  168. }
  169. }
  170. $code .= '
  171. </tr>
  172. <?php
  173. if (!mysql_num_rows($result)) {
  174. echo \'<tr><td colspan=' . count($array) . '>' . trans('no entries') . '</td></tr>\';
  175. } else {
  176. while($row = mysql_fetch_array($result)) {
  177. echo \'<tr class="\' . ((($i++ % 2)==0) ? "tr_even":"tr_odd") . \'">\';';
  178. foreach ($array as $key => $value) {
  179. switch ($simpleType[$key]) {
  180. case 'ckb':
  181. $code .= '
  182. echo \'<td>\' . ((' . varname($value, $listMethod) . ' == \'1\') ? ' . trans('yes', 'php') . ' : ' . trans('no', 'php') . ') . \'</td>\';';
  183. break;
  184. case 'enum':
  185. $code .= '
  186. echo \'<td>\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  187. break;
  188. case 'set':
  189. $code .= '
  190. echo \'<td>\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  191. break;
  192. case 'integer':
  193. $code .= '
  194. echo \'<td style="text-align:\'right\'">\' . ' . ucfirst(varname($value, $listMethod)) . ' . \'</td>\';';
  195. break;
  196. default:
  197. $code .= '
  198. echo \'<td>\' . ' . varname($value, $listMethod) . ' . \'</td>\';';
  199. }
  200. }
  201. $pkfirst = false;
  202. $pkUrl = '';
  203. foreach ($primary as $key => $pk) {
  204. if ($pkfirst) $pkUrl .= '&amp;';
  205. switch ($simpleType[$key]) {
  206. case 'int':
  207. $escaped = '(int) ' . varname($pk, $listMethod);
  208. break;
  209. case 'ckb':
  210. $escaped = '(int) ' . varname($pk, $listMethod);
  211. break;
  212. default:
  213. $escaped = 'htmlspecialchars(' . varname($pk, $listMethod) . (($charset == 'UTF-8') ? ', ENT_QUOTES, \'UTF-8\'' : '') . ')';
  214. }
  215. $pkUrl .= $pk . '=\' . ' . $escaped . ' . \'';
  216. $pkfirst = true;
  217. }
  218. $code .= '
  219. echo \'<td><a href="detail.php?' . $pkUrl . '">' . trans('Update') . '</a></td>\';
  220. echo \'<td><a href="?delete=1&amp;' . $pkUrl . '">' . trans('Delete') . '</a></td>\';
  221. echo \'</tr>\';
  222. }
  223. }';
  224. $code .= '
  225. ?>
  226. </table>
  227. ';
  228. $tableCode = $code;
  229. /***** Pagination ****/
  230. $code = '';
  231. $code .= '
  232. <?php
  233. // Pagination
  234. $sql = "SELECT FOUND_ROWS() as rownum";
  235. $row = mysql_fetch_row(mysql_query($sql));
  236. $totalRows = $row[0];
  237. $pageNum = ceil($totalRows / ROWS_PER_PAGE);
  238. $paginator = "";
  239. for ($i = 1; $i <= $pageNum; $i++) {
  240. $paginator .= \'<a href="?page=\' . $i . \'">\' . (($i == ' . varname('page', $searchMethod) . ') ? \'<b>[\' . $i . \']</b>\' : $i) . \'</a>&nbsp;\';
  241. }
  242. echo $paginator;
  243. ';
  244. $paginationCode = $code;
  245. if ($_REQUEST['list_search']) {
  246. $htmlListCode2 = $listInitCode . $listDeleteCode . $listSearchCode . $listSelectCode . $formCode . $tableCode . (($_REQUEST['list_search']) ? $paginationCode : '');
  247. } else {
  248. $htmlListCode2 = $listInitCode . $listDeleteCode . $listSearchCode . $tableCode . (($_REQUEST['list_search']) ? $paginationCode : '');
  249. }