Skip to content

Commit

Permalink
refs #1915 a first version of persist & restore report settings. not …
Browse files Browse the repository at this point in the history
…everything is saved yet (such as metricsToPlot) but most things are
  • Loading branch information
tsteur committed May 2, 2014
1 parent 882be3d commit 97f500f
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 26 deletions.
24 changes: 22 additions & 2 deletions core/Plugin/ViewDataTable.php
Expand Up @@ -11,6 +11,7 @@
use Piwik\API\Request;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Option;
use Piwik\Period;
use Piwik\Piwik;
use Piwik\View;
Expand Down Expand Up @@ -175,7 +176,7 @@ abstract class ViewDataTable implements ViewInterface
* Posts the {@hook ViewDataTable.configure} event which plugins can use to configure the
* way reports are displayed.
*/
public function __construct($controllerAction, $apiMethodToRequestDataTable)
public function __construct($controllerAction, $apiMethodToRequestDataTable, $overrideParams = array())
{
list($controllerName, $controllerAction) = explode('.', $controllerAction);

Expand Down Expand Up @@ -229,6 +230,7 @@ public function __construct($controllerAction, $apiMethodToRequestDataTable)
$this->requestConfig->filter_excludelowpop_value = $function();
}

$this->overrideViewPropertiesWithParams($overrideParams);
$this->overrideViewPropertiesWithQueryParams();
}

Expand Down Expand Up @@ -399,7 +401,7 @@ private function overrideViewPropertiesWithQueryParams()
if (property_exists($this->requestConfig, $name)) {
$this->requestConfig->$name = $this->getPropertyFromQueryParam($name, $this->requestConfig->$name);
} elseif (property_exists($this->config, $name)) {
$this->config->$name = $this->getPropertyFromQueryParam($name, $this->config->$name);
$this->config->$name = $this->getPropertyFromQueryParam($name, $this->config->$name);
}
}

Expand Down Expand Up @@ -456,4 +458,22 @@ public static function canDisplayViewDataTable(ViewDataTable $view)
{
return $view->config->show_all_views_icons;
}

private function overrideViewPropertiesWithParams($overrideParams)
{
if (empty($overrideParams)) {
return;
}

foreach ($overrideParams as $key => $value) {
if (property_exists($this->requestConfig, $key)) {
$this->requestConfig->$key = $value;
} elseif (property_exists($this->config, $key)) {
$this->config->$key = $value;
} elseif ($key != 'enable_filter_excludelowpop') {
$this->config->custom_parameters[$key] = $value;
}
}
}

}
14 changes: 12 additions & 2 deletions core/Plugin/Visualization.php
Expand Up @@ -144,15 +144,15 @@ class Visualization extends ViewDataTable
private $reportLastUpdatedMessage = null;
private $metadata = null;

final public function __construct($controllerAction, $apiMethodToRequestDataTable)
final public function __construct($controllerAction, $apiMethodToRequestDataTable, $params = array())
{
$templateFile = static::TEMPLATE_FILE;

if (empty($templateFile)) {
throw new \Exception('You have not defined a constant named TEMPLATE_FILE in your visualization class.');
}

parent::__construct($controllerAction, $apiMethodToRequestDataTable);
parent::__construct($controllerAction, $apiMethodToRequestDataTable, $params);
}

protected function buildView()
Expand Down Expand Up @@ -305,6 +305,11 @@ private function applyFilters()

$this->beforeGenericFiltersAreAppliedToLoadedDataTable();

if (!in_array($this->requestConfig->filter_sort_column, $this->config->columns_to_display)) {
$hasNbUniqVisitors = in_array('nb_uniq_visitors', $this->config->columns_to_display);
$this->requestConfig->setDefaultSort($this->config->columns_to_display, $hasNbUniqVisitors);
}

