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 ' . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
72 if (! empty($_REQUEST['view']['column_names'])) {
73 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
76 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
78 if (isset($_REQUEST['view']['with'])) {
79 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
80 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
85 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
86 if (! isset($_REQUEST['ajax_dialog'])) {
87 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
91 $response = PMA\libraries\Response
::getInstance();
94 PMA\libraries\Message
::error(
95 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
96 . $GLOBALS['dbi']->getError()
99 $response->setRequestStatus(false);
103 // If different column names defined for VIEW
104 $view_columns = array();
105 if (isset($_REQUEST['view']['column_names'])) {
106 $view_columns = explode(',', $_REQUEST['view']['column_names']);
109 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
110 $_REQUEST['view']['as'], $view_columns
113 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
114 $pma_transformation_data = $systemDb->getExistingTransformationData(
118 if ($pma_transformation_data !== false) {
120 // SQL for store new transformation details of VIEW
121 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
122 $pma_transformation_data, $column_map,
123 $_REQUEST['view']['name'], $GLOBALS['db']
126 // Store new transformations
127 if ($new_transformations_sql != '') {
128 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
132 unset($pma_transformation_data);
134 if (! isset($_REQUEST['ajax_dialog'])) {
135 $message = PMA\libraries\Message
::success();
136 include 'tbl_structure.php';
138 $response = PMA\libraries\Response
::getInstance();
141 PMA\libraries\Util
::getMessage(PMA\libraries\Message
::success(), $sql_query)
143 $response->setRequestStatus(true);
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()" maxlength="64"'
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="15" cols="40" dir="' . $text_dir . '"';
247 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
248 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
250 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
253 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
254 . '<td><select name="view[with]">'
255 . '<option value=""></option>';
256 foreach ($view_with_options as $option) {
257 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
258 if ($option == $view['with']) {
259 $htmlString .= ' selected="selected"';
261 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
263 $htmlString .= '<select>'
266 $htmlString .= '</table>'
269 if (! isset($_REQUEST['ajax_dialog'])) {
270 $htmlString .= '<fieldset class="tblFooters">'
271 . '<input type="hidden" name="'
272 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
274 . '<input type="submit" name="" value="' . __('Go') . '" />'
277 $htmlString .= '<input type="hidden" name="'
278 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
280 . '<input type="hidden" name="ajax_dialog" value="1" />'
281 . '<input type="hidden" name="ajax_request" value="1" />';
284 $htmlString .= '</form>'