cleanup image view counter
This commit is contained in:
parent
b8c6736327
commit
b45bc1d61c
@ -1,75 +1,32 @@
|
|||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
class ImageViewCounterEvent extends Event
|
|
||||||
{
|
|
||||||
public $image_id;
|
|
||||||
public $user;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class ImageViewCounter extends Extension
|
class ImageViewCounter extends Extension
|
||||||
{
|
{
|
||||||
protected $theme;
|
protected $theme;
|
||||||
private $view_interval = 3600; # allows views to be added each hour
|
private $view_interval = 3600; # allows views to be added each hour
|
||||||
|
|
||||||
# Add Setup Block with options for view counter
|
|
||||||
public function onSetupBuilding(SetupBuildingEvent $event)
|
|
||||||
{
|
|
||||||
$sb = new SetupBlock("Image View Counter");
|
|
||||||
$sb->add_bool_option("image_viewcounter_adminonly", "Display view counter only to admin");
|
|
||||||
|
|
||||||
$event->panel->add_block($sb);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Adds view to database if needed
|
|
||||||
public function onDisplayingImage(DisplayingImageEvent $event)
|
public function onDisplayingImage(DisplayingImageEvent $event)
|
||||||
{
|
|
||||||
$imgid = $event->image->id; // determines image id
|
|
||||||
$this->addview($imgid); // adds a view
|
|
||||||
}
|
|
||||||
|
|
||||||
# display views to user or admin below image if allowed
|
|
||||||
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
|
|
||||||
{
|
|
||||||
global $user, $config;
|
|
||||||
|
|
||||||
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo
|
|
||||||
if ($adminonly == false || ($adminonly && $user->can(Permissions::SEE_IMAGE_VIEW_COUNTS))) {
|
|
||||||
$event->add_part(
|
|
||||||
"<tr><th>Views:</th><td>".
|
|
||||||
$this->get_view_count($event->image->id) .
|
|
||||||
"</tr>",
|
|
||||||
38
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Installs DB table
|
|
||||||
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
|
||||||
{
|
|
||||||
global $database, $config;
|
|
||||||
|
|
||||||
// if the sql table doesn't exist yet, create it
|
|
||||||
if ($config->get_bool("image_viewcounter_installed") == false) { //todo
|
|
||||||
$database->create_table("image_views", "
|
|
||||||
id SCORE_AIPK,
|
|
||||||
image_id INTEGER NOT NULL,
|
|
||||||
user_id INTEGER NOT NULL,
|
|
||||||
timestamp INTEGER NOT NULL,
|
|
||||||
ipaddress SCORE_INET NOT NULL");
|
|
||||||
$config->set_bool("image_viewcounter_installed", true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a view to the item if needed
|
|
||||||
*/
|
|
||||||
private function addview(int $imgid)
|
|
||||||
{
|
{
|
||||||
global $database, $user;
|
global $database, $user;
|
||||||
|
|
||||||
|
$imgid = $event->image->id;
|
||||||
|
|
||||||
|
// counts views from current IP in the last hour
|
||||||
|
$recent_from_ip = (int)$database->get_one(
|
||||||
|
"
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM image_views
|
||||||
|
WHERE ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id
|
||||||
|
",
|
||||||
|
[
|
||||||
|
"ipaddress" => $_SERVER['REMOTE_ADDR'],
|
||||||
|
"lasthour" => time() - $this->view_interval,
|
||||||
|
"image_id" => $imgid
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
// don't add view if person already viewed recently
|
// don't add view if person already viewed recently
|
||||||
if ($this->can_add_view($imgid) === false) {
|
if ($recent_from_ip > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,80 +45,59 @@ class ImageViewCounter extends Extension
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
|
||||||
* Returns true if this IP hasn't recently viewed this image
|
|
||||||
*/
|
|
||||||
private function can_add_view(int $imgid)
|
|
||||||
{
|
{
|
||||||
global $database;
|
global $user, $database;
|
||||||
|
|
||||||
// counts views from current IP in the last hour
|
if ($user->can(Permissions::SEE_IMAGE_VIEW_COUNTS)) {
|
||||||
$recent_from_ip = (int)$database->get_one(
|
|
||||||
"
|
|
||||||
SELECT COUNT(*)
|
|
||||||
FROM image_views
|
|
||||||
WHERE ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id
|
|
||||||
",
|
|
||||||
[
|
|
||||||
"ipaddress" => $_SERVER['REMOTE_ADDR'],
|
|
||||||
"lasthour" => time() - $this->view_interval,
|
|
||||||
"image_id" => $imgid
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// if no views were found with the set criteria, return true
|
|
||||||
if ($recent_from_ip == 0) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the int of the view count from the given image id
|
|
||||||
*/
|
|
||||||
private function get_view_count(int $imgid = 0)
|
|
||||||
{
|
|
||||||
global $database;
|
|
||||||
|
|
||||||
if ($imgid == 0) { // return view count of all images
|
|
||||||
$view_count = (int)$database->get_one(
|
|
||||||
"SELECT COUNT(*) FROM image_views"
|
|
||||||
);
|
|
||||||
} else { // return view count of specified image
|
|
||||||
$view_count = (int)$database->get_one(
|
$view_count = (int)$database->get_one(
|
||||||
"SELECT COUNT(*) FROM image_views WHERE image_id =:image_id",
|
"SELECT COUNT(*) FROM image_views WHERE image_id =:image_id",
|
||||||
["image_id" => $imgid]
|
["image_id" => $event->image->id]
|
||||||
|
);
|
||||||
|
|
||||||
|
$event->add_part(
|
||||||
|
"<tr><th>Views:</th><td>$view_count</td></tr>",
|
||||||
|
38
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the count as int
|
|
||||||
return $view_count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//All of this below is new stuff
|
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
|
||||||
|
{
|
||||||
|
global $database, $config;
|
||||||
|
|
||||||
|
if ($config->get_bool("image_viewcounter_installed") == false) { //todo
|
||||||
|
$database->create_table("image_views", "
|
||||||
|
id SCORE_AIPK,
|
||||||
|
image_id INTEGER NOT NULL,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
timestamp INTEGER NOT NULL,
|
||||||
|
ipaddress SCORE_INET NOT NULL");
|
||||||
|
$config->set_bool("image_viewcounter_installed", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event)
|
public function onPageRequest(PageRequestEvent $event)
|
||||||
{
|
{
|
||||||
global $config, $database, $user, $page;
|
global $database;
|
||||||
|
|
||||||
if ($event->page_matches("popular_images")) {
|
if ($event->page_matches("popular_images")) {
|
||||||
$sql = "SELECT image_id , count(*) as total_views
|
$sql = "
|
||||||
FROM image_views,images
|
SELECT image_id, count(*) AS total_views
|
||||||
WHERE image_views.image_id = image_views.image_id
|
FROM image_views, images
|
||||||
AND image_views.image_id = images.id
|
WHERE image_views.image_id = image_views.image_id
|
||||||
GROUP BY image_views.image_id
|
AND image_views.image_id = images.id
|
||||||
ORDER BY total_views desc";
|
GROUP BY image_views.image_id
|
||||||
$result = $database->get_col($sql);
|
ORDER BY total_views DESC
|
||||||
$images = [];
|
";
|
||||||
|
$result = $database->get_col($sql);
|
||||||
|
$images = [];
|
||||||
foreach ($result as $id) {
|
foreach ($result as $id) {
|
||||||
$images[] = Image::by_id(intval($id));
|
$images[] = Image::by_id(intval($id));
|
||||||
}
|
}
|
||||||
$this->theme->view_popular($images);
|
$this->theme->view_popular($images);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
|
||||||
{
|
{
|
||||||
@ -169,7 +105,4 @@ class ImageViewCounter extends Extension
|
|||||||
$event->add_nav_link("sort_by_visits", new Link('popular_images'), "Popular Images");
|
$event->add_nav_link("sort_by_visits", new Link('popular_images'), "Popular Images");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//This is the end of the struct
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,29 +7,18 @@ class ImageViewCounterTheme extends Themelet
|
|||||||
global $page, $config;
|
global $page, $config;
|
||||||
$pop_images = "";
|
$pop_images = "";
|
||||||
foreach ($images as $image) {
|
foreach ($images as $image) {
|
||||||
$thumb_html = $this->build_thumb_html($image);
|
$pop_images .= $this->build_thumb_html($image) . "\n";
|
||||||
$pop_images .= $thumb_html . "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$html = "\n".
|
|
||||||
"<h3 style='text-align: center;'>\n".
|
|
||||||
" <a href='{$b_dte}'>«</a> {$dte[1]} <a href='{$f_dte}'>»</a>\n".
|
|
||||||
"</h3>\n".
|
|
||||||
"<br/>\n".$pop_images;
|
|
||||||
|
|
||||||
|
|
||||||
$nav_html = "<a href=".make_link().">Index</a>";
|
$nav_html = "<a href=".make_link().">Index</a>";
|
||||||
|
|
||||||
$page->set_heading($config->get_string(SetupConfig::TITLE));
|
$page->set_heading($config->get_string(SetupConfig::TITLE));
|
||||||
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
|
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
|
||||||
$page->add_block(new Block(null, $html, "main", 30));
|
$page->add_block(new Block(null, $pop_images, "main", 30));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function get_help_html()
|
public function get_help_html()
|
||||||
{
|
{
|
||||||
return '<p>Search for images that have received views by users.</p>';
|
return '<p>Search for images that have received views by users.</p>';
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user