Translated using Weblate (Interlingua)
[phpmyadmin.git] / view_create.php
blobca2672c0195af9e3bb5b2d40db61e0aaca4d2a24
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 use PhpMyAdmin\Core;
12 use PhpMyAdmin\Url;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Template;
16 require_once './libraries/common.inc.php';
18 /**
19 * Runs common work
21 require './libraries/db_common.inc.php';
22 $url_params['goto'] = 'tbl_structure.php';
23 $url_params['back'] = 'view_create.php';
25 $response = Response::getInstance();
27 $view_algorithm_options = array(
28 'UNDEFINED',
29 'MERGE',
30 'TEMPTABLE',
33 $view_with_options = array(
34 'CASCADED',
35 'LOCAL'
38 $view_security_options = array(
39 'DEFINER',
40 'INVOKER'
43 if (empty($sql_query)) {
44 $sql_query = '';
47 // View name is a compulsory field
48 if (isset($_REQUEST['view']['name'])
49 && empty($_REQUEST['view']['name'])
50 ) {
51 $message = PhpMyAdmin\Message::error(__('View name can not be empty!'));
52 $response->addJSON(
53 'message',
54 $message
56 $response->setRequestStatus(false);
57 exit;
60 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
61 /**
62 * Creates the view
64 $sep = "\r\n";
66 if (isset($_REQUEST['createview'])) {
67 $sql_query = 'CREATE';
68 if (isset($_REQUEST['view']['or_replace'])) {
69 $sql_query .= ' OR REPLACE';
71 } else {
72 $sql_query = 'ALTER';
75 if (Core::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
76 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
79 if (! empty($_REQUEST['view']['definer'])) {
80 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
81 $sql_query .= $sep . 'DEFINER='
82 . PhpMyAdmin\Util::backquote($_REQUEST['view']['definer']);
83 } else {
84 $arr = explode('@', $_REQUEST['view']['definer']);
85 $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util::backquote($arr[0]);
86 $sql_query .= '@' . PhpMyAdmin\Util::backquote($arr[1]) . ' ';
90 if (isset($_REQUEST['view']['sql_security'])) {
91 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
92 $sql_query .= $sep . ' SQL SECURITY '
93 . $_REQUEST['view']['sql_security'];
97 $sql_query .= $sep . ' VIEW '
98 . PhpMyAdmin\Util::backquote($_REQUEST['view']['name']);
100 if (! empty($_REQUEST['view']['column_names'])) {
101 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
104 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
106 if (isset($_REQUEST['view']['with'])) {
107 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
108 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
109 . ' CHECK OPTION';
113 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
114 if (! isset($_REQUEST['ajax_dialog'])) {
115 $message = PhpMyAdmin\Message::rawError($GLOBALS['dbi']->getError());
116 return;
119 $response->addJSON(
120 'message',
121 PhpMyAdmin\Message::error(
122 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
123 . $GLOBALS['dbi']->getError()
126 $response->setRequestStatus(false);
127 exit;
130 // If different column names defined for VIEW
131 $view_columns = array();
132 if (isset($_REQUEST['view']['column_names'])) {
133 $view_columns = explode(',', $_REQUEST['view']['column_names']);
136 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
137 $_REQUEST['view']['as'], $view_columns
140 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
141 $pma_transformation_data = $systemDb->getExistingTransformationData(
142 $GLOBALS['db']
145 if ($pma_transformation_data !== false) {
147 // SQL for store new transformation details of VIEW
148 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
149 $pma_transformation_data, $column_map,
150 $_REQUEST['view']['name'], $GLOBALS['db']
153 // Store new transformations
154 if ($new_transformations_sql != '') {
155 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
159 unset($pma_transformation_data);
161 if (! isset($_REQUEST['ajax_dialog'])) {
162 $message = PhpMyAdmin\Message::success();
163 include 'tbl_structure.php';
164 } else {
165 $response->addJSON(
166 'message',
167 PhpMyAdmin\Util::getMessage(
168 PhpMyAdmin\Message::success(),
169 $sql_query
172 $response->setRequestStatus(true);
175 exit;
178 // prefill values if not already filled from former submission
179 $view = array(
180 'operation' => 'create',
181 'or_replace' => '',
182 'algorithm' => '',
183 'definer' => '',
184 'sql_security' => '',
185 'name' => '',
186 'column_names' => '',
187 'as' => $sql_query,
188 'with' => '',
191 if (Core::isValid($_REQUEST['view'], 'array')) {
192 $view = array_merge($view, $_REQUEST['view']);
195 $url_params['db'] = $GLOBALS['db'];
196 $url_params['reload'] = 1;
198 echo Template::get('view_create')->render([
199 'ajax_dialog' => isset($_REQUEST['ajax_dialog']),
200 'text_dir' => $text_dir,
201 'url_params' => $url_params,
202 'view' => $view,