Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blob607e52b3a1d56306a01ff4e1dc1bd316098bb42a
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 require_once './libraries/common.inc.php';
13 /**
14 * Runs common work
16 require './libraries/db_common.inc.php';
17 $url_params['goto'] = 'tbl_structure.php';
18 $url_params['back'] = 'view_create.php';
20 $view_algorithm_options = array(
21 'UNDEFINED',
22 'MERGE',
23 'TEMPTABLE',
26 $view_with_options = array(
27 'CASCADED',
28 'LOCAL'
31 $view_security_options = array(
32 'DEFINER',
33 'INVOKER'
36 if (empty($sql_query)) {
37 $sql_query = '';
40 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
41 /**
42 * Creates the view
44 $sep = "\r\n";
46 if (isset($_REQUEST['createview'])) {
47 $sql_query = 'CREATE';
48 if (isset($_REQUEST['view']['or_replace'])) {
49 $sql_query .= ' OR REPLACE';
51 } else {
52 $sql_query = 'ALTER';
55 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
56 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
59 if (! empty($_REQUEST['view']['definer'])) {
60 $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
63 if (isset($_REQUEST['view']['sql_security'])) {
64 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
65 $sql_query .= $sep . ' SQL SECURITY '
66 . $_REQUEST['view']['sql_security'];
70 $sql_query .= $sep . ' VIEW '
71 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
73 if (! empty($_REQUEST['view']['column_names'])) {
74 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
77 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
79 if (isset($_REQUEST['view']['with'])) {
80 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
81 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
82 . ' CHECK OPTION';
86 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
87 if (! isset($_REQUEST['ajax_dialog'])) {
88 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
89 return;
92 $response = PMA\libraries\Response::getInstance();
93 $response->addJSON(
94 'message',
95 PMA\libraries\Message::error(
96 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
97 . $GLOBALS['dbi']->getError()
100 $response->setRequestStatus(false);
101 exit;
104 // If different column names defined for VIEW
105 $view_columns = array();
106 if (isset($_REQUEST['view']['column_names'])) {
107 $view_columns = explode(',', $_REQUEST['view']['column_names']);
110 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
111 $_REQUEST['view']['as'], $view_columns
114 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
115 $pma_transformation_data = $systemDb->getExistingTransformationData(
116 $GLOBALS['db']
119 if ($pma_transformation_data !== false) {
121 // SQL for store new transformation details of VIEW
122 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
123 $pma_transformation_data, $column_map,
124 $_REQUEST['view']['name'], $GLOBALS['db']
127 // Store new transformations
128 if ($new_transformations_sql != '') {
129 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
133 unset($pma_transformation_data);
135 if (! isset($_REQUEST['ajax_dialog'])) {
136 $message = PMA\libraries\Message::success();
137 include 'tbl_structure.php';
138 } else {
139 $response = PMA\libraries\Response::getInstance();
140 $response->addJSON(
141 'message',
142 PMA\libraries\Util::getMessage(
143 PMA\libraries\Message::success(),
144 $sql_query
147 $response->setRequestStatus(true);
150 exit;
153 // prefill values if not already filled from former submission
154 $view = array(
155 'operation' => 'create',
156 'or_replace' => '',
157 'algorithm' => '',
158 'definer' => '',
159 'sql_security' => '',
160 'name' => '',
161 'column_names' => '',
162 'as' => $sql_query,
163 'with' => '',
166 if (PMA_isValid($_REQUEST['view'], 'array')) {
167 $view = array_merge($view, $_REQUEST['view']);
170 $url_params['db'] = $GLOBALS['db'];
171 $url_params['reload'] = 1;
174 * Displays the page
176 $htmlString = '<!-- CREATE VIEW options -->'
177 . '<div id="div_view_options">'
178 . '<form method="post" action="view_create.php">'
179 . PMA_URL_getHiddenInputs($url_params)
180 . '<fieldset>'
181 . '<legend>'
182 . (isset($_REQUEST['ajax_dialog']) ?
183 __('Details') :
184 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
186 . '</legend>'
187 . '<table class="rte_table">';
189 if ($view['operation'] == 'create') {
190 $htmlString .= '<tr>'
191 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
192 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
193 if ($view['or_replace']) {
194 $htmlString .= ' checked="checked"';
196 $htmlString .= ' value="1" /></td></tr>';
199 $htmlString .= '<tr>'
200 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
201 . '<td><select name="view[algorithm]" id="algorithm">';
202 foreach ($view_algorithm_options as $option) {
203 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
204 if ($view['algorithm'] === $option) {
205 $htmlString .= ' selected="selected"';
207 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
209 $htmlString .= '</select>'
210 . '</td></tr>';
212 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
213 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
214 . ' value="' . htmlspecialchars($view['definer']) . '" />'
215 . '</td></tr>';
217 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
218 . '<td><select name="view[sql_security]">'
219 . '<option value=""></option>';
220 foreach ($view_security_options as $option) {
221 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
222 if ($option == $view['sql_security']) {
223 $htmlString .= ' selected="selected"';
225 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
227 $htmlString .= '<select>'
228 . '</td></tr>';
230 if ($view['operation'] == 'create') {
231 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
232 . '<td><input type="text" size="20" name="view[name]"'
233 . ' onfocus="this.select()" maxlength="64"'
234 . ' value="' . htmlspecialchars($view['name']) . '" />'
235 . '</td></tr>';
236 } else {
237 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
238 . ' value="' . htmlspecialchars($view['name']) . '" />'
239 . '</td></tr>';
242 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
243 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
244 . ' onfocus="this.select()"'
245 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
246 . '</td></tr>';
248 $htmlString .= '<tr><td class="nowrap">AS</td>'
249 . '<td>'
250 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
251 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
252 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
254 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
255 . '</td></tr>';
257 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
258 . '<td><select name="view[with]">'
259 . '<option value=""></option>';
260 foreach ($view_with_options as $option) {
261 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
262 if ($option == $view['with']) {
263 $htmlString .= ' selected="selected"';
265 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
267 $htmlString .= '<select>'
268 . '</td></tr>';
270 $htmlString .= '</table>'
271 . '</fieldset>';
273 if (! isset($_REQUEST['ajax_dialog'])) {
274 $htmlString .= '<fieldset class="tblFooters">'
275 . '<input type="hidden" name="'
276 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
277 . '" value="1" />'
278 . '<input type="submit" name="" value="' . __('Go') . '" />'
279 . '</fieldset>';
280 } else {
281 $htmlString .= '<input type="hidden" name="'
282 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
283 . '" value="1" />'
284 . '<input type="hidden" name="ajax_dialog" value="1" />'
285 . '<input type="hidden" name="ajax_request" value="1" />';
288 $htmlString .= '</form>'
289 . '</div>';
291 echo $htmlString;