1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\TableRelationController
*
* @package PMA
*/
namespace PMA\libraries\controllers\table;
require_once 'libraries/index.lib.php';
use PMA\libraries\controllers\TableController;
use PMA\libraries\DatabaseInterface;
use PMA\libraries\Index;
use PMA\libraries\Table;
use PMA\libraries\Template;
use PMA\libraries\Util;
/**
* Handles table relation logic
*
* @package PhpMyAdmin
*/
class TableRelationController extends TableController
{
/**
* @var array $options_array
*/
protected $options_array;
/**
* @var array $cfgRelation
*/
protected $cfgRelation;
/**
* @var array $existrel
*/
protected $existrel;
/**
* @var string $disp
*/
protected $disp;
/**
* @var string $tbl_storage_engine
*/
protected $tbl_storage_engine;
/**
* @var array $existrel_foreign
*/
protected $existrel_foreign;
/**
* @var Table $udp_query
*/
protected $upd_query;
/**
* Constructor
*
* @param array $options_array Options
* @param array $cfgRelation Config relation
* @param string $tbl_storage_engine Table storage engine
* @param array $existrel Relations
* @param array $existrel_foreign External relations
* @param string $disp Display
* @param string $upd_query Update query
*/
public function __construct($options_array, $cfgRelation, $tbl_storage_engine,
$existrel, $existrel_foreign, $disp, $upd_query
) {
parent::__construct();
$this->options_array = $options_array;
$this->cfgRelation = $cfgRelation;
$this->tbl_storage_engine = $tbl_storage_engine;
$this->existrel = $existrel;
$this->existrel_foreign = $existrel_foreign;
$this->disp = $disp;
$this->upd_query = $upd_query;
}
/**
* Index
*
* @return void
*/
public function indexAction()
{
// Send table of column names to populate corresponding dropdowns depending
// on the current selection
if (isset($_REQUEST['getDropdownValues'])
&& $_REQUEST['getDropdownValues'] === 'true'
) {
// if both db and table are selected
if (isset($_REQUEST['foreignTable'])) {
$this->getDropdownValueForTableAction();
} else { // if only the db is selected
$this->getDropdownValueForDbAction();
}
return;
}
$this->response->getHeader()->getScripts()->addFiles(
array(
'tbl_relation.js',
'indexes.js'
)
);
// Gets tables information
include_once 'libraries/tbl_info.inc.php';
// updates for Internal relations
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->updateForInternalRelationAction();
}
// updates for foreign keys
if (isset($_POST['destination_foreign_db'])) {
$this->updateForForeignKeysAction();
}
// Updates for display field
if ($this->cfgRelation['displaywork'] && isset($_POST['display_field'])) {
$this->updateForDisplayField();
}
// If we did an update, refresh our data
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->existrel = PMA_getForeigners(
$this->db, $this->table, '', 'internal'
);
}
if (isset($_POST['destination_foreign_db'])
&& Util::isForeignKeySupported($this->tbl_storage_engine)
) {
$this->existrel_foreign = PMA_getForeigners(
$this->db, $this->table, '', 'foreign'
);
}
if ($this->cfgRelation['displaywork']) {
$this->disp = PMA_getDisplayField($this->db, $this->table);
}
// display secondary level tabs if necessary
$engine = $this->dbi->getTable($this->db, $this->table)
->getStatusInfo('ENGINE');
$this->response->addHTML(
Template::get('table/secondary_tabs')->render(
array(
'url_params' => array(
'db' => $GLOBALS['db'],
'table' => $GLOBALS['table']
),
'engine' => $engine
)
)
);
$this->response->addHTML('<div id="structure_content">');
/**
* Dialog
*/
// Now find out the columns of our $table
// need to use DatabaseInterface::QUERY_STORE with $this->dbi->numRows()
// in mysqli
$columns = $this->dbi->getColumns($this->db, $this->table);
// common form
$this->response->addHTML(
Template::get('table/relation/common_form')->render(
array(
'db' => $this->db,
'table' => $this->table,
'columns' => $columns,
'cfgRelation' => $this->cfgRelation,
'tbl_storage_engine' => $this->tbl_storage_engine,
'existrel' => isset($this->existrel) ? $this->existrel : array(),
'existrel_foreign' => isset($this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data'] : array(),
'options_array' => $this->options_array
)
)
);
if (Util::isForeignKeySupported($this->tbl_storage_engine)) {
$this->response->addHTML(PMA_getHtmlForDisplayIndexes());
}
$this->response->addHTML('</div>');
}
/**
* Update for display field
*
* @return void
*/
public function updateForDisplayField()
{
if ($this->upd_query->updateDisplayField(
$this->disp, $_POST['display_field'], $this->cfgRelation
)
) {
$this->response->addHTML(
Util::getMessage(
__('Display column was successfully updated.'),
'', 'success'
)
);
}
}
/**
* Update for FK
*
* @return void
*/
public function updateForForeignKeysAction()
{
$multi_edit_columns_name = isset($_REQUEST['foreign_key_fields_name'])
? $_REQUEST['foreign_key_fields_name']
: null;
// (for now, one index name only; we keep the definitions if the
// foreign db is not the same)
list($html, $preview_sql_data, $display_query, $seen_error)
= $this->upd_query->updateForeignKeys(
$_POST['destination_foreign_db'],
$multi_edit_columns_name, $_POST['destination_foreign_table'],
$_POST['destination_foreign_column'], $this->options_array,
$this->table,
isset($this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data']
: null
);
$this->response->addHTML($html);
// If there is a request for SQL previewing.
if (isset($_REQUEST['preview_sql'])) {
PMA_previewSQL($preview_sql_data);
}
if (!empty($display_query) && !$seen_error) {
$GLOBALS['display_query'] = $display_query;
$this->response->addHTML(
Util::getMessage(
__('Your SQL query has been executed successfully.'),
null, 'success'
)
);
}
}
/**
* Update for internal relation
*
* @return void
*/
public function updateForInternalRelationAction()
{
$multi_edit_columns_name = isset($_REQUEST['fields_name'])
? $_REQUEST['fields_name']
: null;
if ($this->upd_query->updateInternalRelations(
$multi_edit_columns_name,
$_POST['destination_db'],
$_POST['destination_table'],
$_POST['destination_column'],
$this->cfgRelation,
isset($this->existrel) ? $this->existrel : null
)
) {
$this->response->addHTML(
Util::getMessage(
__('Internal relations were successfully updated.'),
'', 'success'
)
);
}
}
/**
* Send table columns for foreign table dropdown
*
* @return void
*
*/
public function getDropdownValueForTableAction()
{
$foreignTable = $_REQUEST['foreignTable'];
$table_obj = $this->dbi->getTable($_REQUEST['foreignDb'], $foreignTable);
// Since views do not have keys defined on them provide the full list of
// columns
if ($table_obj->isView()) {
$columnList = $table_obj->getColumns(false, false);
} else {
$columnList = $table_obj->getIndexedColumns(false, false);
}
$columns = array();
foreach ($columnList as $column) {
$columns[] = htmlspecialchars($column);
}
$this->response->addJSON('columns', $columns);
// @todo should be: $server->db($db)->table($table)->primary()
$primary = Index::getPrimary($foreignTable, $_REQUEST['foreignDb']);
if (false === $primary) {
return;
}
$this->response->addJSON('primary', array_keys($primary->getColumns()));
}
/**
* Send database selection values for dropdown
*
* @return void
*
*/
public function getDropdownValueForDbAction()
{
$tables = array();
$foreign = isset($_REQUEST['foreign']) && $_REQUEST['foreign'] === 'true';
if ($foreign) {
$query = 'SHOW TABLE STATUS FROM '
. Util::backquote($_REQUEST['foreignDb']);
$tables_rs = $this->dbi->query(
$query,
null,
DatabaseInterface::QUERY_STORE
);
while ($row = $this->dbi->fetchArray($tables_rs)) {
if (isset($row['Engine'])
&& mb_strtoupper($row['Engine']) == $this->tbl_storage_engine
) {
$tables[] = htmlspecialchars($row['Name']);
}
}
} else {
$query = 'SHOW TABLES FROM '
. Util::backquote($_REQUEST['foreignDb']);
$tables_rs = $this->dbi->query(
$query,
null,
DatabaseInterface::QUERY_STORE
);
while ($row = $this->dbi->fetchArray($tables_rs)) {
$tables[] = htmlspecialchars($row[0]);
}
}
$this->response->addJSON('tables', $tables);
}
}
|