Translated using Weblate (Slovenian)
[phpmyadmin.git] / view_create.php
blob841bec6c9b5026d6446d2b6a192ca4d62ea1984d
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 declare(strict_types=1);
12 use PhpMyAdmin\Core;
13 use PhpMyAdmin\Url;
14 use PhpMyAdmin\Response;
15 use PhpMyAdmin\Template;
17 require_once './libraries/common.inc.php';
19 /**
20 * Runs common work
22 require './libraries/db_common.inc.php';
23 $url_params['goto'] = 'tbl_structure.php';
24 $url_params['back'] = 'view_create.php';
26 $response = Response::getInstance();
28 $view_algorithm_options = array(
29 'UNDEFINED',
30 'MERGE',
31 'TEMPTABLE',
34 $view_with_options = array(
35 'CASCADED',
36 'LOCAL'
39 $view_security_options = array(
40 'DEFINER',
41 'INVOKER'
44 if (empty($sql_query)) {
45 $sql_query = '';
48 // View name is a compulsory field
49 if (isset($_REQUEST['view']['name'])
50 && empty($_REQUEST['view']['name'])
51 ) {
52 $message = PhpMyAdmin\Message::error(__('View name can not be empty!'));
53 $response->addJSON(
54 'message',
55 $message
57 $response->setRequestStatus(false);
58 exit;
61 if (isset($_REQUEST['createview']) || isset($_REQUEST['alterview'])) {
62 /**
63 * Creates the view
65 $sep = "\r\n";
67 if (isset($_REQUEST['createview'])) {
68 $sql_query = 'CREATE';
69 if (isset($_REQUEST['view']['or_replace'])) {
70 $sql_query .= ' OR REPLACE';
72 } else {
73 $sql_query = 'ALTER';
76 if (Core::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
77 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
80 if (! empty($_REQUEST['view']['definer'])) {
81 if (strpos($_REQUEST['view']['definer'], '@') === false) {
82 $sql_query .= $sep . 'DEFINER='
83 . PhpMyAdmin\Util::backquote($_REQUEST['view']['definer']);
84 } else {
85 $arr = explode('@', $_REQUEST['view']['definer']);
86 $sql_query .= $sep . 'DEFINER=' . PhpMyAdmin\Util::backquote($arr[0]);
87 $sql_query .= '@' . PhpMyAdmin\Util::backquote($arr[1]) . ' ';
91 if (isset($_REQUEST['view']['sql_security'])) {
92 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
93 $sql_query .= $sep . ' SQL SECURITY '
94 . $_REQUEST['view']['sql_security'];
98 $sql_query .= $sep . ' VIEW '
99 . PhpMyAdmin\Util::backquote($_REQUEST['view']['name']);
101 if (! empty($_REQUEST['view']['column_names'])) {
102 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
105 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
107 if (isset($_REQUEST['view']['with'])) {
108 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
109 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
110 . ' CHECK OPTION';
114 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
115 if (! isset($_REQUEST['ajax_dialog'])) {
116 $message = PhpMyAdmin\Message::rawError($GLOBALS['dbi']->getError());
117 return;
120 $response->addJSON(
121 'message',
122 PhpMyAdmin\Message::error(
123 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
124 . $GLOBALS['dbi']->getError()
127 $response->setRequestStatus(false);
128 exit;
131 // If different column names defined for VIEW
132 $view_columns = array();
133 if (isset($_REQUEST['view']['column_names'])) {
134 $view_columns = explode(',', $_REQUEST['view']['column_names']);
137 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
138 $_REQUEST['view']['as'], $view_columns
141 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
142 $pma_transformation_data = $systemDb->getExistingTransformationData(
143 $GLOBALS['db']
146 if ($pma_transformation_data !== false) {
148 // SQL for store new transformation details of VIEW
149 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
150 $pma_transformation_data, $column_map,
151 $_REQUEST['view']['name'], $GLOBALS['db']
154 // Store new transformations
155 if ($new_transformations_sql != '') {
156 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
160 unset($pma_transformation_data);
162 if (! isset($_REQUEST['ajax_dialog'])) {
163 $message = PhpMyAdmin\Message::success();
164 include 'tbl_structure.php';
165 } else {
166 $response->addJSON(
167 'message',
168 PhpMyAdmin\Util::getMessage(
169 PhpMyAdmin\Message::success(),
170 $sql_query
173 $response->setRequestStatus(true);
176 exit;
179 // prefill values if not already filled from former submission
180 $view = array(
181 'operation' => 'create',
182 'or_replace' => '',
183 'algorithm' => '',
184 'definer' => '',
185 'sql_security' => '',
186 'name' => '',
187 'column_names' => '',
188 'as' => $sql_query,
189 'with' => '',
192 if (Core::isValid($_REQUEST['view'], 'array')) {
193 $view = array_merge($view, $_REQUEST['view']);
196 $url_params['db'] = $GLOBALS['db'];
197 $url_params['reload'] = 1;
199 echo Template::get('view_create')->render([
200 'ajax_dialog' => isset($_REQUEST['ajax_dialog']),
201 'text_dir' => $text_dir,
202 'url_params' => $url_params,
203 'view' => $view,