Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / view_create.php
blob6d478e768c10df07a7b6c1d1059761346f0ae464
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 */
10 use PMA\libraries\URL;
12 require_once './libraries/common.inc.php';
14 /**
15 * Runs common work
17 require './libraries/db_common.inc.php';
18 $url_params['goto'] = 'tbl_structure.php';
19 $url_params['back'] = 'view_create.php';
21 $view_algorithm_options = array(
22 'UNDEFINED',
23 'MERGE',
24 'TEMPTABLE',
27 $view_with_options = array(
28 'CASCADED',
29 'LOCAL'
32 $view_security_options = array(
33 'DEFINER',
34 'INVOKER'
37 if (empty($sql_query)) {
38 $sql_query = '';
41 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
42 /**
43 * Creates the view
45 $sep = "\r\n";
47 if (isset($_REQUEST['createview'])) {
48 $sql_query = 'CREATE';
49 if (isset($_REQUEST['view']['or_replace'])) {
50 $sql_query .= ' OR REPLACE';
52 } else {
53 $sql_query = 'ALTER';
56 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
57 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
60 if (! empty($_REQUEST['view']['definer'])) {
61 $arr = explode('@', $_REQUEST['view']['definer']);
62 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
63 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
66 if (isset($_REQUEST['view']['sql_security'])) {
67 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
68 $sql_query .= $sep . ' SQL SECURITY '
69 . $_REQUEST['view']['sql_security'];
73 $sql_query .= $sep . ' VIEW '
74 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
76 if (! empty($_REQUEST['view']['column_names'])) {
77 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
80 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
82 if (isset($_REQUEST['view']['with'])) {
83 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
84 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
85 . ' CHECK OPTION';
89 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
90 if (! isset($_REQUEST['ajax_dialog'])) {
91 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
92 return;
95 $response = PMA\libraries\Response::getInstance();
96 $response->addJSON(
97 'message',
98 PMA\libraries\Message::error(
99 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
100 . $GLOBALS['dbi']->getError()
103 $response->setRequestStatus(false);
104 exit;
107 // If different column names defined for VIEW
108 $view_columns = array();
109 if (isset($_REQUEST['view']['column_names'])) {
110 $view_columns = explode(',', $_REQUEST['view']['column_names']);
113 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
114 $_REQUEST['view']['as'], $view_columns
117 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
118 $pma_transformation_data = $systemDb->getExistingTransformationData(
119 $GLOBALS['db']
122 if ($pma_transformation_data !== false) {
124 // SQL for store new transformation details of VIEW
125 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
126 $pma_transformation_data, $column_map,
127 $_REQUEST['view']['name'], $GLOBALS['db']
130 // Store new transformations
131 if ($new_transformations_sql != '') {
132 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
136 unset($pma_transformation_data);
138 if (! isset($_REQUEST['ajax_dialog'])) {
139 $message = PMA\libraries\Message::success();
140 include 'tbl_structure.php';
141 } else {
142 $response = PMA\libraries\Response::getInstance();
143 $response->addJSON(
144 'message',
145 PMA\libraries\Util::getMessage(
146 PMA\libraries\Message::success(),
147 $sql_query
150 $response->setRequestStatus(true);
153 exit;
156 // prefill values if not already filled from former submission
157 $view = array(
158 'operation' => 'create',
159 'or_replace' => '',
160 'algorithm' => '',
161 'definer' => '',
162 'sql_security' => '',
163 'name' => '',
164 'column_names' => '',
165 'as' => $sql_query,
166 'with' => '',
169 if (PMA_isValid($_REQUEST['view'], 'array')) {
170 $view = array_merge($view, $_REQUEST['view']);
173 $url_params['db'] = $GLOBALS['db'];
174 $url_params['reload'] = 1;
177 * Displays the page
179 $htmlString = '<!-- CREATE VIEW options -->'
180 . '<div id="div_view_options">'
181 . '<form method="post" action="view_create.php">'
182 . URL::getHiddenInputs($url_params)
183 . '<fieldset>'
184 . '<legend>'
185 . (isset($_REQUEST['ajax_dialog']) ?
186 __('Details') :
187 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
189 . '</legend>'
190 . '<table class="rte_table">';
192 if ($view['operation'] == 'create') {
193 $htmlString .= '<tr>'
194 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
195 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
196 if ($view['or_replace']) {
197 $htmlString .= ' checked="checked"';
199 $htmlString .= ' value="1" /></td></tr>';
202 $htmlString .= '<tr>'
203 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
204 . '<td><select name="view[algorithm]" id="algorithm">';
205 foreach ($view_algorithm_options as $option) {
206 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
207 if ($view['algorithm'] === $option) {
208 $htmlString .= ' selected="selected"';
210 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
212 $htmlString .= '</select>'
213 . '</td></tr>';
215 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
216 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
217 . ' value="' . htmlspecialchars($view['definer']) . '" />'
218 . '</td></tr>';
220 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
221 . '<td><select name="view[sql_security]">'
222 . '<option value=""></option>';
223 foreach ($view_security_options as $option) {
224 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
225 if ($option == $view['sql_security']) {
226 $htmlString .= ' selected="selected"';
228 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
230 $htmlString .= '<select>'
231 . '</td></tr>';
233 if ($view['operation'] == 'create') {
234 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
235 . '<td><input type="text" size="20" name="view[name]"'
236 . ' onfocus="this.select()" maxlength="64"'
237 . ' value="' . htmlspecialchars($view['name']) . '" />'
238 . '</td></tr>';
239 } else {
240 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
241 . ' value="' . htmlspecialchars($view['name']) . '" />'
242 . '</td></tr>';
245 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
246 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
247 . ' onfocus="this.select()"'
248 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
249 . '</td></tr>';
251 $htmlString .= '<tr><td class="nowrap">AS</td>'
252 . '<td>'
253 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
254 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
255 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
257 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
258 . '</td></tr>';
260 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
261 . '<td><select name="view[with]">'
262 . '<option value=""></option>';
263 foreach ($view_with_options as $option) {
264 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
265 if ($option == $view['with']) {
266 $htmlString .= ' selected="selected"';
268 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
270 $htmlString .= '<select>'
271 . '</td></tr>';
273 $htmlString .= '</table>'
274 . '</fieldset>';
276 if (! isset($_REQUEST['ajax_dialog'])) {
277 $htmlString .= '<fieldset class="tblFooters">'
278 . '<input type="hidden" name="'
279 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
280 . '" value="1" />'
281 . '<input type="submit" name="" value="' . __('Go') . '" />'
282 . '</fieldset>';
283 } else {
284 $htmlString .= '<input type="hidden" name="'
285 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
286 . '" value="1" />'
287 . '<input type="hidden" name="ajax_dialog" value="1" />'
288 . '<input type="hidden" name="ajax_request" value="1" />';
291 $htmlString .= '</form>'
292 . '</div>';
294 echo $htmlString;