Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blobb28bfc70993a583813797469812c0004087a7b11
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 $arr = explode('@', $_REQUEST['view']['definer']);
61 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
62 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
65 if (isset($_REQUEST['view']['sql_security'])) {
66 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
67 $sql_query .= $sep . ' SQL SECURITY '
68 . $_REQUEST['view']['sql_security'];
72 $sql_query .= $sep . ' VIEW '
73 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
75 if (! empty($_REQUEST['view']['column_names'])) {
76 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
79 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
81 if (isset($_REQUEST['view']['with'])) {
82 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
83 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
84 . ' CHECK OPTION';
88 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
89 if (! isset($_REQUEST['ajax_dialog'])) {
90 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
91 return;
94 $response = PMA\libraries\Response::getInstance();
95 $response->addJSON(
96 'message',
97 PMA\libraries\Message::error(
98 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
99 . $GLOBALS['dbi']->getError()
102 $response->setRequestStatus(false);
103 exit;
106 // If different column names defined for VIEW
107 $view_columns = array();
108 if (isset($_REQUEST['view']['column_names'])) {
109 $view_columns = explode(',', $_REQUEST['view']['column_names']);
112 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
113 $_REQUEST['view']['as'], $view_columns
116 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
117 $pma_transformation_data = $systemDb->getExistingTransformationData(
118 $GLOBALS['db']
121 if ($pma_transformation_data !== false) {
123 // SQL for store new transformation details of VIEW
124 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
125 $pma_transformation_data, $column_map,
126 $_REQUEST['view']['name'], $GLOBALS['db']
129 // Store new transformations
130 if ($new_transformations_sql != '') {
131 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
135 unset($pma_transformation_data);
137 if (! isset($_REQUEST['ajax_dialog'])) {
138 $message = PMA\libraries\Message::success();
139 include 'tbl_structure.php';
140 } else {
141 $response = PMA\libraries\Response::getInstance();
142 $response->addJSON(
143 'message',
144 PMA\libraries\Util::getMessage(
145 PMA\libraries\Message::success(),
146 $sql_query
149 $response->setRequestStatus(true);
152 exit;
155 // prefill values if not already filled from former submission
156 $view = array(
157 'operation' => 'create',
158 'or_replace' => '',
159 'algorithm' => '',
160 'definer' => '',
161 'sql_security' => '',
162 'name' => '',
163 'column_names' => '',
164 'as' => $sql_query,
165 'with' => '',
168 if (PMA_isValid($_REQUEST['view'], 'array')) {
169 $view = array_merge($view, $_REQUEST['view']);
172 $url_params['db'] = $GLOBALS['db'];
173 $url_params['reload'] = 1;
176 * Displays the page
178 $htmlString = '<!-- CREATE VIEW options -->'
179 . '<div id="div_view_options">'
180 . '<form method="post" action="view_create.php">'
181 . PMA_URL_getHiddenInputs($url_params)
182 . '<fieldset>'
183 . '<legend>'
184 . (isset($_REQUEST['ajax_dialog']) ?
185 __('Details') :
186 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
188 . '</legend>'
189 . '<table class="rte_table">';
191 if ($view['operation'] == 'create') {
192 $htmlString .= '<tr>'
193 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
194 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
195 if ($view['or_replace']) {
196 $htmlString .= ' checked="checked"';
198 $htmlString .= ' value="1" /></td></tr>';
201 $htmlString .= '<tr>'
202 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
203 . '<td><select name="view[algorithm]" id="algorithm">';
204 foreach ($view_algorithm_options as $option) {
205 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
206 if ($view['algorithm'] === $option) {
207 $htmlString .= ' selected="selected"';
209 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
211 $htmlString .= '</select>'
212 . '</td></tr>';
214 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
215 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
216 . ' value="' . htmlspecialchars($view['definer']) . '" />'
217 . '</td></tr>';
219 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
220 . '<td><select name="view[sql_security]">'
221 . '<option value=""></option>';
222 foreach ($view_security_options as $option) {
223 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
224 if ($option == $view['sql_security']) {
225 $htmlString .= ' selected="selected"';
227 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
229 $htmlString .= '<select>'
230 . '</td></tr>';
232 if ($view['operation'] == 'create') {
233 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
234 . '<td><input type="text" size="20" name="view[name]"'
235 . ' onfocus="this.select()" maxlength="64"'
236 . ' value="' . htmlspecialchars($view['name']) . '" />'
237 . '</td></tr>';
238 } else {
239 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
240 . ' value="' . htmlspecialchars($view['name']) . '" />'
241 . '</td></tr>';
244 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
245 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
246 . ' onfocus="this.select()"'
247 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
248 . '</td></tr>';
250 $htmlString .= '<tr><td class="nowrap">AS</td>'
251 . '<td>'
252 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
253 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
254 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
256 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
257 . '</td></tr>';
259 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
260 . '<td><select name="view[with]">'
261 . '<option value=""></option>';
262 foreach ($view_with_options as $option) {
263 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
264 if ($option == $view['with']) {
265 $htmlString .= ' selected="selected"';
267 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
269 $htmlString .= '<select>'
270 . '</td></tr>';
272 $htmlString .= '</table>'
273 . '</fieldset>';
275 if (! isset($_REQUEST['ajax_dialog'])) {
276 $htmlString .= '<fieldset class="tblFooters">'
277 . '<input type="hidden" name="'
278 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
279 . '" value="1" />'
280 . '<input type="submit" name="" value="' . __('Go') . '" />'
281 . '</fieldset>';
282 } else {
283 $htmlString .= '<input type="hidden" name="'
284 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
285 . '" value="1" />'
286 . '<input type="hidden" name="ajax_dialog" value="1" />'
287 . '<input type="hidden" name="ajax_request" value="1" />';
290 $htmlString .= '</form>'
291 . '</div>';
293 echo $htmlString;