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 $arr = explode('@', $_REQUEST['view']['definer']);
61 $sql_query .= $sep . 'DEFINER=' . PMA_Util
::backquote($arr[0]);
62 $sql_query .= '@' . PMA_Util
::backquote($arr[1]) . ' ';
65 if (isset($_REQUEST['view']['sql_security'])) {
66 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
67 $sql_query .= $sep . ' SQL SECURITY '
68 . $_REQUEST['view']['sql_security'];
72 $sql_query .= $sep . ' VIEW '
73 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
75 if (! empty($_REQUEST['view']['column_names'])) {
76 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
79 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
81 if (isset($_REQUEST['view']['with'])) {
82 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
83 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
88 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
89 if (! isset($_REQUEST['ajax_dialog'])) {
90 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
94 $response = PMA\libraries\Response
::getInstance();
97 PMA\libraries\Message
::error(
98 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
99 . $GLOBALS['dbi']->getError()
102 $response->setRequestStatus(false);
106 // If different column names defined for VIEW
107 $view_columns = array();
108 if (isset($_REQUEST['view']['column_names'])) {
109 $view_columns = explode(',', $_REQUEST['view']['column_names']);
112 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
113 $_REQUEST['view']['as'], $view_columns
116 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
117 $pma_transformation_data = $systemDb->getExistingTransformationData(
121 if ($pma_transformation_data !== false) {
123 // SQL for store new transformation details of VIEW
124 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
125 $pma_transformation_data, $column_map,
126 $_REQUEST['view']['name'], $GLOBALS['db']
129 // Store new transformations
130 if ($new_transformations_sql != '') {
131 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
135 unset($pma_transformation_data);
137 if (! isset($_REQUEST['ajax_dialog'])) {
138 $message = PMA\libraries\Message
::success();
139 include 'tbl_structure.php';
141 $response = PMA\libraries\Response
::getInstance();
144 PMA\libraries\Util
::getMessage(
145 PMA\libraries\Message
::success(),
149 $response->setRequestStatus(true);
155 // prefill values if not already filled from former submission
157 'operation' => 'create',
161 'sql_security' => '',
163 'column_names' => '',
168 if (PMA_isValid($_REQUEST['view'], 'array')) {
169 $view = array_merge($view, $_REQUEST['view']);
172 $url_params['db'] = $GLOBALS['db'];
173 $url_params['reload'] = 1;
178 $htmlString = '<!-- CREATE VIEW options -->'
179 . '<div id="div_view_options">'
180 . '<form method="post" action="view_create.php">'
181 . PMA_URL_getHiddenInputs($url_params)
184 . (isset($_REQUEST['ajax_dialog']) ?
186 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
189 . '<table class="rte_table">';
191 if ($view['operation'] == 'create') {
192 $htmlString .= '<tr>'
193 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
194 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
195 if ($view['or_replace']) {
196 $htmlString .= ' checked="checked"';
198 $htmlString .= ' value="1" /></td></tr>';
201 $htmlString .= '<tr>'
202 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
203 . '<td><select name="view[algorithm]" id="algorithm">';
204 foreach ($view_algorithm_options as $option) {
205 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
206 if ($view['algorithm'] === $option) {
207 $htmlString .= ' selected="selected"';
209 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
211 $htmlString .= '</select>'
214 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
215 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
216 . ' value="' . htmlspecialchars($view['definer']) . '" />'
219 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
220 . '<td><select name="view[sql_security]">'
221 . '<option value=""></option>';
222 foreach ($view_security_options as $option) {
223 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
224 if ($option == $view['sql_security']) {
225 $htmlString .= ' selected="selected"';
227 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
229 $htmlString .= '<select>'
232 if ($view['operation'] == 'create') {
233 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
234 . '<td><input type="text" size="20" name="view[name]"'
235 . ' onfocus="this.select()" maxlength="64"'
236 . ' value="' . htmlspecialchars($view['name']) . '" />'
239 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
240 . ' value="' . htmlspecialchars($view['name']) . '" />'
244 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
245 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
246 . ' onfocus="this.select()"'
247 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
250 $htmlString .= '<tr><td class="nowrap">AS</td>'
252 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
253 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
254 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
256 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
259 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
260 . '<td><select name="view[with]">'
261 . '<option value=""></option>';
262 foreach ($view_with_options as $option) {
263 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
264 if ($option == $view['with']) {
265 $htmlString .= ' selected="selected"';
267 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
269 $htmlString .= '<select>'
272 $htmlString .= '</table>'
275 if (! isset($_REQUEST['ajax_dialog'])) {
276 $htmlString .= '<fieldset class="tblFooters">'
277 . '<input type="hidden" name="'
278 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
280 . '<input type="submit" name="" value="' . __('Go') . '" />'
283 $htmlString .= '<input type="hidden" name="'
284 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
286 . '<input type="hidden" name="ajax_dialog" value="1" />'
287 . '<input type="hidden" name="ajax_request" value="1" />';
290 $htmlString .= '</form>'