getParsedBodyParam('selected_fld', $request->getParsedBodyParam('selected')); if (! is_array($selected) || $selected === []) { $this->response->setRequestStatus(false); $this->response->addJSON('message', __('No column selected.')); return; } $this->dbi->selectDb($GLOBALS['db']); $hasPrimary = $this->hasPrimaryKey(); /** @var string|null $deletionConfirmed */ $deletionConfirmed = $request->getParsedBodyParam('mult_btn', null); if ($hasPrimary && $deletionConfirmed === null) { $this->checkParameters(['db', 'table']); $GLOBALS['urlParams'] = ['db' => $GLOBALS['db'], 'table' => $GLOBALS['table']]; $GLOBALS['errorUrl'] = Util::getScriptNameForOption($GLOBALS['cfg']['DefaultTabTable'], 'table'); $GLOBALS['errorUrl'] .= Url::getCommon($GLOBALS['urlParams'], '&'); $databaseName = DatabaseName::tryFrom($request->getParam('db')); if ($databaseName === null || ! $this->dbTableExists->hasDatabase($databaseName)) { if ($request->isAjax()) { $this->response->setRequestStatus(false); $this->response->addJSON('message', Message::error(__('No databases selected.'))); return; } $this->redirect('/', ['reload' => true, 'message' => __('No databases selected.')]); return; } $tableName = TableName::tryFrom($request->getParam('table')); if ($tableName === null || ! $this->dbTableExists->hasTable($databaseName, $tableName)) { if ($request->isAjax()) { $this->response->setRequestStatus(false); $this->response->addJSON('message', Message::error(__('No table selected.'))); return; } $this->redirect('/', ['reload' => true, 'message' => __('No table selected.')]); return; } $this->render('table/structure/primary', [ 'db' => $GLOBALS['db'], 'table' => $GLOBALS['table'], 'selected' => $selected, ]); return; } if ($deletionConfirmed === __('Yes') || ! $hasPrimary) { $GLOBALS['sql_query'] = 'ALTER TABLE ' . Util::backquote($GLOBALS['table']); if ($hasPrimary) { $GLOBALS['sql_query'] .= ' DROP PRIMARY KEY,'; } $GLOBALS['sql_query'] .= ' ADD PRIMARY KEY('; $i = 1; $selectedCount = count($selected); foreach ($selected as $field) { $GLOBALS['sql_query'] .= Util::backquote($field); $GLOBALS['sql_query'] .= $i++ === $selectedCount ? ');' : ', '; } $this->dbi->selectDb($GLOBALS['db']); $result = $this->dbi->tryQuery($GLOBALS['sql_query']); if (! $result) { $GLOBALS['message'] = Message::error($this->dbi->getError()); } } if (empty($GLOBALS['message'])) { $GLOBALS['message'] = Message::success(); } ($this->structureController)($request); } private function hasPrimaryKey(): bool { $result = $this->dbi->query('SHOW KEYS FROM ' . Util::backquote($GLOBALS['table'])); foreach ($result as $row) { if ($row['Key_name'] === 'PRIMARY') { return true; } } return false; } }