Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blobd9f4f007d265d037afc2a7a3ebfa0b82cca56673
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 // View name is a compulsory field
41 if (isset($_REQUEST['view']['name'])
42 && empty($_REQUEST['view']['name'])
43 ) {
44 $message = PMA\libraries\Message::error(__('View name can not be empty'));
45 $response = PMA\libraries\Response::getInstance();
46 $response->addJSON(
47 'message',
48 $message
50 $response->setRequestStatus(false);
51 exit;
54 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
55 /**
56 * Creates the view
58 $sep = "\r\n";
60 if (isset($_REQUEST['createview'])) {
61 $sql_query = 'CREATE';
62 if (isset($_REQUEST['view']['or_replace'])) {
63 $sql_query .= ' OR REPLACE';
65 } else {
66 $sql_query = 'ALTER';
69 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
70 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
73 if (! empty($_REQUEST['view']['definer'])) {
74 $arr = explode('@', $_REQUEST['view']['definer']);
75 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
76 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
79 if (isset($_REQUEST['view']['sql_security'])) {
80 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
81 $sql_query .= $sep . ' SQL SECURITY '
82 . $_REQUEST['view']['sql_security'];
86 $sql_query .= $sep . ' VIEW '
87 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
89 if (! empty($_REQUEST['view']['column_names'])) {
90 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
93 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
95 if (isset($_REQUEST['view']['with'])) {
96 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
97 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
98 . ' CHECK OPTION';
102 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
103 if (! isset($_REQUEST['ajax_dialog'])) {
104 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
105 return;
108 $response = PMA\libraries\Response::getInstance();
109 $response->addJSON(
110 'message',
111 PMA\libraries\Message::error(
112 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
113 . $GLOBALS['dbi']->getError()
116 $response->setRequestStatus(false);
117 exit;
120 // If different column names defined for VIEW
121 $view_columns = array();
122 if (isset($_REQUEST['view']['column_names'])) {
123 $view_columns = explode(',', $_REQUEST['view']['column_names']);
126 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
127 $_REQUEST['view']['as'], $view_columns
130 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
131 $pma_transformation_data = $systemDb->getExistingTransformationData(
132 $GLOBALS['db']
135 if ($pma_transformation_data !== false) {
137 // SQL for store new transformation details of VIEW
138 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
139 $pma_transformation_data, $column_map,
140 $_REQUEST['view']['name'], $GLOBALS['db']
143 // Store new transformations
144 if ($new_transformations_sql != '') {
145 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
149 unset($pma_transformation_data);
151 if (! isset($_REQUEST['ajax_dialog'])) {
152 $message = PMA\libraries\Message::success();
153 include 'tbl_structure.php';
154 } else {
155 $response = PMA\libraries\Response::getInstance();
156 $response->addJSON(
157 'message',
158 PMA\libraries\Util::getMessage(
159 PMA\libraries\Message::success(),
160 $sql_query
163 $response->setRequestStatus(true);
166 exit;
169 // prefill values if not already filled from former submission
170 $view = array(
171 'operation' => 'create',
172 'or_replace' => '',
173 'algorithm' => '',
174 'definer' => '',
175 'sql_security' => '',
176 'name' => '',
177 'column_names' => '',
178 'as' => $sql_query,
179 'with' => '',
182 if (PMA_isValid($_REQUEST['view'], 'array')) {
183 $view = array_merge($view, $_REQUEST['view']);
186 $url_params['db'] = $GLOBALS['db'];
187 $url_params['reload'] = 1;
190 * Displays the page
192 $htmlString = '<!-- CREATE VIEW options -->'
193 . '<div id="div_view_options">'
194 . '<form method="post" action="view_create.php">'
195 . PMA_URL_getHiddenInputs($url_params)
196 . '<fieldset>'
197 . '<legend>'
198 . (isset($_REQUEST['ajax_dialog']) ?
199 __('Details') :
200 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
202 . '</legend>'
203 . '<table class="rte_table">';
205 if ($view['operation'] == 'create') {
206 $htmlString .= '<tr>'
207 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
208 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
209 if ($view['or_replace']) {
210 $htmlString .= ' checked="checked"';
212 $htmlString .= ' value="1" /></td></tr>';
215 $htmlString .= '<tr>'
216 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
217 . '<td><select name="view[algorithm]" id="algorithm">';
218 foreach ($view_algorithm_options as $option) {
219 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
220 if ($view['algorithm'] === $option) {
221 $htmlString .= ' selected="selected"';
223 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
225 $htmlString .= '</select>'
226 . '</td></tr>';
228 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
229 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
230 . ' value="' . htmlspecialchars($view['definer']) . '" />'
231 . '</td></tr>';
233 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
234 . '<td><select name="view[sql_security]">'
235 . '<option value=""></option>';
236 foreach ($view_security_options as $option) {
237 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
238 if ($option == $view['sql_security']) {
239 $htmlString .= ' selected="selected"';
241 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
243 $htmlString .= '<select>'
244 . '</td></tr>';
246 if ($view['operation'] == 'create') {
247 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
248 . '<td><input type="text" size="20" name="view[name]"'
249 . ' onfocus="this.select()" maxlength="64"'
250 . ' value="' . htmlspecialchars($view['name']) . '" />'
251 . '</td></tr>';
252 } else {
253 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
254 . ' value="' . htmlspecialchars($view['name']) . '" />'
255 . '</td></tr>';
258 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
259 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
260 . ' onfocus="this.select()"'
261 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
262 . '</td></tr>';
264 $htmlString .= '<tr><td class="nowrap">AS</td>'
265 . '<td>'
266 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
267 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
268 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
270 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
271 . '</td></tr>';
273 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
274 . '<td><select name="view[with]">'
275 . '<option value=""></option>';
276 foreach ($view_with_options as $option) {
277 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
278 if ($option == $view['with']) {
279 $htmlString .= ' selected="selected"';
281 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
283 $htmlString .= '<select>'
284 . '</td></tr>';
286 $htmlString .= '</table>'
287 . '</fieldset>';
289 if (! isset($_REQUEST['ajax_dialog'])) {
290 $htmlString .= '<fieldset class="tblFooters">'
291 . '<input type="hidden" name="'
292 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
293 . '" value="1" />'
294 . '<input type="submit" name="" value="' . __('Go') . '" />'
295 . '</fieldset>';
296 } else {
297 $htmlString .= '<input type="hidden" name="'
298 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
299 . '" value="1" />'
300 . '<input type="hidden" name="ajax_dialog" value="1" />'
301 . '<input type="hidden" name="ajax_request" value="1" />';
304 $htmlString .= '</form>'
305 . '</div>';
307 echo $htmlString;