if (!$this->requestConfig->areGenericFiltersDisabled()) {
$this->applyGenericFilters();
}
Expand Down Expand Up @@ -563,6 +568,11 @@ private function logMessageIfRequestPropertiesHaveChanged(array $requestProperti
$diff = array_diff_assoc($this->makeSureArrayContainsOnlyStrings($requestProperties),
$this->makeSureArrayContainsOnlyStrings($requestPropertiesBefore));

if (!empty($diff['filter_sort_column'])) {
// this here might be ok as it can be changed after data loaded but before filters applied
unset($diff['filter_sort_column']);
}

if (empty($diff)) {
return;
}
Expand Down
60 changes: 49 additions & 11 deletions core/ViewDataTable/Factory.php
Expand Up @@ -8,7 +8,6 @@
*/
namespace Piwik\ViewDataTable;

use Piwik\API\Proxy;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
Expand Down Expand Up @@ -68,20 +67,20 @@ class Factory
/**
* Creates a {@link Piwik\Plugin\ViewDataTable} instance by ID. If the **viewDataTable** query parameter is set,
* this parameter's value is used as the ID.
*
*
* See {@link Piwik\Plugin\ViewDataTable} to read about the visualizations that are packaged with Piwik.
*
*
* @param string|null $defaultType A ViewDataTable ID representing the default ViewDataTable type to use. If
* the **viewDataTable** query parameter is not found, this value is used as
* the ID of the ViewDataTable to create.
*
*
* If a visualization type is configured for the report being displayed, it
* is used instead of the default type. (See {@hook ViewDataTable.getDefaultType}).
* If nothing is configured for the report and `null` is supplied for this
* argument, **table** is used.
* @param string|false $apiAction The API method for the report that will be displayed, eg,
* @param bool|false|string $apiAction The API method for the report that will be displayed, eg,
* `'UserSettings.getBrowser'`.
* @param string|false $controllerAction The controller name and action dedicated to displaying the report. This
* @param bool|false|string $controllerAction The controller name and action dedicated to displaying the report. This
* action is used when reloading reports or changing the report visualization.
* Defaulted to `$apiAction` if `false` is supplied.
* @param bool $forceDefault If true, then the visualization type that was configured for the report will be
Expand All @@ -101,7 +100,22 @@ public static function build($defaultType = null, $apiAction = false, $controlle
$defaultType = $defaultViewType;
}

$type = Common::getRequestVar('viewDataTable', false, 'string');
$isWidget = Common::getRequestVar('widget', '0', 'string');

if (!empty($isWidget)) {
$params = array();
} else {
$login = Piwik::getCurrentUserLogin();
$params = Manager::getViewDataTableParameters($login, $controllerAction);
}

$savedViewDataTable = false;
if (!empty($params['viewDataTable'])) {
$savedViewDataTable = $params['viewDataTable'];
}

$type = Common::getRequestVar('viewDataTable', $savedViewDataTable, 'string');

// Common::getRequestVar removes backslashes from the defaultValue in case magic quotes are enabled.
// therefore do not pass this as a default value to getRequestVar()
if ('' === $type) {
Expand All @@ -111,19 +125,19 @@ public static function build($defaultType = null, $apiAction = false, $controlle
$visualizations = Manager::getAvailableViewDataTables();

if (array_key_exists($type, $visualizations)) {
return new $visualizations[$type]($controllerAction, $apiAction);
return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params);
}

if (class_exists($type)) {
return new $type($controllerAction, $apiAction);
return self::createViewDataTableInstance($type, $controllerAction, $apiAction, $params);
}

if (array_key_exists($defaultType, $visualizations)) {
return new $visualizations[$defaultType]($controllerAction, $apiAction);
return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params);
}

if (array_key_exists(HtmlTable::ID, $visualizations)) {
return new $visualizations[HtmlTable::ID]($controllerAction, $apiAction);
return self::createViewDataTableInstance($visualizations[HtmlTable::ID], $controllerAction, $apiAction, $params);
}

throw new \Exception('No visualization found to render ViewDataTable');
Expand Down Expand Up @@ -171,4 +185,28 @@ private static function getDefaultTypeViewDataTable()

return self::$defaultViewTypes;
}

/**
* @param string $klass
* @param string $controllerAction
* @param string $apiAction
* @param array $params
*
* @internal param string $viewDataTableId
* @return \Piwik\Plugin\ViewDataTable
*/
private static function createViewDataTableInstance($klass, $controllerAction, $apiAction, $params)
{
if (empty($params)) {
$params = array();
}

if (!is_subclass_of($klass, 'Piwik\Plugin\Visualization')) {
// for now we ignore those params in case it is not a visualization. We do not want to apply
// any of those saved parameters to sparklines etc. Need to find a better solution here
$params = array();
}

return new $klass($controllerAction, $apiAction, $params);
}
}
33 changes: 33 additions & 0 deletions core/ViewDataTable/Manager.php
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\ViewDataTable;

