From e5b28b3c623782aa0416dcecc0e7ad40dcc8a1bc Mon Sep 17 00:00:00 2001 From: mattab Date: Thu, 10 Apr 2014 16:15:35 +1200 Subject: [PATCH] Refs #472 Events controller with left menu selector --- core/View.php | 5 +- plugins/CustomAlerts | 2 +- plugins/Events/API.php | 8 +- plugins/Events/Controller.php | 114 ++++++++----------------- plugins/Events/Events.php | 124 ++++++++++++++++++++++------ plugins/Events/lang/en.json | 6 +- plugins/Events/templates/index.twig | 2 + plugins/VisitorGenerator | 2 +- tests/PHPUnit/UI | 2 +- 9 files changed, 148 insertions(+), 117 deletions(-) create mode 100644 plugins/Events/templates/index.twig diff --git a/core/View.php b/core/View.php index 4afea486616..311b58e3eb7 100644 --- a/core/View.php +++ b/core/View.php @@ -135,7 +135,7 @@ public function __construct($templateFile) $this->piwik_version = Version::VERSION; $this->userLogin = Piwik::getCurrentUserLogin(); - $this->isSuperUser = Access::getInstance()->hasSuperUserAccess(); // TODO: redundancy w/ userIsSuperUser + $this->isSuperUser = Access::getInstance()->hasSuperUserAccess(); try { $this->piwikUrl = SettingsPiwik::getPiwikUrl(); @@ -320,7 +320,6 @@ public function addForm(QuickForm2 $form) /** * Assign value to a variable for use in a template - * ToDo: This is ugly. * @param string|array $var * @param mixed $value * @ignore @@ -354,7 +353,6 @@ static public function clearCompiledTemplates() * * @param string $title The report title. * @param string $reportHtml The report body HTML. - * @param bool $fetch If true, return report contents as a string; otherwise echo to screen. * @return string|void The report contents if `$fetch` is true. */ static public function singleReport($title, $reportHtml) @@ -362,7 +360,6 @@ static public function singleReport($title, $reportHtml) $view = new View('@CoreHome/_singleReport'); $view->title = $title; $view->report = $reportHtml; - return $view->render(); } } diff --git a/plugins/CustomAlerts b/plugins/CustomAlerts index 100b4c2e163..302ad3ad7e0 160000 --- a/plugins/CustomAlerts +++ b/plugins/CustomAlerts @@ -1 +1 @@ -Subproject commit 100b4c2e1633704c51841d7a201139fbee9ddd04 +Subproject commit 302ad3ad7e0a6539e5d7361d035b2594d255bd75 diff --git a/plugins/Events/API.php b/plugins/Events/API.php index 391fe426f3a..1ddbfdfee5c 100644 --- a/plugins/Events/API.php +++ b/plugins/Events/API.php @@ -43,14 +43,14 @@ class API extends \Piwik\Plugin\API /** * @ignore */ - public function getSubtableAction($api) + public function getSubtableAction($apiMethod) { - return $this->mappingApiToApiLoadsubtables[$api]; + return $this->mappingApiToApiLoadsubtables[$apiMethod]; } - protected function getRecordNameForAction($method) + protected function getRecordNameForAction($apiMethod) { - return $this->mappingApiToRecord[$method]; + return $this->mappingApiToRecord[$apiMethod]; } protected function getDataTable($name, $idSite, $period, $date, $segment, $expanded = false, $idSubtable = null) diff --git a/plugins/Events/Controller.php b/plugins/Events/Controller.php index af56986a6af..d03513e5e08 100644 --- a/plugins/Events/Controller.php +++ b/plugins/Events/Controller.php @@ -6,146 +6,104 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * */ -namespace Piwik\Plugins\Actions; +namespace Piwik\Plugins\Events; use Piwik\Piwik; +use Piwik\Plugins\Events\Events; use Piwik\View; use Piwik\ViewDataTable\Factory; /** - * Actions controller + * Events controller * */ class Controller extends \Piwik\Plugin\Controller { - // - // Actions that render whole pages - // - - public function indexPageUrls() - { - return View::singleReport( - Piwik::translate('General_Pages'), - $this->getPageUrls(true)); - } - - public function indexEntryPageUrls() - { - return View::singleReport( - Piwik::translate('Actions_SubmenuPagesEntry'), - $this->getEntryPageUrls(true)); - } - - public function indexExitPageUrls() + public function index() { - return View::singleReport( - Piwik::translate('Actions_SubmenuPagesExit'), - $this->getExitPageUrls(true)); - } - - public function indexSiteSearch() - { - $view = new View('@Actions/indexSiteSearch'); - - $view->keywords = $this->getSiteSearchKeywords(true); - $view->noResultKeywords = $this->getSiteSearchNoResultKeywords(true); - $view->pagesUrlsFollowingSiteSearch = $this->getPageUrlsFollowingSiteSearch(true); - - $categoryTrackingEnabled = \Piwik\Plugin\Manager::getInstance()->isPluginActivated('CustomVariables'); - if ($categoryTrackingEnabled) { - $view->categories = $this->getSiteSearchCategories(true); - } - + $view = new View('@Events/index'); + $view->leftMenuReports = $this->getLeftMenuReports(); return $view->render(); } - public function indexPageTitles() + private function getLeftMenuReports() { - return View::singleReport( - Piwik::translate('Actions_SubmenuPageTitles'), - $this->getPageTitles(true)); - } - - public function indexDownloads() - { - return View::singleReport( - Piwik::translate('General_Downloads'), - $this->getDownloads(true)); - } - - public function indexOutlinks() - { - return View::singleReport( - Piwik::translate('General_Outlinks'), - $this->getOutlinks(true)); + $reports = new View\ReportsByDimension('Events'); + foreach(Events::getLabelTranslations() as $apiAction => $translations) { + // 'getCategory' is the API method, but we are loading 'indexCategory' which displays

