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_tryQuery($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_tryQuery($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>$sql_query</i><br /><br />" . PMA_DBI_getError()
120 $response->isSuccess(false);
126 // prefill values if not already filled from former submission
131 'column_names' => '',
136 if (PMA_isValid($_REQUEST['view'], 'array')) {
137 $view = array_merge($view, $_REQUEST['view']);
140 $url_params['db'] = $GLOBALS['db'];
141 $url_params['reload'] = 1;
146 $htmlString = '<!-- CREATE VIEW options -->'
147 . '<div id="div_view_options">'
148 . '<form method="post" action="view_create.php">'
149 . PMA_generate_common_hidden_inputs($url_params)
151 . '<legend>' . __('Create view')
152 . PMA_Util
::showMySQLDocu('SQL-Syntax', 'CREATE_VIEW') . '</legend>'
153 . '<table class="rte_table">'
154 . '<tr><td><label for="or_replace">OR REPLACE</label></td>'
155 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
156 if ($view['or_replace']) {
157 $htmlString .= ' checked="checked"';
159 $htmlString .= ' value="1" />'
163 . '<td><label for="algorithm">ALGORITHM</label></td>'
164 . '<td><select name="view[algorithm]" id="algorithm">';
166 foreach ($view_algorithm_options as $option) {
167 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
168 if ($view['algorithm'] === $option) {
169 $htmlString .= ' selected="selected"';
171 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
174 $htmlString .= '</select>'
177 . '<tr><td>' . __('VIEW name') . '</td>'
178 . '<td><input type="text" size="20" name="view[name]" onfocus="this.select()"'
179 . ' value="' . htmlspecialchars($view['name']) . '" />'
182 . '<tr><td>' . __('Column names') . '</td>'
183 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
184 . ' onfocus="this.select()"'
185 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
190 . '<textarea name="view[as]" rows="' . $cfg['TextareaRows'] . '"'
191 . ' cols="' . $cfg['TextareaCols'] . '"'
192 . ' dir="' . $text_dir . '"';
194 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
195 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
198 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
201 . '<tr><td>WITH</td>'
204 foreach ($view_with_options as $option) {
205 $htmlString .= '<input type="checkbox" name="view[with][]"';
206 if (in_array($option, $view['with'])) {
207 $htmlString .= ' checked="checked"';
209 $htmlString .= ' id="view_with_'
210 . str_replace(' ', '_', htmlspecialchars($option)) . '"'
211 . ' value="' . htmlspecialchars($option) . '" />'
212 . '<label for="view_with_' . str_replace(' ', '_', htmlspecialchars($option))
214 . htmlspecialchars($option) . '</label><br />';
217 $htmlString .= '</td>'
222 if ($GLOBALS['is_ajax_request'] != true) {
223 $htmlString .= '<fieldset class="tblFooters">'
224 . '<input type="submit" name="createview" value="' . __('Go') . '" />'
227 $htmlString .= '<input type="hidden" name="createview" value="1" />'
228 . '<input type="hidden" name="ajax_request" value="1" />';
231 $htmlString .= '</form>'