Merge remote-tracking branch 'origin/QA_4_0' into QA_4_0
[phpmyadmin.git] / view_create.php
blobcd2ec3b616e6b5455df67628e955971b4de5b0a4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles creation of VIEWs
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 */
11 /**
14 require_once './libraries/common.inc.php';
16 /**
17 * Runs common work
19 require './libraries/db_common.inc.php';
20 $url_params['goto'] = $cfg['DefaultTabDatabase'];
21 $url_params['back'] = 'view_create.php';
23 $view_algorithm_options = array(
24 'UNDEFINED',
25 'MERGE',
26 'TEMPTABLE',
29 $view_with_options = array(
30 'CASCADED CHECK OPTION',
31 'LOCAL CHECK OPTION'
34 if (isset($_REQUEST['createview'])) {
35 /**
36 * Creates the view
38 $sep = "\r\n";
40 $sql_query = 'CREATE';
42 if (isset($_REQUEST['view']['or_replace'])) {
43 $sql_query .= ' OR REPLACE';
46 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
47 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
50 $sql_query .= $sep . ' VIEW ' . PMA_Util::backquote($_REQUEST['view']['name']);
52 if (! empty($_REQUEST['view']['column_names'])) {
53 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
56 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
58 if (isset($_REQUEST['view']['with'])) {
59 $options = array_intersect($_REQUEST['view']['with'], $view_with_options);
60 if (count($options)) {
61 $sql_query .= $sep . ' WITH ' . implode(' ', $options);
65 if (PMA_DBI_try_query($sql_query)) {
67 include_once './libraries/tbl_views.lib.php';
69 // If different column names defined for VIEW
70 $view_columns = array();
71 if (isset($_REQUEST['view']['column_names'])) {
72 $view_columns = explode(',', $_REQUEST['view']['column_names']);
75 $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
76 $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
78 if ($pma_tranformation_data !== false) {
80 // SQL for store new transformation details of VIEW
81 $new_transformations_sql = PMA_getNewTransformationDataSql(
82 $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
83 $GLOBALS['db']
84 );
86 // Store new transformations
87 if ($new_transformations_sql != '') {
88 PMA_DBI_try_query($new_transformations_sql);
92 unset($pma_tranformation_data);
94 if ($GLOBALS['is_ajax_request'] != true) {
95 $message = PMA_Message::success();
96 include './' . $cfg['DefaultTabDatabase'];
97 } else {
98 $response = PMA_Response::getInstance();
99 $response->addJSON(
100 'message',
101 PMA_Util::getMessage(
102 PMA_Message::success(), $sql_query
107 exit;
109 } else {
110 if ($GLOBALS['is_ajax_request'] != true) {
111 $message = PMA_Message::rawError(PMA_DBI_getError());
112 } else {
113 $response = PMA_Response::getInstance();
114 $response->addJSON(
115 'message',
116 PMA_Message::error(
117 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
118 . PMA_DBI_getError()
121 $response->isSuccess(false);
122 exit;
127 // prefill values if not already filled from former submission
128 $view = array(
129 'or_replace' => '',
130 'algorithm' => '',
131 'name' => '',
132 'column_names' => '',
133 'as' => $sql_query,
134 'with' => array(),
137 if (PMA_isValid($_REQUEST['view'], 'array')) {
138 $view = array_merge($view, $_REQUEST['view']);
141 $url_params['db'] = $GLOBALS['db'];
142 $url_params['reload'] = 1;
145 * Displays the page
147 $htmlString = '<!-- CREATE VIEW options -->'
148 . '<div id="div_view_options">'
149 . '<form method="post" action="view_create.php">'
150 . PMA_generate_common_hidden_inputs($url_params)
151 . '<fieldset>'
152 . '<legend>' . __('Create view')
153 . PMA_Util::showMySQLDocu('SQL-Syntax', 'CREATE_VIEW') . '</legend>'
154 . '<table class="rte_table">'
155 . '<tr><td><label for="or_replace">OR REPLACE</label></td>'
156 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
157 if ($view['or_replace']) {
158 $htmlString .= ' checked="checked"';
160 $htmlString .= ' value="1" />'
161 . '</td>'
162 . '</tr>'
163 . '<tr>'
164 . '<td><label for="algorithm">ALGORITHM</label></td>'
165 . '<td><select name="view[algorithm]" id="algorithm">';
167 foreach ($view_algorithm_options as $option) {
168 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
169 if ($view['algorithm'] === $option) {
170 $htmlString .= ' selected="selected"';
172 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
175 $htmlString .= '</select>'
176 . '</td>'
177 . '</tr>'
178 . '<tr><td>' . __('VIEW name') . '</td>'
179 . '<td><input type="text" size="20" name="view[name]" onfocus="this.select()"'
180 . ' value="' . htmlspecialchars($view['name']) . '" />'
181 . '</td>'
182 . '</tr>'
183 . '<tr><td>' . __('Column names') . '</td>'
184 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
185 . ' onfocus="this.select()"'
186 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
187 . '</td>'
188 . '</tr>'
189 . '<tr><td>AS</td>'
190 . '<td>'
191 . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
192 . ' cols="' . $cfg['TextareaCols'] . '"'
193 . ' dir="' . $text_dir . '"';
195 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
196 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
199 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
200 . '</td>'
201 . '</tr>'
202 . '<tr><td>WITH</td>'
203 . '<td>';
205 foreach ($view_with_options as $option) {
206 $htmlString .= '<input type="checkbox" name="view[with][]"';
207 if (in_array($option, $view['with'])) {
208 $htmlString .= ' checked="checked"';
210 $htmlString .= ' id="view_with_'
211 . str_replace(' ', '_', htmlspecialchars($option)) . '"'
212 . ' value="' . htmlspecialchars($option) . '" />'
213 . '<label for="view_with_' . str_replace(' ', '_', htmlspecialchars($option))
214 . '">&nbsp;'
215 . htmlspecialchars($option) . '</label><br />';
218 $htmlString .= '</td>'
219 . '</tr>'
220 . '</table>'
221 . '</fieldset>';
223 if ($GLOBALS['is_ajax_request'] != true) {
224 $htmlString .= '<fieldset class="tblFooters">'
225 . '<input type="submit" name="createview" value="' . __('Go') . '" />'
226 . '</fieldset>';
227 } else {
228 $htmlString .= '<input type="hidden" name="createview" value="1" />'
229 . '<input type="hidden" name="ajax_request" value="1" />';
232 $htmlString .= '</form>'
233 . '</div>';
235 echo $htmlString;