This commit is contained in:
Flavio Copes
2016-07-07 18:55:52 +02:00
parent e8ca08ed8d
commit 1c19190929
6 changed files with 292 additions and 186 deletions

View File

@@ -38,10 +38,10 @@ class Popularity
$this->config = Grav::instance()['config'];
$this->data_path = Grav::instance()['locator']->findResource('log://popularity', true, true);
$this->daily_file = $this->data_path.'/'.self::DAILY_FILE;
$this->monthly_file = $this->data_path.'/'.self::MONTHLY_FILE;
$this->totals_file = $this->data_path.'/'.self::TOTALS_FILE;
$this->visitors_file = $this->data_path.'/'.self::VISITORS_FILE;
$this->daily_file = $this->data_path . '/' . self::DAILY_FILE;
$this->monthly_file = $this->data_path . '/' . self::MONTHLY_FILE;
$this->totals_file = $this->data_path . '/' . self::TOTALS_FILE;
$this->visitors_file = $this->data_path . '/' . self::VISITORS_FILE;
}
@@ -62,7 +62,7 @@ class Popularity
}
// Make sure no 'widcard-style' ignore matches this url
foreach ((array) $this->config->get('plugins.admin.popularity.ignore') as $ignore) {
foreach ((array)$this->config->get('plugins.admin.popularity.ignore') as $ignore) {
if (fnmatch($ignore, $relative_url)) {
return;
}
@@ -121,15 +121,17 @@ class Popularity
$limit = intval($this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7));
$chart_data = array_slice($this->daily_data, -$limit, $limit);
$labels = array();
$data = array();
$labels = [];
$data = [];
foreach ($chart_data as $date => $count) {
$labels[] = Grav::instance()['grav']['admin']->translate(['PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]);
$labels[] = Grav::instance()['grav']['admin']->translate([
'PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))
]);
$data[] = $count;
}
return array('labels' => $labels, 'data' => $data);
return ['labels' => $labels, 'data' => $data];
}
/**
@@ -162,7 +164,9 @@ class Popularity
foreach (array_reverse($this->daily_data) as $daily) {
$total += $daily;
$day++;
if ($day == 7) break;
if ($day == 7) {
break;
}
}
return $total;
@@ -217,14 +221,15 @@ class Popularity
$this->monthly_data = $this->getData($this->monthly_file);
}
$labels = array();
$data = array();
$labels = [];
$data = [];
foreach ($this->monthly_data as $date => $count) {
$labels[] = date('M', strtotime($date));
$data[] = $count;
}
return array('labels' => $labels, 'data' => $data);
return ['labels' => $labels, 'data' => $data];
}
/**
@@ -268,12 +273,13 @@ class Popularity
/**
* @param string $path
*
* @return array
*/
protected function getData($path)
{
if (file_exists($path)) {
return (array) json_decode(file_get_contents($path), true);
return (array)json_decode(file_get_contents($path), true);
} else {
return [];
}
@@ -282,9 +288,9 @@ class Popularity
public function flushPopularity()
{
file_put_contents($this->daily_file, array());
file_put_contents($this->monthly_file, array());
file_put_contents($this->totals_file, array());
file_put_contents($this->visitors_file, array());
file_put_contents($this->daily_file, []);
file_put_contents($this->monthly_file, []);
file_put_contents($this->totals_file, []);
file_put_contents($this->visitors_file, []);
}
}