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 // View name is a compulsory field
41 if (isset($_REQUEST['view']['name'])
42 && empty($_REQUEST['view']['name'])
44 $message = PMA\libraries\Message
::error(__('View name can not be empty'));
45 $response = PMA\libraries\Response
::getInstance();
50 $response->setRequestStatus(false);
54 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
60 if (isset($_REQUEST['createview'])) {
61 $sql_query = 'CREATE';
62 if (isset($_REQUEST['view']['or_replace'])) {
63 $sql_query .= ' OR REPLACE';
69 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
70 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
73 if (! empty($_REQUEST['view']['definer'])) {
74 $arr = explode('@', $_REQUEST['view']['definer']);
75 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util
::backquote($arr[0]);
76 $sql_query .= '@' . PMA\libraries\Util
::backquote($arr[1]) . ' ';
79 if (isset($_REQUEST['view']['sql_security'])) {
80 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
81 $sql_query .= $sep . ' SQL SECURITY '
82 . $_REQUEST['view']['sql_security'];
86 $sql_query .= $sep . ' VIEW '
87 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
89 if (! empty($_REQUEST['view']['column_names'])) {
90 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
93 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
95 if (isset($_REQUEST['view']['with'])) {
96 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
97 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
102 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
103 if (! isset($_REQUEST['ajax_dialog'])) {
104 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
108 $response = PMA\libraries\Response
::getInstance();
111 PMA\libraries\Message
::error(
112 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
113 . $GLOBALS['dbi']->getError()
116 $response->setRequestStatus(false);
120 // If different column names defined for VIEW
121 $view_columns = array();
122 if (isset($_REQUEST['view']['column_names'])) {
123 $view_columns = explode(',', $_REQUEST['view']['column_names']);
126 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
127 $_REQUEST['view']['as'], $view_columns
130 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
131 $pma_transformation_data = $systemDb->getExistingTransformationData(
135 if ($pma_transformation_data !== false) {
137 // SQL for store new transformation details of VIEW
138 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
139 $pma_transformation_data, $column_map,
140 $_REQUEST['view']['name'], $GLOBALS['db']
143 // Store new transformations
144 if ($new_transformations_sql != '') {
145 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
149 unset($pma_transformation_data);
151 if (! isset($_REQUEST['ajax_dialog'])) {
152 $message = PMA\libraries\Message
::success();
153 include 'tbl_structure.php';
155 $response = PMA\libraries\Response
::getInstance();
158 PMA\libraries\Util
::getMessage(
159 PMA\libraries\Message
::success(),
163 $response->setRequestStatus(true);
169 // prefill values if not already filled from former submission
171 'operation' => 'create',
175 'sql_security' => '',
177 'column_names' => '',
182 if (PMA_isValid($_REQUEST['view'], 'array')) {
183 $view = array_merge($view, $_REQUEST['view']);
186 $url_params['db'] = $GLOBALS['db'];
187 $url_params['reload'] = 1;
192 $htmlString = '<!-- CREATE VIEW options -->'
193 . '<div id="div_view_options">'
194 . '<form method="post" action="view_create.php">'
195 . PMA_URL_getHiddenInputs($url_params)
198 . (isset($_REQUEST['ajax_dialog']) ?
200 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
203 . '<table class="rte_table">';
205 if ($view['operation'] == 'create') {
206 $htmlString .= '<tr>'
207 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
208 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
209 if ($view['or_replace']) {
210 $htmlString .= ' checked="checked"';
212 $htmlString .= ' value="1" /></td></tr>';
215 $htmlString .= '<tr>'
216 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
217 . '<td><select name="view[algorithm]" id="algorithm">';
218 foreach ($view_algorithm_options as $option) {
219 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
220 if ($view['algorithm'] === $option) {
221 $htmlString .= ' selected="selected"';
223 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
225 $htmlString .= '</select>'
228 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
229 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
230 . ' value="' . htmlspecialchars($view['definer']) . '" />'
233 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
234 . '<td><select name="view[sql_security]">'
235 . '<option value=""></option>';
236 foreach ($view_security_options as $option) {
237 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
238 if ($option == $view['sql_security']) {
239 $htmlString .= ' selected="selected"';
241 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
243 $htmlString .= '<select>'
246 if ($view['operation'] == 'create') {
247 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
248 . '<td><input type="text" size="20" name="view[name]"'
249 . ' onfocus="this.select()" maxlength="64"'
250 . ' value="' . htmlspecialchars($view['name']) . '" />'
253 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
254 . ' value="' . htmlspecialchars($view['name']) . '" />'
258 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
259 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
260 . ' onfocus="this.select()"'
261 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
264 $htmlString .= '<tr><td class="nowrap">AS</td>'
266 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
267 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
268 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
270 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
273 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
274 . '<td><select name="view[with]">'
275 . '<option value=""></option>';
276 foreach ($view_with_options as $option) {
277 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
278 if ($option == $view['with']) {
279 $htmlString .= ' selected="selected"';
281 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
283 $htmlString .= '<select>'
286 $htmlString .= '</table>'
289 if (! isset($_REQUEST['ajax_dialog'])) {
290 $htmlString .= '<fieldset class="tblFooters">'
291 . '<input type="hidden" name="'
292 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
294 . '<input type="submit" name="" value="' . __('Go') . '" />'
297 $htmlString .= '<input type="hidden" name="'
298 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
300 . '<input type="hidden" name="ajax_dialog" value="1" />'
301 . '<input type="hidden" name="ajax_request" value="1" />';
304 $htmlString .= '</form>'