designview2.php 16 KB

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