Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / view_create.php
blob8fca46c9aff33553c34b389e3ee9905a483829f4
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 */
10 use PMA\libraries\URL;
11 use PMA\libraries\Response;
13 require_once './libraries/common.inc.php';
15 /**
16 * Runs common work
18 require './libraries/db_common.inc.php';
19 $url_params['goto'] = 'tbl_structure.php';
20 $url_params['back'] = 'view_create.php';
22 $response = Response::getInstance();
24 $view_algorithm_options = array(
25 'UNDEFINED',
26 'MERGE',
27 'TEMPTABLE',
30 $view_with_options = array(
31 'CASCADED',
32 'LOCAL'
35 $view_security_options = array(
36 'DEFINER',
37 'INVOKER'
40 if (empty($sql_query)) {
41 $sql_query = '';
44 // View name is a compulsory field
45 if (isset($_REQUEST['view']['name'])
46 && empty($_REQUEST['view']['name'])
47 ) {
48 $message = PMA\libraries\Message::error(__('View name can not be empty!'));
49 $response->addJSON(
50 'message',
51 $message
53 $response->setRequestStatus(false);
54 exit;
57 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
58 /**
59 * Creates the view
61 $sep = "\r\n";
63 if (isset($_REQUEST['createview'])) {
64 $sql_query = 'CREATE';
65 if (isset($_REQUEST['view']['or_replace'])) {
66 $sql_query .= ' OR REPLACE';
68 } else {
69 $sql_query = 'ALTER';
72 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
73 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
76 if (! empty($_REQUEST['view']['definer'])) {
77 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
78 $sql_query .= $sep . 'DEFINER='
79 . PMA\libraries\Util::backquote($_REQUEST['view']['definer']);
80 } else {
81 $arr = explode('@', $_REQUEST['view']['definer']);
82 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util::backquote($arr[0]);
83 $sql_query .= '@' . PMA\libraries\Util::backquote($arr[1]) . ' ';
87 if (isset($_REQUEST['view']['sql_security'])) {
88 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
89 $sql_query .= $sep . ' SQL SECURITY '
90 . $_REQUEST['view']['sql_security'];
94 $sql_query .= $sep . ' VIEW '
95 . PMA\libraries\Util::backquote($_REQUEST['view']['name']);
97 if (! empty($_REQUEST['view']['column_names'])) {
98 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
101 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
103 if (isset($_REQUEST['view']['with'])) {
104 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
105 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
106 . ' CHECK OPTION';
110 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
111 if (! isset($_REQUEST['ajax_dialog'])) {
112 $message = PMA\libraries\Message::rawError($GLOBALS['dbi']->getError());
113 return;
116 $response->addJSON(
117 'message',
118 PMA\libraries\Message::error(
119 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
120 . $GLOBALS['dbi']->getError()
123 $response->setRequestStatus(false);
124 exit;
127 // If different column names defined for VIEW
128 $view_columns = array();
129 if (isset($_REQUEST['view']['column_names'])) {
130 $view_columns = explode(',', $_REQUEST['view']['column_names']);
133 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
134 $_REQUEST['view']['as'], $view_columns
137 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
138 $pma_transformation_data = $systemDb->getExistingTransformationData(
139 $GLOBALS['db']
142 if ($pma_transformation_data !== false) {
144 // SQL for store new transformation details of VIEW
145 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
146 $pma_transformation_data, $column_map,
147 $_REQUEST['view']['name'], $GLOBALS['db']
150 // Store new transformations
151 if ($new_transformations_sql != '') {
152 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
156 unset($pma_transformation_data);
158 if (! isset($_REQUEST['ajax_dialog'])) {
159 $message = PMA\libraries\Message::success();
160 include 'tbl_structure.php';
161 } else {
162 $response->addJSON(
163 'message',
164 PMA\libraries\Util::getMessage(
165 PMA\libraries\Message::success(),
166 $sql_query
169 $response->setRequestStatus(true);
172 exit;
175 // prefill values if not already filled from former submission
176 $view = array(
177 'operation' => 'create',
178 'or_replace' => '',
179 'algorithm' => '',
180 'definer' => '',
181 'sql_security' => '',
182 'name' => '',
183 'column_names' => '',
184 'as' => $sql_query,
185 'with' => '',
188 if (PMA_isValid($_REQUEST['view'], 'array')) {
189 $view = array_merge($view, $_REQUEST['view']);
192 $url_params['db'] = $GLOBALS['db'];
193 $url_params['reload'] = 1;
196 * Displays the page
198 $htmlString = '<!-- CREATE VIEW options -->'
199 . '<div id="div_view_options">'
200 . '<form method="post" action="view_create.php">'
201 . URL::getHiddenInputs($url_params)
202 . '<fieldset>'
203 . '<legend>'
204 . (isset($_REQUEST['ajax_dialog']) ?
205 __('Details') :
206 ($view['operation'] == 'create' ? __('Create view') : __('Edit view'))
208 . '</legend>'
209 . '<table class="rte_table">';
211 if ($view['operation'] == 'create') {
212 $htmlString .= '<tr>'
213 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
214 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
215 if ($view['or_replace']) {
216 $htmlString .= ' checked="checked"';
218 $htmlString .= ' value="1" /></td></tr>';
221 $htmlString .= '<tr>'
222 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
223 . '<td><select name="view[algorithm]" id="algorithm">';
224 foreach ($view_algorithm_options as $option) {
225 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
226 if ($view['algorithm'] === $option) {
227 $htmlString .= ' selected="selected"';
229 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
231 $htmlString .= '</select>'
232 . '</td></tr>';
234 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
235 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
236 . ' value="' . htmlspecialchars($view['definer']) . '" />'
237 . '</td></tr>';
239 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
240 . '<td><select name="view[sql_security]">'
241 . '<option value=""></option>';
242 foreach ($view_security_options as $option) {
243 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
244 if ($option == $view['sql_security']) {
245 $htmlString .= ' selected="selected"';
247 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
249 $htmlString .= '<select>'
250 . '</td></tr>';
252 if ($view['operation'] == 'create') {
253 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
254 . '<td><input type="text" size="20" name="view[name]"'
255 . ' onfocus="this.select()" maxlength="64"'
256 . ' value="' . htmlspecialchars($view['name']) . '" />'
257 . '</td></tr>';
258 } else {
259 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
260 . ' value="' . htmlspecialchars($view['name']) . '" />'
261 . '</td></tr>';
264 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
265 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
266 . ' onfocus="this.select()"'
267 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
268 . '</td></tr>';
270 $htmlString .= '<tr><td class="nowrap">AS</td>'
271 . '<td>'
272 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
273 if ($GLOBALS['cfg']['TextareaAutoSelect'] || true) {
274 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
276 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
277 . '</td></tr>';
279 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
280 . '<td><select name="view[with]">'
281 . '<option value=""></option>';
282 foreach ($view_with_options as $option) {
283 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
284 if ($option == $view['with']) {
285 $htmlString .= ' selected="selected"';
287 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
289 $htmlString .= '<select>'
290 . '</td></tr>';
292 $htmlString .= '</table>'
293 . '</fieldset>';
295 if (! isset($_REQUEST['ajax_dialog'])) {
296 $htmlString .= '<fieldset class="tblFooters">'
297 . '<input type="hidden" name="'
298 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
299 . '" value="1" />'
300 . '<input type="submit" name="" value="' . __('Go') . '" />'
301 . '</fieldset>';
302 } else {
303 $htmlString .= '<input type="hidden" name="'
304 . ($view['operation'] == 'create' ? 'createview' : 'alterview' )
305 . '" value="1" />'
306 . '<input type="hidden" name="ajax_dialog" value="1" />'
307 . '<input type="hidden" name="ajax_request" value="1" />';
310 $htmlString .= '</form>'
311 . '</div>';
313 echo $htmlString;