"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-english/libraries/classes/Controllers/Database/DesignerController.php" (24 Feb 2021, 10050 Bytes) of package /linux/www/phpMyAdmin-5.1.0-english.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "DesignerController.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace PhpMyAdmin\Controllers\Database;
    6 
    7 use PhpMyAdmin\Database\Designer;
    8 use PhpMyAdmin\Database\Designer\Common as DesignerCommon;
    9 use PhpMyAdmin\Response;
   10 use PhpMyAdmin\Template;
   11 use PhpMyAdmin\Url;
   12 use PhpMyAdmin\Util;
   13 use function htmlspecialchars;
   14 use function in_array;
   15 use function sprintf;
   16 
   17 class DesignerController extends AbstractController
   18 {
   19     /** @var Designer */
   20     private $databaseDesigner;
   21 
   22     /** @var DesignerCommon */
   23     private $designerCommon;
   24 
   25     /**
   26      * @param Response $response
   27      * @param string   $db       Database name
   28      */
   29     public function __construct(
   30         $response,
   31         Template $template,
   32         $db,
   33         Designer $databaseDesigner,
   34         DesignerCommon $designerCommon
   35     ) {
   36         parent::__construct($response, $template, $db);
   37         $this->databaseDesigner = $databaseDesigner;
   38         $this->designerCommon = $designerCommon;
   39     }
   40 
   41     public function index(): void
   42     {
   43         global $db, $script_display_field, $tab_column, $tables_all_keys, $tables_pk_or_unique_keys;
   44         global $success, $page, $message, $display_page, $selected_page, $tab_pos, $fullTableNames, $script_tables;
   45         global $script_contr, $params, $tables, $num_tables, $total_num_tables, $sub_part;
   46         global $tooltip_truename, $tooltip_aliasname, $pos, $classes_side_menu, $cfg, $err_url;
   47 
   48         if (isset($_POST['dialog'])) {
   49             if ($_POST['dialog'] === 'edit') {
   50                 $html = $this->databaseDesigner->getHtmlForEditOrDeletePages($_POST['db'], 'editPage');
   51             } elseif ($_POST['dialog'] === 'delete') {
   52                 $html = $this->databaseDesigner->getHtmlForEditOrDeletePages($_POST['db'], 'deletePage');
   53             } elseif ($_POST['dialog'] === 'save_as') {
   54                 $html = $this->databaseDesigner->getHtmlForPageSaveAs($_POST['db']);
   55             } elseif ($_POST['dialog'] === 'export') {
   56                 $html = $this->databaseDesigner->getHtmlForSchemaExport(
   57                     $_POST['db'],
   58                     $_POST['selected_page']
   59                 );
   60             } elseif ($_POST['dialog'] === 'add_table') {
   61                 // Pass the db and table to the getTablesInfo so we only have the table we asked for
   62                 $script_display_field = $this->designerCommon->getTablesInfo($_POST['db'], $_POST['table']);
   63                 $tab_column = $this->designerCommon->getColumnsInfo($script_display_field);
   64                 $tables_all_keys = $this->designerCommon->getAllKeys($script_display_field);
   65                 $tables_pk_or_unique_keys = $this->designerCommon->getPkOrUniqueKeys($script_display_field);
   66 
   67                 $html = $this->databaseDesigner->getDatabaseTables(
   68                     $_POST['db'],
   69                     $script_display_field,
   70                     [],
   71                     -1,
   72                     $tab_column,
   73                     $tables_all_keys,
   74                     $tables_pk_or_unique_keys
   75                 );
   76             }
   77 
   78             if (! empty($html)) {
   79                 $this->response->addHTML($html);
   80             }
   81 
   82             return;
   83         }
   84 
   85         if (isset($_POST['operation'])) {
   86             if ($_POST['operation'] === 'deletePage') {
   87                 $success = $this->designerCommon->deletePage($_POST['selected_page']);
   88                 $this->response->setRequestStatus($success);
   89             } elseif ($_POST['operation'] === 'savePage') {
   90                 if ($_POST['save_page'] === 'same') {
   91                     $page = $_POST['selected_page'];
   92                 } elseif ($this->designerCommon->getPageExists($_POST['selected_value'])) {
   93                     $this->response->addJSON(
   94                         'message',
   95                         /* l10n: The user tries to save a page with an existing name in Designer */
   96                         __(
   97                             sprintf(
   98                                 'There already exists a page named "%s" please rename it to something else.',
   99                                 htmlspecialchars($_POST['selected_value'])
  100                             )
  101                         )
  102                     );
  103                     $this->response->setRequestStatus(false);
  104 
  105                     return;
  106                 } else {
  107                     $page = $this->designerCommon->createNewPage($_POST['selected_value'], $_POST['db']);
  108                     $this->response->addJSON('id', $page);
  109                 }
  110                 $success = $this->designerCommon->saveTablePositions($page);
  111                 $this->response->setRequestStatus($success);
  112             } elseif ($_POST['operation'] === 'setDisplayField') {
  113                 [
  114                     $success,
  115                     $message,
  116                 ] = $this->designerCommon->saveDisplayField(
  117                     $_POST['db'],
  118                     $_POST['table'],
  119                     $_POST['field']
  120                 );
  121                 $this->response->setRequestStatus($success);
  122                 $this->response->addJSON('message', $message);
  123             } elseif ($_POST['operation'] === 'addNewRelation') {
  124                 [$success, $message] = $this->designerCommon->addNewRelation(
  125                     $_POST['db'],
  126                     $_POST['T1'],
  127                     $_POST['F1'],
  128                     $_POST['T2'],
  129                     $_POST['F2'],
  130                     $_POST['on_delete'],
  131                     $_POST['on_update'],
  132                     $_POST['DB1'],
  133                     $_POST['DB2']
  134                 );
  135                 $this->response->setRequestStatus($success);
  136                 $this->response->addJSON('message', $message);
  137             } elseif ($_POST['operation'] === 'removeRelation') {
  138                 [$success, $message] = $this->designerCommon->removeRelation(
  139                     $_POST['T1'],
  140                     $_POST['F1'],
  141                     $_POST['T2'],
  142                     $_POST['F2']
  143                 );
  144                 $this->response->setRequestStatus($success);
  145                 $this->response->addJSON('message', $message);
  146             } elseif ($_POST['operation'] === 'save_setting_value') {
  147                 $success = $this->designerCommon->saveSetting($_POST['index'], $_POST['value']);
  148                 $this->response->setRequestStatus($success);
  149             }
  150 
  151             return;
  152         }
  153 
  154         Util::checkParameters(['db']);
  155 
  156         $err_url = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  157         $err_url .= Url::getCommon(['db' => $db], '&');
  158 
  159         if (! $this->hasDatabase()) {
  160             return;
  161         }
  162 
  163         $script_display_field = $this->designerCommon->getTablesInfo();
  164 
  165         $display_page = -1;
  166         $selected_page = null;
  167 
  168         $visualBuilderMode = isset($_GET['query']);
  169 
  170         if ($visualBuilderMode) {
  171             $display_page = $this->designerCommon->getDefaultPage($_GET['db']);
  172         } elseif (! empty($_GET['page'])) {
  173             $display_page = $_GET['page'];
  174         } else {
  175             $display_page = $this->designerCommon->getLoadingPage($_GET['db']);
  176         }
  177         if ($display_page != -1) {
  178             $selected_page = $this->designerCommon->getPageName($display_page);
  179         }
  180         $tab_pos = $this->designerCommon->getTablePositions($display_page);
  181 
  182         $fullTableNames = [];
  183 
  184         foreach ($script_display_field as $designerTable) {
  185             $fullTableNames[] = $designerTable->getDbTableString();
  186         }
  187 
  188         foreach ($tab_pos as $position) {
  189             if (in_array($position['dbName'] . '.' . $position['tableName'], $fullTableNames)) {
  190                 continue;
  191             }
  192 
  193             $designerTables = $this->designerCommon->getTablesInfo($position['dbName'], $position['tableName']);
  194             foreach ($designerTables as $designerTable) {
  195                 $script_display_field[] = $designerTable;
  196             }
  197         }
  198 
  199         $tab_column = $this->designerCommon->getColumnsInfo($script_display_field);
  200         $script_tables = $this->designerCommon->getScriptTabs($script_display_field);
  201         $tables_pk_or_unique_keys = $this->designerCommon->getPkOrUniqueKeys($script_display_field);
  202         $tables_all_keys = $this->designerCommon->getAllKeys($script_display_field);
  203         $classes_side_menu = $this->databaseDesigner->returnClassNamesFromMenuButtons();
  204 
  205         $script_contr = $this->designerCommon->getScriptContr($script_display_field);
  206 
  207         $params = ['lang' => $GLOBALS['lang']];
  208         if (isset($_GET['db'])) {
  209             $params['db'] = $_GET['db'];
  210         }
  211 
  212         $this->response->getFooter()->setMinimal();
  213         $header = $this->response->getHeader();
  214         $header->setBodyId('designer_body');
  215 
  216         $this->addScriptFiles([
  217             'vendor/jquery/jquery.fullscreen.js',
  218             'designer/database.js',
  219             'designer/objects.js',
  220             'designer/page.js',
  221             'designer/history.js',
  222             'designer/move.js',
  223             'designer/init.js',
  224         ]);
  225 
  226         [
  227             $tables,
  228             $num_tables,
  229             $total_num_tables,
  230             $sub_part,,,
  231             $tooltip_truename,
  232             $tooltip_aliasname,
  233             $pos,
  234         ] = Util::getDbInfo($db, $sub_part ?? '');
  235 
  236         // Embed some data into HTML, later it will be read
  237         // by designer/init.js and converted to JS variables.
  238         $this->response->addHTML(
  239             $this->databaseDesigner->getHtmlForMain(
  240                 $db,
  241                 $_GET['db'],
  242                 $script_display_field,
  243                 $script_tables,
  244                 $script_contr,
  245                 $script_display_field,
  246                 $display_page,
  247                 $visualBuilderMode,
  248                 $selected_page,
  249                 $classes_side_menu,
  250                 $tab_pos,
  251                 $tab_column,
  252                 $tables_all_keys,
  253                 $tables_pk_or_unique_keys
  254             )
  255         );
  256 
  257         $this->response->addHTML('<div id="PMA_disable_floating_menubar"></div>');
  258     }
  259 }