designview.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php require_once 'auth.php'; ?>
  2. <html>
  3. <head>
  4. <title><?=__('Design View')?></title>
  5. <style type='text/css'>
  6. body, th, td, select, input { font-size: 8pt; }
  7. </style>
  8. </head>
  9. <body>
  10. <script language=javascript>
  11. function addtable() {
  12. tableElement = document.forms[0].table;
  13. id = tableElement.options[tableElement.selectedIndex].value;
  14. f = document.fView;
  15. f.addtable.value = 1;
  16. f.idtable.value = id
  17. f.submit();
  18. }
  19. function deletetable(id) {
  20. f = document.fView;
  21. f.deltable.value = 1;
  22. f.idtable.value = id
  23. f.submit();
  24. }
  25. function addcolumn(table, columnElement) {
  26. id = columnElement.options[columnElement.selectedIndex].value;
  27. f = document.fView;
  28. f.addcolumn.value = 1;
  29. f.idcolumn.value = id;
  30. f.tbcolumn.value = table;
  31. f.submit();
  32. }
  33. function deletecolumn(id) {
  34. f = document.fView;
  35. f.idcolumn.value = id;
  36. f.delcolumn.value = 1;
  37. f.submit();
  38. }
  39. function savedesign(f) {
  40. f.submit();
  41. }
  42. function addrelation(f) {
  43. f.addrelation.value = 1;
  44. f.submit();
  45. }
  46. function deleterelation(id) {
  47. f.delrelation.value = 1;
  48. f.idrelation.value = id;
  49. f.submit();
  50. }
  51. function generatesql(f) {
  52. f.generate.value = 1;
  53. f.submit();
  54. }
  55. </script>
  56. <?php
  57. //print_r($_POST);
  58. ?>
  59. <table>
  60. <tr>
  61. <td>
  62. <form method=post action='<?=$_SERVER['PHP_SELF']?>'>
  63. <?=__('Tables')?>: <select name=table>
  64. <?php
  65. $metaTables = $adodb->MetaTables();
  66. foreach ($metaTables as $k => $v) {
  67. if (ereg('^qb_', $v) || ereg('_crc$', $v) || ereg('_flag$', $v)) continue;
  68. echo "<option value='".$v."'>".$v."</option>\n";
  69. }
  70. ?>
  71. </select>
  72. <input type=button style='width:80px' value='<?=__('Add')?>' onClick='addtable()'><br/>
  73. </form>
  74. </td>
  75. </tr>
  76. </table>
  77. <hr/>
  78. <table border=0><tr>
  79. <?php
  80. if (isset($_REQUEST['idView'])) {
  81. $rs = $adodb->Execute("SELECT serialize FROM qb_view ".
  82. " WHERE id = ? ", $_REQUEST['idView']);
  83. $_SESSION['selectedTable'] = array();
  84. $_SESSION['selectedColumn'] = array();
  85. $_SESSION['selectedRelation'] = array();
  86. if (!$rs->EOF) {
  87. $unSerialize = unserialize($rs->fields['serialize']);
  88. if (is_array($unSerialize['selectedTable'])) {
  89. $_SESSION['selectedTable'] = $unSerialize['selectedTable'];
  90. $_SESSION['selectedColumn'] = $unSerialize['selectedColumn'];
  91. $_SESSION['selectedRelation'] = $unSerialize['selectedRelation'];
  92. }
  93. }
  94. $_SESSION['selectedView'] = $_REQUEST['idView'];
  95. }
  96. if (!isset($_SESSION['selectedTable'])) $_SESSION['selectedTable'] = array();
  97. if (!isset($_SESSION['selectedColumn'])) $_SESSION['selectedColumn'] = array();
  98. if (!isset($_SESSION['selectedRelation'])) $_SESSION['selectedRelation'] = array();
  99. if ($_POST['deltable']) {
  100. unset($_SESSION['selectedTable'][$_POST['idtable']]);
  101. }
  102. if ($_POST['addtable']) {
  103. $tableId = $_POST['idtable'];
  104. $_SESSION['selectedTable'][] = $tableId;
  105. }
  106. if ($_POST['delcolumn']) {
  107. unset($_SESSION['selectedColumn'][$_POST['idcolumn']]);
  108. }
  109. if ($_POST['addcolumn']) {
  110. $tableId = $_POST['tbcolumn'];
  111. $_SESSION['selectedColumn'][] = array($tableId, $_POST['idcolumn']);
  112. }
  113. if ($_POST['action'] === 'savedesign') {
  114. if (!is_array($_POST['id'])) $_POST['id'] = array();
  115. foreach ($_POST['id'] as $k => $v) {
  116. if ($_POST['delcolumn'] && $v === $_POST['idcolumn']) continue;
  117. $_SESSION['selectedColumn'][$v]['total'] = $_POST['total'][$k];
  118. $_SESSION['selectedColumn'][$v]['sort'] = $_POST['sort'][$k];
  119. $showVal = $_POST['show'][$k];
  120. if ($showVal === 'on') $_SESSION['selectedColumn'][$v]['show'] = 1;
  121. else $_SESSION['selectedColumn'][$v]['show'] = 0;
  122. $fieldVal = $_POST['field'][$k];
  123. list($table, $field) = explode(".", $fieldVal);
  124. $_SESSION['selectedColumn'][$v][0] = $table;
  125. $_SESSION['selectedColumn'][$v][1] = $field;
  126. $_SESSION['selectedColumn'][$v]['criteria'] = $_POST['criteria'][$k];
  127. $_SESSION['selectedColumn'][$v]['or'] = $_POST['or'][$k];
  128. }
  129. }
  130. if ($_POST['addrelation']) {
  131. $a = $_POST['rLeft'];
  132. $b = $_POST['rJoin'];
  133. $c = $_POST['rRight'];
  134. $_SESSION['selectedRelation'][] = array($a, $b, $c);
  135. }
  136. if ($_POST['delrelation']) {
  137. unset($_SESSION['selectedRelation'][$_POST['idrelation']]);
  138. }
  139. if ($_POST['generate']) {
  140. $column = $where = $orderby = $table = $group = '';
  141. foreach ($_SESSION['selectedColumn'] as $k => $v) {
  142. if (!isset($arrTable[$v[0]])) $arrTable[$v[0]] = 1;
  143. $tableField = $v[0].".".$v[1];
  144. if ($v['criteria']) {
  145. if ($where) $where .= " AND ";
  146. $where .= $tableField." = '".$v['criteria']."'";
  147. }
  148. if ($v['show'] && (!$v['total'] || $v['total'] === 'Group By')) {
  149. if ($column) $column .= ",";
  150. $column .= $tableField;
  151. }
  152. if ($v['sort'] && $v[1] !== '*') {
  153. $order = "ASC";
  154. if ($v['sort'] === 'Descending') $order = "DESC";
  155. if ($orderby) $orderby .= ",";
  156. $orderby .= $tableField." ".$order;
  157. }
  158. if ($v['total']) {
  159. if ($v['total'] === 'Group By') {
  160. if ($group) $group .= ",";
  161. $group .= $tableField;
  162. } else if ($v['show']) {
  163. if ($column) $column .= ",";
  164. $column .= strtoupper($v['total'])."(".$tableField.")";
  165. }
  166. }
  167. }
  168. $countTable = count($arrTable);
  169. if ($countTable > 1) {
  170. $arrRelation = array();
  171. foreach ($_SESSION['selectedRelation'] as $k => $v) {
  172. $left = $v[0];
  173. $join = $v[1];
  174. $right = $v[2];
  175. list($leftTable, $leftField) = explode(".", $left);
  176. list($rightTable, $rightField) = explode(".", $right);
  177. $arrRelation[$leftTable][$join][$rightTable][] = array($v[0],$v[2]);
  178. }
  179. foreach ($arrRelation as $leftTable => $v) {
  180. foreach ($v as $join => $v2) {
  181. foreach ($v2 as $rightTable => $v3) {
  182. if ($table) $table .= $join." ".$rightTable;
  183. else $table .= $leftTable." ".$join." ".$rightTable;
  184. $table .= " ON (";
  185. $using = '';
  186. foreach ($v3 as $k4 => $v4) {
  187. if ($using) $using .= " AND ";
  188. $using .= $v4[0]." = ".$v4[1];
  189. }
  190. $table .= $using.")";
  191. }
  192. }
  193. }
  194. }
  195. if ($table === '') {
  196. foreach ($arrTable as $k => $v) {
  197. if ($table) $table .= ", ";
  198. $table .= $k;
  199. }
  200. }
  201. $sql = "SELECT ".$column."\n".
  202. "FROM ".$table."\n";
  203. if ($where) $sql .= "WHERE ".$where."\n";
  204. if ($group) $sql .= "GROUP BY ".$group."\n";
  205. if ($orderby) $sql .= "ORDER BY ".$orderby;
  206. $sql = addslashes($sql);
  207. echo nl2br($sql);
  208. $sql = str_replace("\n", "\\n", $sql);
  209. echo "<script language=javascript>
  210. form = opener.document.forms[0];
  211. sqlElement = null;
  212. if (form) sqlElement = form.sql;
  213. if (sqlElement) {
  214. opener.document.forms[0].sql.value = '".$sql."';
  215. }
  216. </script>";
  217. }
  218. echo "<form method=post action='".$_SERVER['PHP_SELF']."'>";
  219. $i = 0;
  220. foreach ($_SESSION['selectedTable'] as $k => $v) {
  221. $tableId = $v;
  222. $metaColumns = $adodb->MetaColumns($tableId);
  223. $sizeColumns = count($metaColumns)+1;
  224. if ($sizeColumns>10) $sizeColumns = 10;
  225. $thStyle = "style='white-space: nowrap; background-color:#0055f6;color:white'";
  226. #$selectStyle = "style='width:100%'";
  227. $selectJs = "ondblclick='addcolumn(\"".$tableId."\", this)'";
  228. $selectSize = "size=".$sizeColumns;
  229. $buttonStyle = "style='background-color:#E64C20;color:white'";
  230. $buttonJs = "onClick='deletetable(\"".$k."\")'";
  231. echo "<td valign=top>";
  232. echo "<table style='border:1px #0055f6 solid;'>".
  233. "<tr><th ".$thStyle.">".$tableId." ".
  234. " <input ".$buttonStyle." type=button value=X ".$buttonJs."></th></tr>";
  235. echo "<tr><td>";
  236. echo "<select ".$selectStyle." name=column".$i." ".$selectSize." ".$selectJs.">";
  237. echo "<option value='*'>*</option>";
  238. foreach ($metaColumns as $k => $v) {
  239. echo "<option value='".$v->name."'>".$v->name."</option>";
  240. }
  241. echo "</select>";
  242. echo "</td></tr>";
  243. echo "</table>";
  244. echo "</td>";
  245. $i++;
  246. }
  247. echo "</form>";
  248. ?>
  249. </tr></table>
  250. <hr/>
  251. <form name=fView method=post action='<?=$_SERVER['PHP_SELF']?>'>
  252. <?php
  253. $countTable = array();
  254. foreach ($_SESSION['selectedColumn'] as $k => $v) {
  255. if (!isset($countTable[$v[0]])) $countTable[$v[0]] = 1;
  256. }
  257. $countSelectedTable = count($countTable);
  258. //$countSelectedTable = count($_SESSION['selectedColumn']);
  259. if ($countSelectedTable > 1) {
  260. ?>
  261. <table>
  262. <tr>
  263. <td valign=top>
  264. <select name=rLeft>
  265. <?php
  266. foreach ($_SESSION['selectedColumn'] as $k2 => $v2) {
  267. $metaColumns = $adodb->MetaColumns($v2[0]);
  268. foreach ($metaColumns as $k3 => $v3) {
  269. $optionText = $v2[0].'.'.$v3->name;
  270. echo "<option value='".$optionText."'>".$optionText."</option>\n";
  271. }
  272. }
  273. ?>
  274. </select>
  275. </td>
  276. <td valign=top>
  277. <select name=rJoin>
  278. <option value='INNER JOIN'>INNER JOIN</option>
  279. <option value='LEFT JOIN'>LEFT JOIN</option>
  280. <option value='RIGHT JOIN'>RIGHT JOIN</option>
  281. <option value='CROSS JOIN'>CROSS JOIN</option>
  282. </select>
  283. </td>
  284. <td valign=top>
  285. <select name=rRight>
  286. <?php
  287. foreach ($_SESSION['selectedColumn'] as $k2 => $v2) {
  288. $metaColumns = $adodb->MetaColumns($v2[0]);
  289. foreach ($metaColumns as $k3 => $v3) {
  290. $optionText = $v2[0].'.'.$v3->name;
  291. echo "<option value='".$optionText."'>".$optionText."</option>\n";
  292. }
  293. }
  294. ?>
  295. </select>
  296. </td>
  297. <td>
  298. <input type=hidden name=addrelation>
  299. <input type=hidden name=delrelation>
  300. <input type=hidden name=idrelation>
  301. <input type=button value=<?=__('Add')?> onClick=addrelation(this.form)>
  302. </td>
  303. </tr>
  304. </table>
  305. <hr/>
  306. <table>
  307. <?php
  308. //</form>
  309. foreach ($_SESSION['selectedRelation'] as $k => $v) {
  310. echo "<tr>";
  311. echo "<td>".
  312. "<select name=left[]>".
  313. "<option value='".$v[0]."'>".$v[0]."</option>".
  314. "</select>".
  315. "</td>";
  316. echo "<td>".
  317. "<select name=join[]>".
  318. "<option value='".$v[1]."'>".$v[1]."</option>".
  319. "</select>".
  320. "</td>";
  321. echo "<td>".
  322. "<select name=right[]>".
  323. "<option value='".$v[2]."'>".$v[2]."</option>".
  324. "</select>".
  325. "</td>";
  326. $buttonStyle = "style='background-color:#E64C20;color:white'";
  327. $buttonJs = "onClick='deleterelation(\"".$k."\")'";
  328. echo "<td>".
  329. "<input ".$buttonStyle." type=button value=X ".$buttonJs.">".
  330. "</td>";
  331. echo "</tr>";
  332. }
  333. ?>
  334. </table>
  335. <?php
  336. }
  337. //<form method=post action='<?=$_SERVER['PHP_SELF']? >'>
  338. ?>
  339. <hr/>
  340. <table border=1>
  341. <?php
  342. $arrTd = array('', 'Field', 'Table', 'Total', 'Sort', 'Show', 'Criteria', 'or');
  343. $arrSort = array ('Ascending', 'Descending', '' => '(not sorted)');
  344. $arrTotal = array ('', 'Group By', 'Sum', 'Avg', 'Min', 'Max', 'Count');
  345. //, 'StDev', 'Var', 'First', 'Last', 'Expression', 'Where');
  346. foreach ($arrTd as $k => $v) {
  347. $tdText = '';
  348. if ($v) $tdText = __($v).":";
  349. echo "<tr>";
  350. echo "<td>".$tdText." </td>";
  351. $i = 0;
  352. foreach ($_SESSION['selectedColumn'] as $k2 => $v2) {
  353. if ($v === 'Field') {
  354. #$selectStyle = "style='width:100%'";
  355. echo "<input type=hidden name=id[] value='$k2'>";
  356. echo "<td>".
  357. "<select name=field[] ".$selectStyle.">";
  358. if (!isset($issetTable[$v2[0]])) {
  359. $metaColumns = $adodb->MetaColumns($v2[0]);
  360. $starObj->name = '*';
  361. $metaStar = array('star' => $starObj);
  362. $issetTable[$v2[0]] = array_merge($metaStar, $metaColumns);
  363. }
  364. $metaColumns = $issetTable[$v2[0]];
  365. foreach ($metaColumns as $k3 => $v3) {
  366. $optionText = $v2[0].".".$v3->name;
  367. $selected = '';
  368. if ($v2[1] === $v3->name) $selected = 'selected';
  369. echo "<option value='".$optionText."' ".$selected.">".
  370. "".$optionText."</option>";
  371. }
  372. echo "</select>".
  373. "</td>";
  374. } else if ($v === 'Table') {
  375. #$selectStyle = "style='width:100%'";
  376. echo "<td>".
  377. "<select name=table[] ".$selectStyle.">".
  378. "<option value='".$v2[0]."'>".$v2[0]."</option>".
  379. "</select>".
  380. "</td>";
  381. } else if ($v === 'Show') {
  382. if (!isset($_SESSION['selectedColumn'][$k2]['show'])) {
  383. $_SESSION['selectedColumn'][$k2]['show'] = '1';
  384. }
  385. $checked = '';
  386. if ($_SESSION['selectedColumn'][$k2]['show']) $checked = 'checked';
  387. echo "<td align=center>".
  388. "<input type=checkbox name=show[".$i."] ".$checked.">".
  389. "</td>";
  390. } else if ($v === '') {
  391. $buttonStyle = "style='background-color:#E64C20;color:white'";
  392. $buttonJs = "onClick='deletecolumn(\"".$k2."\")'";
  393. echo "<td align=center>".
  394. "<input ".$buttonStyle." type=button value=X ".$buttonJs.">".
  395. "</td>";
  396. } else if ($v === 'Sort') {
  397. if (!isset($_SESSION['selectedColumn'][$k2]['sort'])) {
  398. $_SESSION['selectedColumn'][$k2]['sort'] = '';
  399. }
  400. #$selectStyle = "style='width:100%'";
  401. echo "<td>".
  402. "<select name=sort[] ".$selectStyle.">";
  403. foreach ($arrSort as $k3 => $v3) {
  404. if (ereg('[0-9]+', $k3)) $k3 = $v3;
  405. $selected = '';
  406. if ($_SESSION['selectedColumn'][$k2]['sort'] === $k3) {
  407. $selected = 'selected';
  408. }
  409. echo "<option value='".$k3."' ".$selected.">".$v3."</option>";
  410. }
  411. echo "</select>".
  412. "</td>";
  413. } else if ($v === 'Total') {
  414. if (!isset($_SESSION['selectedColumn'][$k2]['total'])) {
  415. $_SESSION['selectedColumn'][$k2]['total'] = '';
  416. }
  417. #$selectStyle = "style='width:100%'";
  418. echo "<td>".
  419. "<select name=total[] ".$selectStyle.">";
  420. foreach ($arrTotal as $k3 => $v3) {
  421. if (ereg('[0-9]+', $k3)) $k3 = $v3;
  422. $selected = '';
  423. if ($_SESSION['selectedColumn'][$k2]['total'] === $k3) {
  424. $selected = 'selected';
  425. }
  426. echo "<option value='".$k3."' ".$selected.">".$v3."</option>";
  427. }
  428. echo "</select>".
  429. "</td>";
  430. } else if ($v === 'Criteria') {
  431. $textValue = "value='".$_SESSION['selectedColumn'][$k2]['criteria']."'";
  432. $textStyle = "style='width:100%'";
  433. echo "<td>".
  434. "<input ".$textStyle." type=text name=criteria[] ".$textValue."></td>";
  435. } else if ($v === 'or') {
  436. $textValue = "value='".$_SESSION['selectedColumn'][$k2]['or']."'";
  437. $textStyle = "style='width:100%'";
  438. echo "<td>".
  439. "<input ".$textStyle." type=text name=or[] ".$textValue."></td>";
  440. }
  441. $i++;
  442. }
  443. echo "</tr>";
  444. }
  445. $record = array();
  446. $record['serialize'] = serialize(array(
  447. 'selectedTable' => $_SESSION['selectedTable'],
  448. 'selectedColumn' => $_SESSION['selectedColumn'],
  449. 'selectedRelation' => $_SESSION['selectedRelation']
  450. ));
  451. #$rs = $adodb->Execute("SELECT * FROM qb_view ".
  452. #" WHERE id = ? ", $_SESSION['selectedView']);
  453. #$adodb->Execute($adodb->GetUpdateSQL($rs, $record, 1));
  454. $record['serialize'] = str_replace("\n", "\\n", $record['serialize']);
  455. echo "<script language=javascript>
  456. form = opener.document.forms[0];
  457. sqlElement = null;
  458. if (form) sqlElement = form.sql;
  459. if (sqlElement) {
  460. opener.document.forms[0].serialize.value = '".$record['serialize']."';
  461. }
  462. </script>";
  463. ?>
  464. </table>
  465. <input type=hidden name=deltable>
  466. <input type=hidden name=addtable>
  467. <input type=hidden name=idtable>
  468. <input type=hidden name=addcolumn>
  469. <input type=hidden name=idcolumn>
  470. <input type=hidden name=tbcolumn>
  471. <input type=hidden name=delcolumn>
  472. <input type=hidden name=action value=savedesign>
  473. <input type=hidden name=generate>
  474. <input type=button value='<?=__('Save')?>' onClick=savedesign(this.form)>
  475. <input type=button value='<?=__('Generate')?>' onClick=generatesql(this.form)>
  476. </form>
  477. </body>
  478. </html>