view_create.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles creation of VIEWs
  5. *
  6. * @todo js error when view name is empty (strFormEmpty)
  7. * @todo (also validate if js is disabled, after form submission?)
  8. * @package PhpMyAdmin
  9. */
  10. /**
  11. *
  12. */
  13. require_once './libraries/common.inc.php';
  14. /**
  15. * Runs common work
  16. */
  17. require './libraries/db_common.inc.php';
  18. $url_params['goto'] = $cfg['DefaultTabDatabase'];
  19. $url_params['back'] = 'view_create.php';
  20. $view_algorithm_options = array(
  21. 'UNDEFINED',
  22. 'MERGE',
  23. 'TEMPTABLE',
  24. );
  25. $view_with_options = array(
  26. 'CASCADED CHECK OPTION',
  27. 'LOCAL CHECK OPTION'
  28. );
  29. if (isset($_REQUEST['createview'])) {
  30. /**
  31. * Creates the view
  32. */
  33. $sep = "\r\n";
  34. $sql_query = 'CREATE';
  35. if (isset($_REQUEST['view']['or_replace'])) {
  36. $sql_query .= ' OR REPLACE';
  37. }
  38. if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
  39. $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
  40. }
  41. $sql_query .= $sep . ' VIEW ' . PMA_Util::backquote($_REQUEST['view']['name']);
  42. if (! empty($_REQUEST['view']['column_names'])) {
  43. $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
  44. }
  45. $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
  46. if (isset($_REQUEST['view']['with'])) {
  47. $options = array_intersect($_REQUEST['view']['with'], $view_with_options);
  48. if (count($options)) {
  49. $sql_query .= $sep . ' WITH ' . implode(' ', $options);
  50. }
  51. }
  52. if (PMA_DBI_try_query($sql_query)) {
  53. include_once './libraries/tbl_views.lib.php';
  54. // If different column names defined for VIEW
  55. $view_columns = array();
  56. if (isset($_REQUEST['view']['column_names'])) {
  57. $view_columns = explode(',', $_REQUEST['view']['column_names']);
  58. }
  59. $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
  60. $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
  61. if ($pma_tranformation_data !== false) {
  62. // SQL for store new transformation details of VIEW
  63. $new_transformations_sql = PMA_getNewTransformationDataSql(
  64. $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
  65. $GLOBALS['db']
  66. );
  67. // Store new transformations
  68. if ($new_transformations_sql != '') {
  69. PMA_DBI_try_query($new_transformations_sql);
  70. }
  71. }
  72. unset($pma_tranformation_data);
  73. if ($GLOBALS['is_ajax_request'] != true) {
  74. $message = PMA_Message::success();
  75. include './' . $cfg['DefaultTabDatabase'];
  76. } else {
  77. $response = PMA_Response::getInstance();
  78. $response->addJSON(
  79. 'message',
  80. PMA_Util::getMessage(
  81. PMA_Message::success(), $sql_query
  82. )
  83. );
  84. }
  85. exit;
  86. } else {
  87. if ($GLOBALS['is_ajax_request'] != true) {
  88. $message = PMA_Message::rawError(PMA_DBI_getError());
  89. } else {
  90. $response = PMA_Response::getInstance();
  91. $response->addJSON(
  92. 'message',
  93. PMA_Message::error(
  94. "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
  95. . PMA_DBI_getError()
  96. )
  97. );
  98. $response->isSuccess(false);
  99. exit;
  100. }
  101. }
  102. }
  103. // prefill values if not already filled from former submission
  104. $view = array(
  105. 'or_replace' => '',
  106. 'algorithm' => '',
  107. 'name' => '',
  108. 'column_names' => '',
  109. 'as' => $sql_query,
  110. 'with' => array(),
  111. );
  112. if (PMA_isValid($_REQUEST['view'], 'array')) {
  113. $view = array_merge($view, $_REQUEST['view']);
  114. }
  115. $url_params['db'] = $GLOBALS['db'];
  116. $url_params['reload'] = 1;
  117. /**
  118. * Displays the page
  119. */
  120. $htmlString = '<!-- CREATE VIEW options -->'
  121. . '<div id="div_view_options">'
  122. . '<form method="post" action="view_create.php">'
  123. . PMA_generate_common_hidden_inputs($url_params)
  124. . '<fieldset>'
  125. . '<legend>' . __('Create view')
  126. . PMA_Util::showMySQLDocu('SQL-Syntax', 'CREATE_VIEW') . '</legend>'
  127. . '<table class="rte_table">'
  128. . '<tr><td><label for="or_replace">OR REPLACE</label></td>'
  129. . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
  130. if ($view['or_replace']) {
  131. $htmlString .= ' checked="checked"';
  132. }
  133. $htmlString .= ' value="1" />'
  134. . '</td>'
  135. . '</tr>'
  136. . '<tr>'
  137. . '<td><label for="algorithm">ALGORITHM</label></td>'
  138. . '<td><select name="view[algorithm]" id="algorithm">';
  139. foreach ($view_algorithm_options as $option) {
  140. $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
  141. if ($view['algorithm'] === $option) {
  142. $htmlString .= ' selected="selected"';
  143. }
  144. $htmlString .= '>' . htmlspecialchars($option) . '</option>';
  145. }
  146. $htmlString .= '</select>'
  147. . '</td>'
  148. . '</tr>'
  149. . '<tr><td>' . __('VIEW name') . '</td>'
  150. . '<td><input type="text" size="20" name="view[name]" onfocus="this.select()"'
  151. . ' value="' . htmlspecialchars($view['name']) . '" />'
  152. . '</td>'
  153. . '</tr>'
  154. . '<tr><td>' . __('Column names') . '</td>'
  155. . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
  156. . ' onfocus="this.select()"'
  157. . ' value="' . htmlspecialchars($view['column_names']) . '" />'
  158. . '</td>'
  159. . '</tr>'
  160. . '<tr><td>AS</td>'
  161. . '<td>'
  162. . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
  163. . ' cols="' . $cfg['TextareaCols'] . '"'
  164. . ' dir="' . $text_dir . '"';
  165. if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
  166. $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
  167. }
  168. $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
  169. . '</td>'
  170. . '</tr>'
  171. . '<tr><td>WITH</td>'
  172. . '<td>';
  173. foreach ($view_with_options as $option) {
  174. $htmlString .= '<input type="checkbox" name="view[with][]"';
  175. if (in_array($option, $view['with'])) {
  176. $htmlString .= ' checked="checked"';
  177. }
  178. $htmlString .= ' id="view_with_'
  179. . str_replace(' ', '_', htmlspecialchars($option)) . '"'
  180. . ' value="' . htmlspecialchars($option) . '" />'
  181. . '<label for="view_with_' . str_replace(' ', '_', htmlspecialchars($option))
  182. . '">&nbsp;'
  183. . htmlspecialchars($option) . '</label><br />';
  184. }
  185. $htmlString .= '</td>'
  186. . '</tr>'
  187. . '</table>'
  188. . '</fieldset>';
  189. if ($GLOBALS['is_ajax_request'] != true) {
  190. $htmlString .= '<fieldset class="tblFooters">'
  191. . '<input type="submit" name="createview" value="' . __('Go') . '" />'
  192. . '</fieldset>';
  193. } else {
  194. $htmlString .= '<input type="hidden" name="createview" value="1" />'
  195. . '<input type="hidden" name="ajax_request" value="1" />';
  196. }
  197. $htmlString .= '</form>'
  198. . '</div>';
  199. echo $htmlString;