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?)
10 use PMA\libraries\URL
;
11 use PMA\libraries\Response
;
13 require_once './libraries/common.inc.php';
18 require './libraries/db_common.inc.php';
19 $url_params['goto'] = 'tbl_structure.php';
20 $url_params['back'] = 'view_create.php';
22 $response = Response
::getInstance();
24 $view_algorithm_options = array(
30 $view_with_options = array(
35 $view_security_options = array(
40 if (empty($sql_query)) {
44 // View name is a compulsory field
45 if (isset($_REQUEST['view']['name'])
46 && empty($_REQUEST['view']['name'])
48 $message = PMA\libraries\Message
::error(__('View name can not be empty!'));
53 $response->setRequestStatus(false);
57 if (isset($_REQUEST['createview']) ||
isset($_REQUEST['alterview'])) {
63 if (isset($_REQUEST['createview'])) {
64 $sql_query = 'CREATE';
65 if (isset($_REQUEST['view']['or_replace'])) {
66 $sql_query .= ' OR REPLACE';
72 if (PMA_isValid($_REQUEST['view']['algorithm'], $view_algorithm_options)) {
73 $sql_query .= $sep . ' ALGORITHM = ' . $_REQUEST['view']['algorithm'];
76 if (! empty($_REQUEST['view']['definer'])) {
77 if (strpos($_REQUEST['view']['definer'], '@') === FALSE) {
78 $sql_query .= $sep . 'DEFINER='
79 . PMA\libraries\Util
::backquote($_REQUEST['view']['definer']);
81 $arr = explode('@', $_REQUEST['view']['definer']);
82 $sql_query .= $sep . 'DEFINER=' . PMA\libraries\Util
::backquote($arr[0]);
83 $sql_query .= '@' . PMA\libraries\Util
::backquote($arr[1]) . ' ';
87 if (isset($_REQUEST['view']['sql_security'])) {
88 if (in_array($_REQUEST['view']['sql_security'], $view_security_options)) {
89 $sql_query .= $sep . ' SQL SECURITY '
90 . $_REQUEST['view']['sql_security'];
94 $sql_query .= $sep . ' VIEW '
95 . PMA\libraries\Util
::backquote($_REQUEST['view']['name']);
97 if (! empty($_REQUEST['view']['column_names'])) {
98 $sql_query .= $sep . ' (' . $_REQUEST['view']['column_names'] . ')';
101 $sql_query .= $sep . ' AS ' . $_REQUEST['view']['as'];
103 if (isset($_REQUEST['view']['with'])) {
104 if (in_array($_REQUEST['view']['with'], $view_with_options)) {
105 $sql_query .= $sep . ' WITH ' . $_REQUEST['view']['with']
110 if (!$GLOBALS['dbi']->tryQuery($sql_query)) {
111 if (! isset($_REQUEST['ajax_dialog'])) {
112 $message = PMA\libraries\Message
::rawError($GLOBALS['dbi']->getError());
118 PMA\libraries\Message
::error(
119 "<i>" . htmlspecialchars($sql_query) . "</i><br /><br />"
120 . $GLOBALS['dbi']->getError()
123 $response->setRequestStatus(false);
127 // If different column names defined for VIEW
128 $view_columns = array();
129 if (isset($_REQUEST['view']['column_names'])) {
130 $view_columns = explode(',', $_REQUEST['view']['column_names']);
133 $column_map = $GLOBALS['dbi']->getColumnMapFromSql(
134 $_REQUEST['view']['as'], $view_columns
137 $systemDb = $GLOBALS['dbi']->getSystemDatabase();
138 $pma_transformation_data = $systemDb->getExistingTransformationData(
142 if ($pma_transformation_data !== false) {
144 // SQL for store new transformation details of VIEW
145 $new_transformations_sql = $systemDb->getNewTransformationDataSql(
146 $pma_transformation_data, $column_map,
147 $_REQUEST['view']['name'], $GLOBALS['db']
150 // Store new transformations
151 if ($new_transformations_sql != '') {
152 $GLOBALS['dbi']->tryQuery($new_transformations_sql);
156 unset($pma_transformation_data);
158 if (! isset($_REQUEST['ajax_dialog'])) {
159 $message = PMA\libraries\Message
::success();
160 include 'tbl_structure.php';
164 PMA\libraries\Util
::getMessage(
165 PMA\libraries\Message
::success(),
169 $response->setRequestStatus(true);
175 // prefill values if not already filled from former submission
177 'operation' => 'create',
181 'sql_security' => '',
183 'column_names' => '',
188 if (PMA_isValid($_REQUEST['view'], 'array')) {
189 $view = array_merge($view, $_REQUEST['view']);
192 $url_params['db'] = $GLOBALS['db'];
193 $url_params['reload'] = 1;
198 $htmlString = '<!-- CREATE VIEW options -->'
199 . '<div id="div_view_options">'
200 . '<form method="post" action="view_create.php">'
201 . URL
::getHiddenInputs($url_params)
204 . (isset($_REQUEST['ajax_dialog']) ?
206 ($view['operation'] == 'create' ?
__('Create view') : __('Edit view'))
209 . '<table class="rte_table">';
211 if ($view['operation'] == 'create') {
212 $htmlString .= '<tr>'
213 . '<td class="nowrap"><label for="or_replace">OR REPLACE</label></td>'
214 . '<td><input type="checkbox" name="view[or_replace]" id="or_replace"';
215 if ($view['or_replace']) {
216 $htmlString .= ' checked="checked"';
218 $htmlString .= ' value="1" /></td></tr>';
221 $htmlString .= '<tr>'
222 . '<td class="nowrap"><label for="algorithm">ALGORITHM</label></td>'
223 . '<td><select name="view[algorithm]" id="algorithm">';
224 foreach ($view_algorithm_options as $option) {
225 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
226 if ($view['algorithm'] === $option) {
227 $htmlString .= ' selected="selected"';
229 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
231 $htmlString .= '</select>'
234 $htmlString .= '<tr><td class="nowrap">' . __('Definer') . '</td>'
235 . '<td><input type="text" maxlength="100" size="50" name="view[definer]"'
236 . ' value="' . htmlspecialchars($view['definer']) . '" />'
239 $htmlString .= '<tr><td class="nowrap">SQL SECURITY</td>'
240 . '<td><select name="view[sql_security]">'
241 . '<option value=""></option>';
242 foreach ($view_security_options as $option) {
243 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
244 if ($option == $view['sql_security']) {
245 $htmlString .= ' selected="selected"';
247 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
249 $htmlString .= '<select>'
252 if ($view['operation'] == 'create') {
253 $htmlString .= '<tr><td class="nowrap">' . __('VIEW name') . '</td>'
254 . '<td><input type="text" size="20" name="view[name]"'
255 . ' onfocus="this.select()" maxlength="64"'
256 . ' value="' . htmlspecialchars($view['name']) . '" />'
259 $htmlString .= '<tr><td><input type="hidden" name="view[name]"'
260 . ' value="' . htmlspecialchars($view['name']) . '" />'
264 $htmlString .= '<tr><td class="nowrap">' . __('Column names') . '</td>'
265 . '<td><input type="text" maxlength="100" size="50" name="view[column_names]"'
266 . ' onfocus="this.select()"'
267 . ' value="' . htmlspecialchars($view['column_names']) . '" />'
270 $htmlString .= '<tr><td class="nowrap">AS</td>'
272 . '<textarea name="view[as]" rows="15" cols="40" dir="' . $text_dir . '"';
273 if ($GLOBALS['cfg']['TextareaAutoSelect'] ||
true) {
274 $htmlString .= ' onclick="selectContent(this, sql_box_locked, true)"';
276 $htmlString .= '>' . htmlspecialchars($view['as']) . '</textarea>'
279 $htmlString .= '<tr><td class="nowrap">WITH CHECK OPTION</td>'
280 . '<td><select name="view[with]">'
281 . '<option value=""></option>';
282 foreach ($view_with_options as $option) {
283 $htmlString .= '<option value="' . htmlspecialchars($option) . '"';
284 if ($option == $view['with']) {
285 $htmlString .= ' selected="selected"';
287 $htmlString .= '>' . htmlspecialchars($option) . '</option>';
289 $htmlString .= '<select>'
292 $htmlString .= '</table>'
295 if (! isset($_REQUEST['ajax_dialog'])) {
296 $htmlString .= '<fieldset class="tblFooters">'
297 . '<input type="hidden" name="'
298 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
300 . '<input type="submit" name="" value="' . __('Go') . '" />'
303 $htmlString .= '<input type="hidden" name="'
304 . ($view['operation'] == 'create' ?
'createview' : 'alterview' )
306 . '<input type="hidden" name="ajax_dialog" value="1" />'
307 . '<input type="hidden" name="ajax_request" value="1" />';
310 $htmlString .= '</form>'