diff --git a/core/ViewDataTable.php b/core/ViewDataTable.php index c997969b1be..bd83dea97bf 100644 --- a/core/ViewDataTable.php +++ b/core/ViewDataTable.php @@ -36,6 +36,7 @@ * } * * + * @see Piwik_ViewDataTable_Properties for core DataTable display properties. * @see factory() for all the available output (cloud tags, html table, pie chart, vertical bar chart) * @package Piwik * @subpackage Piwik_ViewDataTable @@ -112,7 +113,6 @@ abstract class Piwik_ViewDataTable */ protected $view = null; - /** * Default constructor. */ @@ -173,6 +173,37 @@ public function __construct() } } + /** + * Gets a view property by reference. + * + * @param string $name A valid view property name. @see Piwik_ViewDataTable_Properties for all + * valid view properties. + * @return mixed + * @throws Exception if the property name is invalid. + */ + public function &__get($name) + { + Piwik_ViewDataTable_Properties::checkValidPropertyName($name); + + return $this->viewProperties[$name]; + } + + /** + * Sets a view property. + * + * @param string $name A valid view property name. @see Piwik_ViewDataTable_Properties for all + * valid view properties. + * @param mixed $value + * @return mixed Returns $value. + * @throws Exception if the property name is invalid. + */ + public function __set($name, $value) + { + Piwik_ViewDataTable_Properties::checkValidPropertyName($name); + + return $this->viewProperties[$name] = $value; + } + /** * Method to be implemented by the ViewDataTable_*. * This method should create and initialize a $this->view object @see Piwik_View_Interface @@ -196,16 +227,21 @@ abstract protected function getViewDataTableId(); * If force is set to true, a ViewDataTable of the $defaultType will be returned in all cases. * * @param string $defaultType Any of these: table, cloud, graphPie, graphVerticalBar, graphEvolution, sparkline, generateDataChart* - * @param string|bool $action + * @param string|bool $apiAction + * @param string|bool $controllerAction * @return Piwik_ViewDataTable */ - static public function factory($defaultType = null, $action = false) + static public function factory($defaultType = null, $apiAction = false, $controllerAction = false) { - if ($action !== false) { - $defaultProperties = self::getDefaultPropertiesForReport($action); + if ($apiAction !== false) { + $defaultProperties = self::getDefaultPropertiesForReport($apiAction); if (isset($defaultProperties['default_view_type'])) { $defaultType = $defaultProperties['default_view_type']; } + + if ($controllerAction === false) { + $controllerAction = $apiAction; + } } if ($defaultType === null) { @@ -248,15 +284,15 @@ static public function factory($defaultType = null, $action = false) break; } - if ($action !== false) { - list($plugin, $controllerAction) = explode('.', $action); + if ($apiAction !== false) { + list($plugin, $controllerAction) = explode('.', $controllerAction); $subtableAction = $controllerAction; if (isset($defaultProperties['subtable_action'])) { $subtableAction = $defaultProperties['subtable_action']; } - $result->init($plugin, $controllerAction, $action, $subtableAction, $defaultProperties); + $result->init($plugin, $controllerAction, $apiAction, $subtableAction, $defaultProperties); } return $result; @@ -1583,7 +1619,7 @@ private function getBaseReportUrl($module, $action, $queryParams = array()) * @param bool $fetch If true, the result is returned, if false it is echo'd. * @return string|null See $fetch. */ - static public function render($pluginName, $apiAction, $fetch = true) + static public function renderReport($pluginName, $apiAction, $fetch = true) { $apiClassName = 'Piwik_'.$pluginName.'_API'; if (!method_exists($apiClassName::getInstance(), $apiAction)) { @@ -1591,8 +1627,7 @@ static public function render($pluginName, $apiAction, $fetch = true) } $view = self::factory(null, $pluginName.'.'.$apiAction); - $view->main(); - $rendered = $view->getView()->render(); + $rendered = $view->render(); if ($fetch) { return $rendered; @@ -1600,6 +1635,17 @@ static public function render($pluginName, $apiAction, $fetch = true) echo $rendered; } } + + /** + * Convenience function. Calls main() & renders the view that gets built. + * + * @return string The result of rendering. + */ + public function render() + { + $this->main(); + return $this->getView()->render(); + } /** * Returns whether the DataTable result will have to be expanded for the @@ -1624,7 +1670,7 @@ public static function shouldLoadExpanded() * @param array|string $columnsToCheckFor eg, array('nb_visits', 'nb_uniq_visitors') * @return bool */ - private function dataTableColumnsContains($columns, $columnsToCheckFor) + protected function dataTableColumnsContains($columns, $columnsToCheckFor) { if (!is_array($columnsToCheckFor)) { $columnsToCheckFor = array($columnsToCheckFor); @@ -1659,7 +1705,7 @@ protected function buildView($visualization, $template = false) $view->visualization = $visualization; - if (!$this->isDataAvailable) { + if (!$this->dataTable === null) { $view->dataTable = null; } else { $view->dataTable = $this->dataTable; diff --git a/core/ViewDataTable/Cloud.php b/core/ViewDataTable/Cloud.php index 02fed10119d..28127ca281e 100644 --- a/core/ViewDataTable/Cloud.php +++ b/core/ViewDataTable/Cloud.php @@ -49,11 +49,12 @@ public function main() } $this->mainAlreadyExecuted = true; - $this->isDataAvailable = true; try { $this->loadDataTableFromAPI(); } catch (Exception $e) { - $this->isDataAvailable = false; + Piwik::log("Failed to get data from API: " . $e->getMessage()); + + $this->loadingError = array('message' => $e->getMessage()); } $this->checkStandardDataTable(); diff --git a/core/ViewDataTable/GenerateGraphHTML.php b/core/ViewDataTable/GenerateGraphHTML.php index 24ff479624a..2114eb38fa0 100644 --- a/core/ViewDataTable/GenerateGraphHTML.php +++ b/core/ViewDataTable/GenerateGraphHTML.php @@ -221,7 +221,6 @@ public function main() // will be done on the table before the labels are enhanced (see ReplaceColumnNames) $this->disableQueuedFilters(); - $this->isDataAvailable = true; try { $this->loadDataTableFromAPI(); } catch (Piwik_Access_NoAccessException $e) { @@ -229,7 +228,6 @@ public function main() } catch (Exception $e) { Piwik::log("Failed to get data from API: " . $e->getMessage()); - $this->isDataAvailable = false; $this->loadingError = array('message' => $e->getMessage()); } diff --git a/core/ViewDataTable/HtmlTable.php b/core/ViewDataTable/HtmlTable.php index bc4cc034579..2830ab5cb51 100644 --- a/core/ViewDataTable/HtmlTable.php +++ b/core/ViewDataTable/HtmlTable.php @@ -64,7 +64,6 @@ public function main() } $this->mainAlreadyExecuted = true; - $this->isDataAvailable = true; try { $this->loadDataTableFromAPI(); } catch (Piwik_Access_NoAccessException $e) { @@ -72,7 +71,6 @@ public function main() } catch (Exception $e) { Piwik::log("Failed to get data from API: " . $e->getMessage()); - $this->isDataAvailable = false; $this->loadingError = array('message' => $e->getMessage()); } diff --git a/core/ViewDataTable/HtmlTable/AllColumns.php b/core/ViewDataTable/HtmlTable/AllColumns.php index 9b1a1f77d31..3426ef4af3c 100644 --- a/core/ViewDataTable/HtmlTable/AllColumns.php +++ b/core/ViewDataTable/HtmlTable/AllColumns.php @@ -38,9 +38,8 @@ protected function postDataTableLoadedFromAPI() $valid = parent::postDataTableLoadedFromAPI(); if (!$valid) return false; - Piwik_Controller::setPeriodVariablesView($this); $columnUniqueVisitors = false; - if ($this->period == 'day') { + if ($this->dataTableColumnsContains($this->dataTable->getColumns(), 'nb_uniq_visitors')) { $columnUniqueVisitors = 'nb_uniq_visitors'; } diff --git a/core/ViewDataTable/HtmlTable/Goals.php b/core/ViewDataTable/HtmlTable/Goals.php index 3ca37b6c20d..618ed20edd5 100644 --- a/core/ViewDataTable/HtmlTable/Goals.php +++ b/core/ViewDataTable/HtmlTable/Goals.php @@ -15,6 +15,9 @@ */ class Piwik_ViewDataTable_HtmlTable_Goals extends Piwik_ViewDataTable_HtmlTable { + private $processOnlyIdGoal = null; + private $isEcommerce = false; + protected function getViewDataTableId() { return 'tableGoals'; diff --git a/core/ViewDataTable/Properties.php b/core/ViewDataTable/Properties.php new file mode 100644 index 00000000000..1a899f37b1d --- /dev/null +++ b/core/ViewDataTable/Properties.php @@ -0,0 +1,141 @@ +getConstants()); + } + + return $propertiesCache; + } + + /** + * Checks if a property is a valid ViewDataTable property, and if not, throws an exception. + * + * @param string $name The property name. + * @throws Exception + */ + public static function checkValidPropertyName($name) + { + $properties = self::getAllProperties(); + if (!isset($properties[$name])) { + throw new Exception("Invalid ViewDataTable display property '$name'. Is this a visualization property? " + . "If so, set it with \$view->visualization_properties->$name = ..."); + } + } +} \ No newline at end of file diff --git a/core/ViewDataTable/Sparkline.php b/core/ViewDataTable/Sparkline.php index 062a9267b2d..c49759cd1f1 100644 --- a/core/ViewDataTable/Sparkline.php +++ b/core/ViewDataTable/Sparkline.php @@ -43,10 +43,8 @@ public function main() $_GET['period'] = $period; $values = $this->getValuesFromDataTable($this->dataTable); - $this->isDataAvailable = true; if (empty($values)) { $values = array_fill(0, 30, 0); - $this->isDataAvailable = false; } $graph = new Piwik_Visualization_Sparkline(); diff --git a/plugins/Actions/Controller.php b/plugins/Actions/Controller.php index f7597825e56..578ca3bf2a4 100644 --- a/plugins/Actions/Controller.php +++ b/plugins/Actions/Controller.php @@ -84,66 +84,66 @@ public function indexOutlinks($fetch = false) public function getPageUrls($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getEntryPageUrls($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getExitPageUrls($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getSiteSearchKeywords($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getSiteSearchNoResultKeywords($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getSiteSearchCategories($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getPageUrlsFollowingSiteSearch($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getPageTitlesFollowingSiteSearch($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getPageTitles($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getEntryPageTitles($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getExitPageTitles($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getDownloads($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getOutlinks($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } } diff --git a/plugins/CustomVariables/Controller.php b/plugins/CustomVariables/Controller.php index 83b83fb7799..ae7fcaad8cf 100644 --- a/plugins/CustomVariables/Controller.php +++ b/plugins/CustomVariables/Controller.php @@ -23,12 +23,12 @@ public function index($fetch = false) public function getCustomVariables($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getCustomVariablesValuesFromNameId($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } } diff --git a/plugins/DBStats/Controller.php b/plugins/DBStats/Controller.php index 430b2126efc..e5109ee5774 100644 --- a/plugins/DBStats/Controller.php +++ b/plugins/DBStats/Controller.php @@ -53,7 +53,7 @@ public function index() public function getDatabaseUsageSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -67,7 +67,7 @@ public function getDatabaseUsageSummary($fetch = false) public function getTrackerDataSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -81,7 +81,7 @@ public function getTrackerDataSummary($fetch = false) public function getMetricDataSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -95,7 +95,7 @@ public function getMetricDataSummary($fetch = false) public function getMetricDataSummaryByYear($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -109,7 +109,7 @@ public function getMetricDataSummaryByYear($fetch = false) public function getReportDataSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -123,7 +123,7 @@ public function getReportDataSummary($fetch = false) public function getReportDataSummaryByYear($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -139,7 +139,7 @@ public function getReportDataSummaryByYear($fetch = false) public function getIndividualReportsSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -155,7 +155,7 @@ public function getIndividualReportsSummary($fetch = false) public function getIndividualMetricsSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** @@ -172,6 +172,6 @@ public function getIndividualMetricsSummary($fetch = false) public function getAdminDataSummary($fetch = false) { Piwik::checkUserIsSuperUser(); - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } } diff --git a/plugins/DevicesDetection/Controller.php b/plugins/DevicesDetection/Controller.php index 94584e60b9c..fffae636b80 100644 --- a/plugins/DevicesDetection/Controller.php +++ b/plugins/DevicesDetection/Controller.php @@ -25,37 +25,37 @@ public function index($fetch = false) public function getType($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getBrand($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getModel($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getOsFamilies($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getOsVersions($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getBrowserFamilies($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } public function getBrowserVersions($fetch = false) { - return Piwik_ViewDataTable::render($this->pluginName, __FUNCTION__, $fetch); + return Piwik_ViewDataTable::renderReport($this->pluginName, __FUNCTION__, $fetch); } /** diff --git a/plugins/ExampleUI/Controller.php b/plugins/ExampleUI/Controller.php index 1535c07220c..7e4f02d630e 100644 --- a/plugins/ExampleUI/Controller.php +++ b/plugins/ExampleUI/Controller.php @@ -14,61 +14,61 @@ */ class Piwik_ExampleUI_Controller extends Piwik_Controller { - function dataTables() + public function dataTables() { - $view = Piwik_ViewDataTable::factory('table'); - $view->init($this->pluginName, __FUNCTION__, 'ExampleUI.getTemperatures'); - $view->setColumnTranslation('value', "Temperature in °C"); - $view->setColumnTranslation('label', "Hour of day"); - $view->setSortedColumn('label', 'asc'); - $view->setGraphLimit(24); - $view->setLimit(24); - $view->disableExcludeLowPopulation(); - $view->disableShowAllColumns(); - $view->disableRowEvolution(); - $view->setAxisYUnit('°C'); // useful if the user requests the bar graph - return $this->renderView($view); + $view = Piwik_ViewDataTable::factory('table', 'ExampleUI.getTemperatures', $controllerAction = 'ExampleUI.dataTables'); + $view->translations['value'] = "Temperature in °C"; + $view->translations['label'] = "Hour of day"; + $view->filter_sort_column = 'label'; + $view->filter_sort_order = 'asc'; + $view->graph_limit = 24; + $view->filter_limit = 24; + $view->show_exclude_low_population = false; + $view->show_table_all_columns = false; + $view->disable_row_evolution = true; + $view->y_axis_unit = '°C'; // useful if the user requests the bar graph + echo $view->render(); } - function evolutionGraph() + public function evolutionGraph() { echo "