Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blobb8415c5a93851d961c7068df723b5b15345b72c5
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 ' . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
72 if (! empty($_REQUEST['view']['column_names'])) {
73 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
76 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
78 if (isset($_REQUEST['view']['with'])) {
79 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
80 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
81 . ' CHECK OPTION';
85 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
86 if (! isset($_REQUEST['ajax_dialog'])) {
87 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
88 return;
91 $response = PMA\libraries\Response::getInstance();
92 $response->addJSON(
93 'message',
94 PMA\libraries\Message::error(
95 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
96 . $GLOBALS['dbi']->getError()
99 $response->setRequestStatus(false);
100 exit;
103 // If different column names defined for VIEW
104 $view_columns = array();
105 if (isset($_REQUEST['view']['column_names'])) {
106 $view_columns = explode(',', $_REQUEST['view']['column_names']);
109 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
110 $_REQUEST['view']['as'], $view_columns
113 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
114 $pma_transformation_data = $systemDb->getExistingTransformationData(
115 $GLOBALS['db']
118 if ($pma_transformation_data !== false) {
120 // SQL for store new transformation details of VIEW
121 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
122 $pma_transformation_data, $column_map,
123 $_REQUEST['view']['name'], $GLOBALS['db']
126 // Store new transformations
127 if ($new_transformations_sql != '') {
128 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
132 unset($pma_transformation_data);
134 if (! isset($_REQUEST['ajax_dialog'])) {
135 $message = PMA\libraries\Message::success();
136 include 'tbl_structure.php';
137 } else {
138 $response = PMA\libraries\Response::getInstance();
139 $response->addJSON(
140 'message',
141 PMA\libraries\Util::getMessage(PMA\libraries\Message::success(), $sql_query)
143 $response->setRequestStatus(true);
146 exit;
149 // prefill values if not already filled from former submission
150 $view = array(
151 'operation' => 'create',
152 'or_replace' => '',
153 'algorithm' => '',
154 'definer' => '',
155 'sql_security' => '',
156 'name' => '',
157 'column_names' => '',
158 'as' => $sql_query,
159 'with' => '',
162 if (PMA_isValid($_REQUEST['view'], 'array')) {
163 $view = array_merge($view, $_REQUEST['view']);
166 $url_params['db'] = $GLOBALS['db'];
167 $url_params['reload'] = 1;
170 * Displays the page
172 $htmlString = '<!-- CREATE VIEW options -->'
173 . '<div id="div_view_options">'
174 . '<form method="post" action="view_create.php">'
175 . PMA_URL_getHiddenInputs($url_params)
176 . '<fieldset>'
177 . '<legend>'
178 . (isset($_REQUEST['ajax_dialog']) ?
179 __('Details') :
180 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
182 . '</legend>'
183 . '<table class="rte_table">';
185 if ($view['operation'] == 'create') {
186 $htmlString .= '<tr>'
187 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
188 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
189 if ($view['or_replace']) {
190 $htmlString .= ' checked="checked"';
192 $htmlString .= ' value="1" /></td></tr>';
195 $htmlString .= '<tr>'
196 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
197 . '<td><select name="view[algorithm]" id="algorithm">';
198 foreach ($view_algorithm_options as $option) {
199 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
200 if ($view['algorithm'] === $option) {
201 $htmlString .= ' selected="selected"';
203 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
205 $htmlString .= '</select>'
206 . '</td></tr>';
208 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
209 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
210 . ' value="' . htmlspecialchars($view['definer']) . '" />'
211 . '</td></tr>';
213 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
214 . '<td><select name="view[sql_security]">'
215 . '<option value=""></option>';
216 foreach ($view_security_options as $option) {
217 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
218 if ($option == $view['sql_security']) {
219 $htmlString .= ' selected="selected"';
221 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
223 $htmlString .= '<select>'
224 . '</td></tr>';
226 if ($view['operation'] == 'create') {
227 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
228 . '<td><input type="text" size="20" name="view[name]"'
229 . ' onfocus="this.select()" maxlength="64"'
230 . ' value="' . htmlspecialchars($view['name']) . '" />'
231 . '</td></tr>';
232 } else {
233 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
234 . ' value="' . htmlspecialchars($view['name']) . '" />'
235 . '</td></tr>';
238 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
239 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
240 . ' onfocus="this.select()"'
241 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
242 . '</td></tr>';
244 $htmlString .= '<tr><td class="nowrap">AS</td>'
245 . '<td>'
246 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
247 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
248 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
250 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
251 . '</td></tr>';
253 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
254 . '<td><select name="view[with]">'
255 . '<option value=""></option>';
256 foreach ($view_with_options as $option) {
257 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
258 if ($option == $view['with']) {
259 $htmlString .= ' selected="selected"';
261 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
263 $htmlString .= '<select>'
264 . '</td></tr>';
266 $htmlString .= '</table>'
267 . '</fieldset>';
269 if (! isset($_REQUEST['ajax_dialog'])) {
270 $htmlString .= '<fieldset class="tblFooters">'
271 . '<input type="hidden" name="'
272 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
273 . '" value="1" />'
274 . '<input type="submit" name="" value="' . __('Go') . '" />'
275 . '</fieldset>';
276 } else {
277 $htmlString .= '<input type="hidden" name="'
278 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
279 . '" value="1" />'
280 . '<input type="hidden" name="ajax_dialog" value="1" />'
281 . '<input type="hidden" name="ajax_request" value="1" />';
284 $htmlString .= '</form>'
285 . '</div>';
287 echo $htmlString;