Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blobfaeff038a96a554b3f79686975a21da9d2d50218
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 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
76 $sql_query .= $sep . 'DEFINER='
77 . PMA\libraries\Util::backquote($_REQUEST['view']['definer']);
78 } else {
79 $arr = explode('@', $_REQUEST['view']['definer']);
80 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
81 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
85 if (isset($_REQUEST['view']['sql_security'])) {
86 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
87 $sql_query .= $sep . ' SQL SECURITY '
88 . $_REQUEST['view']['sql_security'];
92 $sql_query .= $sep . ' VIEW '
93 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
95 if (! empty($_REQUEST['view']['column_names'])) {
96 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
99 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
101 if (isset($_REQUEST['view']['with'])) {
102 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
103 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
104 . ' CHECK OPTION';
108 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
109 if (! isset($_REQUEST['ajax_dialog'])) {
110 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
111 return;
114 $response = PMA\libraries\Response::getInstance();
115 $response->addJSON(
116 'message',
117 PMA\libraries\Message::error(
118 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
119 . $GLOBALS['dbi']->getError()
122 $response->setRequestStatus(false);
123 exit;
126 // If different column names defined for VIEW
127 $view_columns = array();
128 if (isset($_REQUEST['view']['column_names'])) {
129 $view_columns = explode(',', $_REQUEST['view']['column_names']);
132 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
133 $_REQUEST['view']['as'], $view_columns
136 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
137 $pma_transformation_data = $systemDb->getExistingTransformationData(
138 $GLOBALS['db']
141 if ($pma_transformation_data !== false) {
143 // SQL for store new transformation details of VIEW
144 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
145 $pma_transformation_data, $column_map,
146 $_REQUEST['view']['name'], $GLOBALS['db']
149 // Store new transformations
150 if ($new_transformations_sql != '') {
151 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
155 unset($pma_transformation_data);
157 if (! isset($_REQUEST['ajax_dialog'])) {
158 $message = PMA\libraries\Message::success();
159 include 'tbl_structure.php';
160 } else {
161 $response = PMA\libraries\Response::getInstance();
162 $response->addJSON(
163 'message',
164 PMA\libraries\Util::getMessage(
165 PMA\libraries\Message::success(),
166 $sql_query
169 $response->setRequestStatus(true);
172 exit;
175 // prefill values if not already filled from former submission
176 $view = array(
177 'operation' => 'create',
178 'or_replace' => '',
179 'algorithm' => '',
180 'definer' => '',
181 'sql_security' => '',
182 'name' => '',
183 'column_names' => '',
184 'as' => $sql_query,
185 'with' => '',
188 if (PMA_isValid($_REQUEST['view'], 'array')) {
189 $view = array_merge($view, $_REQUEST['view']);
192 $url_params['db'] = $GLOBALS['db'];
193 $url_params['reload'] = 1;
196 * Displays the page
198 $htmlString = '<!-- CREATE VIEW options -->'
199 . '<div id="div_view_options">'
200 . '<form method="post" action="view_create.php">'
201 . URL::getHiddenInputs($url_params)
202 . '<fieldset>'
203 . '<legend>'
204 . (isset($_REQUEST['ajax_dialog']) ?
205 __('Details') :
206 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
208 . '</legend>'
209 . '<table class="rte_table">';
211 if ($view['operation'] == 'create') {
212 $htmlString .= '<tr>'
213 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
214 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
215 if ($view['or_replace']) {
216 $htmlString .= ' checked="checked"';
218 $htmlString .= ' value="1" /></td></tr>';
221 $htmlString .= '<tr>'
222 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
223 . '<td><select name="view[algorithm]" id="algorithm">';
224 foreach ($view_algorithm_options as $option) {
225 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
226 if ($view['algorithm'] === $option) {
227 $htmlString .= ' selected="selected"';
229 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
231 $htmlString .= '</select>'
232 . '</td></tr>';
234 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
235 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
236 . ' value="' . htmlspecialchars($view['definer']) . '" />'
237 . '</td></tr>';
239 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
240 . '<td><select name="view[sql_security]">'
241 . '<option value=""></option>';
242 foreach ($view_security_options as $option) {
243 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
244 if ($option == $view['sql_security']) {
245 $htmlString .= ' selected="selected"';
247 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
249 $htmlString .= '<select>'
250 . '</td></tr>';
252 if ($view['operation'] == 'create') {
253 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
254 . '<td><input type="text" size="20" name="view[name]"'
255 . ' onfocus="this.select()" maxlength="64"'
256 . ' value="' . htmlspecialchars($view['name']) . '" />'
257 . '</td></tr>';
258 } else {
259 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
260 . ' value="' . htmlspecialchars($view['name']) . '" />'
261 . '</td></tr>';
264 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
265 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
266 . ' onfocus="this.select()"'
267 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
268 . '</td></tr>';
270 $htmlString .= '<tr><td class="nowrap">AS</td>'
271 . '<td>'
272 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
273 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
274 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
276 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
277 . '</td></tr>';
279 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
280 . '<td><select name="view[with]">'
281 . '<option value=""></option>';
282 foreach ($view_with_options as $option) {
283 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
284 if ($option == $view['with']) {
285 $htmlString .= ' selected="selected"';
287 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
289 $htmlString .= '<select>'
290 . '</td></tr>';
292 $htmlString .= '</table>'
293 . '</fieldset>';
295 if (! isset($_REQUEST['ajax_dialog'])) {
296 $htmlString .= '<fieldset class="tblFooters">'
297 . '<input type="hidden" name="'
298 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
299 . '" value="1" />'
300 . '<input type="submit" name="" value="' . __('Go') . '" />'
301 . '</fieldset>';
302 } else {
303 $htmlString .= '<input type="hidden" name="'
304 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
305 . '" value="1" />'
306 . '<input type="hidden" name="ajax_dialog" value="1" />'
307 . '<input type="hidden" name="ajax_request" value="1" />';
310 $htmlString .= '</form>'
311 . '</div>';
313 echo $htmlString;