Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / pmd_common.php
blob078b824f47d78a9dde1645920500a5da6500726b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common functions for Designer
6 * @package PhpMyAdmin-Designer
7 */
8 use PhpMyAdmin\Relation;
9 use PhpMyAdmin\Table;
11 /**
12 * Block attempts to directly run this script
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 $GLOBALS['PMD']['STYLE'] = 'default';
20 $cfgRelation = Relation::getRelationsParam();
22 /**
23 * Retrieves table info and stores it in $GLOBALS['PMD']
25 * @return array with table info
27 function PMA_getTablesInfo()
29 $retval = array();
31 $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
32 $GLOBALS['PMD']['OWNER'] = array();
33 $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
35 $tables = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
36 // seems to be needed later
37 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
38 $i = 0;
39 foreach ($tables as $one_table) {
40 $GLOBALS['PMD']['TABLE_NAME'][$i]
41 = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
42 $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
43 $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = htmlspecialchars(
44 $one_table['TABLE_NAME'], ENT_QUOTES
47 $GLOBALS['PMD_URL']['TABLE_NAME'][$i]
48 = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
49 $GLOBALS['PMD_URL']['OWNER'][$i] = $GLOBALS['db'];
50 $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i]
51 = $one_table['TABLE_NAME'];
53 $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars(
54 $GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES
56 $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars(
57 $GLOBALS['db'], ENT_QUOTES
59 $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars(
60 $one_table['TABLE_NAME'], ENT_QUOTES
63 $GLOBALS['PMD']['TABLE_TYPE'][$i] = mb_strtoupper(
64 $one_table['ENGINE']
67 $DF = Relation::getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
68 if ($DF != '') {
69 $retval[$GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i]] = $DF;
72 $i++;
75 return $retval;
78 /**
79 * Retrieves table column info
81 * @return array table column nfo
83 function PMA_getColumnsInfo()
85 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
86 $tab_column = array();
87 for ($i = 0, $cnt = count($GLOBALS['PMD']["TABLE_NAME"]); $i < $cnt; $i++) {
88 $fields_rs = $GLOBALS['dbi']->query(
89 $GLOBALS['dbi']->getColumnsSql(
90 $GLOBALS['db'],
91 $GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i],
92 null,
93 true
95 null,
96 PhpMyAdmin\DatabaseInterface::QUERY_STORE
98 $tbl_name_i = $GLOBALS['PMD']['TABLE_NAME'][$i];
99 $j = 0;
100 while ($row = $GLOBALS['dbi']->fetchAssoc($fields_rs)) {
101 $tab_column[$tbl_name_i]['COLUMN_ID'][$j] = $j;
102 $tab_column[$tbl_name_i]['COLUMN_NAME'][$j] = $row['Field'];
103 $tab_column[$tbl_name_i]['TYPE'][$j] = $row['Type'];
104 $tab_column[$tbl_name_i]['NULLABLE'][$j] = $row['Null'];
105 $j++;
108 return $tab_column;
112 * Returns JavaScript code for initializing vars
114 * @return string JavaScript code
116 function PMA_getScriptContr()
118 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
119 $con = array();
120 $con["C_NAME"] = array();
121 $i = 0;
122 $alltab_rs = $GLOBALS['dbi']->query(
123 'SHOW TABLES FROM ' . PhpMyAdmin\Util::backquote($GLOBALS['db']),
124 null,
125 PhpMyAdmin\DatabaseInterface::QUERY_STORE
127 while ($val = @$GLOBALS['dbi']->fetchRow($alltab_rs)) {
128 $row = Relation::getForeigners($GLOBALS['db'], $val[0], '', 'internal');
130 if ($row !== false) {
131 foreach ($row as $field => $value) {
132 $con['C_NAME'][$i] = '';
133 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
134 $con['DCN'][$i] = urlencode($field);
135 $con['STN'][$i] = urlencode(
136 $value['foreign_db'] . "." . $value['foreign_table']
138 $con['SCN'][$i] = urlencode($value['foreign_field']);
139 $i++;
142 $row = Relation::getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
144 if ($row !== false) {
145 foreach ($row['foreign_keys_data'] as $one_key) {
146 foreach ($one_key['index_list'] as $index => $one_field) {
147 $con['C_NAME'][$i] = $one_key['constraint'];
148 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
149 $con['DCN'][$i] = urlencode($one_field);
150 $con['STN'][$i] = urlencode(
151 (isset($one_key['ref_db_name']) ?
152 $one_key['ref_db_name'] : $GLOBALS['db'])
153 . "." . $one_key['ref_table_name']
155 $con['SCN'][$i] = urlencode($one_key['ref_index_list'][$index]);
156 $i++;
162 $ti = 0;
163 $retval = array();
164 for ($i = 0, $cnt = count($con["C_NAME"]); $i < $cnt; $i++) {
165 $c_name_i = $con['C_NAME'][$i];
166 $dtn_i = $con['DTN'][$i];
167 $retval[$ti] = array();
168 $retval[$ti][$c_name_i] = array();
169 if (in_array($dtn_i, $GLOBALS['PMD_URL']["TABLE_NAME"])
170 && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
172 $retval[$ti][$c_name_i][$dtn_i] = array();
173 $retval[$ti][$c_name_i][$dtn_i][$con['DCN'][$i]] = array(
174 0 => $con['STN'][$i],
175 1 => $con['SCN'][$i]
178 $ti++;
180 return $retval;
184 * Returns UNIQUE and PRIMARY indices
186 * @return array unique or primary indices
188 function PMA_getPKOrUniqueKeys()
190 return PMA_getAllKeys(true);
194 * Returns all indices
196 * @param bool $unique_only whether to include only unique ones
198 * @return array indices
200 function PMA_getAllKeys($unique_only = false)
202 $keys = array();
204 foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
205 $schema = $GLOBALS['PMD']['OWNER'][$I];
206 // for now, take into account only the first index segment
207 foreach (PhpMyAdmin\Index::getFromTable($table, $schema) as $index) {
208 if ($unique_only && ! $index->isUnique()) {
209 continue;
211 $columns = $index->getColumns();
212 foreach ($columns as $column_name => $dummy) {
213 $keys[$schema . '.' . $table . '.' . $column_name] = 1;
217 return $keys;
221 * Return script to create j_tab and h_tab arrays
223 * @return string
225 function PMA_getScriptTabs()
227 $retval = array(
228 'j_tabs' => array(),
229 'h_tabs' => array()
232 for ($i = 0, $cnt = count($GLOBALS['PMD']['TABLE_NAME']); $i < $cnt; $i++) {
233 $j = 0;
234 if (PhpMyAdmin\Util::isForeignKeySupported($GLOBALS['PMD']['TABLE_TYPE'][$i])) {
235 $j = 1;
237 $retval['j_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = $j;
238 $retval['h_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = 1;
240 return $retval;
244 * Returns table positions of a given pdf page
246 * @param int $pg pdf page id
248 * @return array of table positions
250 function PMA_getTablePositions($pg)
252 $cfgRelation = Relation::getRelationsParam();
253 if (! $cfgRelation['pdfwork']) {
254 return null;
257 $query = "
258 SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
259 `x` AS `X`,
260 `y` AS `Y`,
261 1 AS `V`,
262 1 AS `H`
263 FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
264 . "." . PhpMyAdmin\Util::backquote($cfgRelation['table_coords']) . "
265 WHERE pdf_page_number = " . intval($pg);
267 $tab_pos = $GLOBALS['dbi']->fetchResult(
268 $query,
269 'name',
270 null,
271 $GLOBALS['controllink'],
272 PhpMyAdmin\DatabaseInterface::QUERY_STORE
274 return $tab_pos;
278 * Returns page name of a given pdf page
280 * @param int $pg pdf page id
282 * @return String table name
284 function PMA_getPageName($pg)
286 $cfgRelation = Relation::getRelationsParam();
287 if (! $cfgRelation['pdfwork']) {
288 return null;
291 $query = "SELECT `page_descr`"
292 . " FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
293 . "." . PhpMyAdmin\Util::backquote($cfgRelation['pdf_pages'])
294 . " WHERE " . PhpMyAdmin\Util::backquote('page_nr') . " = " . intval($pg);
295 $page_name = $GLOBALS['dbi']->fetchResult(
296 $query,
297 null,
298 null,
299 $GLOBALS['controllink'],
300 PhpMyAdmin\DatabaseInterface::QUERY_STORE
302 return count($page_name) ? $page_name[0] : null;
306 * Deletes a given pdf page and its corresponding coordinates
308 * @param int $pg page id
310 * @return boolean success/failure
312 function PMA_deletePage($pg)
314 $cfgRelation = Relation::getRelationsParam();
315 if (! $cfgRelation['pdfwork']) {
316 return false;
319 $query = "DELETE FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
320 . "." . PhpMyAdmin\Util::backquote($cfgRelation['table_coords'])
321 . " WHERE " . PhpMyAdmin\Util::backquote('pdf_page_number') . " = " . intval($pg);
322 $success = Relation::queryAsControlUser(
323 $query, true, PhpMyAdmin\DatabaseInterface::QUERY_STORE
326 if ($success) {
327 $query = "DELETE FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
328 . "." . PhpMyAdmin\Util::backquote($cfgRelation['pdf_pages'])
329 . " WHERE " . PhpMyAdmin\Util::backquote('page_nr') . " = " . intval($pg);
330 $success = Relation::queryAsControlUser(
331 $query, true, PhpMyAdmin\DatabaseInterface::QUERY_STORE
335 return (boolean) $success;
339 * Returns the id of the default pdf page of the database.
340 * Default page is the one which has the same name as the database.
342 * @param string $db database
344 * @return int id of the default pdf page for the database
346 function PMA_getDefaultPage($db)
348 $cfgRelation = Relation::getRelationsParam();
349 if (! $cfgRelation['pdfwork']) {
350 return null;
353 $query = "SELECT `page_nr`"
354 . " FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
355 . "." . PhpMyAdmin\Util::backquote($cfgRelation['pdf_pages'])
356 . " WHERE `db_name` = '" . $GLOBALS['dbi']->escapeString($db) . "'"
357 . " AND `page_descr` = '" . $GLOBALS['dbi']->escapeString($db) . "'";
359 $default_page_no = $GLOBALS['dbi']->fetchResult(
360 $query,
361 null,
362 null,
363 $GLOBALS['controllink'],
364 PhpMyAdmin\DatabaseInterface::QUERY_STORE
367 if (count($default_page_no)) {
368 return intval($default_page_no[0]);
370 return -1;
374 * Get the id of the page to load. If a default page exists it will be returned.
375 * If no such exists, returns the id of the first page of the database.
377 * @param string $db database
379 * @return int id of the page to load
381 function PMA_getLoadingPage($db)
383 $cfgRelation = Relation::getRelationsParam();
384 if (! $cfgRelation['pdfwork']) {
385 return null;
388 $page_no = -1;
390 $default_page_no = PMA_getDefaultPage($db);
391 if ($default_page_no != -1) {
392 $page_no = $default_page_no;
393 } else {
394 $query = "SELECT MIN(`page_nr`)"
395 . " FROM " . PhpMyAdmin\Util::backquote($cfgRelation['db'])
396 . "." . PhpMyAdmin\Util::backquote($cfgRelation['pdf_pages'])
397 . " WHERE `db_name` = '" . $GLOBALS['dbi']->escapeString($db) . "'";
399 $min_page_no = $GLOBALS['dbi']->fetchResult(
400 $query,
401 null,
402 null,
403 $GLOBALS['controllink'],
404 PhpMyAdmin\DatabaseInterface::QUERY_STORE
406 if (count($min_page_no[0])) {
407 $page_no = $min_page_no[0];
410 return intval($page_no);
414 * Creates a new page and returns its auto-incrementing id
416 * @param string $pageName name of the page
417 * @param string $db name of the database
419 * @return int|null
421 function PMA_createNewPage($pageName, $db)
423 $cfgRelation = Relation::getRelationsParam();
424 if ($cfgRelation['pdfwork']) {
425 $pageNumber = Relation::createPage(
426 $pageName,
427 $cfgRelation,
430 return $pageNumber;
432 return null;
436 * Saves positions of table(s) of a given pdf page
438 * @param int $pg pdf page id
440 * @return boolean success/failure
442 function PMA_saveTablePositions($pg)
444 $cfgRelation = Relation::getRelationsParam();
445 if (! $cfgRelation['pdfwork']) {
446 return false;
449 $query = "DELETE FROM "
450 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['db'])
451 . "." . PhpMyAdmin\Util::backquote(
452 $GLOBALS['cfgRelation']['table_coords']
454 . " WHERE `db_name` = '" . $GLOBALS['dbi']->escapeString($_REQUEST['db'])
455 . "'"
456 . " AND `pdf_page_number` = '" . $GLOBALS['dbi']->escapeString($pg)
457 . "'";
459 $res = Relation::queryAsControlUser(
460 $query,
461 true,
462 PhpMyAdmin\DatabaseInterface::QUERY_STORE
465 if (!$res) {
466 return (boolean)$res;
469 foreach ($_REQUEST['t_h'] as $key => $value) {
470 list($DB, $TAB) = explode(".", $key);
471 if (!$value) {
472 continue;
475 $query = "INSERT INTO "
476 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['db']) . "."
477 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['table_coords'])
478 . " (`db_name`, `table_name`, `pdf_page_number`, `x`, `y`)"
479 . " VALUES ("
480 . "'" . $GLOBALS['dbi']->escapeString($DB) . "', "
481 . "'" . $GLOBALS['dbi']->escapeString($TAB) . "', "
482 . "'" . $GLOBALS['dbi']->escapeString($pg) . "', "
483 . "'" . $GLOBALS['dbi']->escapeString($_REQUEST['t_x'][$key]) . "', "
484 . "'" . $GLOBALS['dbi']->escapeString($_REQUEST['t_y'][$key]) . "')";
486 $res = Relation::queryAsControlUser(
487 $query, true, PhpMyAdmin\DatabaseInterface::QUERY_STORE
491 return (boolean) $res;
495 * Saves the display field for a table.
497 * @param string $db database name
498 * @param string $table table name
499 * @param string $field display field name
501 * @return boolean
503 function PMA_saveDisplayField($db, $table, $field)
505 $cfgRelation = Relation::getRelationsParam();
506 if (!$cfgRelation['displaywork']) {
507 return false;
510 $upd_query = new Table($table, $db, $GLOBALS['dbi']);
511 $upd_query->updateDisplayField($field, $cfgRelation);
513 return true;
517 * Adds a new foreign relation
519 * @param string $db database name
520 * @param string $T1 foreign table
521 * @param string $F1 foreign field
522 * @param string $T2 master table
523 * @param string $F2 master field
524 * @param string $on_delete on delete action
525 * @param string $on_update on update action
527 * @return array array of success/failure and message
529 function PMA_addNewRelation($db, $T1, $F1, $T2, $F2, $on_delete, $on_update, $DB1, $DB2)
531 $tables = $GLOBALS['dbi']->getTablesFull($DB1, $T1);
532 $type_T1 = mb_strtoupper($tables[$T1]['ENGINE']);
533 $tables = $GLOBALS['dbi']->getTablesFull($DB2, $T2);
534 $type_T2 = mb_strtoupper($tables[$T2]['ENGINE']);
536 // native foreign key
537 if (PhpMyAdmin\Util::isForeignKeySupported($type_T1)
538 && PhpMyAdmin\Util::isForeignKeySupported($type_T2)
539 && $type_T1 == $type_T2
541 // relation exists?
542 $existrel_foreign = Relation::getForeigners($DB2, $T2, '', 'foreign');
543 $foreigner = Relation::searchColumnInForeigners($existrel_foreign, $F2);
544 if ($foreigner
545 && isset($foreigner['constraint'])
547 return array(false, __('Error: relationship already exists.'));
549 // note: in InnoDB, the index does not requires to be on a PRIMARY
550 // or UNIQUE key
551 // improve: check all other requirements for InnoDB relations
552 $result = $GLOBALS['dbi']->query(
553 'SHOW INDEX FROM ' . PhpMyAdmin\Util::backquote($DB1)
554 . '.' . PhpMyAdmin\Util::backquote($T1) . ';'
557 // will be use to emphasis prim. keys in the table view
558 $index_array1 = array();
559 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
560 $index_array1[$row['Column_name']] = 1;
562 $GLOBALS['dbi']->freeResult($result);
564 $result = $GLOBALS['dbi']->query(
565 'SHOW INDEX FROM ' . PhpMyAdmin\Util::backquote($DB2)
566 . '.' . PhpMyAdmin\Util::backquote($T2) . ';'
568 // will be used to emphasis prim. keys in the table view
569 $index_array2 = array();
570 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
571 $index_array2[$row['Column_name']] = 1;
573 $GLOBALS['dbi']->freeResult($result);
575 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
576 $upd_query = 'ALTER TABLE ' . PhpMyAdmin\Util::backquote($DB2)
577 . '.' . PhpMyAdmin\Util::backquote($T2)
578 . ' ADD FOREIGN KEY ('
579 . PhpMyAdmin\Util::backquote($F2) . ')'
580 . ' REFERENCES '
581 . PhpMyAdmin\Util::backquote($DB1) . '.'
582 . PhpMyAdmin\Util::backquote($T1) . '('
583 . PhpMyAdmin\Util::backquote($F1) . ')';
585 if ($on_delete != 'nix') {
586 $upd_query .= ' ON DELETE ' . $on_delete;
588 if ($on_update != 'nix') {
589 $upd_query .= ' ON UPDATE ' . $on_update;
591 $upd_query .= ';';
592 if ($GLOBALS['dbi']->tryQuery($upd_query)) {
593 return array(true, __('FOREIGN KEY relationship has been added.'));
596 $error = $GLOBALS['dbi']->getError();
597 return array(
598 false,
599 __('Error: FOREIGN KEY relationship could not be added!')
600 . "<br/>" . $error
604 return array(false, __('Error: Missing index on column(s).'));
607 // internal (pmadb) relation
608 if ($GLOBALS['cfgRelation']['relwork'] == false) {
609 return array(false, __('Error: Relational features are disabled!'));
612 // no need to recheck if the keys are primary or unique at this point,
613 // this was checked on the interface part
615 $q = "INSERT INTO "
616 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['db'])
617 . "."
618 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['relation'])
619 . "(master_db, master_table, master_field, "
620 . "foreign_db, foreign_table, foreign_field)"
621 . " values("
622 . "'" . $GLOBALS['dbi']->escapeString($DB2) . "', "
623 . "'" . $GLOBALS['dbi']->escapeString($T2) . "', "
624 . "'" . $GLOBALS['dbi']->escapeString($F2) . "', "
625 . "'" . $GLOBALS['dbi']->escapeString($DB1) . "', "
626 . "'" . $GLOBALS['dbi']->escapeString($T1) . "', "
627 . "'" . $GLOBALS['dbi']->escapeString($F1) . "')";
629 if (Relation::queryAsControlUser($q, false, PhpMyAdmin\DatabaseInterface::QUERY_STORE)
631 return array(true, __('Internal relationship has been added.'));
634 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
635 return array(
636 false,
637 __('Error: Internal relationship could not be added!')
638 . "<br/>" . $error
643 * Removes a foreign relation
645 * @param string $T1 foreign db.table
646 * @param string $F1 foreign field
647 * @param string $T2 master db.table
648 * @param string $F2 master field
650 * @return array array of success/failure and message
652 function PMA_removeRelation($T1, $F1, $T2, $F2)
654 list($DB1, $T1) = explode(".", $T1);
655 list($DB2, $T2) = explode(".", $T2);
657 $tables = $GLOBALS['dbi']->getTablesFull($DB1, $T1);
658 $type_T1 = mb_strtoupper($tables[$T1]['ENGINE']);
659 $tables = $GLOBALS['dbi']->getTablesFull($DB2, $T2);
660 $type_T2 = mb_strtoupper($tables[$T2]['ENGINE']);
662 if (PhpMyAdmin\Util::isForeignKeySupported($type_T1)
663 && PhpMyAdmin\Util::isForeignKeySupported($type_T2)
664 && $type_T1 == $type_T2
666 // InnoDB
667 $existrel_foreign = Relation::getForeigners($DB2, $T2, '', 'foreign');
668 $foreigner = Relation::searchColumnInForeigners($existrel_foreign, $F2);
670 if (isset($foreigner['constraint'])) {
671 $upd_query = 'ALTER TABLE ' . PhpMyAdmin\Util::backquote($DB2)
672 . '.' . PhpMyAdmin\Util::backquote($T2) . ' DROP FOREIGN KEY '
673 . PhpMyAdmin\Util::backquote($foreigner['constraint']) . ';';
674 if ($GLOBALS['dbi']->query($upd_query)) {
675 return array(true, __('FOREIGN KEY relationship has been removed.'));
678 $error = $GLOBALS['dbi']->getError();
679 return array(
680 false,
681 __('Error: FOREIGN KEY relationship could not be removed!')
682 . "<br/>" . $error
687 // internal relations
688 $delete_query = "DELETE FROM "
689 . PhpMyAdmin\Util::backquote($GLOBALS['cfgRelation']['db']) . "."
690 . $GLOBALS['cfgRelation']['relation'] . " WHERE "
691 . "master_db = '" . $GLOBALS['dbi']->escapeString($DB2) . "'"
692 . " AND master_table = '" . $GLOBALS['dbi']->escapeString($T2) . "'"
693 . " AND master_field = '" . $GLOBALS['dbi']->escapeString($F2) . "'"
694 . " AND foreign_db = '" . $GLOBALS['dbi']->escapeString($DB1) . "'"
695 . " AND foreign_table = '" . $GLOBALS['dbi']->escapeString($T1) . "'"
696 . " AND foreign_field = '" . $GLOBALS['dbi']->escapeString($F1) . "'";
698 $result = Relation::queryAsControlUser(
699 $delete_query,
700 false,
701 PhpMyAdmin\DatabaseInterface::QUERY_STORE
704 if (!$result) {
705 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
706 return array(
707 false,
708 __('Error: Internal relationship could not be removed!') . "<br/>" . $error
712 return array(true, __('Internal relationship has been removed.'));
716 * Save value for a designer setting
718 * @param string $index setting
719 * @param string $value value
721 * @return bool whether the operation succeeded
723 function PMA_saveDesignerSetting($index, $value)
725 $cfgRelation = Relation::getRelationsParam();
726 $cfgDesigner = array(
727 'user' => $GLOBALS['cfg']['Server']['user'],
728 'db' => $cfgRelation['db'],
729 'table' => $cfgRelation['designer_settings']
732 $success = true;
733 if ($GLOBALS['cfgRelation']['designersettingswork']) {
735 $orig_data_query = "SELECT settings_data"
736 . " FROM " . PhpMyAdmin\Util::backquote($cfgDesigner['db'])
737 . "." . PhpMyAdmin\Util::backquote($cfgDesigner['table'])
738 . " WHERE username = '"
739 . $GLOBALS['dbi']->escapeString($cfgDesigner['user']) . "';";
741 $orig_data = $GLOBALS['dbi']->fetchSingleRow(
742 $orig_data_query, 'ASSOC', $GLOBALS['controllink']
745 if (! empty($orig_data)) {
746 $orig_data = json_decode($orig_data['settings_data'], true);
747 $orig_data[$index] = $value;
748 $orig_data = json_encode($orig_data);
750 $save_query = "UPDATE "
751 . PhpMyAdmin\Util::backquote($cfgDesigner['db'])
752 . "." . PhpMyAdmin\Util::backquote($cfgDesigner['table'])
753 . " SET settings_data = '" . $orig_data . "'"
754 . " WHERE username = '"
755 . $GLOBALS['dbi']->escapeString($cfgDesigner['user']) . "';";
757 $success = Relation::queryAsControlUser($save_query);
758 } else {
759 $save_data = array($index => $value);
761 $query = "INSERT INTO "
762 . PhpMyAdmin\Util::backquote($cfgDesigner['db'])
763 . "." . PhpMyAdmin\Util::backquote($cfgDesigner['table'])
764 . " (username, settings_data)"
765 . " VALUES('" . $cfgDesigner['user'] . "',"
766 . " '" . json_encode($save_data) . "');";
768 $success = Relation::queryAsControlUser($query);
772 return (bool) $success;