use Piwik\Common;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugin\ViewDataTable;
use Piwik\Plugins\CoreVisualizations\Visualizations\Cloud;
Expand Down Expand Up @@ -270,4 +271,36 @@ private static function getFooterIconFor($viewDataTableId)
'icon' => $klass::FOOTER_ICON,
);
}

public static function getViewDataTableParameters($login, $controllerAction)
{
$paramsKey = self::buildViewDataTableParametersOptionKey($login, $controllerAction);
$params = Option::get($paramsKey);

if (empty($params)) {
return array();
}

$params = json_decode($params);
$params = (array) $params;

return $params;
}

public static function saveViewDataTableParameters($login, $controllerAction, $parametersToOverride)
{
$params = self::getViewDataTableParameters($login, $controllerAction);

foreach ($parametersToOverride as $key => $value) {
$params[$key] = $value;
}

$paramsKey = self::buildViewDataTableParametersOptionKey($login, $controllerAction);
Option::set($paramsKey, json_encode($params));
}

private static function buildViewDataTableParametersOptionKey($login, $controllerAction)
{
return sprintf('ReportConfig_%s_%s', $login, $controllerAction);
}
}
2 changes: 2 additions & 0 deletions core/ViewDataTable/Request.php
Expand Up @@ -73,6 +73,8 @@ public function getRequestArray()
'filter_excludelowpop_value',
'filter_column',
'filter_pattern',
'flat',
'expanded'
);

foreach ($toSetEventually as $varToSet) {
Expand Down
23 changes: 21 additions & 2 deletions core/ViewDataTable/RequestConfig.php
Expand Up @@ -88,7 +88,9 @@ class RequestConfig
'filter_excludelowpop_value',
'filter_pattern',
'filter_column',
'filter_offset'
'filter_offset',
'flat',
'expanded'
);

/**
Expand All @@ -104,7 +106,9 @@ class RequestConfig
'filter_excludelowpop',
'filter_excludelowpop_value',
'disable_generic_filters',
'disable_queued_filters'
'disable_queued_filters',
'flat',
'expanded'
);

/**
Expand All @@ -130,6 +134,21 @@ class RequestConfig
*/
public $filter_limit = false;

/**
* If set to true, the returned data will contain the flattened view of the table data set.
* The children of all first level rows will be aggregated under one row.
*
* Default value: false
*/
public $flat = false;

/**
* If set to true, the returned data will contain the first level results, as well as all sub-tables.
*
* Default value: false
*/
public $expanded = false;

/**
* The number of items from the start of the data set that should be ignored.
*
Expand Down
4 changes: 0 additions & 4 deletions plugins/Actions/Actions.php
Expand Up @@ -603,10 +603,6 @@ private function addBaseDisplayProperties(ViewDataTable $view)
$view->config->show_embedded_subtable = true;
}

// if the flat parameter is not provided, make sure it is set to 0 in the URL,
// so users can see that they can set it to 1 (see #3365)
$view->config->custom_parameters = array('flat' => 0);

if (Request::shouldLoadExpanded()) {

if ($view->isViewDataTableId(HtmlTable::ID)) {
Expand Down
14 changes: 14 additions & 0 deletions plugins/CoreHome/Controller.php
Expand Up @@ -15,7 +15,9 @@
use Piwik\FrontController;
use Piwik\Menu\MenuMain;
use Piwik\Notification\Manager as NotificationManager;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\ViewDataTable\Manager as ViewDataTableManager;
use Piwik\Plugins\CoreHome\DataTableRowAction\MultiRowEvolution;
use Piwik\Plugins\CoreHome\DataTableRowAction\RowEvolution;
use Piwik\Plugins\CorePluginsAdmin\MarketplaceApiClient;
Expand Down Expand Up @@ -239,4 +241,16 @@ public function getPeriodSelector()
$this->setGeneralVariablesView($view);
return $view->render();
}

public function saveViewDataTableParameters()
{
Piwik::checkUserIsNotAnonymous();
$this->checkTokenInUrl();

$reportId = Common::getRequestVar('report_id', null, 'string');
$parameters = (array) Common::getRequestVar('parameters', null, 'json');
$login = Piwik::getCurrentUserLogin();

ViewDataTableManager::saveViewDataTableParameters($login, $reportId, $parameters);
}
}

0 comments on commit 97f500f

Please sign in to comment.