<?php
require_once dirname(__FILE__) . "/../Page.php";

class HomePage extends Page {
	public function __construct() {
		parent::__construct();
	}

	protected function showContent() {
		$this->echoln("<p>Welcome at the AEGEE Photo page. On this page you can find photos of a lot of AEGEE events from the past. On this page, members could upload their own photos.</p>");
		$this->echoln("<p>There are currently " . number_format($this->getPhotoCount(), 0, ",", ".") . " photos online in " . number_format($this->getBookCount(), 0, ",", ".") . " books. ");
		$this->echoln("There are " . number_format($this->getEventCount(), 0, ",", ".") . " events of which you can find photos on this page.</p>");
		$photos = $this->getRandomPhotos(2 * MAX_PHOTO_THUMB_COLS + 1);
		$this->echoln("<p>Random photos:</p>");
		if( $photos != null ) {
			$this->echoln("<table width=\"100%\">");
			$r = 1;
			$colWidth = 100 / MAX_PHOTO_THUMB_COLS;
			while( $r <= MAX_PHOTO_THUMB_COLS * 2 ) {
				if( $r % MAX_PHOTO_THUMB_COLS == 1 ) {
					$this->echoln("<tr>");
				}
				$this->echoln("<td height=\"" . (MAX_SIZE_THUMB + 20) . "\" width=\"" . $colWidth . "%\" align=\"center\" valign=\"middle\">");
				if( isset($photos[$r]) ) {
					$title = $this->getPhotoTitle($photos[$r]);
					$this->echoln("<a href=\"./photo.php?book=" . $photos[$r]['book_id'] . "&amp;photo=" . $photos[$r]['nr'] . "&amp;size=big\" title=\"" . $title . "\">");
					$this->echoln("<img src=\"./image/photo.php?id=" . $photos[$r]['photo_id'] . "&amp;size=thumb\" alt=\"" . $title . "\" border=\"0\" height=\"" . $photos[$r]['height_thumb'] . "\" width=\"" . $photos[$r]['width_thumb'] . "\">");
					$this->echoln("</a>");
				}
				$this->echoln("</td>");
				if( $r % MAX_PHOTO_THUMB_COLS == 0 ) {
					$this->echoln("</tr>");
				}
				$r++;
			}
			$this->echoln("</table>");
			$this->echoln("<p>Featured photo:</p>");
			$title = $this->getPhotoTitle($photos[0]);
			$this->echoln("<p>");
			$this->echoln("<img src=\"./image/photo.php?id=" . $photos[0]['photo_id'] . "&amp;size=big\" alt=\"" . $title . "\" border=\"0\" height=\"" . $photos[0]['height_big'] . "\" width=\"" . $photos[0]['width_big'] . "\">");
			$this->echoln("<br>" . $title);
			$this->echoln("</p>");
		}
	}
	
	private function getPhotoTitle($photo) {
		return $photo['event'] . " (" . $this->formatDate($photo['event_start']) . " - " . $this->formatDate($photo['event_end']) . ") - " .
			$photo['book'] . " - " .
			($photo['comment'] == null ? $photo['nr'] : $photo['nr'] . ": " . $photo['comment']);
	}

	private function getPhotoCount() {
		$queryBuilder = new QueryBuilder($this->getDbMySQL(), "photo");
		$queryBuilder->addField(new DBMethodCount("id"), "photoCount");
		$result = $this->getDbMySQL()->select($queryBuilder->toQuery());
		if( $result->numRows() == 1 ) {
			$row = $result->fetchAssoc();
			return $row['photoCount'];
		}else {
			return "unknown";
		}
	}

	private function getBookCount() {
		$queryBuilder = new QueryBuilder($this->getDbMySQL(), "book");
		$queryBuilder->addField(new DBMethodCount("id"), "bookCount");
		$queryBuilder->addWhereGreater("uid", 0);
		$queryBuilder->addWhereGreater("photocount", 0);
		$result = $this->getDbMySQL()->select($queryBuilder->toQuery());
		if( $result->numRows() == 1 ) {
			$row = $result->fetchAssoc();
			return $row['bookCount'];
		}else {
			return "unknown";
		}
	}

	private function getEventCount() {
		$queryBuilder = new QueryBuilder($this->getDbMySQL(), "event");
		$queryBuilder->addField("event.eventcode", "eventCount");
		$queryBuilder->addInnerJoin("book", "eventcode", "event.eventcode");
		$queryBuilder->addWhereGreater("book.photocount", 0);
		$queryBuilder->addGroup("event.eventcode");
		$result = $this->getDbMySQL()->select($queryBuilder->toQuery());
		return $result->numRows();
	}
	
	private function getRandomPhotos($count, $invokeCount = 0) {
		$queryBuilder = new QueryBuilder($this->getDbMySQL(), "photo");
		$queryBuilder->addField(new DBMethodMax("id"), "maxId");
		$result = $this->getDbMySQL()->select($queryBuilder->toQuery());
		if( $result->numRows() == 1 ) {
			$maxId = $result->fetchAssoc()['maxId'];
			$randomIds = array();
			for( $i = 0; $i < $count * 2; $i++ ) {
				$randomIds[] = mt_rand(1, $maxId);
			}
			$queryBuilder = new QueryBuilder($this->getDbMySQL(), "photo");
			$queryBuilder->addField("photo.id", "photo_id");
			$queryBuilder->addField("photo.comment", "comment");
			$queryBuilder->addField(array("book_id", "nr", "date_add", "height_thumb", "width_thumb", "height_big", "width_big"));
			$queryBuilder->addField("book.title", "book");
			$queryBuilder->addField("event.title", "event");
			$queryBuilder->addField("event.datestart", "event_start");
			$queryBuilder->addField("event.dateend", "event_end");
			$queryBuilder->addInnerJoin("book", "id", "photo.book_id", "book");
			$queryBuilder->addInnerJoin("event", "eventcode", "book.eventcode", "event");
			$queryBuilder->addWhereIn("photo.id", $randomIds);
			$queryBuilder->addOrderRandom();
			$queryBuilder->setLimit($count);
			$result = $this->getDbMySQL()->select($queryBuilder->toQuery());
			if( $result != null ) {
				$photos = $result->fetchAssocAll();
				if( count($photos) < $count ) {
					if( $invokeCount < 4 ) {
						$photos = array_merge($photos, $this->getRandomPhotos($count - count($photos), $invokeCount + 1));
					}
				}
				return $photos;
			}
		}
		return null;
	}
}
?>