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(
39 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
45 if (isset($_REQUEST['createview'])) {
46 $sql_query = 'CREATE';
47 if (isset($_REQUEST['view']['or_replace'])) {
48 $sql_query .= ' OR REPLACE';
54 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
55 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
58 if (! empty($_REQUEST['view']['definer'])) {
59 $sql_query .= $sep . ' DEFINER = ' . $_REQUEST['view']['definer'];
62 if (isset($_REQUEST['view']['sql_security'])) {
63 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
64 $sql_query .= $sep . ' SQL SECURITY '
65 . $_REQUEST['view']['sql_security'];
69 $sql_query .= $sep . ' VIEW ' . PMA_Util
::backquote($_REQUEST['view']['name']);
71 if (! empty($_REQUEST['view']['column_names'])) {
72 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
75 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
77 if (isset($_REQUEST['view']['with'])) {
78 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
79 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
84 if ($GLOBALS['dbi']->tryQuery($sql_query)) {
86 include_once './libraries/tbl_views.lib.php';
88 // If different column names defined for VIEW
89 $view_columns = array();
90 if (isset($_REQUEST['view']['column_names'])) {
91 $view_columns = explode(',', $_REQUEST['view']['column_names']);
94 $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
95 $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
97 if ($pma_tranformation_data !== false) {
99 // SQL for store new transformation details of VIEW
100 $new_transformations_sql = PMA_getNewTransformationDataSql(
101 $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
105 // Store new transformations
106 if ($new_transformations_sql != '') {
107 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
111 unset($pma_tranformation_data);
113 if (! isset($_REQUEST['ajax_dialog'])) {
114 $message = PMA_Message
::success();
115 include 'tbl_structure.php';
117 $response = PMA_Response
::getInstance();
120 PMA_Util
::getMessage(
121 PMA_Message
::success(), $sql_query
124 $response->isSuccess(true);
130 if (! isset($_REQUEST['ajax_dialog'])) {
131 $message = PMA_Message
::rawError($GLOBALS['dbi']->getError());
133 $response = PMA_Response
::getInstance();
137 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
138 . $GLOBALS['dbi']->getError()
141 $response->isSuccess(false);
147 // prefill values if not already filled from former submission
149 'operation' => 'create',
153 'sql_security' => '',
155 'column_names' => '',
160 if (PMA_isValid($_REQUEST['view'], 'array')) {
161 $view = array_merge($view, $_REQUEST['view']);
164 $url_params['db'] = $GLOBALS['db'];
165 $url_params['reload'] = 1;
170 $htmlString = '<!-- CREATE VIEW options -->'
171 . '<div id="div_view_options">'
172 . '<form method="post" action="view_create.php">'
173 . PMA_URL_getHiddenInputs($url_params)
176 . (isset($_REQUEST['ajax_dialog']) ?
178 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
181 . '<table class="rte_table">';
183 if ($view['operation'] == 'create') {
184 $htmlString .= '<tr>'
185 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
186 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
187 if ($view['or_replace']) {
188 $htmlString .= ' checked="checked"';
190 $htmlString .= ' value="1" /></td></tr>';
193 $htmlString .= '<tr>'
194 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
195 . '<td><select name="view[algorithm]" id="algorithm">';
196 foreach ($view_algorithm_options as $option) {
197 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
198 if ($view['algorithm'] === $option) {
199 $htmlString .= ' selected="selected"';
201 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
203 $htmlString .= '</select>'
206 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
207 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
208 . ' value="' . htmlspecialchars($view['definer']) . '" />'
211 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
212 . '<td><select name="view[sql_security]">'
213 . '<option value=""></option>';
214 foreach ($view_security_options as $option) {
215 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
216 if ($option == $view['sql_security']) {
217 $htmlString .= ' selected="selected"';
219 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
221 $htmlString .= '<select>'
224 if ($view['operation'] == 'create') {
225 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
226 . '<td><input type="text" size="20" name="view[name]"'
227 . ' onfocus="this.select()"'
228 . ' value="' . htmlspecialchars($view['name']) . '" />'
231 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
232 . ' value="' . htmlspecialchars($view['name']) . '" />'
236 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
237 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
238 . ' onfocus="this.select()"'
239 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
242 $htmlString .= '<tr><td class="nowrap">AS</td>'
244 . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
245 . ' cols="' . $cfg['TextareaCols'] . '" dir="' . $text_dir . '"';
246 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
247 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
249 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
252 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
253 . '<td><select name="view[with]">'
254 . '<option value=""></option>';
255 foreach ($view_with_options as $option) {
256 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
257 if ($option == $view['with']) {
258 $htmlString .= ' selected="selected"';
260 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
262 $htmlString .= '<select>'
265 $htmlString .= '</table>'
268 if (! isset($_REQUEST['ajax_dialog'])) {
269 $htmlString .= '<fieldset class="tblFooters">'
270 . '<input type="hidden" name="'
271 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
273 . '<input type="submit" name="" value="' . __('Go') . '" />'
276 $htmlString .= '<input type="hidden" name="'
277 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
279 . '<input type="hidden" name="ajax_dialog" value="1" />'
280 . '<input type="hidden" name="ajax_request" value="1" />';
283 $htmlString .= '</form>'