Minor improvement to prior commit.
[openemr.git] / phpmyadmin / libraries / central_columns.lib.php
blob743913acc04d41bf52221cb206bfa0464db48995
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 require_once './libraries/Template.class.php';
14 /**
15 * Defines the central_columns parameters for the current user
17 * @return array the central_columns parameters for the current user
18 * @access public
20 function PMA_centralColumnsGetParams()
22 static $cfgCentralColumns = null;
24 if (null !== $cfgCentralColumns) {
25 return $cfgCentralColumns;
28 $cfgRelation = PMA_getRelationsParam();
30 if ($cfgRelation['centralcolumnswork']) {
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 (! empty($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 (! empty($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 (!empty($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 (! empty($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 = \''
543 . implode(',', array($col_extra, $col_attribute)) . '\''
544 . ', col_default = \'' . PMA_Util::sqlAddSlashes($col_default) . '\''
545 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\' '
546 . 'AND col_name = \'' . PMA_Util::sqlAddSlashes($orig_col_name)
547 . '\'';
549 if (!$GLOBALS['dbi']->tryQuery($query, $GLOBALS['controllink'])) {
550 return PMA_Message::error(
551 $GLOBALS['dbi']->getError($GLOBALS['controllink'])
554 return true;
558 * Update Multiple column in central columns list if a chnage is requested
560 * @return true|PMA_Message
562 function PMA_updateMultipleColumn()
564 $db = $_POST['db'];
565 $col_name = $_POST['field_name'];
566 $orig_col_name = $_POST['orig_col_name'];
567 $col_default = $_POST['field_default_type'];
568 $col_length = $_POST['field_length'];
569 $col_attribute = $_POST['field_attribute'];
570 $col_type = $_POST['field_type'];
571 $collation = $_POST['field_collation'];
572 $col_isNull = array();
573 $col_extra = array();
574 $num_central_fields = count($orig_col_name);
575 for ($i = 0; $i < $num_central_fields ; $i++) {
576 $col_isNull[$i] = isset($_POST['field_null'][$i]) ? 1 : 0;
577 $col_extra[$i] = isset($_POST['col_extra'][$i])
578 ? $_POST['col_extra'][$i] : '';
580 if ($col_default[$i] == 'NONE') {
581 $col_default[$i] = "";
582 } else if ($col_default[$i] == 'USER_DEFINED') {
583 $col_default[$i] = $_POST['field_default_value'][$i];
586 $message = PMA_updateOneColumn(
587 $db, $orig_col_name[$i], $col_name[$i], $col_type[$i],
588 $col_attribute[$i], $col_length[$i], $col_isNull[$i], $collation[$i],
589 $col_extra[$i], $col_default[$i]
591 if (!is_bool($message)) {
592 return $message;
595 return true;
599 * get the html for table navigation in Central columns page
601 * @param int $total_rows total number of rows in complete result set
602 * @param int $pos offset of first result with complete result set
603 * @param string $db current database
605 * @return string html for table navigation in Central columns page
607 function PMA_getHTMLforTableNavigation($total_rows, $pos, $db)
609 $max_rows = $GLOBALS['cfg']['MaxRows'];
610 $pageNow = ($pos / $max_rows) + 1;
611 $nbTotalPage = ceil($total_rows / $max_rows);
612 $table_navigation_html = '<table style="display:inline-block;max-width:49%" '
613 . 'class="navigation nospacing nopadding">'
614 . '<tr>'
615 . '<td class="navigation_separator"></td>';
616 if ($pos - $max_rows >= 0) {
617 $table_navigation_html .= '<td>'
618 . '<form action="db_central_columns.php" method="post">'
619 . PMA_URL_getHiddenInputs(
622 . '<input type="hidden" name="pos" value="' . ($pos - $max_rows) . '" />'
623 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
624 . '<input type="submit" name="navig"'
625 . ' class="ajax" '
626 . 'value="&lt" />'
627 . '</form>'
628 . '</td>';
630 if ($nbTotalPage > 1) {
631 $table_navigation_html .= '<td>';
632 $table_navigation_html .= '<form action="db_central_columns.php'
633 . '" method="post">'
634 . PMA_URL_getHiddenInputs(
637 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>';
638 $table_navigation_html .= PMA_Util::pageselector(
639 'pos', $max_rows, $pageNow, $nbTotalPage
641 $table_navigation_html .= '</form>'
642 . '</td>';
644 if ($pos + $max_rows < $total_rows) {
645 $table_navigation_html .= '<td>'
646 . '<form action="db_central_columns.php" method="post">'
647 . PMA_URL_getHiddenInputs(
650 . '<input type="hidden" name="pos" value="' . ($pos + $max_rows) . '" />'
651 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
652 . '<input type="submit" name="navig"'
653 . ' class="ajax" '
654 . 'value="&gt" />'
655 . '</form>'
656 . '</td>';
658 $table_navigation_html .= '</form>'
659 . '</td>'
660 . '<td class="navigation_separator"></td>'
661 . '<td>'
662 . '<span>' . __('Filter rows') . ':</span>'
663 . '<input type="text" class="filter_rows" placeholder="'
664 . __('Search this table') . '">'
665 . '</td>'
666 . '<td class="navigation_separator"></td>'
667 . '</tr>'
668 . '</table>';
670 return $table_navigation_html;
674 * function generate and return the table header for central columns page
676 * @param string $class styling class of 'th' elements
677 * @param string $title title of the 'th' elements
678 * @param integer $actionCount number of actions
680 * @return string html for table header in central columns view/edit page
682 function PMA_getCentralColumnsTableHeader($class='', $title='', $actionCount=0)
684 $action = '';
685 if ($actionCount > 0) {
686 $action .= '<th class="column_action" colspan="' . $actionCount . '">'
687 . __('Action') . '</th>';
689 $tableheader = '<thead>';
690 $tableheader .= '<tr>'
691 . '<th class="' . $class . '"></th>'
692 . '<th class="" style="display:none"></th>'
693 . $action
694 . '<th class="' . $class . '" title="' . $title . '" data-column="name">'
695 . __('Name') . '<div class="sorticon"></div></th>'
696 . '<th class="' . $class . '" title="' . $title . '" data-column="type">'
697 . __('Type') . '<div class="sorticon"></div></th>'
698 . '<th class="' . $class . '" title="' . $title . '" data-column="length">'
699 . __('Length/Values') . '<div class="sorticon"></div></th>'
700 . '<th class="' . $class . '" title="' . $title . '" data-column="default">'
701 . __('Default') . '<div class="sorticon"></div></th>'
702 . '<th class="' . $class . '" title="' . $title . '" data-column="collation"'
703 . '>' . __('Collation') . '<div class="sorticon"></div></th>'
704 . '<th class="' . $class . '" title="' . $title
705 . '" data-column="attribute">'
706 . __('Attribute') . '<div class="sorticon"></div></th>'
707 . '<th class="' . $class . '" title="' . $title . '" data-column="isnull">'
708 . __('Null') . '<div class="sorticon"></div></th>'
709 . '<th class="' . $class . '" title="' . $title . '" data-column="extra">'
710 . __('A_I') . '<div class="sorticon"></div></th>'
711 . '</tr>';
712 $tableheader .= '</thead>';
713 return $tableheader;
717 * Function generate and return the table header for
718 * multiple edit central columns page
720 * @param array $header_cells headers list
722 * @return string html for table header in central columns multi edit page
724 function PMA_getCentralColumnsEditTableHeader($header_cells)
726 $html = '<table id="table_columns" class="noclick"'
727 . ' style="min-width: 100%;">';
728 $html .= '<caption class="tblHeaders">' . __('Structure');
729 $html .= '<tr>';
730 foreach ($header_cells as $header_val) {
731 $html .= '<th>' . $header_val . '</th>';
733 $html .= '</tr>';
734 return $html;
738 * build the dropdown select html for tables of given database
740 * @param string $db current database
742 * @return string html dropdown for selecting table
744 function PMA_getHTMLforTableDropdown($db)
746 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
747 $tables = $GLOBALS['dbi']->getTables($db, $GLOBALS['userlink']);
748 $selectHtml = '<select name="table-select" id="table-select">'
749 . '<option value="" disabled="disabled" selected="selected">'
750 . __('Select a table') . '</option>';
751 foreach ($tables as $table) {
752 $selectHtml .= '<option value="' . htmlspecialchars($table) . '">'
753 . htmlspecialchars($table) . '</option>';
755 $selectHtml .= '</select>';
756 return $selectHtml;
760 * build dropdown select html to select column in selected table,
761 * include only columns which are not already in central list
763 * @param string $db current database to which selected table belongs
764 * @param string $selected_tbl selected table
766 * @return string html to select column
768 function PMA_getHTMLforColumnDropdown($db, $selected_tbl)
770 $existing_cols = PMA_getCentralColumnsFromTable($db, $selected_tbl);
771 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
772 $columns = (array) $GLOBALS['dbi']->getColumnNames(
773 $db, $selected_tbl, $GLOBALS['userlink']
775 $selectColHtml = "";
776 foreach ($columns as $column) {
777 if (!in_array($column, $existing_cols)) {
778 $selectColHtml .= '<option value="' . htmlspecialchars($column) . '">'
779 . htmlspecialchars($column)
780 . '</option>';
783 return $selectColHtml;
787 * html to display the form that let user to add a column on Central columns page
789 * @param int $total_rows total number of rows in complete result set
790 * @param int $pos offset of first result with complete result set
791 * @param string $db current database
793 * @return string html to add a column in the central list
795 function PMA_getHTMLforAddCentralColumn($total_rows, $pos, $db)
797 $columnAdd = '<table style="display:inline-block;margin-left:1%;max-width:50%" '
798 . 'class="navigation nospacing nopadding">'
799 . '<tr>'
800 . '<td class="navigation_separator"></td>'
801 . '<td style="padding:1.5% 0em">'
802 . PMA_Util::getIcon(
803 'centralColumns_add.png',
804 __('Add column')
806 . '<form id="add_column" action="db_central_columns.php" method="post">'
807 . PMA_URL_getHiddenInputs(
810 . '<input type="hidden" name="add_column" value="add">'
811 . '<input type="hidden" name="pos" value="' . $pos . '" />'
812 . '<input type="hidden" name="total_rows" value="' . $total_rows . '"/>'
813 . PMA_getHTMLforTableDropdown($db)
814 . '<select name="column-select" id="column-select">'
815 . '<option value="" selected="selected">'
816 . __('Select a column.') . '</option>'
817 . '</select></form>'
818 . '</td>'
819 . '<td class="navigation_separator"></td>'
820 . '</tr>'
821 . '</table>';
823 return $columnAdd;
827 * build html for a row in central columns table
829 * @param array $row array contains complete information of
830 * a particular row of central list table
831 * @param boolean $odd_row set true if the row is at odd number position
832 * @param int $row_num position the row in the table
833 * @param string $db current database
835 * @return string html of a particular row in the central columns table.
837 function PMA_getHTMLforCentralColumnsTableRow($row, $odd_row, $row_num, $db)
839 $tableHtml = '<tr data-rownum="' . $row_num . '" id="f_' . $row_num . '" '
840 . 'class="' . ($odd_row ? 'odd' : 'even') . '">'
841 . PMA_URL_getHiddenInputs(
844 . '<input type="hidden" name="edit_save" value="save">'
845 . '<td class="nowrap">'
846 . '<input type="checkbox" class="checkall" name="selected_fld[]" '
847 . 'value="' . htmlspecialchars($row['col_name']) . '" '
848 . 'id="checkbox_row_' . $row_num . '"/>'
849 . '</td>'
850 . '<td id="edit_' . $row_num . '" class="edit center">'
851 . '<a href="#">' . PMA_Util::getIcon('b_edit.png', __('Edit')) . '</a></td>'
852 . '<td class="del_row" data-rownum = "' . $row_num . '">'
853 . '<a hrf="#">' . PMA_Util::getIcon('b_drop.png', __('Delete')) . '</a>'
854 . '<input type="submit" data-rownum = "' . $row_num . '"'
855 . ' class="edit_cancel_form" value="Cancel"></td>'
856 . '<td id="save_' . $row_num . '" style="display:none">'
857 . '<input type="submit" data-rownum = "' . $row_num . '"'
858 . ' class="edit_save_form" value="Save"></td>';
860 $tableHtml .=
861 '<td name="col_name" class="nowrap">'
862 . '<span>' . htmlspecialchars($row['col_name']) . '</span>'
863 . '<input name="orig_col_name" type="hidden" '
864 . 'value="' . htmlspecialchars($row['col_name']) . '">'
865 . PMA\Template::get('columns_definitions/column_name')
866 ->render(
867 array(
868 'columnNumber' => $row_num,
869 'ci' => 0,
870 'ci_offset' => 0,
871 'columnMeta' => array(
872 'Field'=>$row['col_name']
874 'cfgRelation' => array(
875 'centralcolumnswork' => false
879 . '</td>';
880 $tableHtml .=
881 '<td name = "col_type" class="nowrap"><span>'
882 . htmlspecialchars($row['col_type']) . '</span>'
883 . PMA\Template::get('columns_definitions/column_type')
884 ->render(
885 array(
886 'columnNumber' => $row_num,
887 'ci' => 1,
888 'ci_offset' => 0,
889 'type_upper' => /*overload*/mb_strtoupper($row['col_type']),
890 'columnMeta' => array()
893 . '</td>';
894 $tableHtml .=
895 '<td class="nowrap" name="col_length">'
896 . '<span>' . ($row['col_length']?htmlspecialchars($row['col_length']):"")
897 . '</span>'
898 . PMA\Template::get('columns_definitions/column_length')->render(
899 array(
900 'columnNumber' => $row_num,
901 'ci' => 2,
902 'ci_offset' => 0,
903 'length_values_input_size' => 8,
904 'length_to_display' => $row['col_length']
907 . '</td>';
909 $meta = array();
910 if (!isset($row['col_default']) || $row['col_default'] == '') {
911 $meta['DefaultType'] = 'NONE';
912 } else {
913 if ($row['col_default'] == 'CURRENT_TIMESTAMP'
914 || $row['col_default'] == 'NULL'
916 $meta['DefaultType'] = $row['col_default'];
917 } else {
918 $meta['DefaultType'] = 'USER_DEFINED';
919 $meta['DefaultValue'] = $row['col_default'];
922 $tableHtml .=
923 '<td class="nowrap" name="col_default"><span>' . (isset($row['col_default'])
924 ? htmlspecialchars($row['col_default']) : 'None')
925 . '</span>'
926 . PMA\Template::get('columns_definitions/column_default')
927 ->render(
928 array(
929 'columnNumber' => $row_num,
930 'ci' => 3,
931 'ci_offset' => 0,
932 'type_upper' => /*overload*/mb_strtoupper($row['col_type']),
933 'columnMeta' => $meta
936 . '</td>';
938 $tableHtml .=
939 '<td name="collation" class="nowrap">'
940 . '<span>' . htmlspecialchars($row['col_collation']) . '</span>'
941 . PMA_generateCharsetDropdownBox(
942 PMA_CSDROPDOWN_COLLATION, 'field_collation[' . $row_num . ']',
943 'field_' . $row_num . '_4', $row['col_collation'], false
945 . '</td>';
946 $tableHtml .=
947 '<td class="nowrap" name="col_attribute">'
948 . '<span>' .
949 ($row['col_attribute']
950 ? htmlspecialchars($row['col_attribute']) : "" )
951 . '</span>'
952 . PMA\Template::get('columns_definitions/column_attribute')
953 ->render(
954 array(
955 'columnNumber' => $row_num,
956 'ci' => 5,
957 'ci_offset' => 0,
958 'extracted_columnspec' => array(),
959 'columnMeta' => $row['col_attribute'],
960 'submit_attribute' => false,
963 . '</td>';
964 $tableHtml .=
965 '<td class="nowrap" name="col_isNull">'
966 . '<span>' . ($row['col_isNull'] ? __('Yes') : __('No'))
967 . '</span>'
968 . PMA\Template::get('columns_definitions/column_null')
969 ->render(
970 array(
971 'columnNumber' => $row_num,
972 'ci' => 6,
973 'ci_offset' => 0,
974 'columnMeta' => array(
975 'Null' => $row['col_isNull']
979 . '</td>';
981 $tableHtml .=
982 '<td class="nowrap" name="col_extra"><span>'
983 . htmlspecialchars($row['col_extra']) . '</span>'
984 . PMA\Template::get('columns_definitions/column_extra')->render(
985 array(
986 'columnNumber' => $row_num,
987 'ci' => 7,
988 'ci_offset' => 0,
989 'columnMeta' => array('Extra'=>$row['col_extra'])
992 . '</td>';
994 $tableHtml .= '</tr>';
996 return $tableHtml;
1000 * build html for editing a row in central columns table
1002 * @param array $row array contains complete information of
1003 * a particular row of central list table
1004 * @param boolean $odd_row set true if the row is at odd number position
1005 * @param int $row_num position the row in the table
1007 * @return string html of a particular row in the central columns table.
1009 function PMA_getHTMLforCentralColumnsEditTableRow($row, $odd_row, $row_num)
1011 $tableHtml = '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
1012 . '<input name="orig_col_name[' . $row_num . ']" type="hidden" '
1013 . 'value="' . htmlspecialchars($row['col_name']) . '">'
1014 . '<td name="col_name" class="nowrap">'
1015 . PMA\Template::get('columns_definitions/column_name')
1016 ->render(
1017 array(
1018 'columnNumber' => $row_num,
1019 'ci' => 0,
1020 'ci_offset' => 0,
1021 'columnMeta' => array(
1022 'Field' => $row['col_name']
1024 'cfgRelation' => array(
1025 'centralcolumnswork' => false
1029 . '</td>';
1030 $tableHtml .=
1031 '<td name = "col_type" class="nowrap">'
1032 . PMA\Template::get('columns_definitions/column_type')
1033 ->render(
1034 array(
1035 'columnNumber' => $row_num,
1036 'ci' => 1,
1037 'ci_offset' => 0,
1038 'type_upper' => /*overload*/mb_strtoupper($row['col_type']),
1039 'columnMeta' => array()
1042 . '</td>';
1043 $tableHtml .=
1044 '<td class="nowrap" name="col_length">'
1045 . PMA\Template::get('columns_definitions/column_length')->render(
1046 array(
1047 'columnNumber' => $row_num,
1048 'ci' => 2,
1049 'ci_offset' => 0,
1050 'length_values_input_size' => 8,
1051 'length_to_display' => $row['col_length']
1054 . '</td>';
1055 $meta = array();
1056 if (!isset($row['col_default']) || $row['col_default'] == '') {
1057 $meta['DefaultType'] = 'NONE';
1058 } else {
1059 if ($row['col_default'] == 'CURRENT_TIMESTAMP'
1060 || $row['col_default'] == 'NULL'
1062 $meta['DefaultType'] = $row['col_default'];
1063 } else {
1064 $meta['DefaultType'] = 'USER_DEFINED';
1065 $meta['DefaultValue'] = $row['col_default'];
1068 $tableHtml .=
1069 '<td class="nowrap" name="col_default">'
1070 . PMA\Template::get('columns_definitions/column_default')
1071 ->render(
1072 array(
1073 'columnNumber' => $row_num,
1074 'ci' => 3,
1075 'ci_offset' => 0,
1076 'type_upper' => /*overload*/mb_strtoupper($row['col_default']),
1077 'columnMeta' => $meta
1080 . '</td>';
1081 $tableHtml .=
1082 '<td name="collation" class="nowrap">'
1083 . PMA_generateCharsetDropdownBox(
1084 PMA_CSDROPDOWN_COLLATION, 'field_collation[' . $row_num . ']',
1085 'field_' . $row_num . '_4', $row['col_collation'], false
1087 . '</td>';
1088 $tableHtml .=
1089 '<td class="nowrap" name="col_attribute">'
1090 . PMA\Template::get('columns_definitions/column_attribute')
1091 ->render(
1092 array(
1093 'columnNumber' => $row_num,
1094 'ci' => 5,
1095 'ci_offset' => 0,
1096 'extracted_columnspec' => array(
1097 'attribute' => $row['col_attribute']
1099 'columnMeta' => array(),
1100 'submit_attribute' => false,
1103 . '</td>';
1104 $tableHtml .=
1105 '<td class="nowrap" name="col_isNull">'
1106 . PMA\Template::get('columns_definitions/column_null')
1107 ->render(
1108 array(
1109 'columnNumber' => $row_num,
1110 'ci' => 6,
1111 'ci_offset' => 0,
1112 'columnMeta' => array(
1113 'Null' => $row['col_isNull']
1117 . '</td>';
1119 $tableHtml .=
1120 '<td class="nowrap" name="col_extra">'
1121 . PMA\Template::get('columns_definitions/column_extra')->render(
1122 array(
1123 'columnNumber' => $row_num,
1124 'ci' => 7,
1125 'ci_offset' => 0,
1126 'columnMeta' => array('Extra' => $row['col_extra'])
1129 . '</td>';
1130 $tableHtml .= '</tr>';
1131 return $tableHtml;
1135 * get the list of columns in given database excluding
1136 * the columns present in current table
1138 * @param string $db selected database
1139 * @param string $table current table name
1141 * @return string encoded list of columns present in central list for the given
1142 * database
1144 function PMA_getCentralColumnsListRaw($db, $table)
1146 $cfgCentralColumns = PMA_centralColumnsGetParams();
1147 if (empty($cfgCentralColumns)) {
1148 return json_encode(array());
1150 $centralTable = $cfgCentralColumns['table'];
1151 if (empty($table) || $table == '') {
1152 $query = 'SELECT * FROM ' . PMA_Util::backquote($centralTable) . ' '
1153 . 'WHERE db_name = \'' . $db . '\';';
1154 } else {
1155 $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']);
1156 $columns = (array) $GLOBALS['dbi']->getColumnNames(
1157 $db, $table, $GLOBALS['userlink']
1159 $cols = '';
1160 foreach ($columns as $col_select) {
1161 $cols .= '\'' . PMA_Util::sqlAddSlashes($col_select) . '\',';
1163 $cols = trim($cols, ',');
1164 $query = 'SELECT * FROM ' . PMA_Util::backquote($centralTable) . ' '
1165 . 'WHERE db_name = \'' . $db . '\'';
1166 if ($cols) {
1167 $query .= ' AND col_name NOT IN (' . $cols . ')';
1169 $query .= ';';
1171 $GLOBALS['dbi']->selectDb($cfgCentralColumns['db'], $GLOBALS['controllink']);
1172 $columns_list = (array)$GLOBALS['dbi']->fetchResult(
1173 $query, null, null, $GLOBALS['controllink']
1175 PMA_handleColumnExtra($columns_list);
1176 return json_encode($columns_list);
1180 * Get HTML for "check all" check box with "with selected" dropdown
1182 * @param string $pmaThemeImage pma theme image url
1183 * @param string $text_dir url for text directory
1185 * @return string $html_output
1187 function PMA_getCentralColumnsTableFooter($pmaThemeImage, $text_dir)
1189 $html_output = PMA_Util::getWithSelected(
1190 $pmaThemeImage, $text_dir, "tableslistcontainer"
1192 $html_output .= PMA_Util::getButtonOrImage(
1193 'edit_central_columns', 'mult_submit change_central_columns',
1194 'submit_mult_change', __('Edit'), 'b_edit.png', 'edit central columns'
1196 $html_output .= PMA_Util::getButtonOrImage(
1197 'delete_central_columns', 'mult_submit',
1198 'submit_mult_central_columns_remove',
1199 __('Delete'), 'b_drop.png',
1200 'remove_from_central_columns'
1202 return $html_output;
1206 * function generate and return the table footer for
1207 * multiple edit central columns page
1209 * @return string html for table footer in central columns multi edit page
1211 function PMA_getCentralColumnsEditTableFooter()
1213 $html_output = '<fieldset class="tblFooters">'
1214 . '<input type="submit" '
1215 . 'name="save_multi_central_column_edit" value="' . __('Save') . '" />'
1216 . '</fieldset>';
1217 return $html_output;
1220 * Column `col_extra` is used to store both extra and attributes for a column.
1221 * This method separates them.
1223 * @param array &$columns_list columns list
1225 * @return void
1227 function PMA_handleColumnExtra(&$columns_list)
1229 foreach ($columns_list as &$row) {
1230 $vals = explode(',', $row['col_extra']);
1232 if (in_array('BINARY', $vals)) {
1233 $row['col_attribute'] = 'BINARY';
1234 } elseif (in_array('UNSIGNED', $vals)) {
1235 $row['col_attribute'] = 'UNSIGNED';
1236 } elseif (in_array('UNSIGNED ZEROFILL', $vals)) {
1237 $row['col_attribute'] = 'UNSIGNED ZEROFILL';
1238 } elseif (in_array('on update CURRENT_TIMESTAMP', $vals)) {
1239 $row['col_attribute'] = 'on update CURRENT_TIMESTAMP';
1240 } else {
1241 $row['col_attribute'] = '';
1244 if (in_array('auto_increment', $vals)) {
1245 $row['col_extra'] = 'auto_increment';
1246 } else {
1247 $row['col_extra'] = '';
1253 * build html for adding a new user defined column to central list
1255 * @param string $db current database
1257 * @return string html of the form to let user add a new user defined column to the
1258 * list
1260 function PMA_getHTMLforAddNewColumn($db)
1262 $addNewColumn = '<div id="add_col_div"><a href="#">'
1263 . '<span>+</span> ' . __('Add new column') . '</a>'
1264 . '<form id="add_new" style="min-width:100%;display:none" '
1265 . 'method="post" action="db_central_columns.php">'
1266 . PMA_URL_getHiddenInputs(
1269 . '<input type="hidden" name="add_new_column" value="add_new_column">'
1270 . '<table>';
1271 $addNewColumn .= PMA_getCentralColumnsTableHeader();
1272 $addNewColumn .= '<tr>'
1273 . '<td></td>'
1274 . '<td name="col_name" class="nowrap">'
1275 . PMA\Template::get('columns_definitions/column_name')
1276 ->render(
1277 array(
1278 'columnNumber' => 0,
1279 'ci' => 0,
1280 'ci_offset' => 0,
1281 'columnMeta' => array(),
1282 'cfgRelation' => array(
1283 'centralcolumnswork' => false
1287 . '</td>'
1288 . '<td name = "col_type" class="nowrap">'
1289 . PMA\Template::get('columns_definitions/column_type')
1290 ->render(
1291 array(
1292 'columnNumber' => 0,
1293 'ci' => 1,
1294 'ci_offset' => 0,
1295 'type_upper' => '',
1296 'columnMeta' => array()
1299 . '</td>'
1300 . '<td class="nowrap" name="col_length">'
1301 . PMA\Template::get('columns_definitions/column_length')->render(
1302 array(
1303 'columnNumber' => 0,
1304 'ci' => 2,
1305 'ci_offset' => 0,
1306 'length_values_input_size' => 8,
1307 'length_to_display' => ''
1310 . '</td>'
1311 . '<td class="nowrap" name="col_default">'
1312 . PMA\Template::get('columns_definitions/column_default')
1313 ->render(
1314 array(
1315 'columnNumber' => 0,
1316 'ci' => 3,
1317 'ci_offset' => 0,
1318 'type_upper' => '',
1319 'columnMeta' => array()
1322 . '</td>'
1323 . '<td name="collation" class="nowrap">'
1324 . PMA_generateCharsetDropdownBox(
1325 PMA_CSDROPDOWN_COLLATION, 'field_collation[0]',
1326 'field_0_4', null, false
1328 . '</td>'
1329 . '<td class="nowrap" name="col_attribute">'
1330 . PMA\Template::get('columns_definitions/column_attribute')
1331 ->render(
1332 array(
1333 'columnNumber' => 0,
1334 'ci' => 5,
1335 'ci_offset' => 0,
1336 'extracted_columnspec' => array(),
1337 'columnMeta' => array(),
1338 'submit_attribute' => false,
1341 . '</td>'
1342 . '<td class="nowrap" name="col_isNull">'
1343 . PMA\Template::get('columns_definitions/column_null')
1344 ->render(
1345 array(
1346 'columnNumber' => 0,
1347 'ci' => 6,
1348 'ci_offset' => 0,
1349 'columnMeta' => array()
1352 . '</td>'
1353 . '<td class="nowrap" name="col_extra">'
1354 . PMA\Template::get('columns_definitions/column_extra')->render(
1355 array(
1356 'columnNumber' => 0,
1357 'ci' => 7,
1358 'ci_offset' => 0,
1359 'columnMeta' => array()
1362 . '</td>'
1363 . ' <td>'
1364 . '<input id="add_column_save" type="submit" '
1365 . ' value="Save"/></td>'
1366 . '</tr>';
1367 $addNewColumn .= '</table></form></div>';
1368 return $addNewColumn;
1372 * Get HTML for editing page central columns
1374 * @param array $selected_fld Array containing the selected fields
1375 * @param string $selected_db String containing the name of database
1377 * @return string HTML for complete editing page for central columns
1379 function PMA_getHTMLforEditingPage($selected_fld,$selected_db)
1381 $html = '<form id="multi_edit_central_columns">';
1382 $header_cells = array(
1383 __('Name'), __('Type'), __('Length/Values'), __('Default'),
1384 __('Collation'), __('Attributes'), __('Null'), __('A_I')
1386 $html .= PMA_getCentralColumnsEditTableHeader($header_cells);
1387 $selected_fld_safe = array();
1388 foreach ($selected_fld as $key) {
1389 $selected_fld_safe[] = PMA_Util::sqlAddSlashes($key);
1391 $columns_list = implode("','", $selected_fld_safe);
1392 $columns_list = "'" . $columns_list . "'";
1393 $list_detail_cols = PMA_findExistingColNames($selected_db, $columns_list, true);
1394 $odd_row = false;
1395 $row_num = 0;
1396 foreach ($list_detail_cols as $row) {
1397 $tableHtmlRow = PMA_getHTMLforCentralColumnsEditTableRow(
1398 $row, $odd_row, $row_num
1400 $html .= $tableHtmlRow;
1401 $odd_row = !$odd_row;
1402 $row_num++;
1404 $html .= '</table>';
1405 $html .= PMA_getCentralColumnsEditTableFooter();
1406 $html .= '</form>';
1407 return $html;