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'] = $cfg['DefaultTabDatabase'];
21 $url_params['back'] = 'view_create.php';
23 $view_algorithm_options = array(
29 $view_with_options = array(
30 'CASCADED CHECK OPTION',
34 if (isset($_REQUEST['createview'])) {
40 $sql_query = 'CREATE';
42 if (isset($_REQUEST['view']['or_replace'])) {
43 $sql_query .= ' OR REPLACE';
46 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
47 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
50 $sql_query .= $sep . ' VIEW ' . PMA_Util
::backquote($_REQUEST['view']['name']);
52 if (! empty($_REQUEST['view']['column_names'])) {
53 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
56 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
58 if (isset($_REQUEST['view']['with'])) {
59 $options = array_intersect($_REQUEST['view']['with'], $view_with_options);
60 if (count($options)) {
61 $sql_query .= $sep . ' WITH ' . implode(' ', $options);
65 if (PMA_DBI_try_query($sql_query)) {
67 include_once './libraries/tbl_views.lib.php';
69 // If different column names defined for VIEW
70 $view_columns = array();
71 if (isset($_REQUEST['view']['column_names'])) {
72 $view_columns = explode(',', $_REQUEST['view']['column_names']);
75 $column_map = PMA_getColumnMap($_REQUEST['view']['as'], $view_columns);
76 $pma_tranformation_data = PMA_getExistingTranformationData($GLOBALS['db']);
78 if ($pma_tranformation_data !== false) {
80 // SQL for store new transformation details of VIEW
81 $new_transformations_sql = PMA_getNewTransformationDataSql(
82 $pma_tranformation_data, $column_map, $_REQUEST['view']['name'],
86 // Store new transformations
87 if ($new_transformations_sql != '') {
88 PMA_DBI_try_query($new_transformations_sql);
92 unset($pma_tranformation_data);
94 if ($GLOBALS['is_ajax_request'] != true) {
95 $message = PMA_Message
::success();
96 include './' . $cfg['DefaultTabDatabase'];
98 $response = PMA_Response
::getInstance();
101 PMA_Util
::getMessage(
102 PMA_Message
::success(), $sql_query
110 if ($GLOBALS['is_ajax_request'] != true) {
111 $message = PMA_Message
::rawError(PMA_DBI_getError());
113 $response = PMA_Response
::getInstance();
117 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
121 $response->isSuccess(false);
127 // prefill values if not already filled from former submission
132 'column_names' => '',
137 if (PMA_isValid($_REQUEST['view'], 'array')) {
138 $view = array_merge($view, $_REQUEST['view']);
141 $url_params['db'] = $GLOBALS['db'];
142 $url_params['reload'] = 1;
147 $htmlString = '<!-- CREATE VIEW options -->'
148 . '<div id="div_view_options">'
149 . '<form method="post" action="view_create.php">'
150 . PMA_generate_common_hidden_inputs($url_params)
152 . '<legend>' . __('Create view')
153 . PMA_Util
::showMySQLDocu('SQL-Syntax', 'CREATE_VIEW') . '</legend>'
154 . '<table class="rte_table">'
155 . '<tr><td><label for="or_replace">OR REPLACE</label></td>'
156 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
157 if ($view['or_replace']) {
158 $htmlString .= ' checked="checked"';
160 $htmlString .= ' value="1" />'
164 . '<td><label for="algorithm">ALGORITHM</label></td>'
165 . '<td><select name="view[algorithm]" id="algorithm">';
167 foreach ($view_algorithm_options as $option) {
168 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
169 if ($view['algorithm'] === $option) {
170 $htmlString .= ' selected="selected"';
172 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
175 $htmlString .= '</select>'
178 . '<tr><td>' . __('VIEW name') . '</td>'
179 . '<td><input type="text" size="20" name="view[name]" onfocus="this.select()"'
180 . ' value="' . htmlspecialchars($view['name']) . '" />'
183 . '<tr><td>' . __('Column names') . '</td>'
184 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
185 . ' onfocus="this.select()"'
186 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
191 . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
192 . ' cols="' . $cfg['TextareaCols'] . '"'
193 . ' dir="' . $text_dir . '"';
195 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
196 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
199 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
202 . '<tr><td>WITH</td>'
205 foreach ($view_with_options as $option) {
206 $htmlString .= '<input type="checkbox" name="view[with][]"';
207 if (in_array($option, $view['with'])) {
208 $htmlString .= ' checked="checked"';
210 $htmlString .= ' id="view_with_'
211 . str_replace(' ', '_', htmlspecialchars($option)) . '"'
212 . ' value="' . htmlspecialchars($option) . '" />'
213 . '<label for="view_with_' . str_replace(' ', '_', htmlspecialchars($option))
215 . htmlspecialchars($option) . '</label><br />';
218 $htmlString .= '</td>'
223 if ($GLOBALS['is_ajax_request'] != true) {
224 $htmlString .= '<fieldset class="tblFooters">'
225 . '<input type="submit" name="createview" value="' . __('Go') . '" />'
228 $htmlString .= '<input type="hidden" name="createview" value="1" />'
229 . '<input type="hidden" name="ajax_request" value="1" />';
232 $htmlString .= '</form>'