cleaned up popular_by pages

my old code was so messy :<
This commit is contained in:
Daku 2014-02-16 00:08:11 +00:00
parent eb16ab09ed
commit e8607ab6f2
2 changed files with 46 additions and 66 deletions

View File

@ -63,7 +63,7 @@ class NumericScore extends Extension {
} }
die($html); die($html);
} }
if($event->page_matches("numeric_score_vote") && $user->check_auth_token()) { else if($event->page_matches("numeric_score_vote") && $user->check_auth_token()) {
if(!$user->is_anonymous()) { if(!$user->is_anonymous()) {
$image_id = int_escape($_POST['image_id']); $image_id = int_escape($_POST['image_id']);
$char = $_POST['vote']; $char = $_POST['vote'];
@ -76,7 +76,7 @@ class NumericScore extends Extension {
$page->set_redirect(make_link("post/view/$image_id")); $page->set_redirect(make_link("post/view/$image_id"));
} }
} }
if($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) { else if($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) {
if($user->can("edit_other_vote")) { if($user->can("edit_other_vote")) {
$image_id = int_escape($_POST['image_id']); $image_id = int_escape($_POST['image_id']);
$database->execute( $database->execute(
@ -89,83 +89,62 @@ class NumericScore extends Extension {
$page->set_redirect(make_link("post/view/$image_id")); $page->set_redirect(make_link("post/view/$image_id"));
} }
} }
if($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) { else if($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) {
if($user->can("edit_other_vote")) { if($user->can("edit_other_vote")) {
$this->delete_votes_by(int_escape($_POST['user_id'])); $this->delete_votes_by(int_escape($_POST['user_id']));
$page->set_mode("redirect"); $page->set_mode("redirect");
$page->set_redirect(make_link()); $page->set_redirect(make_link());
} }
} }
if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) { else if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) {
$t_images = $config->get_int("index_images"); //FIXME: popular_by isn't linked from anywhere
list($day, $month, $year) = array(date("d"), date("m"), date("Y"));
//TODO: Add Popular_by_week. if(!empty($_GET['day'])){
$D = (int) $_GET['day'];
if($D >= 1 && $D <= 31) $day = $D;
}
if(!empty($_GET['month'])){
$M = (int) $_GET['month'];
if($M >= 1 && $M <= 12) $month = $M;
}
if(!empty($_GET['year'])){
$Y = (int) $_GET['year'];
if($Y >= 1970 && $Y < 2100) $year = $Y;
}
//year
if(empty($_GET['year'])){
$year = date("Y");
}else{
$year = $_GET['year'];
}
//month
if(empty($_GET['month']) || int_escape($_GET['month']) > 12){
$month = date("m");
}else{
$month = $_GET['month'];
}
//day
if(empty($_GET['day']) || int_escape($_GET['day']) > 31){
$day = date("d");
}else{
$day = $_GET['day'];
}
$totaldate = $year."/".$month."/".$day; $totaldate = $year."/".$month."/".$day;
$sql = $sql = "SELECT id FROM images
"SELECT * FROM images WHERE EXTRACT(YEAR FROM posted) = :year
WHERE EXTRACT(YEAR FROM posted) = :year ";
"; $args = array("limit" => $config->get_int("index_images"), "year" => $year);
$agrs = array("limit" => $t_images, "year" => $year);
if($event->page_matches("popular_by_day")){ if($event->page_matches("popular_by_day")){
$sql .= $sql .=
"AND EXTRACT(MONTH FROM posted) = :month "AND EXTRACT(MONTH FROM posted) = :month
AND EXTRACT(DAY FROM posted) = :day AND EXTRACT(DAY FROM posted) = :day";
AND NOT numeric_score=0
";
//array_push doesn't seem to like using double arrows
//this requires us to instead create two arrays and merge
$sgra = array("month" => $month, "day" => $day);
$args = array_merge($agrs, $sgra);
$args = array_merge($args, array("month" => $month, "day" => $day));
$dte = array($totaldate, date("F jS, Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d", "day"); $dte = array($totaldate, date("F jS, Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d", "day");
} }
if($event->page_matches("popular_by_month")){ else if($event->page_matches("popular_by_month")){
$sql .= $sql .= "AND EXTRACT(MONTH FROM posted) = :month";
"AND EXTRACT(MONTH FROM posted) = :month
AND NOT numeric_score=0
";
$sgra = array("month" => $month);
$args = array_merge($agrs, $sgra);
$title = date("F Y", (strtotime($totaldate))); $args = array_merge($args, array("month" => $month));
$dte = array($totaldate, $title, "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month"); $dte = array($totaldate, date("F Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month");
} }
if($event->page_matches("popular_by_year")){ else if($event->page_matches("popular_by_year")){
$sql .= "AND NOT numeric_score=0"; $dte = array($totaldate, $year, "\\y\\e\\a\\r\=Y", "year");
$dte = array($totaldate, $year, "\y\e\a\\r\=Y", "year");
$args = $agrs;
} }
$sql .= " ORDER BY numeric_score DESC LIMIT :limit OFFSET 0"; $sql .= " AND NOT numeric_score=0 ORDER BY numeric_score DESC LIMIT :limit OFFSET 0";
//filter images by year/score != 0 > limit to max images on one page > order from highest to lowest score //filter images by score != 0 + date > limit to max images on one page > order from highest to lowest score
$result = $database->get_all($sql, $args);
$result = $database->get_col($sql, $args);
$images = array(); $images = array();
foreach($result as $singleResult) { foreach($result as $id) { $images[] = Image::by_id($id); }
$images[] = Image::by_id($singleResult["id"]);
}
$this->theme->view_popular($images, $dte); $this->theme->view_popular($images, $dte);
} }
} }

View File

@ -62,27 +62,28 @@ class NumericScoreTheme extends Themelet {
} }
public function view_popular($images, $dte) { public function view_popular($images, $dte) {
global $user, $page; 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 .= '<span class="thumb">'.
'<a href="$image_link">'.$thumb_html.'</a>'.
'</span>';
} }
$b_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('-1 '.$dte[3], strtotime($dte[0]))))); $b_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('-1 '.$dte[3], strtotime($dte[0])))));
$f_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('+1 '.$dte[3], strtotime($dte[0]))))); $f_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('+1 '.$dte[3], strtotime($dte[0])))));
$html = '<center><h3><a href="'.$b_dte.'">&laquo;</a> '.$dte[1] $html = "\n".
.' <a href="'.$f_dte.'">&raquo;</a>' "<center>\n".
.'</h3></center> " <h3>\n".
<br>'.$pop_images; " <a href='{$b_dte}'>&laquo;</a> {$dte[1]} <a href='{$f_dte}'>&raquo;</a>\n".
" </h3>\n".
"</center>\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('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, $html, "main", 30));
} }