diff options
Diffstat (limited to 'plugins/Referrers/Controller.php')
-rw-r--r-- | plugins/Referrers/Controller.php | 288 |
1 files changed, 4 insertions, 284 deletions
diff --git a/plugins/Referrers/Controller.php b/plugins/Referrers/Controller.php index bc2d25fb7e..66184b8816 100644 --- a/plugins/Referrers/Controller.php +++ b/plugins/Referrers/Controller.php @@ -12,6 +12,7 @@ use Piwik\API\Request; use Piwik\Common; use Piwik\DataTable\Filter\CalculateEvolutionFilter; use Piwik\DataTable\Map; +use Piwik\FrontController; use Piwik\Metrics; use Piwik\NumberFormatter; use Piwik\Period\Range; @@ -41,213 +42,12 @@ class Controller extends \Piwik\Plugin\Controller public function getSparklines() { - $metrics = $this->getReferrersVisitorsByType(); - $distinctMetrics = $this->getDistinctReferrersMetrics(); + $_GET['forceView'] = '1'; + $_GET['viewDataTable'] = Sparklines::ID; - $numberFormatter = NumberFormatter::getInstance(); - - $totalVisits = array_sum($metrics); - foreach ($metrics as $name => $value) { - - // calculate percent of total, if there were any visits - if ($value != 0 && $totalVisits != 0) { - $percentName = $name . 'Percent'; - $metrics[$percentName] = round(($value / $totalVisits) * 100, 0); - } - } - - // calculate evolution for visit metrics & distinct metrics - list($lastPeriodDate, $ignore) = Range::getLastDate(); - if ($lastPeriodDate !== false) { - $date = Common::getRequestVar('date'); - $period = Common::getRequestVar('period'); - - $prettyDate = self::getPrettyDate($date, $period); - $prettyLastPeriodDate = self::getPrettyDate($lastPeriodDate, $period); - - // visit metrics - $previousValues = $this->getReferrersVisitorsByType($lastPeriodDate); - $metrics = $this->addEvolutionPropertiesToView($prettyDate, $metrics, $prettyLastPeriodDate, $previousValues); - - // distinct metrics - $previousValues = $this->getDistinctReferrersMetrics($lastPeriodDate); - $distinctMetrics = $this->addEvolutionPropertiesToView($prettyDate, $distinctMetrics, $prettyLastPeriodDate, $previousValues); - } - - /** @var Sparklines $view */ - $view = ViewDataTable\Factory::build(Sparklines::ID, $api = '', $controller = '', $force = true, $loadUserParams = false); - - // DIRECT ENTRY - $metrics['visitorsFromDirectEntry'] = $numberFormatter->formatNumber($metrics['visitorsFromDirectEntry']); - $values = array($metrics['visitorsFromDirectEntry']); - $descriptions = array(Piwik::translate('Referrers_TypeDirectEntries')); - - if (!empty($metrics['visitorsFromDirectEntryPercent'])) { - $metrics['visitorsFromDirectEntryPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromDirectEntryPercent'], $precision = 1); - $values[] = $metrics['visitorsFromDirectEntryPercent']; - $descriptions[] = Piwik::translate('Referrers_XPercentOfVisits'); - } - - $directEntryParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_DIRECT_ENTRY); - - $view->config->addSparkline($directEntryParams, $values, $descriptions, @$metrics['visitorsFromDirectEntryEvolution']); - - - // WEBSITES - $metrics['visitorsFromWebsites'] = $numberFormatter->formatNumber($metrics['visitorsFromWebsites']); - $values = array($metrics['visitorsFromWebsites']); - $descriptions = array(Piwik::translate('Referrers_TypeWebsites')); - - if (!empty($metrics['visitorsFromWebsitesPercent'])) { - $metrics['visitorsFromWebsitesPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromWebsitesPercent'], $precision = 1); - $values[] = $metrics['visitorsFromWebsitesPercent']; - $descriptions[] = Piwik::translate('Referrers_XPercentOfVisits'); - } - - $searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_WEBSITE); - - $view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromWebsitesEvolution']); - - - // SEARCH ENGINES - $metrics['visitorsFromSearchEngines'] = $numberFormatter->formatNumber($metrics['visitorsFromSearchEngines']); - $values = array($metrics['visitorsFromSearchEngines']); - $descriptions = array(Piwik::translate('Referrers_TypeSearchEngines')); - - if (!empty($metrics['visitorsFromSearchEnginesPercent'])) { - $metrics['visitorsFromSearchEnginesPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromSearchEnginesPercent'], $precision = 1); - $values[] = $metrics['visitorsFromSearchEnginesPercent']; - $descriptions[] = Piwik::translate('Referrers_XPercentOfVisits'); - } - $searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_SEARCH_ENGINE); - - $view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromSearchEnginesEvolution']); - - // SOCIAL NETWORKS - $metrics['visitorsFromSocialNetworks'] = $numberFormatter->formatNumber($metrics['visitorsFromSocialNetworks']); - $values = array($metrics['visitorsFromSocialNetworks']); - $descriptions = array(Piwik::translate('Referrers_TypeSocialNetworks')); - - if (!empty($metrics['visitorsFromSocialNetworksPercent'])) { - $metrics['visitorsFromSocialNetworksPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromSocialNetworksPercent'], $precision = 1); - $values[] = $metrics['visitorsFromSocialNetworksPercent']; - $descriptions[] = Piwik::translate('Referrers_XPercentOfVisits'); - } - $socialNetworkParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_SOCIAL_NETWORK); - - $view->config->addSparkline($socialNetworkParams, $values, $descriptions, @$metrics['visitorsFromSocialNetworksEvolution']); - - - // CAMPAIGNS - $metrics['visitorsFromCampaigns'] = $numberFormatter->formatNumber($metrics['visitorsFromCampaigns']); - $values = array($metrics['visitorsFromCampaigns']); - $descriptions = array(Piwik::translate('Referrers_TypeCampaigns')); - - if (!empty($metrics['visitorsFromCampaignsPercent'])) { - $metrics['visitorsFromCampaignsPercent'] = $numberFormatter->formatPercent($metrics['visitorsFromCampaignsPercent'], $precision = 1); - $values[] = $metrics['visitorsFromCampaignsPercent']; - $descriptions[] = Piwik::translate('Referrers_XPercentOfVisits'); - } - - $searchEngineParams = $this->getReferrerSparklineParams(Common::REFERRER_TYPE_CAMPAIGN); - - $view->config->addSparkline($searchEngineParams, $values, $descriptions, @$metrics['visitorsFromCampaignsEvolution']); - - - // DISTINCT SEARCH ENGINES - $sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctSearchEnginesGraph'); - $value = $distinctMetrics['numberDistinctSearchEngines']; - $value = $numberFormatter->formatNumber($value); - $description = Piwik::translate('Referrers_DistinctSearchEngines'); - - $view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctSearchEnginesEvolution']); - - - // DISTINCT SOCIAL NETWORKS - $sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctSocialNetworksGraph'); - $value = $distinctMetrics['numberDistinctSocialNetworks']; - $value = $numberFormatter->formatNumber($value); - $description = Piwik::translate('Referrers_DistinctSocialNetworks'); - - $view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctSocialNetworksEvolution']); - - - // DISTINCT WEBSITES - $sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctWebsitesGraph'); - - $distinctMetrics['numberDistinctWebsites'] = $numberFormatter->formatNumber($distinctMetrics['numberDistinctWebsites']); - $distinctMetrics['numberDistinctWebsitesUrls'] = $numberFormatter->formatNumber($distinctMetrics['numberDistinctWebsitesUrls']); - - $values = array($distinctMetrics['numberDistinctWebsites'], $distinctMetrics['numberDistinctWebsitesUrls']); - $descriptions = array(Piwik::translate('Referrers_DistinctWebsites'), Piwik::translate('Referrers_UsingNDistinctUrls')); - - $view->config->addSparkline($sparklineParams, $values, $descriptions, @$distinctMetrics['numberDistinctWebsitesEvolution']); - - - // DISTINCT KEYWORDS - $sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctKeywordsGraph'); - $value = $distinctMetrics['numberDistinctKeywords']; - $value = $numberFormatter->formatNumber($value); - $description = Piwik::translate('Referrers_DistinctKeywords'); - - $view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctKeywordsEvolution']); - - - // DISTINCT CAMPAIGNS - $sparklineParams = $this->getDistinctSparklineUrlParams('getLastDistinctCampaignsGraph'); - $value = $distinctMetrics['numberDistinctCampaigns']; - $value = $numberFormatter->formatNumber($value); - $description = Piwik::translate('Referrers_DistinctCampaigns'); - - $view->config->addSparkline($sparklineParams, $value, $description, @$distinctMetrics['numberDistinctCampaignsEvolution']); - - return $view->render(); - } - - private function getDistinctSparklineUrlParams($action) - { - return array('module' => $this->pluginName, 'action' => $action); + return FrontController::getInstance()->fetchDispatch('Referrers', 'get'); } - protected function getReferrersVisitorsByType($date = false) - { - if ($date === false) { - $date = Common::getRequestVar('date', false); - } - - // we disable the queued filters because here we want to get the visits coming from search engines - // if the filters were applied we would have to look up for a label looking like "Search Engines" - // which is not good when we have translations - $dataTableReferrersType = Request::processRequest( - "Referrers.getReferrerType", array('disable_queued_filters' => '1', 'date' => $date)); - - $nameToColumnId = array( - 'visitorsFromSearchEngines' => Common::REFERRER_TYPE_SEARCH_ENGINE, - 'visitorsFromSocialNetworks' => Common::REFERRER_TYPE_SOCIAL_NETWORK, - 'visitorsFromDirectEntry' => Common::REFERRER_TYPE_DIRECT_ENTRY, - 'visitorsFromWebsites' => Common::REFERRER_TYPE_WEBSITE, - 'visitorsFromCampaigns' => Common::REFERRER_TYPE_CAMPAIGN, - ); - $return = array(); - foreach ($nameToColumnId as $nameVar => $columnId) { - $value = 0; - $row = $dataTableReferrersType->getRowFromLabel($columnId); - if ($row !== false) { - $value = $row->getColumn(Metrics::INDEX_NB_VISITS); - } - $return[$nameVar] = $value; - } - return $return; - } - - protected $referrerTypeToLabel = array( - Common::REFERRER_TYPE_DIRECT_ENTRY => 'Referrers_DirectEntry', - Common::REFERRER_TYPE_SEARCH_ENGINE => 'Referrers_SearchEngines', - Common::REFERRER_TYPE_SOCIAL_NETWORK => 'Referrers_Socials', - Common::REFERRER_TYPE_WEBSITE => 'Referrers_Websites', - Common::REFERRER_TYPE_CAMPAIGN => 'Referrers_Campaigns', - ); - public function getEvolutionGraph($typeReferrer = false, array $columns = array(), array $defaultColumns = array()) { $view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, 'Referrers.getReferrerType'); @@ -369,84 +169,4 @@ class Controller extends \Piwik\Plugin\Controller return Piwik::translate($label); } - /** - * Returns the URL for the sparkline of visits with a specific referrer type. - * - * @param int $referrerType The referrer type. Referrer types are defined in Common class. - * @return string The URL that can be used to get a sparkline image. - */ - private function getReferrerSparklineParams($referrerType) - { - $totalRow = $this->translator->translate('General_Total'); - - return array( - 'columns' => array('nb_visits'), - 'rows' => array(self::getTranslatedReferrerTypeLabel($referrerType), $totalRow), - 'typeReferrer' => $referrerType, - 'module' => $this->pluginName, - 'action' => 'getReferrerType' - ); - } - - /** - * Returns an array containing the number of distinct referrers for each - * referrer type. - * - * @param bool|string $date The date to use when getting metrics. If false, the - * date query param is used. - * @return array The metrics. - */ - private function getDistinctReferrersMetrics($date = false) - { - $propertyToAccessorMapping = array( - 'numberDistinctSearchEngines' => 'getNumberOfDistinctSearchEngines', - 'numberDistinctSocialNetworks' => 'getNumberOfDistinctSocialNetworks', - 'numberDistinctKeywords' => 'getNumberOfDistinctKeywords', - 'numberDistinctWebsites' => 'getNumberOfDistinctWebsites', - 'numberDistinctWebsitesUrls' => 'getNumberOfDistinctWebsitesUrls', - 'numberDistinctCampaigns' => 'getNumberOfDistinctCampaigns', - ); - - $result = array(); - foreach ($propertyToAccessorMapping as $property => $method) { - $result[$property] = $this->getNumericValue('Referrers.' . $method, $date); - } - return $result; - } - - /** - * Utility method that calculates evolution values for a set of current & past values - * and sets properties on a View w/ HTML that displays the evolution percents. - * - * @param string $date The date of the current values. - * @param array $currentValues Array mapping view property names w/ present values. - * @param string $lastPeriodDate The date of the period in the past. - * @param array $previousValues Array mapping view property names w/ past values. Keys - * in this array should be the same as keys in $currentValues. - * @return array Added current values - */ - private function addEvolutionPropertiesToView($date, $currentValues, $lastPeriodDate, $previousValues) - { - foreach ($previousValues as $name => $pastValue) { - $currentValue = $currentValues[$name]; - $evolutionName = $name . 'Evolution'; - - $currentValueFormatted = NumberFormatter::getInstance()->format($currentValue); - $pastValueFormatted = NumberFormatter::getInstance()->format($pastValue); - - $currentValues[$evolutionName] = array( - 'currentValue' => $currentValue, - 'pastValue' => $pastValue, - 'tooltip' => Piwik::translate('General_EvolutionSummaryGeneric', array( - Piwik::translate('General_NVisits', $currentValueFormatted), - $date, - Piwik::translate('General_NVisits', $pastValueFormatted), - $lastPeriodDate, - CalculateEvolutionFilter::calculate($currentValue, $pastValue, $precision = 1) - )) - ); - } - - return $currentValues; - } } |