1
0
Fork 0
mirror of https://github.com/Findus23/plugin-ClassicCounter.git synced 2024-09-19 15:23:45 +02:00

support changing color and support more values

This commit is contained in:
Lukas Winkler 2019-07-29 20:36:41 +02:00
parent d9c14e70c8
commit e3291d17f5
Signed by: lukas
GPG key ID: 54DE4D798D244853
3 changed files with 61 additions and 22 deletions

View file

@ -12,15 +12,15 @@ use Piwik\Access;
class ClassicCounter extends \Piwik\Plugin class ClassicCounter extends \Piwik\Plugin
{ {
public static function getVisitorCount($idSite) { public static function getVisitorData($idSite) {
$visitsCount = Access::getInstance()->doAsSuperUser(function () use ($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, 'idSite' => $idSite,
'period' => "range", 'period' => "range",
'date' => "2000-01-01,2030-01-01", 'date' => "2000-01-01,2030-01-01",
))->getFirstRow()["nb_visits"]; ))->getFirstRow()->export()[0];
}); });
return (int)$visitsCount; return $visitsCount;
} }
} }

View file

@ -25,33 +25,68 @@ class Controller extends \Piwik\Plugin\Controller
"6" => [true, false, true, true, true, true, true], "6" => [true, false, true, true, true, true, true],
"7" => [true, true, true, false, false, false, false], "7" => [true, true, true, false, false, false, false],
"8" => [true, true, true, true, true, true, true], "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() { public function svg() {
$idSite = Common::getRequestVar('idSite', $this->idSite, 'int'); $idSite = Common::getRequestVar('idSite', $this->idSite, 'int');
$mode = Common::getRequestVar('mode', "nb_visits", 'string');
$cache = Cache::getLazyCache(); $colors = [
$cacheKey = "ClassicCounter_Visits_" . $idSite; "backgroundColor" => Common::getRequestVar('backgroundColor', "000", 'string'),
if ($cache->contains($cacheKey)) { "foregroundColor" => Common::getRequestVar('foregroundColor', "f00", 'string'),
$visitCount = $cache->fetch($cacheKey); "lightColor" => Common::getRequestVar('lightColor', "222", 'string'),
} else { ];
$visitCount = ClassicCounter::getVisitorCount($idSite); try {
$cache->save($cacheKey, $visitCount, 60); 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 = new View("@ClassicCounter/svg");
$view->setContentType("image/svg+xml"); $view->setContentType("image/svg+xml");
$view->number = $visitCount; $view->number = $text;
$chars = str_split($visitCount); $view->errorMessage = $errorMessage;
$view->colors = $colors;
$chars = str_split($text);
foreach ($chars as $char) { foreach ($chars as $char) {
if (empty($this->sevenSegment[$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->digits[] = $this->sevenSegment[$char];
} }
$view->length = strlen($visitCount); $view->length = strlen($text);
return $view->render(); return $view->render();
} }
} }

View file

@ -2,16 +2,20 @@
version="1.1" version="1.1"
baseProfile="full" baseProfile="full"
viewBox="-1 -1 {{ length*12 }} 20"> viewBox="-1 -1 {{ length*12 }} 20">
{% if errorMessage %}
<title>{{ errorMessage }}</title>
{% else %}
<title>{{ number }}</title> <title>{{ number }}</title>
{% endif %}
<style> <style>
svg { svg {
background-color: black; background-color: {{'#' ~ colors.backgroundColor}};
} }
polygon { polygon {
fill-opacity: 0.75; fill-opacity: 0.75;
fill-rule: evenodd; fill-rule: evenodd;
stroke: black; stroke: {{'#' ~ colors.backgroundColor}};
stroke-width: 0.25; stroke-width: 0.25;
stroke-opacity: 1.0; stroke-opacity: 1.0;
stroke-linecap: butt; stroke-linecap: butt;
@ -19,11 +23,11 @@
} }
.on { .on {
fill: red; fill: {{'#' ~ colors.foregroundColor}};
} }
use { use {
fill: #222222; fill: {{'#' ~ colors.lightColor}};
} }
</style> </style>
<defs id="defs"> <defs id="defs">
@ -47,4 +51,4 @@
<use xlink:href="#g"{{ digit[6] ? " class=\"on\"":"" }}/> <use xlink:href="#g"{{ digit[6] ? " class=\"on\"":"" }}/>
</g> </g>
{% endfor %} {% endfor %}
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB