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 require_once './libraries/common.inc.php';
16 require './libraries/db_common.inc.php';
17 $url_params['goto'] = 'tbl_structure.php';
18 $url_params['back'] = 'view_create.php';
20 $view_algorithm_options = array(
26 $view_with_options = array(
31 $view_security_options = array(
36 if (empty($sql_query)) {
40 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
46 if (isset($_REQUEST['createview'])) {
47 $sql_query = 'CREATE';
48 if (isset($_REQUEST['view']['or_replace'])) {
49 $sql_query .= ' OR REPLACE';
55 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
56 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
59 if (! empty($_REQUEST['view']['definer'])) {
60 $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
63 if (isset($_REQUEST['view']['sql_security'])) {
64 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
65 $sql_query .= $sep . ' SQL SECURITY '
66 . $_REQUEST['view']['sql_security'];
70 $sql_query .= $sep . ' VIEW '
71 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
73 if (! empty($_REQUEST['view']['column_names'])) {
74 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
77 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
79 if (isset($_REQUEST['view']['with'])) {
80 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
81 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
86 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
87 if (! isset($_REQUEST['ajax_dialog'])) {
88 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
92 $response = PMA\libraries\Response
::getInstance();
95 PMA\libraries\Message
::error(
96 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
97 . $GLOBALS['dbi']->getError()
100 $response->setRequestStatus(false);
104 // If different column names defined for VIEW
105 $view_columns = array();
106 if (isset($_REQUEST['view']['column_names'])) {
107 $view_columns = explode(',', $_REQUEST['view']['column_names']);
110 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
111 $_REQUEST['view']['as'], $view_columns
114 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
115 $pma_transformation_data = $systemDb->getExistingTransformationData(
119 if ($pma_transformation_data !== false) {
121 // SQL for store new transformation details of VIEW
122 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
123 $pma_transformation_data, $column_map,
124 $_REQUEST['view']['name'], $GLOBALS['db']
127 // Store new transformations
128 if ($new_transformations_sql != '') {
129 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
133 unset($pma_transformation_data);
135 if (! isset($_REQUEST['ajax_dialog'])) {
136 $message = PMA\libraries\Message
::success();
137 include 'tbl_structure.php';
139 $response = PMA\libraries\Response
::getInstance();
142 PMA\libraries\Util
::getMessage(
143 PMA\libraries\Message
::success(),
147 $response->setRequestStatus(true);
153 // prefill values if not already filled from former submission
155 'operation' => 'create',
159 'sql_security' => '',
161 'column_names' => '',
166 if (PMA_isValid($_REQUEST['view'], 'array')) {
167 $view = array_merge($view, $_REQUEST['view']);
170 $url_params['db'] = $GLOBALS['db'];
171 $url_params['reload'] = 1;
176 $htmlString = '<!-- CREATE VIEW options -->'
177 . '<div id="div_view_options">'
178 . '<form method="post" action="view_create.php">'
179 . PMA_URL_getHiddenInputs($url_params)
182 . (isset($_REQUEST['ajax_dialog']) ?
184 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
187 . '<table class="rte_table">';
189 if ($view['operation'] == 'create') {
190 $htmlString .= '<tr>'
191 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
192 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
193 if ($view['or_replace']) {
194 $htmlString .= ' checked="checked"';
196 $htmlString .= ' value="1" /></td></tr>';
199 $htmlString .= '<tr>'
200 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
201 . '<td><select name="view[algorithm]" id="algorithm">';
202 foreach ($view_algorithm_options as $option) {
203 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
204 if ($view['algorithm'] === $option) {
205 $htmlString .= ' selected="selected"';
207 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
209 $htmlString .= '</select>'
212 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
213 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
214 . ' value="' . htmlspecialchars($view['definer']) . '" />'
217 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
218 . '<td><select name="view[sql_security]">'
219 . '<option value=""></option>';
220 foreach ($view_security_options as $option) {
221 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
222 if ($option == $view['sql_security']) {
223 $htmlString .= ' selected="selected"';
225 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
227 $htmlString .= '<select>'
230 if ($view['operation'] == 'create') {
231 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
232 . '<td><input type="text" size="20" name="view[name]"'
233 . ' onfocus="this.select()" maxlength="64"'
234 . ' value="' . htmlspecialchars($view['name']) . '" />'
237 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
238 . ' value="' . htmlspecialchars($view['name']) . '" />'
242 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
243 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
244 . ' onfocus="this.select()"'
245 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
248 $htmlString .= '<tr><td class="nowrap">AS</td>'
250 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
251 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
252 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
254 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
257 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
258 . '<td><select name="view[with]">'
259 . '<option value=""></option>';
260 foreach ($view_with_options as $option) {
261 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
262 if ($option == $view['with']) {
263 $htmlString .= ' selected="selected"';
265 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
267 $htmlString .= '<select>'
270 $htmlString .= '</table>'
273 if (! isset($_REQUEST['ajax_dialog'])) {
274 $htmlString .= '<fieldset class="tblFooters">'
275 . '<input type="hidden" name="'
276 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
278 . '<input type="submit" name="" value="' . __('Go') . '" />'
281 $htmlString .= '<input type="hidden" name="'
282 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
284 . '<input type="hidden" name="ajax_dialog" value="1" />'
285 . '<input type="hidden" name="ajax_request" value="1" />';
288 $htmlString .= '</form>'