Translated using Weblate (Dutch)
[phpmyadmin.git] / view_create.php
blob2b865b450a12a433257dcae882236f865216a62b
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 use PMA\libraries\Core;
12 use PMA\libraries\URL;
13 use PMA\libraries\Response;
15 require_once './libraries/common.inc.php';
17 /**
18 * Runs common work
20 require './libraries/db_common.inc.php';
21 $url_params['goto'] = 'tbl_structure.php';
22 $url_params['back'] = 'view_create.php';
24 $response = Response::getInstance();
26 $view_algorithm_options = array(
27 'UNDEFINED',
28 'MERGE',
29 'TEMPTABLE',
32 $view_with_options = array(
33 'CASCADED',
34 'LOCAL'
37 $view_security_options = array(
38 'DEFINER',
39 'INVOKER'
42 if (empty($sql_query)) {
43 $sql_query = '';
46 // View name is a compulsory field
47 if (isset($_REQUEST['view']['name'])
48 && empty($_REQUEST['view']['name'])
49 ) {
50 $message = PMA\libraries\Message::error(__('View name can not be empty!'));
51 $response->addJSON(
52 'message',
53 $message
55 $response->setRequestStatus(false);
56 exit;
59 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
60 /**
61 * Creates the view
63 $sep = "\r\n";
65 if (isset($_REQUEST['createview'])) {
66 $sql_query = 'CREATE';
67 if (isset($_REQUEST['view']['or_replace'])) {
68 $sql_query .= ' OR REPLACE';
70 } else {
71 $sql_query = 'ALTER';
74 if (Core::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
75 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
78 if (! empty($_REQUEST['view']['definer'])) {
79 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
80 $sql_query .= $sep . 'DEFINER='
81 . PMA\libraries\Util::backquote($_REQUEST['view']['definer']);
82 } else {
83 $arr = explode('@', $_REQUEST['view']['definer']);
84 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
85 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
89 if (isset($_REQUEST['view']['sql_security'])) {
90 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
91 $sql_query .= $sep . ' SQL SECURITY '
92 . $_REQUEST['view']['sql_security'];
96 $sql_query .= $sep . ' VIEW '
97 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
99 if (! empty($_REQUEST['view']['column_names'])) {
100 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
103 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
105 if (isset($_REQUEST['view']['with'])) {
106 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
107 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
108 . ' CHECK OPTION';
112 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
113 if (! isset($_REQUEST['ajax_dialog'])) {
114 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
115 return;
118 $response->addJSON(
119 'message',
120 PMA\libraries\Message::error(
121 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
122 . $GLOBALS['dbi']->getError()
125 $response->setRequestStatus(false);
126 exit;
129 // If different column names defined for VIEW
130 $view_columns = array();
131 if (isset($_REQUEST['view']['column_names'])) {
132 $view_columns = explode(',', $_REQUEST['view']['column_names']);
135 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
136 $_REQUEST['view']['as'], $view_columns
139 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
140 $pma_transformation_data = $systemDb->getExistingTransformationData(
141 $GLOBALS['db']
144 if ($pma_transformation_data !== false) {
146 // SQL for store new transformation details of VIEW
147 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
148 $pma_transformation_data, $column_map,
149 $_REQUEST['view']['name'], $GLOBALS['db']
152 // Store new transformations
153 if ($new_transformations_sql != '') {
154 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
158 unset($pma_transformation_data);
160 if (! isset($_REQUEST['ajax_dialog'])) {
161 $message = PMA\libraries\Message::success();
162 include 'tbl_structure.php';
163 } else {
164 $response->addJSON(
165 'message',
166 PMA\libraries\Util::getMessage(
167 PMA\libraries\Message::success(),
168 $sql_query
171 $response->setRequestStatus(true);
174 exit;
177 // prefill values if not already filled from former submission
178 $view = array(
179 'operation' => 'create',
180 'or_replace' => '',
181 'algorithm' => '',
182 'definer' => '',
183 'sql_security' => '',
184 'name' => '',
185 'column_names' => '',
186 'as' => $sql_query,
187 'with' => '',
190 if (Core::isValid($_REQUEST['view'], 'array')) {
191 $view = array_merge($view, $_REQUEST['view']);
194 $url_params['db'] = $GLOBALS['db'];
195 $url_params['reload'] = 1;
198 * Displays the page
200 $htmlString = '<!-- CREATE VIEW options -->'
201 . '<div id="div_view_options">'
202 . '<form method="post" action="view_create.php">'
203 . URL::getHiddenInputs($url_params)
204 . '<fieldset>'
205 . '<legend>'
206 . (isset($_REQUEST['ajax_dialog']) ?
207 __('Details') :
208 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
210 . '</legend>'
211 . '<table class="rte_table">';
213 if ($view['operation'] == 'create') {
214 $htmlString .= '<tr>'
215 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
216 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
217 if ($view['or_replace']) {
218 $htmlString .= ' checked="checked"';
220 $htmlString .= ' value="1" /></td></tr>';
223 $htmlString .= '<tr>'
224 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
225 . '<td><select name="view[algorithm]" id="algorithm">';
226 foreach ($view_algorithm_options as $option) {
227 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
228 if ($view['algorithm'] === $option) {
229 $htmlString .= ' selected="selected"';
231 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
233 $htmlString .= '</select>'
234 . '</td></tr>';
236 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
237 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
238 . ' value="' . htmlspecialchars($view['definer']) . '" />'
239 . '</td></tr>';
241 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
242 . '<td><select name="view[sql_security]">'
243 . '<option value=""></option>';
244 foreach ($view_security_options as $option) {
245 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
246 if ($option == $view['sql_security']) {
247 $htmlString .= ' selected="selected"';
249 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
251 $htmlString .= '<select>'
252 . '</td></tr>';
254 if ($view['operation'] == 'create') {
255 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
256 . '<td><input type="text" size="20" name="view[name]"'
257 . ' onfocus="this.select()" maxlength="64"'
258 . ' value="' . htmlspecialchars($view['name']) . '" />'
259 . '</td></tr>';
260 } else {
261 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
262 . ' value="' . htmlspecialchars($view['name']) . '" />'
263 . '</td></tr>';
266 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
267 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
268 . ' onfocus="this.select()"'
269 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
270 . '</td></tr>';
272 $htmlString .= '<tr><td class="nowrap">AS</td>'
273 . '<td>'
274 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
275 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
276 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
278 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
279 . '</td></tr>';
281 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
282 . '<td><select name="view[with]">'
283 . '<option value=""></option>';
284 foreach ($view_with_options as $option) {
285 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
286 if ($option == $view['with']) {
287 $htmlString .= ' selected="selected"';
289 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
291 $htmlString .= '<select>'
292 . '</td></tr>';
294 $htmlString .= '</table>'
295 . '</fieldset>';
297 if (! isset($_REQUEST['ajax_dialog'])) {
298 $htmlString .= '<fieldset class="tblFooters">'
299 . '<input type="hidden" name="'
300 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
301 . '" value="1" />'
302 . '<input type="submit" name="" value="' . __('Go') . '" />'
303 . '</fieldset>';
304 } else {
305 $htmlString .= '<input type="hidden" name="'
306 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
307 . '" value="1" />'
308 . '<input type="hidden" name="ajax_dialog" value="1" />'
309 . '<input type="hidden" name="ajax_request" value="1" />';
312 $htmlString .= '</form>'
313 . '</div>';
315 echo $htmlString;