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 // View name is a compulsory field
42 if (isset($_REQUEST['view']['name'])
43 && empty($_REQUEST['view']['name'])
45 $message = PMA\libraries\Message
::error(__('View name can not be empty'));
46 $response = PMA\libraries\Response
::getInstance();
51 $response->setRequestStatus(false);
55 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
61 if (isset($_REQUEST['createview'])) {
62 $sql_query = 'CREATE';
63 if (isset($_REQUEST['view']['or_replace'])) {
64 $sql_query .= ' OR REPLACE';
70 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
71 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
74 if (! empty($_REQUEST['view']['definer'])) {
75 $arr = explode('@', $_REQUEST['view']['definer']);
76 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util
::backquote($arr[0]);
77 $sql_query .= '@' . PMA\libraries\Util
::backquote($arr[1]) . ' ';
80 if (isset($_REQUEST['view']['sql_security'])) {
81 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
82 $sql_query .= $sep . ' SQL SECURITY '
83 . $_REQUEST['view']['sql_security'];
87 $sql_query .= $sep . ' VIEW '
88 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
90 if (! empty($_REQUEST['view']['column_names'])) {
91 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
94 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
96 if (isset($_REQUEST['view']['with'])) {
97 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
98 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
103 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
104 if (! isset($_REQUEST['ajax_dialog'])) {
105 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
109 $response = PMA\libraries\Response
::getInstance();
112 PMA\libraries\Message
::error(
113 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
114 . $GLOBALS['dbi']->getError()
117 $response->setRequestStatus(false);
121 // If different column names defined for VIEW
122 $view_columns = array();
123 if (isset($_REQUEST['view']['column_names'])) {
124 $view_columns = explode(',', $_REQUEST['view']['column_names']);
127 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
128 $_REQUEST['view']['as'], $view_columns
131 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
132 $pma_transformation_data = $systemDb->getExistingTransformationData(
136 if ($pma_transformation_data !== false) {
138 // SQL for store new transformation details of VIEW
139 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
140 $pma_transformation_data, $column_map,
141 $_REQUEST['view']['name'], $GLOBALS['db']
144 // Store new transformations
145 if ($new_transformations_sql != '') {
146 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
150 unset($pma_transformation_data);
152 if (! isset($_REQUEST['ajax_dialog'])) {
153 $message = PMA\libraries\Message
::success();
154 include 'tbl_structure.php';
156 $response = PMA\libraries\Response
::getInstance();
159 PMA\libraries\Util
::getMessage(
160 PMA\libraries\Message
::success(),
164 $response->setRequestStatus(true);
170 // prefill values if not already filled from former submission
172 'operation' => 'create',
176 'sql_security' => '',
178 'column_names' => '',
183 if (PMA_isValid($_REQUEST['view'], 'array')) {
184 $view = array_merge($view, $_REQUEST['view']);
187 $url_params['db'] = $GLOBALS['db'];
188 $url_params['reload'] = 1;
193 $htmlString = '<!-- CREATE VIEW options -->'
194 . '<div id="div_view_options">'
195 . '<form method="post" action="view_create.php">'
196 . URL
::getHiddenInputs($url_params)
199 . (isset($_REQUEST['ajax_dialog']) ?
201 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
204 . '<table class="rte_table">';
206 if ($view['operation'] == 'create') {
207 $htmlString .= '<tr>'
208 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
209 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
210 if ($view['or_replace']) {
211 $htmlString .= ' checked="checked"';
213 $htmlString .= ' value="1" /></td></tr>';
216 $htmlString .= '<tr>'
217 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
218 . '<td><select name="view[algorithm]" id="algorithm">';
219 foreach ($view_algorithm_options as $option) {
220 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
221 if ($view['algorithm'] === $option) {
222 $htmlString .= ' selected="selected"';
224 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
226 $htmlString .= '</select>'
229 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
230 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
231 . ' value="' . htmlspecialchars($view['definer']) . '" />'
234 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
235 . '<td><select name="view[sql_security]">'
236 . '<option value=""></option>';
237 foreach ($view_security_options as $option) {
238 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
239 if ($option == $view['sql_security']) {
240 $htmlString .= ' selected="selected"';
242 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
244 $htmlString .= '<select>'
247 if ($view['operation'] == 'create') {
248 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
249 . '<td><input type="text" size="20" name="view[name]"'
250 . ' onfocus="this.select()" maxlength="64"'
251 . ' value="' . htmlspecialchars($view['name']) . '" />'
254 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
255 . ' value="' . htmlspecialchars($view['name']) . '" />'
259 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
260 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
261 . ' onfocus="this.select()"'
262 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
265 $htmlString .= '<tr><td class="nowrap">AS</td>'
267 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
268 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
269 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
271 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
274 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
275 . '<td><select name="view[with]">'
276 . '<option value=""></option>';
277 foreach ($view_with_options as $option) {
278 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
279 if ($option == $view['with']) {
280 $htmlString .= ' selected="selected"';
282 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
284 $htmlString .= '<select>'
287 $htmlString .= '</table>'
290 if (! isset($_REQUEST['ajax_dialog'])) {
291 $htmlString .= '<fieldset class="tblFooters">'
292 . '<input type="hidden" name="'
293 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
295 . '<input type="submit" name="" value="' . __('Go') . '" />'
298 $htmlString .= '<input type="hidden" name="'
299 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
301 . '<input type="hidden" name="ajax_dialog" value="1" />'
302 . '<input type="hidden" name="ajax_request" value="1" />';
305 $htmlString .= '</form>'