From e3291d17f593c7b7293ca1469f20cf32d6475080 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Mon, 29 Jul 2019 20:36:41 +0200 Subject: [PATCH] support changing color and support more values --- ClassicCounter.php | 8 +++--- Controller.php | 61 ++++++++++++++++++++++++++++++++++++---------- templates/svg.twig | 14 +++++++---- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/ClassicCounter.php b/ClassicCounter.php index 916a9ba..dd51869 100644 --- a/ClassicCounter.php +++ b/ClassicCounter.php @@ -12,15 +12,15 @@ use Piwik\Access; class ClassicCounter extends \Piwik\Plugin { - public static function getVisitorCount($idSite) { + public static function getVisitorData($idSite) { $visitsCount = Access::getInstance()->doAsSuperUser(function () use ($idSite) { - return \Piwik\API\Request::processRequest('VisitsSummary.getVisits', array( + return \Piwik\API\Request::processRequest('VisitsSummary.get', array( 'idSite' => $idSite, 'period' => "range", 'date' => "2000-01-01,2030-01-01", - ))->getFirstRow()["nb_visits"]; + ))->getFirstRow()->export()[0]; }); - return (int)$visitsCount; + return $visitsCount; } } diff --git a/Controller.php b/Controller.php index ea9ebb3..25d4576 100644 --- a/Controller.php +++ b/Controller.php @@ -25,33 +25,68 @@ class Controller extends \Piwik\Plugin\Controller "6" => [true, false, true, true, true, true, true], "7" => [true, true, true, false, false, false, false], "8" => [true, true, true, true, true, true, true], - "9" => [true, true, true, true, false, true, true] + "9" => [true, true, true, true, false, true, true], + "E" => [true, false, false, true, true, true, true], + "r" => [false, false, false, false, true, false, true], + "o" => [false, false, true, true, true, false, true], ]; + private $modes = ["nb_visits", "nb_actions", "nb_visits_converted", "bounce_count", "sum_visit_length", "max_actions", "bounce_rate", "nb_actions_per_visit", "avg_time_on_site"]; + + private $colorRegex = '/^(?:[0-9a-fA-F]{3}){1,2}$/m'; public function svg() { $idSite = Common::getRequestVar('idSite', $this->idSite, 'int'); - - $cache = Cache::getLazyCache(); - $cacheKey = "ClassicCounter_Visits_" . $idSite; - if ($cache->contains($cacheKey)) { - $visitCount = $cache->fetch($cacheKey); - } else { - $visitCount = ClassicCounter::getVisitorCount($idSite); - $cache->save($cacheKey, $visitCount, 60); + $mode = Common::getRequestVar('mode', "nb_visits", 'string'); + $colors = [ + "backgroundColor" => Common::getRequestVar('backgroundColor', "000", 'string'), + "foregroundColor" => Common::getRequestVar('foregroundColor', "f00", 'string'), + "lightColor" => Common::getRequestVar('lightColor', "222", 'string'), + ]; + try { + if (!in_array($mode, $this->modes)) { + $modestring = implode(", ", $this->modes); + throw new \Exception("mode can only be one of $modestring"); + } + foreach ($colors as $name => $color) { + if (!preg_match($this->colorRegex, $color)) { + $colors = [ + "backgroundColor" => "000", + "foregroundColor" => "f00", + "lightColor" => "222", + ]; + throw new \Exception("$name has to be a valid hex color (without the #)"); + } + } + $cache = Cache::getLazyCache(); + $cacheKey = "ClassicCounter_Data_" . $idSite; + if ($cache->contains($cacheKey) && false) { + $visitData = $cache->fetch($cacheKey); + } else { + $visitData = ClassicCounter::getVisitorData($idSite); + $cache->save($cacheKey, $visitData, 60); + } + $text = $visitData[$mode]; + $text = (int)$text; + $errorMessage = false; + } catch (\Exception $e) { + $text = "Error"; + $errorMessage = $e->getMessage(); } $view = new View("@ClassicCounter/svg"); $view->setContentType("image/svg+xml"); - $view->number = $visitCount; - $chars = str_split($visitCount); + $view->number = $text; + $view->errorMessage = $errorMessage; + $view->colors = $colors; + $chars = str_split($text); foreach ($chars as $char) { if (empty($this->sevenSegment[$char])) { - throw new \Exception("character can't be shown on seven segment display"); + throw new \Exception("character '$char' can't be shown on seven segment display"); } $view->digits[] = $this->sevenSegment[$char]; } - $view->length = strlen($visitCount); + $view->length = strlen($text); return $view->render(); } } diff --git a/templates/svg.twig b/templates/svg.twig index 327f0cb..0633fe0 100644 --- a/templates/svg.twig +++ b/templates/svg.twig @@ -2,16 +2,20 @@ version="1.1" baseProfile="full" viewBox="-1 -1 {{ length*12 }} 20"> + {% if errorMessage %} + {{ errorMessage }} + {% else %} {{ number }} + {% endif %} @@ -47,4 +51,4 @@ {% endfor %} - \ No newline at end of file +