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?)
14 require_once './libraries/common.inc.php';
19 require './libraries/db_common.inc.php';
20 $url_params['goto'] = 'tbl_structure.php';
21 $url_params['back'] = 'view_create.php';
23 $view_algorithm_options = array(
29 $view_with_options = array(
34 $view_security_options = array(
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)) {
88 include_once './libraries/tbl_views.lib.php';
90 // If different column names defined for VIEW
91 $view_columns = array();
92 if (isset($_REQUEST['view']['column_names'])) {
93 $view_columns = explode(',', $_REQUEST['view']['column_names']);
96 $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
97 $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
99 if ($pma_tranformation_data !== false) {
101 // SQL for store new transformation details of VIEW
102 $new_transformations_sql = PMA_getNewTransformationDataSql(
103 $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
107 // Store new transformations
108 if ($new_transformations_sql != '') {
109 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
113 unset($pma_tranformation_data);
115 if (! isset($_REQUEST['ajax_dialog'])) {
116 $message = PMA_Message
::success();
117 include 'tbl_structure.php';
119 $response = PMA_Response
::getInstance();
122 PMA_Util
::getMessage(
123 PMA_Message
::success(), $sql_query
126 $response->isSuccess(true);
132 if (! isset($_REQUEST['ajax_dialog'])) {
133 $message = PMA_Message
::rawError($GLOBALS['dbi']->getError());
135 $response = PMA_Response
::getInstance();
139 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
140 . $GLOBALS['dbi']->getError()
143 $response->isSuccess(false);
149 // prefill values if not already filled from former submission
151 'operation' => 'create',
155 'sql_security' => '',
157 'column_names' => '',
162 if (PMA_isValid($_REQUEST['view'], 'array')) {
163 $view = array_merge($view, $_REQUEST['view']);
166 $url_params['db'] = $GLOBALS['db'];
167 $url_params['reload'] = 1;
172 $htmlString = '<!-- CREATE VIEW options -->'
173 . '<div id="div_view_options">'
174 . '<form method="post" action="view_create.php">'
175 . PMA_URL_getHiddenInputs($url_params)
178 . (isset($_REQUEST['ajax_dialog']) ?
180 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
183 . '<table class="rte_table">';
185 if ($view['operation'] == 'create') {
186 $htmlString .= '<tr>'
187 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
188 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
189 if ($view['or_replace']) {
190 $htmlString .= ' checked="checked"';
192 $htmlString .= ' value="1" /></td></tr>';
195 $htmlString .= '<tr>'
196 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
197 . '<td><select name="view[algorithm]" id="algorithm">';
198 foreach ($view_algorithm_options as $option) {
199 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
200 if ($view['algorithm'] === $option) {
201 $htmlString .= ' selected="selected"';
203 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
205 $htmlString .= '</select>'
208 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
209 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
210 . ' value="' . htmlspecialchars($view['definer']) . '" />'
213 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
214 . '<td><select name="view[sql_security]">'
215 . '<option value=""></option>';
216 foreach ($view_security_options as $option) {
217 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
218 if ($option == $view['sql_security']) {
219 $htmlString .= ' selected="selected"';
221 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
223 $htmlString .= '<select>'
226 if ($view['operation'] == 'create') {
227 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
228 . '<td><input type="text" size="20" name="view[name]"'
229 . ' onfocus="this.select()"'
230 . ' value="' . htmlspecialchars($view['name']) . '" />'
233 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
234 . ' value="' . htmlspecialchars($view['name']) . '" />'
238 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
239 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
240 . ' onfocus="this.select()"'
241 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
244 $htmlString .= '<tr><td class="nowrap">AS</td>'
246 . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
247 . ' cols="' . $cfg['TextareaCols'] . '" 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>'