mirror of
https://github.com/Findus23/plugin-ClassicCounter.git
synced 2024-09-18 14:13:45 +02:00
support changing color and support more values
This commit is contained in:
parent
d9c14e70c8
commit
e3291d17f5
3 changed files with 61 additions and 22 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,20 @@
|
|||
version="1.1"
|
||||
baseProfile="full"
|
||||
viewBox="-1 -1 {{ length*12 }} 20">
|
||||
{% if errorMessage %}
|
||||
<title>{{ errorMessage }}</title>
|
||||
{% else %}
|
||||
<title>{{ number }}</title>
|
||||
{% endif %}
|
||||
<style>
|
||||
svg {
|
||||
background-color: black;
|
||||
background-color: {{'#' ~ colors.backgroundColor}};
|
||||
}
|
||||
|
||||
polygon {
|
||||
fill-opacity: 0.75;
|
||||
fill-rule: evenodd;
|
||||
stroke: black;
|
||||
stroke: {{'#' ~ colors.backgroundColor}};
|
||||
stroke-width: 0.25;
|
||||
stroke-opacity: 1.0;
|
||||
stroke-linecap: butt;
|
||||
|
@ -19,11 +23,11 @@
|
|||
}
|
||||
|
||||
.on {
|
||||
fill: red;
|
||||
fill: {{'#' ~ colors.foregroundColor}};
|
||||
}
|
||||
|
||||
use {
|
||||
fill: #222222;
|
||||
fill: {{'#' ~ colors.lightColor}};
|
||||
}
|
||||
</style>
|
||||
<defs id="defs">
|
||||
|
@ -47,4 +51,4 @@
|
|||
<use xlink:href="#g"{{ digit[6] ? " class=\"on\"":"" }}/>
|
||||
</g>
|
||||
{% endfor %}
|
||||
</svg>
|
||||
</svg>
|
||||
|
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 2.1 KiB |
Loading…
Reference in a new issue