<?php

class PDFController {
	public static function download($id, $type, $filename) {

		$rev = new Revision($id);
		$path = $rev->path();

		$rev->downloads++;
		$rev->last_download = time();
		$rev->save();

		$disp = "attachment";
		$mime = "application/octet-stream";

		$file = null;

		switch ($type) {
			case "view":
				$file = new PDF($path . "/doc.pdf");
				break;
			case "download":
				$file = new PDF($path . "/doc.pdf");
				$file->force_download();
				break;
			case "viewocr":
				$file = new PDF($path . "/ocr.pdf");
				break;
			case "downloadocr":
				$file = new PDF($path . "/ocr.pdf");
				$file->force_download();
				break;
		}
		$file->fake_filename($filename);

		if ($file == null) {
			return [404, "Not Found 1"];
		}

		if (!$file->exists()) {
			return [404, "Not Found 2"];
		}

		return $file;
	}

	public static function get_page($id, $page) {
		$rev = new Revision($id);
		$page = $rev->get_page($page, 150);
		if ($page) {
			return $page;
		}	
		return [404, "Not Found"];
	}
}
