diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2013-10-21 01:06:47 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2013-10-21 01:06:47 +0400 |
commit | 2560e83517536859ca62440c14b04ec9db2af00e (patch) | |
tree | 6edea6ec0752aa562d85cc4a45655cbe0e867d54 /core/ViewDataTable.php | |
parent | 37fc1f1cd10dfef86ac429af0f6254436d0d6ccb (diff) |
It was confusing to have a coreViewDataTable and pluginViewDataTable class. In addition the coreViewDataTable did two things: Instance creation and managing all viewDataTables --> two new classes
Diffstat (limited to 'core/ViewDataTable.php')
-rw-r--r-- | core/ViewDataTable.php | 233 |
1 files changed, 0 insertions, 233 deletions
diff --git a/core/ViewDataTable.php b/core/ViewDataTable.php deleted file mode 100644 index 3cfb95826a..0000000000 --- a/core/ViewDataTable.php +++ /dev/null @@ -1,233 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - * @category Piwik - * @package Piwik - */ -namespace Piwik; - -use Piwik\API\Proxy; -use Piwik\API\Request; -use Piwik\Plugins\API\API; -use Piwik\Plugin\Visualization; - -/** - * This class is used to load (from the API) and customize the output of a given DataTable. - * The main() method will create an object implementing ViewInterface - * You can customize the dataTable using the disable* methods. - * - * You can also customize the dataTable rendering using row metadata: - * - 'html_label_prefix': If this metadata is present on a row, it's contents will be prepended - * the label in the HTML output. - * - 'html_label_suffix': If this metadata is present on a row, it's contents will be appended - * after the label in the HTML output. - * - * Example: - * In the Controller of the plugin VisitorInterest - * <pre> - * function getNumberOfVisitsPerVisitDuration( $fetch = false) - * { - * $view = ViewDataTable::factory( 'cloud' ); - * $view->init( $this->pluginName, __FUNCTION__, 'VisitorInterest.getNumberOfVisitsPerVisitDuration' ); - * $view->setColumnsToDisplay( array('label','nb_visits') ); - * $view->disableSort(); - * $view->disableExcludeLowPopulation(); - * $view->disableOffsetInformation(); - * - * return $this->renderView($view, $fetch); - * } - * </pre> - * - * @see factory() for all the available output (cloud tags, html table, pie chart, vertical bar chart) - * @package Piwik - * @subpackage ViewDataTable - * - * @api - */ -class ViewDataTable -{ - - /** - * Cache for getDefaultTypeViewDataTable result. - * - * @var array - */ - private static $defaultViewTypes = null; - - /** - * Returns a Piwik_ViewDataTable_* object. - * By default it will return a ViewDataTable_Html - * If there is a viewDataTable parameter in the URL, a ViewDataTable of this 'viewDataTable' type will be returned. - * If defaultType is specified and if there is no 'viewDataTable' in the URL, a ViewDataTable of this $defaultType will be returned. - * 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 $apiAction - * @param string|bool $controllerAction - * @param bool $forceDefault - * - * @throws \Exception - * @return \Piwik\Plugin\ViewDataTable|\Piwik\Plugin\Visualization|\Piwik\Plugins\CoreVisualizations\Visualizations\Sparkline; - */ - public static function factory($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false) - { - if (false === $controllerAction) { - $controllerAction = $apiAction; - } - - $defaultViewType = self::getDefaultViewTypeForReport($apiAction); - - if (!$forceDefault && !empty($defaultViewType)) { - $defaultType = $defaultViewType; - } - - $type = Common::getRequestVar('viewDataTable', $defaultType ? : 'table', 'string'); - - $visualizations = static::getAvailableVisualizations(); - - if (array_key_exists($type, $visualizations)) { - return new $visualizations[$type]($controllerAction, $apiAction); - } - - if (class_exists($type)) { - return new $type($controllerAction, $apiAction); - } - - throw new \Exception(sprintf('Visuzalization type %s not found', $type)); - } - - /** - * Returns all registered visualization classes. Uses the 'Visualization.getAvailable' - * event to retrieve visualizations. - * - * @return array Array mapping visualization IDs with their associated visualization classes. - * @throws \Exception If a visualization class does not exist or if a duplicate visualization ID - * is found. - * @return array - */ - public static function getAvailableVisualizations() - { - /** @var string[] $visualizations */ - $visualizations = array(); - - /** - * This event is used to gather all available DataTable visualizations. Callbacks should add visualization - * class names to the incoming array. - */ - Piwik::postEvent('Visualization.addVisualizations', array(&$visualizations)); - - $result = array(); - - foreach ($visualizations as $viz) { - if (!class_exists($viz)) { - throw new \Exception("Invalid visualization class '$viz' found in Visualization.getAvailableVisualizations."); - } - - if (!is_subclass_of($viz, '\\Piwik\\Plugin\\ViewDataTable')) { - throw new \Exception("Visualization class '$viz' does not extend Plugin/ViewDataTable"); - } - - $vizId = $viz::getViewDataTableId(); - - if (isset($result[$vizId])) { - throw new \Exception("Visualization ID '$vizId' is already in use!"); - } - - $result[$vizId] = $viz; - } - - return $result; - } - - /** - * Returns all available visualizations that are not part of the CoreVisualizations plugin. - * - * @return array Array mapping visualization IDs with their associated visualization classes. - */ - public static function getNonCoreViewDataTables() - { - $result = array(); - - foreach (static::getAvailableVisualizations() as $vizId => $vizClass) { - if (false === strpos($vizClass, 'Piwik\\Plugins\\CoreVisualizations') - && false === strpos($vizClass, 'Piwik\\Plugins\\Goals\\Visualizations\\Goals')) { - $result[$vizId] = $vizClass; - } - } - - return $result; - } - - /** - * Convenience method that creates and renders a ViewDataTable for a API method. - * - * @param string $pluginName The name of the plugin (eg, UserSettings). - * @param string $apiAction The name of the API action (eg, getResolution). - * @param bool $fetch If true, the result is returned, if false it is echo'd. - * @throws \Exception - * @return string|null See $fetch. - */ - public static function renderReport($pluginName, $apiAction, $fetch = true) - { - /** @var Proxy $apiProxy */ - $apiProxy = Proxy::getInstance(); - - if (!$apiProxy->isExistingApiAction($pluginName, $apiAction)) { - throw new \Exception("Invalid action name '$apiAction' for '$pluginName' plugin."); - } - - $apiAction = $apiProxy->buildApiActionName($pluginName, $apiAction); - - $view = static::factory(null, $apiAction); - $rendered = $view->render(); - - if ($fetch) { - return $rendered; - } else { - echo $rendered; - } - } - - /** - * Returns the default viewDataTable ID to use when determining which visualization to use. - */ - private static function getDefaultViewTypeForReport($apiAction) - { - $defaultViewTypes = self::getDefaultTypeViewDataTable(); - return isset($defaultViewTypes[$apiAction]) ? $defaultViewTypes[$apiAction] : false; - } - - /** - * Returns a list of default viewDataTables ID to use when determining which visualization to use for multiple - * reports. - */ - private static function getDefaultTypeViewDataTable() - { - if (null === self::$defaultViewTypes) { - self::$defaultViewTypes = array(); - /** - * This event is triggered to gather the default view types for each available report. By default a table - * is used. If you define your own report, you may want to subscribe to this event to define another - * Visualization that should be used by default to display your report. For instance a Pie, a Bar or a - * Cloud. - * - * Example: - * ``` - * public function getDefaultTypeViewDataTable(&$defaultViewTypes) - * { - * $defaultViewTypes['Referrers.getSocials'] = HtmlTable::ID; - * $defaultViewTypes['Referrers.getUrlsForSocial'] = Pie::ID; - * ) - * } - * ``` - */ - Piwik::postEvent('Visualization.getDefaultTypeViewDataTable', array(&self::$defaultViewTypes)); - } - - return self::$defaultViewTypes; - } -}
\ No newline at end of file |