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?)
11 use PMA\libraries\Core
;
12 use PMA\libraries\URL
;
13 use PMA\libraries\Response
;
15 require_once './libraries/common.inc.php';
20 require './libraries/db_common.inc.php';
21 $url_params['goto'] = 'tbl_structure.php';
22 $url_params['back'] = 'view_create.php';
24 $response = Response
::getInstance();
26 $view_algorithm_options = array(
32 $view_with_options = array(
37 $view_security_options = array(
42 if (empty($sql_query)) {
46 // View name is a compulsory field
47 if (isset($_REQUEST['view']['name'])
48 && empty($_REQUEST['view']['name'])
50 $message = PMA\libraries\Message
::error(__('View name can not be empty!'));
55 $response->setRequestStatus(false);
59 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
65 if (isset($_REQUEST['createview'])) {
66 $sql_query = 'CREATE';
67 if (isset($_REQUEST['view']['or_replace'])) {
68 $sql_query .= ' OR REPLACE';
74 if (Core
::isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
75 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
78 if (! empty($_REQUEST['view']['definer'])) {
79 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
80 $sql_query .= $sep . 'DEFINER='
81 . PMA\libraries\Util
::backquote($_REQUEST['view']['definer']);
83 $arr = explode('@', $_REQUEST['view']['definer']);
84 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util
::backquote($arr[0]);
85 $sql_query .= '@' . PMA\libraries\Util
::backquote($arr[1]) . ' ';
89 if (isset($_REQUEST['view']['sql_security'])) {
90 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
91 $sql_query .= $sep . ' SQL SECURITY '
92 . $_REQUEST['view']['sql_security'];
96 $sql_query .= $sep . ' VIEW '
97 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
99 if (! empty($_REQUEST['view']['column_names'])) {
100 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
103 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
105 if (isset($_REQUEST['view']['with'])) {
106 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
107 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
112 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
113 if (! isset($_REQUEST['ajax_dialog'])) {
114 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
120 PMA\libraries\Message
::error(
121 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
122 . $GLOBALS['dbi']->getError()
125 $response->setRequestStatus(false);
129 // If different column names defined for VIEW
130 $view_columns = array();
131 if (isset($_REQUEST['view']['column_names'])) {
132 $view_columns = explode(',', $_REQUEST['view']['column_names']);
135 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
136 $_REQUEST['view']['as'], $view_columns
139 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
140 $pma_transformation_data = $systemDb->getExistingTransformationData(
144 if ($pma_transformation_data !== false) {
146 // SQL for store new transformation details of VIEW
147 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
148 $pma_transformation_data, $column_map,
149 $_REQUEST['view']['name'], $GLOBALS['db']
152 // Store new transformations
153 if ($new_transformations_sql != '') {
154 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
158 unset($pma_transformation_data);
160 if (! isset($_REQUEST['ajax_dialog'])) {
161 $message = PMA\libraries\Message
::success();
162 include 'tbl_structure.php';
166 PMA\libraries\Util
::getMessage(
167 PMA\libraries\Message
::success(),
171 $response->setRequestStatus(true);
177 // prefill values if not already filled from former submission
179 'operation' => 'create',
183 'sql_security' => '',
185 'column_names' => '',
190 if (Core
::isValid($_REQUEST['view'], 'array')) {
191 $view = array_merge($view, $_REQUEST['view']);
194 $url_params['db'] = $GLOBALS['db'];
195 $url_params['reload'] = 1;
200 $htmlString = '<!-- CREATE VIEW options -->'
201 . '<div id="div_view_options">'
202 . '<form method="post" action="view_create.php">'
203 . URL
::getHiddenInputs($url_params)
206 . (isset($_REQUEST['ajax_dialog']) ?
208 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
211 . '<table class="rte_table">';
213 if ($view['operation'] == 'create') {
214 $htmlString .= '<tr>'
215 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
216 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
217 if ($view['or_replace']) {
218 $htmlString .= ' checked="checked"';
220 $htmlString .= ' value="1" /></td></tr>';
223 $htmlString .= '<tr>'
224 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
225 . '<td><select name="view[algorithm]" id="algorithm">';
226 foreach ($view_algorithm_options as $option) {
227 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
228 if ($view['algorithm'] === $option) {
229 $htmlString .= ' selected="selected"';
231 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
233 $htmlString .= '</select>'
236 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
237 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
238 . ' value="' . htmlspecialchars($view['definer']) . '" />'
241 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
242 . '<td><select name="view[sql_security]">'
243 . '<option value=""></option>';
244 foreach ($view_security_options as $option) {
245 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
246 if ($option == $view['sql_security']) {
247 $htmlString .= ' selected="selected"';
249 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
251 $htmlString .= '<select>'
254 if ($view['operation'] == 'create') {
255 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
256 . '<td><input type="text" size="20" name="view[name]"'
257 . ' onfocus="this.select()" maxlength="64"'
258 . ' value="' . htmlspecialchars($view['name']) . '" />'
261 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
262 . ' value="' . htmlspecialchars($view['name']) . '" />'
266 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
267 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
268 . ' onfocus="this.select()"'
269 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
272 $htmlString .= '<tr><td class="nowrap">AS</td>'
274 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
275 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
276 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
278 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
281 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
282 . '<td><select name="view[with]">'
283 . '<option value=""></option>';
284 foreach ($view_with_options as $option) {
285 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
286 if ($option == $view['with']) {
287 $htmlString .= ' selected="selected"';
289 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
291 $htmlString .= '<select>'
294 $htmlString .= '</table>'
297 if (! isset($_REQUEST['ajax_dialog'])) {
298 $htmlString .= '<fieldset class="tblFooters">'
299 . '<input type="hidden" name="'
300 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
302 . '<input type="submit" name="" value="' . __('Go') . '" />'
305 $htmlString .= '<input type="hidden" name="'
306 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
308 . '<input type="hidden" name="ajax_dialog" value="1" />'
309 . '<input type="hidden" name="ajax_request" value="1" />';
312 $htmlString .= '</form>'