2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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?)
10 use PMA\libraries\URL
;
12 require_once './libraries/common.inc.php';
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(
27 $view_with_options = array(
32 $view_security_options = array(
37 if (empty($sql_query)) {
41 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
47 if (isset($_REQUEST['createview'])) {
48 $sql_query = 'CREATE';
49 if (isset($_REQUEST['view']['or_replace'])) {
50 $sql_query .= ' OR REPLACE';
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 $arr = explode('@', $_REQUEST['view']['definer']);
62 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util
::backquote($arr[0]);
63 $sql_query .= '@' . PMA\libraries\Util
::backquote($arr[1]) . ' ';
66 if (isset($_REQUEST['view']['sql_security'])) {
67 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
68 $sql_query .= $sep . ' SQL SECURITY '
69 . $_REQUEST['view']['sql_security'];
73 $sql_query .= $sep . ' VIEW '
74 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
76 if (! empty($_REQUEST['view']['column_names'])) {
77 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
80 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
82 if (isset($_REQUEST['view']['with'])) {
83 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
84 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
89 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
90 if (! isset($_REQUEST['ajax_dialog'])) {
91 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
95 $response = PMA\libraries\Response
::getInstance();
98 PMA\libraries\Message
::error(
99 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
100 . $GLOBALS['dbi']->getError()
103 $response->setRequestStatus(false);
107 // If different column names defined for VIEW
108 $view_columns = array();
109 if (isset($_REQUEST['view']['column_names'])) {
110 $view_columns = explode(',', $_REQUEST['view']['column_names']);
113 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
114 $_REQUEST['view']['as'], $view_columns
117 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
118 $pma_transformation_data = $systemDb->getExistingTransformationData(
122 if ($pma_transformation_data !== false) {
124 // SQL for store new transformation details of VIEW
125 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
126 $pma_transformation_data, $column_map,
127 $_REQUEST['view']['name'], $GLOBALS['db']
130 // Store new transformations
131 if ($new_transformations_sql != '') {
132 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
136 unset($pma_transformation_data);
138 if (! isset($_REQUEST['ajax_dialog'])) {
139 $message = PMA\libraries\Message
::success();
140 include 'tbl_structure.php';
142 $response = PMA\libraries\Response
::getInstance();
145 PMA\libraries\Util
::getMessage(
146 PMA\libraries\Message
::success(),
150 $response->setRequestStatus(true);
156 // prefill values if not already filled from former submission
158 'operation' => 'create',
162 'sql_security' => '',
164 'column_names' => '',
169 if (PMA_isValid($_REQUEST['view'], 'array')) {
170 $view = array_merge($view, $_REQUEST['view']);
173 $url_params['db'] = $GLOBALS['db'];
174 $url_params['reload'] = 1;
179 $htmlString = '<!-- CREATE VIEW options -->'
180 . '<div id="div_view_options">'
181 . '<form method="post" action="view_create.php">'
182 . URL
::getHiddenInputs($url_params)
185 . (isset($_REQUEST['ajax_dialog']) ?
187 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
190 . '<table class="rte_table">';
192 if ($view['operation'] == 'create') {
193 $htmlString .= '<tr>'
194 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
195 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
196 if ($view['or_replace']) {
197 $htmlString .= ' checked="checked"';
199 $htmlString .= ' value="1" /></td></tr>';
202 $htmlString .= '<tr>'
203 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
204 . '<td><select name="view[algorithm]" id="algorithm">';
205 foreach ($view_algorithm_options as $option) {
206 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
207 if ($view['algorithm'] === $option) {
208 $htmlString .= ' selected="selected"';
210 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
212 $htmlString .= '</select>'
215 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
216 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
217 . ' value="' . htmlspecialchars($view['definer']) . '" />'
220 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
221 . '<td><select name="view[sql_security]">'
222 . '<option value=""></option>';
223 foreach ($view_security_options as $option) {
224 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
225 if ($option == $view['sql_security']) {
226 $htmlString .= ' selected="selected"';
228 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
230 $htmlString .= '<select>'
233 if ($view['operation'] == 'create') {
234 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
235 . '<td><input type="text" size="20" name="view[name]"'
236 . ' onfocus="this.select()" maxlength="64"'
237 . ' value="' . htmlspecialchars($view['name']) . '" />'
240 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
241 . ' value="' . htmlspecialchars($view['name']) . '" />'
245 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
246 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
247 . ' onfocus="this.select()"'
248 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
251 $htmlString .= '<tr><td class="nowrap">AS</td>'
253 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
254 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
255 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
257 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
260 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
261 . '<td><select name="view[with]">'
262 . '<option value=""></option>';
263 foreach ($view_with_options as $option) {
264 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
265 if ($option == $view['with']) {
266 $htmlString .= ' selected="selected"';
268 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
270 $htmlString .= '<select>'
273 $htmlString .= '</table>'
276 if (! isset($_REQUEST['ajax_dialog'])) {
277 $htmlString .= '<fieldset class="tblFooters">'
278 . '<input type="hidden" name="'
279 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
281 . '<input type="submit" name="" value="' . __('Go') . '" />'
284 $htmlString .= '<input type="hidden" name="'
285 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
287 . '<input type="hidden" name="ajax_dialog" value="1" />'
288 . '<input type="hidden" name="ajax_request" value="1" />';
291 $htmlString .= '</form>'