UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / central_columns.lib.php
blob030e7c77197ee9670f8cf746901a5697cb69cc0d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions for displaying user preferences pages
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Defines the central_columns parameters for the current user
15 * @return array the central_columns parameters for the current user
16 * @access public
18 function PMA_centralColumnsGetParams()
20 static $cfgCentralColumns = null;
22 if (null !== $cfgCentralColumns) {
23 return $cfgCentralColumns;
26 $cfgRelation = PMA_getRelationsParam();
28 if (isset($cfgRelation['central_columnswork'])
29 && $cfgRelation['central_columnswork']
30 ) {
31 $cfgCentralColumns = array(
32 'user' => $GLOBALS['cfg']['Server']['user'],
33 'db' => $cfgRelation['db'],
34 'table' => $cfgRelation['central_columns'],
36 } else {
37 $cfgCentralColumns = false;
40 return $cfgCentralColumns;
43 /**
44 * get $num columns of given database from central columns list
45 * starting at offset $from
47 * @param string $db selected database
48 * @param int $from starting offset of first result
49 * @param int $num maximum number of results to return
51 * @return array list of $num columns present in central columns list
52 * starting at offset $from for the given database
54 function PMA_getColumnsList($db, $from=0, $num=25)
56 $cfgCentralColumns = PMA_centralColumnsGetParams();
57 if (empty($cfgCentralColumns)) {
58 return array();
60 $pmadb = $cfgCentralColumns['db'];
61 $GLOBALS['dbi']->selectDb($pmadb, $GLOBALS['controllink']);
62 $central_list_table = $cfgCentralColumns['table'];
63 //get current values of $db from central column list
64 if ($num == 0) {
65 $query = 'SELECT * FROM ' . PMA_Util::backquote($central_list_table) . ' '
66 . 'WHERE db_name = \'' . $db . '\';';
67 } else {
68 $query = 'SELECT * FROM ' . PMA_Util::backquote($central_list_table) . ' '
69 . 'WHERE db_name = \'' . $db . '\' '
70 . 'LIMIT ' . $from . ', ' . $num . ';';
72 $has_list = (array) $GLOBALS['dbi']->fetchResult(
73 $query, null, null, $GLOBALS['controllink']
75 PMA_handleColumnExtra($has_list);
76 return $has_list;
79 /**
80 * get the number of columns present in central list for given db
82 * @param string $db current database
84 * @return int number of columns in central list of columns for $db
86 function PMA_getCentralColumnsCount($db)
88 $cfgCentralColumns = PMA_centralColumnsGetParams();
89 if (empty($cfgCentralColumns)) {
90 return 0;
92 $pmadb = $cfgCentralColumns['db'];
93 $GLOBALS['dbi']->selectDb($pmadb, $GLOBALS['controllink']);
94 $central_list_table = $cfgCentralColumns['table'];
95 $query = 'SELECT count(db_name) FROM ' .
96 PMA_Util::backquote($central_list_table) . ' '
97 . 'WHERE db_name = \'' . $db . '\';';
98 $res = $GLOBALS['dbi']->fetchResult(
99 $query, null, null, $GLOBALS['controllink']
101 if (isset($res[0])) {
102 return $res[0];
103 } else {
104 return 0;
108 * return the existing columns in central list among the given list of columns
110 * @param string $db the selected database
111 * @param string $cols comma separated list of given columns
112 * @param boolean $allFields set if need all the fields of existing columns,
113 * otherwise only column_name is returned
115 * @return array list of columns in central columns among given set of columns
117 function PMA_findExistingColNames($db, $cols, $allFields=false)
119 $cfgCentralColumns = PMA_centralColumnsGetParams();
120 if (empty($cfgCentralColumns)) {
121 return array();
123 $pmadb = $cfgCentralColumns['db'];
124 $GLOBALS['dbi']->selectDb($pmadb, $GLOBALS['controllink']);
125 $central_list_table = $cfgCentralColumns['table'];
126 if ($allFields) {
127 $query = 'SELECT * FROM ' . PMA_Util::backquote($central_list_table) . ' '
128 . 'WHERE db_name = \'' . $db . '\' AND col_name IN (' . $cols . ');';
129 $has_list = (array) $GLOBALS['dbi']->fetchResult(
130 $query, null, null, $GLOBALS['controllink']
132 PMA_handleColumnExtra($has_list);
133 } else {
134 $query = 'SELECT col_name FROM '
135 . PMA_Util::backquote($central_list_table) . ' '
136 . 'WHERE db_name = \'' . $db . '\' AND col_name IN (' . $cols . ');';
137 $has_list = (array) $GLOBALS['dbi']->fetchResult(
138 $query, null, null, $GLOBALS['controllink']
142 return $has_list;
146 * return error message to be displayed if central columns
147 * configuration storage is not completely configured
149 * @return PMA_Message
151 function PMA_configErrorMessage()
153 return PMA_Message::error(
155 'The configuration storage is not ready for the central list'
156 . ' of columns feature.'
162 * build the insert query for central columns list given PMA storage
163 * db, central_columns table, column name and corresponding definition to be added
165 * @param string $column column to add into central list
166 * @param array $def list of attributes of the column being added
167 * @param string $db PMA configuration storage database name
168 * @param string $central_list_table central columns configuration storage table name
170 * @return string query string to insert the given column
171 * with definition into central list
173 function PMA_getInsertQuery($column, $def, $db, $central_list_table)
175 $type = "";
176 $length = 0;
177 $attribute = "";
178 if (isset($def['Type'])) {
179 $extracted_columnspec = PMA_Util::extractColumnSpec($def['Type']);
180 $attribute = trim($extracted_columnspec[ 'attribute']);
181 $type = $extracted_columnspec['type'];
182 $length = $extracted_columnspec['spec_in_brackets'];
184 if (isset($def['Attribute'])) {
185 $attribute = $def['Attribute'];
187 $collation = isset($def['Collation'])?$def['Collation']:"";
188 $isNull = ($def['Null'] == "NO")?0:1;
189 $extra = isset($def['Extra'])?$def['Extra']:"";
190 $default = isset($def['Default'])?$def['Default']:"";
191 $insQuery = 'INSERT INTO '
192 . PMA_Util::backquote($central_list_table) . ' '
193 . 'VALUES ( \'' . PMA_Util::sqlAddSlashes($db) . '\' ,'
194 . '\'' . PMA_Util::sqlAddSlashes($column) . '\',\''
195 . PMA_Util::sqlAddSlashes($type) . '\','
196 . '\'' . PMA_Util::sqlAddSlashes($length) . '\',\''
197 . PMA_Util::sqlAddSlashes($collation) . '\','
198 . '\'' . PMA_Util::sqlAddSlashes($isNull) . '\','
199 . '\'' . implode(',', array($extra, $attribute))
200 . '\',\'' . PMA_Util::sqlAddSlashes($default) . '\');';
201 return $insQuery;
205 * If $isTable is true then unique columns from given tables as $field_select
206 * are added to central list otherwise the $field_select is considered as
207 * list of columns and these columns are added to central list if not already added
209 * @param array $field_select if $isTable is true selected tables list
210 * otherwise selected columns list
211 * @param bool $isTable if passed array is of tables or columns
212 * @param string $table if $isTable is false,
213 * then table name to which columns belong
215 * @return true|PMA_Message
217 function PMA_syncUniqueColumns($field_select, $isTable=true, $table=null)
219 $cfgCentralColumns = PMA_centralColumnsGetParams();
220 if (empty($cfgCentralColumns)) {
221 return PMA_configErrorMessage();
223 $db = $_REQUEST['db'];
224 $pmadb = $cfgCentralColumns['db'];
225 $central_list_table = $cfgCentralColumns['table'];
226 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
227 $existingCols = array();
228 $cols = "";
229 $insQuery = array();
230 $fields = array();
231 $message = true;
232 if ($isTable) {
233 foreach ($field_select as $table) {
234 $fields[$table] = (array) $GLOBALS['dbi']->getColumns(
235 $db, $table, null, true, $GLOBALS['userlink']
237 foreach ($fields[$table] as $field => $def) {
238 $cols .= "'" . PMA_Util::sqlAddSlashes($field) . "',";
242 $has_list = PMA_findExistingColNames($db, trim($cols, ','));
243 foreach ($field_select as $table) {
244 foreach ($fields[$table] as $field => $def) {
245 if (!in_array($field, $has_list)) {
246 $has_list[] = $field;
247 $insQuery[] = PMA_getInsertQuery(
248 $field, $def, $db, $central_list_table
250 } else {
251 $existingCols[] = "'" . $field . "'";
255 } else {
256 if ($table == null) {
257 $table = $_REQUEST['table'];
259 foreach ($field_select as $column) {
260 $cols .= "'" . PMA_Util::sqlAddSlashes($column) . "',";
262 $has_list = PMA_findExistingColNames($db, trim($cols, ','));
263 foreach ($field_select as $column) {
264 if (!in_array($column, $has_list)) {
265 $has_list[] = $column;
266 $field = (array) $GLOBALS['dbi']->getColumns(
267 $db, $table, $column,
268 true, $GLOBALS['userlink']
270 $insQuery[] = PMA_getInsertQuery(
271 $column, $field, $db, $central_list_table
273 } else {
274 $existingCols[] = "'" . $column . "'";
278 if ($existingCols) {
279 $existingCols = implode(",", array_unique($existingCols));
280 $message = PMA_Message::notice(
281 sprintf(
283 'Could not add %1$s as they already exist in central list!'
284 ), htmlspecialchars($existingCols)
287 $message->addMessage(
288 PMA_Message::notice(
289 "Please remove them first "
290 . "from central list if you want to update above columns"
294 $GLOBALS['dbi']->selectDb($pmadb, $GLOBALS['controllink']);
295 if ($insQuery) {
296 foreach ($insQuery as $query) {
297 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
298 $message = PMA_Message::error(__('Could not add columns!'));
299 $message->addMessage(
300 PMA_Message::rawError(
301 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
304 break;
308 return $message;
312 * if $isTable is true it removes all columns of given tables as $field_select from
313 * central columns list otherwise $field_select is columns list and it removes
314 * given columns if present in central list
316 * @param array $field_select if $isTable selected list of tables otherwise
317 * selected list of columns to remove from central list
318 * @param bool $isTable if passed array is of tables or columns
320 * @return true|PMA_Message
322 function PMA_deleteColumnsFromList($field_select, $isTable=true)
324 $cfgCentralColumns = PMA_centralColumnsGetParams();
325 if (empty($cfgCentralColumns)) {
326 return PMA_configErrorMessage();
328 $db = $_REQUEST['db'];
329 $pmadb = $cfgCentralColumns['db'];
330 $central_list_table = $cfgCentralColumns['table'];
331 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
332 $message = true;
333 $colNotExist = array();
334 $fields = array();
335 if ($isTable) {
336 $cols = '';
337 foreach ($field_select as $table) {
338 $fields[$table] = (array) $GLOBALS['dbi']->getColumnNames(
339 $db, $table, $GLOBALS['userlink']
341 foreach ($fields[$table] as $col_select) {
342 $cols .= '\'' . PMA_Util::sqlAddSlashes($col_select) . '\',';
345 $cols = trim($cols, ',');
346 $has_list = PMA_findExistingColNames($db, $cols);
347 foreach ($field_select as $table) {
348 foreach ($fields[$table] as $column) {
349 if (!in_array($column, $has_list)) {
350 $colNotExist[] = "'" . $column . "'";
355 } else {
356 $cols = '';
357 foreach ($field_select as $col_select) {
358 $cols .= '\'' . PMA_Util::sqlAddSlashes($col_select) . '\',';
360 $cols = trim($cols, ',');
361 $has_list = PMA_findExistingColNames($db, $cols);
362 foreach ($field_select as $column) {
363 if (!in_array($column, $has_list)) {
364 $colNotExist[] = "'" . $column . "'";
368 if ($colNotExist) {
369 $colNotExist = implode(",", array_unique($colNotExist));
370 $message = PMA_Message::notice(
371 sprintf(
373 'Couldn\'t remove Column(s) %1$s '
374 . 'as they don\'t exist in central columns list!'
375 ), htmlspecialchars($colNotExist)
379 $GLOBALS['dbi']->selectDb($pmadb, $GLOBALS['controllink']);
381 $query = 'DELETE FROM ' . PMA_Util::backquote($central_list_table) . ' '
382 . 'WHERE db_name = \'' . $db . '\' AND col_name IN (' . $cols . ');';
384 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
385 $message = PMA_Message::error(__('Could not remove columns!'));
386 $message->addMessage('<br />' . htmlspecialchars($cols) . '<br />');
387 $message->addMessage(
388 PMA_Message::rawError(
389 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
393 return $message;
397 * make the columns of given tables consistent with central list of columns.
398 * Updates only those columns which are not being referenced.
400 * @param string $db current database
401 * @param array $selected_tables list of selected tables.
403 * @return true|PMA_Message
405 function PMA_makeConsistentWithList($db, $selected_tables)
407 $message = true;
408 foreach ($selected_tables as $table) {
409 $query = 'ALTER TABLE ' . PMA_Util::backquote($table);
410 $has_list = PMA_getCentralColumnsFromTable($db, $table, true);
411 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
412 foreach ($has_list as $column) {
413 $column_status = PMA_checkChildForeignReferences(
414 $db, $table, $column['col_name']
416 //column definition can only be changed if
417 //it is not referenced by another column
418 if ($column_status['isEditable']) {
419 $query .= ' MODIFY ' . PMA_Util::backquote($column['col_name']) . ' '
420 . PMA_Util::sqlAddSlashes($column['col_type']);
421 if ($column['col_length']) {
422 $query .= '(' . $column['col_length'] . ')';
425 $query .= ' ' . $column['col_attribute'];
426 if ($column['col_isNull']) {
427 $query .= ' NULL';
428 } else {
429 $query .= ' NOT NULL';
432 $query .= ' ' . $column['col_extra'];
433 if ($column['col_default']) {
434 if ($column['col_default'] != 'CURRENT_TIMESTAMP') {
435 $query .= ' DEFAULT \'' . PMA_Util::sqlAddSlashes(
436 $column['col_default']
437 ) . '\'';
438 } else {
439 $query .= ' DEFAULT ' . PMA_Util::sqlAddSlashes(
440 $column['col_default']
444 $query .= ',';
447 $query = trim($query, " ,") . ";";
448 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['userlink'])) {
449 if ($message === true) {
450 $message = PMA_Message::error(
451 $GLOBALS['dbi']->getError($GLOBALS['userlink'])
453 } else {
454 $message->addMessage('<br />');
455 $message->addMessage(
456 $GLOBALS['dbi']->getError($GLOBALS['userlink'])
461 return $message;
465 * return the columns present in central list of columns for a given
466 * table of a given database
468 * @param string $db given database
469 * @param string $table given table
470 * @param boolean $allFields set if need all the fields of existing columns,
471 * otherwise only column_name is returned
473 * @return array columns present in central list from given table of given db.
475 function PMA_getCentralColumnsFromTable($db, $table, $allFields=false)
477 $cfgCentralColumns = PMA_centralColumnsGetParams();
478 if (empty($cfgCentralColumns)) {
479 return array();
481 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
482 $fields = (array) $GLOBALS['dbi']->getColumnNames(
483 $db, $table, $GLOBALS['userlink']
485 $cols = '';
486 foreach ($fields as $col_select) {
487 $cols .= '\'' . PMA_Util::sqlAddSlashes($col_select) . '\',';
489 $cols = trim($cols, ',');
490 $has_list = PMA_findExistingColNames($db, $cols, $allFields);
491 if (isset($has_list) && $has_list) {
492 return (array)$has_list;
493 } else {
494 return array();
499 * update a column in central columns list if a edit is requested
501 * @param string $db current database
502 * @param string $orig_col_name original column name before edit
503 * @param string $col_name new column name
504 * @param string $col_type new column type
505 * @param string $col_attribute new column attribute
506 * @param string $col_length new column length
507 * @param int $col_isNull value 1 if new column isNull is true, 0 otherwise
508 * @param string $collation new column collation
509 * @param string $col_extra new column extra property
510 * @param string $col_default new column default value
512 * @return true|PMA_Message
514 function PMA_updateOneColumn($db, $orig_col_name, $col_name, $col_type,
515 $col_attribute,$col_length, $col_isNull, $collation, $col_extra, $col_default
517 $cfgCentralColumns = PMA_centralColumnsGetParams();
518 if (empty($cfgCentralColumns)) {
519 return PMA_configErrorMessage();
521 $centralTable = $cfgCentralColumns['table'];
522 $GLOBALS['dbi']->selectDb($cfgCentralColumns['db'], $GLOBALS['controllink']);
523 if ($orig_col_name == "") {
524 $def = array();
525 $def['Type'] = $col_type;
526 if ($col_length) {
527 $def['Type'] .= '(' . $col_length . ')';
529 $def['Collation'] = $collation;
530 $def['Null'] = $col_isNull?__('YES'):__('NO');
531 $def['Extra'] = $col_extra;
532 $def['Attribute'] = $col_attribute;
533 $def['Default'] = $col_default;
534 $query = PMA_getInsertQuery($col_name, $def, $db, $centralTable);
535 } else {
536 $query = 'UPDATE ' . PMA_Util::backquote($centralTable)
537 . ' SET col_type = \'' . PMA_Util::sqlAddSlashes($col_type) . '\''
538 . ', col_name = \'' . PMA_Util::sqlAddSlashes($col_name) . '\''
539 . ', col_length = \'' . PMA_Util::sqlAddSlashes($col_length) . '\''
540 . ', col_isNull = ' . $col_isNull
541 . ', col_collation = \'' . PMA_Util::sqlAddSlashes($collation) . '\''
542 . ', col_extra = \'' . implode(',', array($col_extra, $col_attribute)) . '\''
543 . ', col_default = \'' . PMA_Util::sqlAddSlashes($col_default) . '\''
544 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\' '
545 . 'AND col_name = \'' . PMA_Util::sqlAddSlashes($orig_col_name)
546 . '\'';
548 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
549 return PMA_Message::error(
550 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
553 return true;
557 * get the html for table navigation in Central columns page
559 * @param int $total_rows total number of rows in complete result set
560 * @param int $pos offset of first result with complete result set
561 * @param string $db current database
563 * @return html for table navigation in Central columns page
565 function PMA_getHTMLforTableNavigation($total_rows, $pos, $db)
567 $max_rows = $GLOBALS['cfg']['MaxRows'];
568 $pageNow = ($pos / $max_rows) + 1;
569 $nbTotalPage = ceil($total_rows / $max_rows);
570 $table_navigation_html = '<table style="display:inline-block;max-width:49%" '
571 . 'class="navigation nospacing nopadding">'
572 . '<tr>'
573 . '<td class="navigation_separator"></td>';
574 if ($pos - $max_rows >= 0) {
575 $table_navigation_html .= '<td>'
576 . '<form action="db_central_columns.php" method="post">'
577 . PMA_URL_getHiddenInputs(
580 . '<input type="hidden" name="pos" value="' . ($pos - $max_rows) . '" />'
581 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
582 . '<input type="submit" name="navig"'
583 . ' class="ajax" '
584 . 'value="&lt" />'
585 . '</form>'
586 . '</td>';
588 if ($nbTotalPage > 1) {
589 $table_navigation_html .= '<td>';
590 $table_navigation_html .= '<form action="db_central_columns.php'
591 . '" method="post">'
592 . PMA_URL_getHiddenInputs(
595 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>';
596 $table_navigation_html .= PMA_Util::pageselector(
597 'pos', $max_rows, $pageNow, $nbTotalPage
599 $table_navigation_html .= '</form>'
600 . '</td>';
602 if ($pos + $max_rows < $total_rows) {
603 $table_navigation_html .= '<td>'
604 . '<form action="db_central_columns.php" method="post">'
605 . PMA_URL_getHiddenInputs(
608 . '<input type="hidden" name="pos" value="' . ($pos + $max_rows) . '" />'
609 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
610 . '<input type="submit" name="navig"'
611 . ' class="ajax" '
612 . 'value="&gt" />'
613 . '</form>'
614 . '</td>';
616 $table_navigation_html .= '</form>'
617 . '</td>'
618 . '<td class="navigation_separator"></td>'
619 . '<td>'
620 . '<span>' . __('Filter rows') . ':</span>'
621 . '<input type="text" class="filter_rows" placeholder="'
622 . __('Search this table') . '">'
623 . '</td>'
624 . '<td class="navigation_separator"></td>'
625 . '</tr>'
626 . '</table>';
628 return $table_navigation_html;
632 * function generate and return the table header for central columns page
634 * @param string $class styling class of 'th' elements
635 * @param string $title title of the 'th' elements
636 * @param integer $actionCount number of actions
638 * @return html for table header in central columns view/edit page
640 function PMA_getCentralColumnsTableHeader($class='', $title='', $actionCount=0)
642 $action = '';
643 if ($actionCount > 0) {
644 $action .= '<th class="column_action" colspan="' . $actionCount . '">'
645 . __('Action') . '</th>';
647 $tableheader = '<thead>';
648 $tableheader .= '<tr>'
649 . $action
650 . '<th class="" style="display:none"></th>'
651 . '<th class="' . $class . '" title="' . $title . '" data-column="name">'
652 . __('Name') . '<div class="sorticon"></div></th>'
653 . '<th class="' . $class . '" title="' . $title . '" data-column="type">'
654 . __('Type') . '<div class="sorticon"></div></th>'
655 . '<th class="' . $class . '" title="' . $title . '" data-column="length">'
656 . __('Length/Values') . '<div class="sorticon"></div></th>'
657 . '<th class="' . $class . '" title="' . $title . '" data-column="collation"'
658 . '>' . __('Collation') . '<div class="sorticon"></div></th>'
659 . '<th class="' . $class . '" title="' . $title . '" data-column="attribute">'
660 . __('Attribute') . '<div class="sorticon"></div></th>'
661 . '<th class="' . $class . '" title="' . $title . '" data-column="isnull">'
662 . __('Null') . '<div class="sorticon"></div></th>'
663 . '<th class="' . $class . '" title="' . $title . '" data-column="extra">'
664 . __('Extra') . '<div class="sorticon"></div></th>'
665 . '<th class="' . $class . '" title="' . $title . '" data-column="default">'
666 . __('Default') . '<div class="sorticon"></div></th>'
667 . '</tr>';
668 $tableheader .= '</thead>';
669 return $tableheader;
673 * build the dropdown select html for tables of given database
675 * @param string $db current database
677 * @return html dropdown for selecting table
679 function PMA_getHTMLforTableDropdown($db)
681 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
682 $tables = $GLOBALS['dbi']->getTables($db, $GLOBALS['userlink']);
683 $selectHtml = '<select name="table-select" id="table-select">'
684 . '<option value="" disabled="disabled" selected="selected">'
685 . __('Select a table') . '</option>';
686 foreach ($tables as $table) {
687 $selectHtml .= '<option value="' . htmlspecialchars($table) . '">'
688 . htmlspecialchars($table) . '</option>';
690 $selectHtml .= '</select>';
691 return $selectHtml;
695 * build dropdown select html to select column in selected table,
696 * include only columns which are not already in central list
698 * @param string $db current database to which selected table belongs
699 * @param string $selected_tbl selected table
701 * @return html to select column
703 function PMA_getHTMLforColumnDropdown($db, $selected_tbl)
705 $existing_cols = PMA_getCentralColumnsFromTable($db, $selected_tbl);
706 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
707 $columns = (array) $GLOBALS['dbi']->getColumnNames(
708 $db, $selected_tbl, $GLOBALS['userlink']
710 $selectColHtml = "";
711 foreach ($columns as $column) {
712 if (!in_array($column, $existing_cols)) {
713 $selectColHtml .= '<option value="' . htmlspecialchars($column) . '">'
714 . htmlspecialchars($column)
715 . '</option>';
718 return $selectColHtml;
722 * html to display the form that let user to add a column on Central columns page
724 * @param int $total_rows total number of rows in complete result set
725 * @param int $pos offset of first result with complete result set
726 * @param string $db current database
728 * @return html to add a column in the central list
730 function PMA_getHTMLforAddCentralColumn($total_rows, $pos, $db)
732 $columnAdd = '<table style="display:inline-block;margin-left:1%;max-width:50%" '
733 . 'class="navigation nospacing nopadding">'
734 . '<tr>'
735 . '<td class="navigation_separator"></td>'
736 . '<td style="padding:1.5% 0em">'
737 . PMA_Util::getIcon(
738 'centralColumns_add.png',
739 __('Add column')
741 . '<form id="add_column" action="db_central_columns.php" method="post">'
742 . PMA_URL_getHiddenInputs(
745 . '<input type="hidden" name="add_column" value="add">'
746 . '<input type="hidden" name="pos" value="' . $pos . '" />'
747 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
748 . PMA_getHTMLforTableDropdown($db)
749 . '<select name="column-select" id="column-select">'
750 . '<option value="" selected="selected">'
751 . __('Select a column.') . '</option>'
752 . '</select></form>'
753 . '</td>'
754 . '<td class="navigation_separator"></td>'
755 . '</tr>'
756 . '</table>';
758 return $columnAdd;
762 * build html for a row in central columns table
764 * @param array $row array contains complete information of
765 * a particular row of central list table
766 * @param boolean $odd_row set true if the row is at odd number position
767 * @param int $row_num position the row in the table
768 * @param string $db current database
770 * @return html of a particular row in the central columns table.
772 function PMA_getHTMLforCentralColumnsTableRow($row, $odd_row, $row_num, $db)
774 $tableHtml = '<tr data-rownum="' . $row_num . '" id="f_' . $row_num . '" '
775 . 'class="' . ($odd_row ? 'odd' : 'even') . '">'
776 . PMA_URL_getHiddenInputs(
779 . '<input type="hidden" name="edit_save" value="save">'
780 . '<td id="edit_' . $row_num . '" class="edit center">'
781 . '<a href="#">' . PMA_Util::getIcon('b_edit.png', __('Edit')) . '</a></td>'
782 . '<td class="del_row" data-rownum = "' . $row_num . '">'
783 . '<a hrf="#">' . PMA_Util::getIcon('b_drop.png', __('Delete')) . '</a>'
784 . '<input type="submit" data-rownum = "' . $row_num . '"'
785 . ' class="edit_cancel_form" value="Cancel"></td>'
786 . '<td id="save_' . $row_num . '" style="display:none">'
787 . '<input type="submit" data-rownum = "' . $row_num . '"'
788 . ' class="edit_save_form" value="Save"></td>';
790 $tableHtml .=
791 '<td name="col_name" class="nowrap">'
792 . '<span>' . htmlspecialchars($row['col_name']) . '</span>'
793 . '<input name="orig_col_name" type="hidden" '
794 . 'value="' . htmlspecialchars($row['col_name']) . '">'
795 . PMA_getHtmlForColumnName(
796 $row_num, 0, 0, array('Field'=>$row['col_name']),
797 array('central_columnswork'=>false)
799 . '</td>';
800 $tableHtml .=
801 '<td name = "col_type" class="nowrap"><span>'
802 . htmlspecialchars($row['col_type']) . '</span>'
803 . PMA_getHtmlForColumnType(
804 $row_num, 1, 0, /*overload*/mb_strtoupper($row['col_type']), array()
806 . '</td>';
807 $tableHtml .=
808 '<td class="nowrap" name="col_length">'
809 . '<span>' . ($row['col_length']?htmlspecialchars($row['col_length']):"")
810 . '</span>'
811 . PMA_getHtmlForColumnLength($row_num, 2, 0, 8, $row['col_length'])
812 . '</td>';
814 $tableHtml .=
815 '<td name="collation" class="nowrap">'
816 . '<span>' . htmlspecialchars($row['col_collation']) . '</span>'
817 . PMA_getHtmlForColumnCollation(
818 $row_num, 3, 0, array('Collation'=>$row['col_collation'])
820 . '</td>';
821 $tableHtml .=
822 '<td class="nowrap" name="col_attribute">'
823 . '<span>' .
824 ($row['col_attribute']
825 ? htmlspecialchars($row['col_attribute']) : "" )
826 . '</span>'
827 . PMA_getHtmlForColumnAttribute(
828 $row_num, 4, 0, array(), $row['col_attribute'], false, null
830 . '</td>';
831 $tableHtml .=
832 '<td class="nowrap" name="col_isNull">'
833 . '<span>' . ($row['col_isNull'] ? __('Yes') : __('No'))
834 . '</span>'
835 . PMA_getHtmlForColumnNull($row_num, 5, 0, array('Null'=>$row['col_isNull']))
836 . '</td>';
838 $tableHtml .=
839 '<td class="nowrap" name="col_extra"><span>'
840 . htmlspecialchars($row['col_extra']) . '</span>'
841 . '<select name="col_extra"><option value=""></option>'
842 . '<option value="auto_increment">' . __('auto_increment') . '</option>'
843 . '</select>'
844 . '</td>';
845 $meta = array();
846 if (!isset($row['col_default']) || $row['col_default'] == '') {
847 $meta['DefaultType'] = 'NONE';
848 } else {
849 if ($row['col_default'] == 'CURRENT_TIMESTAMP'
850 || $row['col_default'] == 'NULL'
852 $meta['DefaultType'] = $row['col_default'];
853 } else {
854 $meta['DefaultType'] = 'USER_DEFINED';
855 $meta['DefaultValue'] = $row['col_default'];
858 $tableHtml .=
859 '<td class="nowrap" name="col_default"><span>' . (isset($row['col_default'])
860 ? htmlspecialchars($row['col_default']) : 'None')
861 . '</span>'
862 . PMA_getHtmlForColumnDefault(
863 $row_num, 6, 0, /*overload*/mb_strtoupper($row['col_type']), '', $meta
865 . '</td>';
866 $tableHtml .= '</tr>';
867 return $tableHtml;
871 * get the list of columns in given database excluding
872 * the columns present in current table
874 * @param string $db selected database
875 * @param string $table current table name
877 * @return encoded list of columns present in central list for the given database
879 function PMA_getCentralColumnsListRaw($db, $table)
881 $cfgCentralColumns = PMA_centralColumnsGetParams();
882 if (empty($cfgCentralColumns)) {
883 return json_encode(array());
885 $centralTable = $cfgCentralColumns['table'];
886 if (empty($table) || $table == '') {
887 $query = 'SELECT * FROM ' . PMA_Util::backquote($centralTable) . ' '
888 . 'WHERE db_name = \'' . $db . '\';';
889 } else {
890 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
891 $columns = (array) $GLOBALS['dbi']->getColumnNames(
892 $db, $table, $GLOBALS['userlink']
894 $cols = '';
895 foreach ($columns as $col_select) {
896 $cols .= '\'' . PMA_Util::sqlAddSlashes($col_select) . '\',';
898 $cols = trim($cols, ',');
899 $query = 'SELECT * FROM ' . PMA_Util::backquote($centralTable) . ' '
900 . 'WHERE db_name = \'' . $db . '\'';
901 if ($cols) {
902 $query .= ' AND col_name NOT IN (' . $cols . ')';
904 $query .= ';';
906 $GLOBALS['dbi']->selectDb($cfgCentralColumns['db'], $GLOBALS['controllink']);
907 $columns_list = (array)$GLOBALS['dbi']->fetchResult(
908 $query, null, null, $GLOBALS['controllink']
910 PMA_handleColumnExtra($columns_list);
911 return json_encode($columns_list);
915 * Column `col_extra` is used to store both extra and attributes for a column.
916 * This method separates them.
918 * @param array columns_list columns list
920 function PMA_handleColumnExtra(&$columns_list)
922 foreach ($columns_list as &$row) {
923 $vals = explode(',', $row['col_extra']);
925 if (in_array('BINARY', $vals)) {
926 $row['col_attribute'] = 'BINARY';
927 } elseif (in_array('UNSIGNED', $vals)) {
928 $row['col_attribute'] = 'UNSIGNED';
929 } elseif (in_array('UNSIGNED ZEROFILL', $vals)) {
930 $row['col_attribute'] = 'UNSIGNED ZEROFILL';
931 } elseif (in_array('on update CURRENT_TIMESTAMP', $vals)) {
932 $row['col_attribute'] = 'on update CURRENT_TIMESTAMP';
933 } else {
934 $row['col_attribute'] = '';
937 if (in_array('auto_increment', $vals)) {
938 $row['col_extra'] = 'auto_increment';
939 } else {
940 $row['col_extra'] = '';
946 * build html for adding a new user defined column to central list
948 * @param string $db current database
950 * @return html of the form to let user add a new user defined column to the list
952 function PMA_getHTMLforAddNewColumn($db)
954 $addNewColumn = '<div id="add_col_div"><a href="#">'
955 . '<span>+</span> ' . __('Add new column') . '</a>'
956 . '<form id="add_new" style="min-width:100%;display:none" '
957 . 'method="post" action="db_central_columns.php">'
958 . PMA_URL_getHiddenInputs(
961 . '<input type="hidden" name="add_new_column" value="add_new_column">'
962 . '<table>';
963 $addNewColumn .= PMA_getCentralColumnsTableHeader();
964 $addNewColumn .= '<tr>'
965 . '<td name="col_name" class="nowrap">'
966 . PMA_getHtmlForColumnName(
967 0, 0, 0, array(), array('central_columnswork'=>false)
969 . '</td>'
970 . '<td name = "col_type" class="nowrap">'
971 . PMA_getHtmlForColumnType(0, 1, 0, '', array())
972 . '</td>'
973 . '<td class="nowrap" name="col_length">'
974 . PMA_getHtmlForColumnLength(0, 2, 0, 8, '')
975 . '</td>'
976 . '<td name="collation" class="nowrap">'
977 . PMA_getHtmlForColumnCollation(
978 0, 3, 0, array()
980 . '</td>'
981 . '<td class="nowrap" name="col_attribute">'
982 . PMA_getHtmlForColumnAttribute(0, 4, 0, array(), array(), false, null)
983 . '</td>'
984 . '<td class="nowrap" name="col_isNull">'
985 . PMA_getHtmlForColumnNull(0, 5, 0, array())
986 . '</td>'
987 . '<td class="nowrap" name="col_extra">'
988 . '<select name="col_extra"><option value="">'
989 . '<option value="auto_increment">' . __('auto_increment') . '</option>'
990 . '<option value="on update CURRENT_TIMESTAMP">'
991 . __('on update CURRENT_TIMESTAMP') . '</option></select>'
992 . '</td>'
993 . '<td class="nowrap" name="col_default">'
994 . PMA_getHtmlForColumnDefault(0, 6, 0, '', '', array())
995 . '</td>'
996 . ' <td>'
997 . '<input id="add_column_save" type="submit" '
998 . ' value="Save"/></td>'
999 . '</tr>';
1000 $addNewColumn .= '</table></form></div>';
1001 return $addNewColumn;