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';
12 require_once './libraries/SystemDatabase.class.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 $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
64 if (isset($_REQUEST['view']['sql_security'])) {
65 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
66 $sql_query .= $sep . ' SQL SECURITY '
67 . $_REQUEST['view']['sql_security'];
71 $sql_query .= $sep . ' VIEW ' . PMA_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_Message
::rawError($GLOBALS['dbi']->getError());
92 $response = PMA_Response
::getInstance();
96 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
97 . $GLOBALS['dbi']->getError()
100 $response->isSuccess(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_Message
::success();
137 include 'tbl_structure.php';
139 $response = PMA_Response
::getInstance();
142 PMA_Util
::getMessage(PMA_Message
::success(), $sql_query)
144 $response->isSuccess(true);
150 // prefill values if not already filled from former submission
152 'operation' => 'create',
156 'sql_security' => '',
158 'column_names' => '',
163 if (PMA_isValid($_REQUEST['view'], 'array')) {
164 $view = array_merge($view, $_REQUEST['view']);
167 $url_params['db'] = $GLOBALS['db'];
168 $url_params['reload'] = 1;
173 $htmlString = '<!-- CREATE VIEW options -->'
174 . '<div id="div_view_options">'
175 . '<form method="post" action="view_create.php">'
176 . PMA_URL_getHiddenInputs($url_params)
179 . (isset($_REQUEST['ajax_dialog']) ?
181 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
184 . '<table class="rte_table">';
186 if ($view['operation'] == 'create') {
187 $htmlString .= '<tr>'
188 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
189 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
190 if ($view['or_replace']) {
191 $htmlString .= ' checked="checked"';
193 $htmlString .= ' value="1" /></td></tr>';
196 $htmlString .= '<tr>'
197 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
198 . '<td><select name="view[algorithm]" id="algorithm">';
199 foreach ($view_algorithm_options as $option) {
200 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
201 if ($view['algorithm'] === $option) {
202 $htmlString .= ' selected="selected"';
204 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
206 $htmlString .= '</select>'
209 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
210 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
211 . ' value="' . htmlspecialchars($view['definer']) . '" />'
214 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
215 . '<td><select name="view[sql_security]">'
216 . '<option value=""></option>';
217 foreach ($view_security_options as $option) {
218 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
219 if ($option == $view['sql_security']) {
220 $htmlString .= ' selected="selected"';
222 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
224 $htmlString .= '<select>'
227 if ($view['operation'] == 'create') {
228 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
229 . '<td><input type="text" size="20" name="view[name]"'
230 . ' onfocus="this.select()" maxlength="64"'
231 . ' value="' . htmlspecialchars($view['name']) . '" />'
234 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
235 . ' value="' . htmlspecialchars($view['name']) . '" />'
239 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
240 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
241 . ' onfocus="this.select()"'
242 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
245 $htmlString .= '<tr><td class="nowrap">AS</td>'
247 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
248 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
249 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
251 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
254 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
255 . '<td><select name="view[with]">'
256 . '<option value=""></option>';
257 foreach ($view_with_options as $option) {
258 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
259 if ($option == $view['with']) {
260 $htmlString .= ' selected="selected"';
262 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
264 $htmlString .= '<select>'
267 $htmlString .= '</table>'
270 if (! isset($_REQUEST['ajax_dialog'])) {
271 $htmlString .= '<fieldset class="tblFooters">'
272 . '<input type="hidden" name="'
273 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
275 . '<input type="submit" name="" value="' . __('Go') . '" />'
278 $htmlString .= '<input type="hidden" name="'
279 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
281 . '<input type="hidden" name="ajax_dialog" value="1" />'
282 . '<input type="hidden" name="ajax_request" value="1" />';
285 $htmlString .= '</form>'