Added a list that sorts by total views

Most of the code is stolen from numeric_score and ported in order to be usable here
This commit is contained in:
MetallicAchu 2020-05-15 10:22:54 +03:00 committed by Shish
parent c11f0dafd0
commit b8c6736327
2 changed files with 77 additions and 0 deletions

View file

@ -1,7 +1,15 @@
<?php declare(strict_types=1);
class ImageViewCounterEvent extends Event
{
public $image_id;
public $user;
}
class ImageViewCounter extends Extension
{
protected $theme;
private $view_interval = 3600; # allows views to be added each hour
# Add Setup Block with options for view counter
@ -130,4 +138,38 @@ class ImageViewCounter extends Extension
// returns the count as int
return $view_count;
}
//All of this below is new stuff
public function onPageRequest(PageRequestEvent $event)
{
global $config, $database, $user, $page;
if ($event->page_matches("popular_images")) {
$sql = "SELECT image_id , count(*) as total_views
FROM image_views,images
WHERE image_views.image_id = image_views.image_id
AND image_views.image_id = images.id
GROUP BY image_views.image_id
ORDER BY total_views desc";
$result = $database->get_col($sql);
$images = [];
foreach ($result as $id) {
$images[] = Image::by_id(intval($id));
}
$this->theme->view_popular($images);
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
if ($event->parent=="posts") {
$event->add_nav_link("sort_by_visits", new Link('popular_images'), "Popular Images");
}
}
//This is the end of the struct
}

View file

@ -0,0 +1,35 @@
<?php declare(strict_types=1);
class ImageViewCounterTheme extends Themelet
{
public function view_popular($images)
{
global $page, $config;
$pop_images = "";
foreach ($images as $image) {
$thumb_html = $this->build_thumb_html($image);
$pop_images .= $thumb_html . "\n";
}
$html = "\n".
"<h3 style='text-align: center;'>\n".
" <a href='{$b_dte}'>&laquo;</a> {$dte[1]} <a href='{$f_dte}'>&raquo;</a>\n".
"</h3>\n".
"<br/>\n".$pop_images;
$nav_html = "<a href=".make_link().">Index</a>";
$page->set_heading($config->get_string(SetupConfig::TITLE));
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
$page->add_block(new Block(null, $html, "main", 30));
}
public function get_help_html()
{
return '<p>Search for images that have received views by users.</p>';
}
}