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);
}
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()) {
$image_id = int_escape($_POST['image_id']);
$char = $_POST['vote'];
@ -76,7 +76,7 @@ class NumericScore extends Extension {
$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")) {
$image_id = int_escape($_POST['image_id']);
$database->execute(
@ -89,83 +89,62 @@ class NumericScore extends Extension {
$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")) {
$this->delete_votes_by(int_escape($_POST['user_id']));
$page->set_mode("redirect");
$page->set_redirect(make_link());
}
}
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");
else if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) {
//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;
$sql =
"SELECT * FROM images
$sql = "SELECT id FROM images
WHERE EXTRACT(YEAR FROM posted) = :year
";
$agrs = array("limit" => $t_images, "year" => $year);
$args = array("limit" => $config->get_int("index_images"), "year" => $year);
if($event->page_matches("popular_by_day")){
$sql .=
"AND EXTRACT(MONTH FROM posted) = :month
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);
AND EXTRACT(DAY FROM posted) = :day";
$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");
}
if($event->page_matches("popular_by_month")){
$sql .=
"AND EXTRACT(MONTH FROM posted) = :month
AND NOT numeric_score=0
";
$sgra = array("month" => $month);
$args = array_merge($agrs, $sgra);
else if($event->page_matches("popular_by_month")){
$sql .= "AND EXTRACT(MONTH FROM posted) = :month";
$title = date("F Y", (strtotime($totaldate)));
$dte = array($totaldate, $title, "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month");
$args = array_merge($args, array("month" => $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")){
$sql .= "AND NOT numeric_score=0";
$dte = array($totaldate, $year, "\y\e\a\\r\=Y", "year");
$args = $agrs;
else if($event->page_matches("popular_by_year")){
$dte = array($totaldate, $year, "\\y\\e\\a\\r\=Y", "year");
}
$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
$result = $database->get_all($sql, $args);
//filter images by score != 0 + date > limit to max images on one page > order from highest to lowest score
$result = $database->get_col($sql, $args);
$images = array();
foreach($result as $singleResult) {
$images[] = Image::by_id($singleResult["id"]);
}
foreach($result as $id) { $images[] = Image::by_id($id); }
$this->theme->view_popular($images, $dte);
}
}

View File

@ -62,27 +62,28 @@ class NumericScoreTheme extends Themelet {
}
public function view_popular($images, $dte) {
global $user, $page;
global $page, $config;
$pop_images = '';
$pop_images = "";
foreach($images as $image) {
$thumb_html = $this->build_thumb_html($image);
$pop_images .= '<span class="thumb">'.
'<a href="$image_link">'.$thumb_html.'</a>'.
'</span>';
$pop_images .= $this->build_thumb_html($image)."\n";
}
$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])))));
$html = '<center><h3><a href="'.$b_dte.'">&laquo;</a> '.$dte[1]
.' <a href="'.$f_dte.'">&raquo;</a>'
.'</h3></center>
<br>'.$pop_images;
$html = "\n".
"<center>\n".
" <h3>\n".
" <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>";
$page->set_heading($config->get_string('title'));
$page->add_block(new Block("Navigation", $nav_html, "left", 10));
$page->add_block(new Block(null, $html, "main", 30));
}