/**
* 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 bool|false|string $apiAction The API method for the report that will be displayed, eg,
* `'UserSettings.getBrowser'`.
* @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
* ignored and `$defaultType` will be used as the default.
* @throws \Exception
* @return \Piwik\Plugin\ViewDataTable
*/
public static function build($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false)
{
if (false === $controllerAction) {
$controllerAction = $apiAction;
}
$defaultViewType = self::getDefaultViewTypeForReport($apiAction);
if (!$forceDefault && !empty($defaultViewType)) {
$defaultType = $defaultViewType;
}
$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) {
$type = $defaultType ?: HtmlTable::ID;
}
$visualizations = Manager::getAvailableViewDataTables();
if (array_key_exists($type, $visualizations)) {
return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params);
}
if (class_exists($type)) {
return self::createViewDataTableInstance($type, $controllerAction, $apiAction, $params);
}
if (array_key_exists($defaultType, $visualizations)) {
return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params);
}
if (array_key_exists(HtmlTable::ID, $visualizations)) {
return self::createViewDataTableInstance($visualizations[HtmlTable::ID], $controllerAction, $apiAction, $params);
}
throw new \Exception('No visualization found to render ViewDataTable');
}