Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / view_create.php
blobe16f749367dd088857253ad86bfd979478d6fe4d
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 // View name is a compulsory field
42 if (isset($_REQUEST['view']['name'])
43 && empty($_REQUEST['view']['name'])
44 ) {
45 $message = PMA\libraries\Message::error(__('View name can not be empty'));
46 $response = PMA\libraries\Response::getInstance();
47 $response->addJSON(
48 'message',
49 $message
51 $response->setRequestStatus(false);
52 exit;
55 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
56 /**
57 * Creates the view
59 $sep = "\r\n";
61 if (isset($_REQUEST['createview'])) {
62 $sql_query = 'CREATE';
63 if (isset($_REQUEST['view']['or_replace'])) {
64 $sql_query .= ' OR REPLACE';
66 } else {
67 $sql_query = 'ALTER';
70 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
71 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
74 if (! empty($_REQUEST['view']['definer'])) {
75 $arr = explode('@', $_REQUEST['view']['definer']);
76 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
77 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
80 if (isset($_REQUEST['view']['sql_security'])) {
81 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
82 $sql_query .= $sep . ' SQL SECURITY '
83 . $_REQUEST['view']['sql_security'];
87 $sql_query .= $sep . ' VIEW '
88 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
90 if (! empty($_REQUEST['view']['column_names'])) {
91 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
94 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
96 if (isset($_REQUEST['view']['with'])) {
97 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
98 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
99 . ' CHECK OPTION';
103 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
104 if (! isset($_REQUEST['ajax_dialog'])) {
105 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
106 return;
109 $response = PMA\libraries\Response::getInstance();
110 $response->addJSON(
111 'message',
112 PMA\libraries\Message::error(
113 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
114 . $GLOBALS['dbi']->getError()
117 $response->setRequestStatus(false);
118 exit;
121 // If different column names defined for VIEW
122 $view_columns = array();
123 if (isset($_REQUEST['view']['column_names'])) {
124 $view_columns = explode(',', $_REQUEST['view']['column_names']);
127 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
128 $_REQUEST['view']['as'], $view_columns
131 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
132 $pma_transformation_data = $systemDb->getExistingTransformationData(
133 $GLOBALS['db']
136 if ($pma_transformation_data !== false) {
138 // SQL for store new transformation details of VIEW
139 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
140 $pma_transformation_data, $column_map,
141 $_REQUEST['view']['name'], $GLOBALS['db']
144 // Store new transformations
145 if ($new_transformations_sql != '') {
146 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
150 unset($pma_transformation_data);
152 if (! isset($_REQUEST['ajax_dialog'])) {
153 $message = PMA\libraries\Message::success();
154 include 'tbl_structure.php';
155 } else {
156 $response = PMA\libraries\Response::getInstance();
157 $response->addJSON(
158 'message',
159 PMA\libraries\Util::getMessage(
160 PMA\libraries\Message::success(),
161 $sql_query
164 $response->setRequestStatus(true);
167 exit;
170 // prefill values if not already filled from former submission
171 $view = array(
172 'operation' => 'create',
173 'or_replace' => '',
174 'algorithm' => '',
175 'definer' => '',
176 'sql_security' => '',
177 'name' => '',
178 'column_names' => '',
179 'as' => $sql_query,
180 'with' => '',
183 if (PMA_isValid($_REQUEST['view'], 'array')) {
184 $view = array_merge($view, $_REQUEST['view']);
187 $url_params['db'] = $GLOBALS['db'];
188 $url_params['reload'] = 1;
191 * Displays the page
193 $htmlString = '<!-- CREATE VIEW options -->'
194 . '<div id="div_view_options">'
195 . '<form method="post" action="view_create.php">'
196 . URL::getHiddenInputs($url_params)
197 . '<fieldset>'
198 . '<legend>'
199 . (isset($_REQUEST['ajax_dialog']) ?
200 __('Details') :
201 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
203 . '</legend>'
204 . '<table class="rte_table">';
206 if ($view['operation'] == 'create') {
207 $htmlString .= '<tr>'
208 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
209 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
210 if ($view['or_replace']) {
211 $htmlString .= ' checked="checked"';
213 $htmlString .= ' value="1" /></td></tr>';
216 $htmlString .= '<tr>'
217 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
218 . '<td><select name="view[algorithm]" id="algorithm">';
219 foreach ($view_algorithm_options as $option) {
220 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
221 if ($view['algorithm'] === $option) {
222 $htmlString .= ' selected="selected"';
224 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
226 $htmlString .= '</select>'
227 . '</td></tr>';
229 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
230 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
231 . ' value="' . htmlspecialchars($view['definer']) . '" />'
232 . '</td></tr>';
234 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
235 . '<td><select name="view[sql_security]">'
236 . '<option value=""></option>';
237 foreach ($view_security_options as $option) {
238 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
239 if ($option == $view['sql_security']) {
240 $htmlString .= ' selected="selected"';
242 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
244 $htmlString .= '<select>'
245 . '</td></tr>';
247 if ($view['operation'] == 'create') {
248 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
249 . '<td><input type="text" size="20" name="view[name]"'
250 . ' onfocus="this.select()" maxlength="64"'
251 . ' value="' . htmlspecialchars($view['name']) . '" />'
252 . '</td></tr>';
253 } else {
254 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
255 . ' value="' . htmlspecialchars($view['name']) . '" />'
256 . '</td></tr>';
259 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
260 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
261 . ' onfocus="this.select()"'
262 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
263 . '</td></tr>';
265 $htmlString .= '<tr><td class="nowrap">AS</td>'
266 . '<td>'
267 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
268 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
269 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
271 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
272 . '</td></tr>';
274 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
275 . '<td><select name="view[with]">'
276 . '<option value=""></option>';
277 foreach ($view_with_options as $option) {
278 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
279 if ($option == $view['with']) {
280 $htmlString .= ' selected="selected"';
282 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
284 $htmlString .= '<select>'
285 . '</td></tr>';
287 $htmlString .= '</table>'
288 . '</fieldset>';
290 if (! isset($_REQUEST['ajax_dialog'])) {
291 $htmlString .= '<fieldset class="tblFooters">'
292 . '<input type="hidden" name="'
293 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
294 . '" value="1" />'
295 . '<input type="submit" name="" value="' . __('Go') . '" />'
296 . '</fieldset>';
297 } else {
298 $htmlString .= '<input type="hidden" name="'
299 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
300 . '" value="1" />'
301 . '<input type="hidden" name="ajax_dialog" value="1" />'
302 . '<input type="hidden" name="ajax_request" value="1" />';
305 $htmlString .= '</form>'
306 . '</div>';
308 echo $htmlString;