diff options
author | Felipe Figueroa <amenadiel@gmail.com> | 2018-05-22 12:53:15 +0300 |
---|---|---|
committer | Felipe Figueroa <amenadiel@gmail.com> | 2018-05-22 12:53:15 +0300 |
commit | a92413b66063c492e2c89ebc81126da6622025c2 (patch) | |
tree | df25200efc16cbaf388f0d25946fc4791f6b637c /src/controllers/ViewpropertiesController.php | |
parent | f2d04b3542b47760fab4b38d81a1ff2e29511753 (diff) |
Enhancements to export and materialized views
- Export form logic is now in a trait for tables, views and matviews
- Common logic for viewproperties and matviewproperties was offloaded
to a trait (WIP)
- Added link to refresh matviews
Diffstat (limited to 'src/controllers/ViewpropertiesController.php')
-rw-r--r-- | src/controllers/ViewpropertiesController.php | 325 |
1 files changed, 21 insertions, 304 deletions
diff --git a/src/controllers/ViewpropertiesController.php b/src/controllers/ViewpropertiesController.php index 2eaf53ad..a5fc40b7 100644 --- a/src/controllers/ViewpropertiesController.php +++ b/src/controllers/ViewpropertiesController.php @@ -6,8 +6,6 @@ namespace PHPPgAdmin\Controller; -use PHPPgAdmin\Decorators\Decorator; - /** * Base controller class. * @@ -15,7 +13,11 @@ use PHPPgAdmin\Decorators\Decorator; */ class ViewpropertiesController extends BaseController { + use \PHPPgAdmin\Traits\ExportTrait; + use \PHPPgAdmin\Traits\ViewsAndMaterializedViewsTrait; + public $controller_title = 'strviews'; + public $subject = 'view'; /** * Default method to render the controller according to the action parameter. @@ -26,7 +28,7 @@ class ViewpropertiesController extends BaseController return $this->doTree(); } - $this->printHeader($this->headerTitle('', '', $_REQUEST['view'])); + $this->printHeader($this->headerTitle('', '', $_REQUEST[$this->subject])); $this->printBody(); switch ($this->action) { @@ -92,190 +94,13 @@ class ViewpropertiesController extends BaseController } /** - * Show view definition and virtual columns. - * - * @param mixed $msg - */ - public function doDefault($msg = '') - { - $data = $this->misc->getDatabaseAccessor(); - - $attPre = function (&$rowdata) use ($data) { - $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']); - }; - - $this->printTrail('view'); - $this->printTabs('view', 'columns'); - $this->printMsg($msg); - - // Get view - $vdata = $data->getView($_REQUEST['view']); - // Get columns (using same method for getting a view) - $attrs = $data->getTableAttributes($_REQUEST['view']); - - // Show comment if any - if (null !== $vdata->fields['relcomment']) { - echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n"; - } - - $columns = [ - 'column' => [ - 'title' => $this->lang['strcolumn'], - 'field' => Decorator::field('attname'), - 'url' => "colproperties?subject=column&{$this->misc->href}&view=".urlencode($_REQUEST['view']).'&', - 'vars' => ['column' => 'attname'], - ], - 'type' => [ - 'title' => $this->lang['strtype'], - 'field' => Decorator::field('+type'), - ], - 'default' => [ - 'title' => $this->lang['strdefault'], - 'field' => Decorator::field('adsrc'), - ], - 'actions' => [ - 'title' => $this->lang['stractions'], - ], - 'comment' => [ - 'title' => $this->lang['strcomment'], - 'field' => Decorator::field('comment'), - ], - ]; - - $actions = [ - 'alter' => [ - 'content' => $this->lang['stralter'], - 'attr' => [ - 'href' => [ - 'url' => 'viewproperties', - 'urlvars' => [ - 'action' => 'properties', - 'view' => $_REQUEST['view'], - 'column' => Decorator::field('attname'), - ], - ], - ], - ], - ]; - - echo $this->printTable($attrs, $columns, $actions, 'viewproperties-viewproperties', null, $attPre); - - echo "<br />\n"; - - $navlinks = [ - 'browse' => [ - 'attr' => [ - 'href' => [ - 'url' => 'display', - 'urlvars' => [ - 'server' => $_REQUEST['server'], - 'database' => $_REQUEST['database'], - 'schema' => $_REQUEST['schema'], - 'view' => $_REQUEST['view'], - 'subject' => 'view', - 'return' => 'view', - ], - ], - ], - 'content' => $this->lang['strbrowse'], - ], - 'select' => [ - 'attr' => [ - 'href' => [ - 'url' => 'views', - 'urlvars' => [ - 'action' => 'confselectrows', - 'server' => $_REQUEST['server'], - 'database' => $_REQUEST['database'], - 'schema' => $_REQUEST['schema'], - 'view' => $_REQUEST['view'], - ], - ], - ], - 'content' => $this->lang['strselect'], - ], - 'drop' => [ - 'attr' => [ - 'href' => [ - 'url' => 'views', - 'urlvars' => [ - 'action' => 'confirm_drop', - 'server' => $_REQUEST['server'], - 'database' => $_REQUEST['database'], - 'schema' => $_REQUEST['schema'], - 'view' => $_REQUEST['view'], - ], - ], - ], - 'content' => $this->lang['strdrop'], - ], - 'alter' => [ - 'attr' => [ - 'href' => [ - 'url' => 'viewproperties', - 'urlvars' => [ - 'action' => 'confirm_alter', - 'server' => $_REQUEST['server'], - 'database' => $_REQUEST['database'], - 'schema' => $_REQUEST['schema'], - 'view' => $_REQUEST['view'], - ], - ], - ], - 'content' => $this->lang['stralter'], - ], - ]; - - $this->printNavLinks($navlinks, 'viewproperties-viewproperties', get_defined_vars()); - } - - public function doTree() - { - $data = $this->misc->getDatabaseAccessor(); - - $reqvars = $this->misc->getRequestVars('column'); - $columns = $data->getTableAttributes($_REQUEST['view']); - - $attrs = [ - 'text' => Decorator::field('attname'), - 'action' => Decorator::actionurl( - 'colproperties', - $reqvars, - [ - 'view' => $_REQUEST['view'], - 'column' => Decorator::field('attname'), - ] - ), - 'icon' => 'Column', - 'iconAction' => Decorator::url( - 'display', - $reqvars, - [ - 'view' => $_REQUEST['view'], - 'column' => Decorator::field('attname'), - 'query' => Decorator::replace( - 'SELECT "%column%", count(*) AS "count" FROM %view% GROUP BY "%column%" ORDER BY "%column%"', - [ - '%column%' => Decorator::field('attname'), - '%view%' => $_REQUEST['view'], - ] - ), - ] - ), - 'toolTip' => Decorator::field('comment'), - ]; - - return $this->printTree($columns, $attrs, 'viewcolumns'); - } - - /** * Function to save after editing a view. */ public function doSaveEdit() { $data = $this->misc->getDatabaseAccessor(); - $status = $data->setView($_POST['view'], $_POST['formDefinition'], $_POST['formComment']); + $status = $data->setView($_POST[$this->subject], $_POST['formDefinition'], $_POST['formComment']); if (0 == $status) { $this->doDefinition($this->lang['strviewupdated']); } else { @@ -292,11 +117,11 @@ class ViewpropertiesController extends BaseController { $data = $this->misc->getDatabaseAccessor(); - $this->printTrail('view'); + $this->printTrail($this->subject); $this->printTitle($this->lang['stredit'], 'pg.view.alter'); $this->printMsg($msg); - $viewdata = $data->getView($_REQUEST['view']); + $viewdata = $data->getView($_REQUEST[$this->subject]); if ($viewdata->recordCount() > 0) { if (!isset($_POST['formDefinition'])) { @@ -304,7 +129,7 @@ class ViewpropertiesController extends BaseController $_POST['formComment'] = $viewdata->fields['relcomment']; } - echo '<form action="'.\SUBFOLDER."/src/views/viewproperties\" method=\"post\">\n"; + echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n"; echo "<table style=\"width: 100%\">\n"; echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n"; echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">", @@ -314,7 +139,7 @@ class ViewpropertiesController extends BaseController htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; echo "</table>\n"; echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n"; - echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; + echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n"; echo $this->misc->form; echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n"; echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; @@ -325,114 +150,6 @@ class ViewpropertiesController extends BaseController } /** - * Allow the dumping of the data "in" a view - * NOTE:: PostgreSQL doesn't currently support dumping the data in a view - * so I have disabled the data related parts for now. In the future - * we should allow it conditionally if it becomes supported. This is - * a SMOP since it is based on pg_dump version not backend version. - * - * @param mixed $msg - */ - public function doExport($msg = '') - { - $this->printTrail('view'); - $this->printTabs('view', 'export'); - $this->printMsg($msg); - - echo '<form action="'.\SUBFOLDER."/src/views/dataexport\" method=\"post\">\n"; - echo "<table>\n"; - echo "<tr><th class=\"data\">{$this->lang['strformat']}</th><th class=\"data\" colspan=\"2\">{$this->lang['stroptions']}</th></tr>\n"; - // Data only - echo "<!--\n"; - echo '<tr><th class="data left">'; - echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" /><label for=\"what1\">{$this->lang['strdataonly']}</label></th>\n"; - echo "<td>{$this->lang['strformat']}</td>\n"; - echo "<td><select name=\"d_format\" >\n"; - echo "<option value=\"copy\">COPY</option>\n"; - echo "<option value=\"sql\">SQL</option>\n"; - echo "<option value=\"csv\">CSV</option>\n"; - echo "<option value=\"tab\">{$this->lang['strtabbed']}</option>\n"; - echo "<option value=\"html\">XHTML</option>\n"; - echo "<option value=\"xml\">XML</option>\n"; - echo "</select>\n</td>\n</tr>\n"; - echo "-->\n"; - - // Structure only - echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" checked=\"checked\" /><label for=\"what2\">{$this->lang['strstructureonly']}</label></th>\n"; - echo "<td><label for=\"s_clean\">{$this->lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /></td>\n</tr>\n"; - // Structure and data - echo "<!--\n"; - echo '<tr><th class="data left" rowspan="2">'; - echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$this->lang['strstructureanddata']}</label></th>\n"; - echo "<td>{$this->lang['strformat']}</td>\n"; - echo "<td><select name=\"sd_format\">\n"; - echo "<option value=\"copy\">COPY</option>\n"; - echo "<option value=\"sql\">SQL</option>\n"; - echo "</select>\n</td>\n</tr>\n"; - echo "<td><label for=\"sd_clean\">{$this->lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /></td>\n</tr>\n"; - echo "-->\n"; - echo "</table>\n"; - - echo "<h3>{$this->lang['stroptions']}</h3>\n"; - echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$this->lang['strshow']}</label>\n"; - echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$this->lang['strdownload']}</label></p>\n"; - - echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n"; - echo $this->misc->form; - echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n"; - echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; - echo "<input type=\"submit\" value=\"{$this->lang['strexport']}\" /></p>\n"; - echo "</form>\n"; - } - - /** - * Show definition for a view. - * - * @param mixed $msg - */ - public function doDefinition($msg = '') - { - $data = $this->misc->getDatabaseAccessor(); - - // Get view - $vdata = $data->getView($_REQUEST['view']); - - $this->printTrail('view'); - $this->printTabs('view', 'definition'); - $this->printMsg($msg); - - if ($vdata->recordCount() > 0) { - // Show comment if any - if (null !== $vdata->fields['relcomment']) { - echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n"; - } - - echo "<table style=\"width: 100%\">\n"; - echo "<tr><th class=\"data\">{$this->lang['strdefinition']}</th></tr>\n"; - echo '<tr><td class="data1">', $this->misc->printVal($vdata->fields['vwdefinition']), "</td></tr>\n"; - echo "</table>\n"; - } else { - echo "<p>{$this->lang['strnodata']}</p>\n"; - } - - $this->printNavLinks(['alter' => [ - 'attr' => [ - 'href' => [ - 'url' => 'viewproperties', - 'urlvars' => [ - 'action' => 'edit', - 'server' => $_REQUEST['server'], - 'database' => $_REQUEST['database'], - 'schema' => $_REQUEST['schema'], - 'view' => $_REQUEST['view'], - ], - ], - ], - 'content' => $this->lang['stralter'], - ]], 'viewproperties-definition', get_defined_vars()); - } - - /** * Displays a screen where they can alter a column in a view. * * @param mixed $msg @@ -452,14 +169,14 @@ class ViewpropertiesController extends BaseController $this->printTitle($this->lang['stralter'], 'pg.column.alter'); $this->printMsg($msg); - echo '<form action="'.\SUBFOLDER."/src/views/viewproperties\" method=\"post\">\n"; + echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n"; // Output view header echo "<table>\n"; echo "<tr><th class=\"data required\">{$this->lang['strname']}</th><th class=\"data required\">{$this->lang['strtype']}</th>"; echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>"; - $column = $data->getTableAttributes($_REQUEST['view'], $_REQUEST['column']); + $column = $data->getTableAttributes($_REQUEST[$this->subject], $_REQUEST['column']); if (!isset($_REQUEST['default'])) { $_REQUEST['field'] = $column->fields['attname']; @@ -480,7 +197,7 @@ class ViewpropertiesController extends BaseController echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n"; echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; echo $this->misc->form; - echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; + echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n"; echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n"; echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n"; echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n"; @@ -500,7 +217,7 @@ class ViewpropertiesController extends BaseController // Alter the view column list($status, $sql) = $data->alterColumn( - $_REQUEST['view'], + $_REQUEST[$this->subject], $_REQUEST['column'], $_REQUEST['field'], false, @@ -533,12 +250,12 @@ class ViewpropertiesController extends BaseController $data = $this->misc->getDatabaseAccessor(); if ($confirm) { - $this->printTrail('view'); + $this->printTrail($this->subject); $this->printTitle($this->lang['stralter'], 'pg.view.alter'); $this->printMsg($msg); // Fetch view info - $view = $data->getView($_REQUEST['view']); + $view = $data->getView($_REQUEST[$this->subject]); if ($view->recordCount() > 0) { if (!isset($_POST['name'])) { @@ -557,7 +274,7 @@ class ViewpropertiesController extends BaseController $_POST['comment'] = $view->fields['relcomment']; } - echo '<form action="'.\SUBFOLDER."/src/views/viewproperties\" method=\"post\">\n"; + echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n"; echo "<table>\n"; echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n"; echo '<td class="data1">'; @@ -598,7 +315,7 @@ class ViewpropertiesController extends BaseController htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n"; echo "</table>\n"; echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n"; - echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; + echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n"; echo $this->misc->form; echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n"; echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; @@ -616,13 +333,13 @@ class ViewpropertiesController extends BaseController $_POST['newschema'] = null; } - $status = $data->alterView($_POST['view'], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']); + $status = $data->alterView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']); if (0 == $status) { // If view has been renamed, need to change to the new name and // reload the browser frame. - if ($_POST['view'] != $_POST['name']) { + if ($_POST[$this->subject] != $_POST['name']) { // Jump them to the new view name - $_REQUEST['view'] = $_POST['name']; + $_REQUEST[$this->subject] = $_POST['name']; // Force a browser reload $this->misc->setReloadBrowser(true); } |