+ $controllerAction = str_replace("get", "index", $apiAction, $count = 1); + $reports->addReport('Events_TopEvents', $translations[0], 'Events.' . $controllerAction); + } + return $reports->render(); } - // - // Actions that render individual reports - // - - public function getPageUrls() + public function indexCategory() { - return $this->renderReport(__FUNCTION__); + return $this->indexEvent(__FUNCTION__); } - public function getEntryPageUrls() + public function indexAction() { - return $this->renderReport(__FUNCTION__); + return $this->indexEvent(__FUNCTION__); } - public function getExitPageUrls() + public function indexName() { - return $this->renderReport(__FUNCTION__); + return $this->indexEvent(__FUNCTION__); } - public function getSiteSearchKeywords() + public function getCategory() { return $this->renderReport(__FUNCTION__); } - public function getSiteSearchNoResultKeywords() + public function getAction() { return $this->renderReport(__FUNCTION__); } - public function getSiteSearchCategories() + public function getName() { return $this->renderReport(__FUNCTION__); } - public function getPageUrlsFollowingSiteSearch() + public function getActionFromCategoryId() { return $this->renderReport(__FUNCTION__); } - public function getPageTitlesFollowingSiteSearch() + public function getNameFromCategoryId() { return $this->renderReport(__FUNCTION__); } - public function getPageTitles() + public function getCategoryFromActionId() { return $this->renderReport(__FUNCTION__); } - public function getEntryPageTitles() + public function getNameFromActionId() { return $this->renderReport(__FUNCTION__); } - public function getExitPageTitles() + public function getActionFromNameId() { return $this->renderReport(__FUNCTION__); } - public function getDownloads() + public function getCategoryFromNameId() { return $this->renderReport(__FUNCTION__); } - public function getOutlinks() + protected function indexEvent($controllerMethod) { - return $this->renderReport(__FUNCTION__); + $apiMethod = str_replace('index', 'get', $controllerMethod, $count = 1); + $events = new Events; + return View::singleReport( + $events->getReportTitleTranslation($apiMethod), + $this->$apiMethod() + ); } } diff --git a/plugins/Events/Events.php b/plugins/Events/Events.php index 38fdd0f26a4..f9ac53f6c04 100644 --- a/plugins/Events/Events.php +++ b/plugins/Events/Events.php @@ -8,8 +8,10 @@ */ namespace Piwik\Plugins\Events; +use Piwik\Menu\MenuMain; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; +use Piwik\WidgetsList; /** */ @@ -24,9 +26,24 @@ public function getListHooksRegistered() 'API.getSegmentDimensionMetadata' => 'getSegmentsMetadata', 'Metrics.getDefaultMetricTranslations' => 'addMetricTranslations', 'API.getReportMetadata' => 'getReportMetadata', + 'ViewDataTable.configure' => 'configureViewDataTable', + 'Menu.Reporting.addItems' => 'addMenus', + 'WidgetsList.addWidgets' => 'addWidgets', ); } + public function addWidgets() + { + foreach(self::getLabelTranslations() as $apiMethod => $labels) { + WidgetsList::add('Events_Events', $labels[0], 'Events', $apiMethod); + } + } + + public function addMenus() + { + MenuMain::getInstance()->add('General_Actions', 'Events_Events', array('module' => 'Events', 'action' => 'index'), true, 20); + } + public function addMetricTranslations(&$translations) { $translations = array_merge($translations, $this->getMetricTranslations()); @@ -34,24 +51,30 @@ public function addMetricTranslations(&$translations) public function getMetricDocumentation() { - return array( + $documentation = array( 'nb_events' => 'Events_TotalEventsDocumentation', 'sum_event_value' => 'Events_TotalValueDocumentation', 'min_event_value' => 'Events_MinValueDocumentation', 'max_event_value' => 'Events_MaxValueDocumentation', + 'avg_event_value' => 'Events_AvgValueDocumentation', 'nb_events_with_value' => 'Events_EventsWithValueDocumentation', ); + $documentation = array_map(array('\\Piwik\\Piwik', 'translate'), $documentation); + return $documentation; } protected function getMetricTranslations() { - return array( + $metrics = array( 'nb_events' => 'Events_TotalEvents', 'sum_event_value' => 'Events_TotalValue', 'min_event_value' => 'Events_MinValue', 'max_event_value' => 'Events_MaxValue', + 'avg_event_value' => 'Events_AvgValue', 'nb_events_with_value' => 'Events_EventsWithValue', ); + $metrics = array_map(array('\\Piwik\\Piwik', 'translate'), $metrics); + return $metrics; } public function getSegmentsMetadata(&$segments) @@ -115,12 +138,6 @@ public function getReportMetadata(&$reports) { $metrics = $this->getMetricTranslations(); $documentation = $this->getMetricDocumentation(); - - // Translate - $callback = array('\\Piwik\\Piwik', 'translate'); - $metrics = array_map($callback, $metrics); - $documentation = array_map($callback, $documentation); - $labelTranslations = $this->getLabelTranslations(); $order = 0; @@ -142,34 +159,89 @@ public function getReportMetadata(&$reports) } /** - * Given Events.getCategory, returns the translations to use - * - * @param $apiReport - * @throws \Exception * @return array */ - protected function getLabelTranslation($apiReport) + static public function getLabelTranslations() + { + return array( + 'getCategory' => array('Events_EventCategories', 'Events_EventCategory'), + 'getAction' => array('Events_EventActions', 'Events_EventAction'), + 'getName' => array('Events_EventNames', 'Events_EventName'), + ); + } + + + /** + * Given getCategory, returns "Event Categories" + * + * @param $apiMethod + * @return string + */ + public function getReportTitleTranslation($apiMethod) + { + return $this->getTranslation($apiMethod, $index = 0); + } + + /** + * Given getCategory, returns "Event Category" + * + * @param $apiMethod + * @return string + */ + protected function getColumnTranslation($apiMethod) + { + return $this->getTranslation($apiMethod, $index = 1); + } + + + protected function getTranslation($apiMethod, $index) { $labels = $this->getLabelTranslations(); - foreach($labels as $action => $translations) { - $action = 'Events.' . $action; - if($apiReport == $action) { - return $translations; + foreach ($labels as $action => $translations) { + // Events.getActionFromCategoryId returns translation for Events.getAction + if (strpos($apiMethod, $action) === 0) { + $columnLabel = $translations[$index]; + return Piwik::translate($columnLabel); } } - throw new \Exception("Translation not found for report $apiReport"); + throw new \Exception("Translation not found for report $apiMethod"); } - /** - * @return array - */ - protected function getLabelTranslations() + public function configureViewDataTable(ViewDataTable $view) { - return array( - 'getCategory' => array('Events_EventCategories', 'Events_EventCategory'), - 'getAction' => array('Events_EventActions', 'Events_EventAction'), - 'getName' => array('Events_EventNames', 'Events_EventName'), + // eg. 'Events.getCategory' + $apiMethod = $view->requestConfig->getApiMethodToRequest(); + + if($view->requestConfig->getApiModuleToRequest() != 'Events') { + // this is not an Events apiMethod + return; + } + + $view->config->addTranslation('label', $this->getColumnTranslation($apiMethod)); + $view->config->addTranslations($this->getMetricTranslations()); + $view->config->columns_to_display = array('label', 'nb_events', 'sum_event_value'); + $view->config->subtable_controller_action = API::getInstance()->getSubtableAction($apiMethod); + + // Creates the tooltip message for Event Value column + $tooltipCallback = function ($hits, $min, $max, $avg) { + if (!$hits) { + return false; + } + $msgEventMinMax = Piwik::translate("Events_EventValueTooltip", array($hits, "
", $min, $max)); + $msgEventAvg = Piwik::translate("Events_AvgEventValue", $avg); + return $msgEventMinMax . "
" . $msgEventAvg; + }; + + // Add tooltip metadata column to the DataTable + $view->config->filters[] = array('ColumnCallbackAddMetadata', + array( + array('nb_events', 'min_event_value', 'max_event_value', 'avg_event_value'), + 'sum_event_value_tooltip', + $tooltipCallback + ) ); + + $view->config->custom_parameters = array('flat' => 0); } } diff --git a/plugins/Events/lang/en.json b/plugins/Events/lang/en.json index 88a2bc1656a..12a193e0a5c 100644 --- a/plugins/Events/lang/en.json +++ b/plugins/Events/lang/en.json @@ -15,12 +15,14 @@ "MaxValue": "Maximum value", "EventsWithValue": "Events with a value", "TotalEventsDocumentation": "Total number of events", - "TotalValueDocumentation": "Total value of events (sum of event values)", + "TotalValueDocumentation": "Total value of events (this is the sum of event values)", "MinValueDocumentation": "Minimum value for this event", "MaxValueDocumentation": "Maximum value for this event", "EventsWithValueDocumentation": "Number of events where an Event value was set", "NameNotSet": "(Event Name not set)", "EventValueTooltip": "Total Event value based on %s events %s between %s and %s.", - "AvgEventValue": "Average Event value is: %s" + "AvgEventValue": "Average Event value is: %s", + "TopEvents": "Top Events", + "ViewEvents": "View Events" } } \ No newline at end of file diff --git a/plugins/Events/templates/index.twig b/plugins/Events/templates/index.twig new file mode 100644 index 00000000000..a12afb651af --- /dev/null +++ b/plugins/Events/templates/index.twig @@ -0,0 +1,2 @@ +{{ leftMenuReports|raw }} + diff --git a/plugins/VisitorGenerator b/plugins/VisitorGenerator index 99e45b4eb48..728645ac2db 160000 --- a/plugins/VisitorGenerator +++ b/plugins/VisitorGenerator @@ -1 +1 @@ -Subproject commit 99e45b4eb480e8b949b21f03de570808820f188a +Subproject commit 728645ac2dbd9a1f74ac59ecea156f44e138c601 diff --git a/tests/PHPUnit/UI b/tests/PHPUnit/UI index 909cef7f14a..9d2267f0b3c 160000 --- a/tests/PHPUnit/UI +++ b/tests/PHPUnit/UI @@ -1 +1 @@ -Subproject commit 909cef7f14a0f85f3e92623e2f236f7a40fc7ed5 +Subproject commit 9d2267f0b3c88a98a8e780d79c170cd6803fab80