Translated using Weblate (French)
[phpmyadmin.git] / view_create.php
blob5ea2aa33643bc40d6488a61edaa3d7ada88e9f6b
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';
12 require_once './libraries/SystemDatabase.class.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 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
42 /**
43 * Creates the view
45 $sep = "\r\n";
47 if (isset($_REQUEST['createview'])) {
48 $sql_query = 'CREATE';
49 if (isset($_REQUEST['view']['or_replace'])) {
50 $sql_query .= ' OR REPLACE';
52 } else {
53 $sql_query = 'ALTER';
56 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
57 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
60 if (! empty($_REQUEST['view']['definer'])) {
61 $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
64 if (isset($_REQUEST['view']['sql_security'])) {
65 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
66 $sql_query .= $sep . ' SQL SECURITY '
67 . $_REQUEST['view']['sql_security'];
71 $sql_query .= $sep . ' VIEW ' . PMA_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_Message::rawError($GLOBALS['dbi']->getError());
89 return;
92 $response = PMA_Response::getInstance();
93 $response->addJSON(
94 'message',
95 PMA_Message::error(
96 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
97 . $GLOBALS['dbi']->getError()
100 $response->isSuccess(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_Message::success();
137 include 'tbl_structure.php';
138 } else {
139 $response = PMA_Response::getInstance();
140 $response->addJSON(
141 'message',
142 PMA_Util::getMessage(PMA_Message::success(), $sql_query)
144 $response->isSuccess(true);
147 exit;
150 // prefill values if not already filled from former submission
151 $view = array(
152 'operation' => 'create',
153 'or_replace' => '',
154 'algorithm' => '',
155 'definer' => '',
156 'sql_security' => '',
157 'name' => '',
158 'column_names' => '',
159 'as' => $sql_query,
160 'with' => '',
163 if (PMA_isValid($_REQUEST['view'], 'array')) {
164 $view = array_merge($view, $_REQUEST['view']);
167 $url_params['db'] = $GLOBALS['db'];
168 $url_params['reload'] = 1;
171 * Displays the page
173 $htmlString = '<!-- CREATE VIEW options -->'
174 . '<div id="div_view_options">'
175 . '<form method="post" action="view_create.php">'
176 . PMA_URL_getHiddenInputs($url_params)
177 . '<fieldset>'
178 . '<legend>'
179 . (isset($_REQUEST['ajax_dialog']) ?
180 __('Details') :
181 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
183 . '</legend>'
184 . '<table class="rte_table">';
186 if ($view['operation'] == 'create') {
187 $htmlString .= '<tr>'
188 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
189 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
190 if ($view['or_replace']) {
191 $htmlString .= ' checked="checked"';
193 $htmlString .= ' value="1" /></td></tr>';
196 $htmlString .= '<tr>'
197 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
198 . '<td><select name="view[algorithm]" id="algorithm">';
199 foreach ($view_algorithm_options as $option) {
200 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
201 if ($view['algorithm'] === $option) {
202 $htmlString .= ' selected="selected"';
204 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
206 $htmlString .= '</select>'
207 . '</td></tr>';
209 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
210 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
211 . ' value="' . htmlspecialchars($view['definer']) . '" />'
212 . '</td></tr>';
214 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
215 . '<td><select name="view[sql_security]">'
216 . '<option value=""></option>';
217 foreach ($view_security_options as $option) {
218 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
219 if ($option == $view['sql_security']) {
220 $htmlString .= ' selected="selected"';
222 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
224 $htmlString .= '<select>'
225 . '</td></tr>';
227 if ($view['operation'] == 'create') {
228 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
229 . '<td><input type="text" size="20" name="view[name]"'
230 . ' onfocus="this.select()" maxlength="64"'
231 . ' value="' . htmlspecialchars($view['name']) . '" />'
232 . '</td></tr>';
233 } else {
234 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
235 . ' value="' . htmlspecialchars($view['name']) . '" />'
236 . '</td></tr>';
239 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
240 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
241 . ' onfocus="this.select()"'
242 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
243 . '</td></tr>';
245 $htmlString .= '<tr><td class="nowrap">AS</td>'
246 . '<td>'
247 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
248 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
249 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
251 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
252 . '</td></tr>';
254 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
255 . '<td><select name="view[with]">'
256 . '<option value=""></option>';
257 foreach ($view_with_options as $option) {
258 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
259 if ($option == $view['with']) {
260 $htmlString .= ' selected="selected"';
262 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
264 $htmlString .= '<select>'
265 . '</td></tr>';
267 $htmlString .= '</table>'
268 . '</fieldset>';
270 if (! isset($_REQUEST['ajax_dialog'])) {
271 $htmlString .= '<fieldset class="tblFooters">'
272 . '<input type="hidden" name="'
273 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
274 . '" value="1" />'
275 . '<input type="submit" name="" value="' . __('Go') . '" />'
276 . '</fieldset>';
277 } else {
278 $htmlString .= '<input type="hidden" name="'
279 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
280 . '" value="1" />'
281 . '<input type="hidden" name="ajax_dialog" value="1" />'
282 . '<input type="hidden" name="ajax_request" value="1" />';
285 $htmlString .= '</form>'
286 . '</div>';
288 echo $htmlString;