/** * Run method with main page logic * * Read in list of albums and the latest photos for each album. Pagination enabled. * Populate template with data and display results in the page. * @access public */ public function run() { $PAGINATION_LIMIT = 10; $session = Session::getInstance(); $user = $session->getUser(); $albumDAO = AlbumDAO::getInstance(); $photoDAO = PhotoDAO::getInstance(); $page = isset($_GET["page"]) && is_numeric($_GET["page"]) ? intval($_GET["page"]) : 1; if ($page < 1) { $page = 1; } $count = $paginator = $paginator_page = null; $album = $photo_info_array = null; $title = ""; $count = $albumDAO->count(); $paginator = new Paginator($count, $PAGINATION_LIMIT); $paginator_page = $paginator->getPage($page); $album_array = $albumDAO->all(array("limit" => $paginator_page)); $photo_info_array = array(); foreach ($album_array as $album) { $count = $photoDAO->countByAlbum($album); if ($count > 0) { $tmp_paginator = new Paginator($count, 1); $tmp_paginator_page = $paginator->getPage($page); // Only get latest item list($latest_photo) = $photoDAO->allByAlbum($album, array("order" => "id DESC", "limit" => $tmp_paginator_page)); $photo_info_array[] = array($count, $latest_photo); } } $this->template->render(array("title" => "Album List", "main_page" => "album_list_tpl.php", "session" => $session, "album_array" => $album_array, "photo_info_array" => $photo_info_array, "paginator_page" => $paginator_page)); }
/** * Run method with main page logic * * Read in album information and photos associated with an album from the database. * Populate template and display results in the page. Pagination possible * @access public */ public function run() { $PAGINATION_LIMIT = 10; $session = Session::getInstance(); $user = $session->getUser(); $albumDAO = AlbumDAO::getInstance(); $photoDAO = PhotoDAO::getInstance(); $album = $photo_array = $photo_count = $paginator_page = $queryVars = null; $title = ""; $page = isset($_GET["page"]) && is_numeric($_GET["page"]) ? intval($_GET["page"]) : 1; if ($page < 1) { $page = 1; } $id = isset($_GET["id"]) && is_numeric($_GET["id"]) ? intval($_GET["id"]) : 0; if ($id <= 0) { header("Location: " . BASE_URL); return; } $album = $albumDAO->load($id, array("joins" => true)); if ($album) { $title = $album->getTitle(); $count = $photoDAO->countByAlbum($album); $paginator = new Paginator($count, $PAGINATION_LIMIT); $paginator_page = $paginator->getPage($page); $photo_array = $photoDAO->allByAlbum($album, array("limit" => $paginator_page)); $queryVars = array("id" => $id); } $this->template->render(array("title" => "View Album - {$title}", "session" => $session, "album" => $album, "photo_array" => $photo_array, "paginator_page" => $paginator_page, "queryVars" => $queryVars, "main_page" => "view_album_tpl.php")); }
/** * Run method with main page logic * * Populate template and display confirmation for photo deletion. For POST requests, * check user credentials, check if photo exists and then delete entry from database. * Available to admins only * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); if ($user == null || !$user->isAdmin()) { $session->setMessage("Do not have permission to access", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } $photoDAO = PhotoDAO::getInstance(); $delete_photo = null; $form_errors = array(); $form_values = array("id" => ""); if (!empty($_POST)) { $id = isset($_POST["id"]) ? trim($_POST["id"]) : ""; if (empty($id)) { header("Location: " . BASE_URL); return; } else { if (is_numeric($id)) { $delete_photo = $photoDAO->load($id); if ($delete_photo) { if ($photoDAO->delete($delete_photo)) { unlink($delete_photo->getFileLoc()); if ($delete_photo->getThumbLoc()) { unlink($delete_photo->getThumbLoc()); } $session->setMessage("Photo deleted"); header("Location: " . BASE_URL); return; } else { $session->setMessage("Could not delete photo", Session::MESSAGE_ERROR); } } } } } else { if (!empty($_GET)) { $id = isset($_GET["id"]) ? trim($_GET["id"]) : ""; if (empty($id)) { header("Location: " . BASE_URL); return; } else { if (is_numeric($id)) { $delete_photo = $photoDAO->load($id); if ($delete_photo) { $form_values["id"] = $delete_photo->getId(); } } } } else { header("Location: " . BASE_URL); return; } } $this->template->render(array("title" => "Delete Profile", "main_page" => "delete_photo_tpl.php", "session" => $session, "delete_photo" => $delete_photo, "form_errors" => $form_errors, "form_values" => $form_values)); }
/** * Run method with main page logic * * Read in the specified photo from the database. Read in album data as well. * Populate template and display photo in the page. * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); $photoDAO = PhotoDAO::getInstance(); $photo = $next_photo = $prev_photo = $photo_index = $photo_count = null; $title = ""; if (!empty($_GET["id"]) && is_numeric($_GET["id"])) { $photo_id = intval($_GET["id"]); $photo = $photoDAO->load($photo_id, array("joins" => true)); if ($photo) { $title .= " - {$photo->getTitle()}"; // Load next and previous photos as well as position of current photo in album $next_photo = $photoDAO->loadNext($photo); $prev_photo = $photoDAO->loadPrevious($photo); $photo_index = $photoDAO->countPosition($photo, $photo->getAlbum()); $photo_count = $photoDAO->countByAlbum($photo->getAlbum()); } } $this->template->render(array("title" => "View Photo" . $title, "main_page" => "view_photo_tpl.php", "session" => $session, "photo" => $photo, "next_photo" => $next_photo, "prev_photo" => $prev_photo, "photo_index" => $photo_index, "photo_count" => $photo_count)); }
/** * Run method with main page logic * * Populate template and display form for editing an photo entry. For POST requests, * check user credentials, check if photo exists and then update entry in database. * Available to admins only * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); if (!$user || !$user->isAdmin()) { $session->setMessage("Do not have permission to access", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } $photoDAO = PhotoDAO::getInstance(); $albumDAO = AlbumDAO::getInstance(); $photo = null; $form_errors = array(); $form_values = array("id" => "", "albumid" => "", "title" => "", "description" => ""); if (!empty($_POST)) { $form_values["id"] = isset($_POST["id"]) && is_numeric($_POST["id"]) ? intval($_POST["id"]) : ""; $form_values["albumid"] = isset($_POST["albumid"]) && is_numeric($_POST["albumid"]) ? intval($_POST["albumid"]) : ""; $form_values["title"] = isset($_POST["title"]) ? trim($_POST["title"]) : ""; $form_values["description"] = isset($_POST["description"]) ? trim($_POST["description"]) : ""; if (empty($form_values["id"])) { $form_errors["id"] = "No id specified"; } $photo = $photoDAO->load($form_values["id"]); if (!$photo) { $form_errors["id"] = "Photo does not exist"; } if (empty($form_values["albumid"])) { $form_errors["albumid"] = "No albumid specified"; } else { if (!$albumDAO->load($form_values["albumid"])) { $form_errors["albumid"] = "Album does not exist"; } } if (empty($form_values["title"])) { $form_errors["title"] = "No title specified"; } if (empty($form_values["description"])) { $form_errors["description"] = "No description specified"; } // Check if image will be changed $upload_path = ""; if (!empty($_FILES["imagefile"]) && $_FILES["imagefile"]["error"] != UPLOAD_ERR_NO_FILE) { if ($_FILES["imagefile"]["error"] != UPLOAD_ERR_OK) { $form_errors["imagefile"] = "File upload failed"; } else { $info = getimagesize($_FILES["imagefile"]["tmp_name"]); $path = pathinfo($_FILES["imagefile"]["name"]); $upload_path = joinPath(Photo::UPLOAD_DIR, strftime("%Y_%m"), basename($_FILES['imagefile']['name'])); $thumbLoc = joinPath(Photo::THUMBNAIL_DIR, strftime("%Y_%m"), $path["filename"] . "_thumb.jpg"); $smallThumbLoc = joinPath(Photo::THUMBNAIL_DIR, strftime("%Y_%m"), $path["filename"] . "_thumb_small.jpg"); if (!$info || !(strtolower($path["extension"]) != ".png" && strtolower($path["extension"]) != ".jpg" && strtolower($path["extension"]) != ".jpeg")) { $form_errors["imagefile"] = "An invalid file was uploaded"; } else { if (file_exists($upload_path)) { unlink($upload_path); if (file_exists($thumbLoc)) { unlink($thumbLoc); } if (file_exists($smallThumbLoc)) { unlink($smallThumbLoc); } //$form_errors["imagefile"] = "Filename already exists. Please choose different name or delete file first"; } } } } if (empty($form_errors)) { $photo->setAlbumId($form_values["albumid"]); $photo->setTitle($form_values["title"]); $photo->setDescription($form_values["description"]); // New image has been uploaded if (!empty($_FILES["imagefile"]) && $_FILES["imagefile"]["error"] != UPLOAD_ERR_NO_FILE) { if (!file_exists(dirname($upload_path))) { mkdir(dirname($upload_path)); } if (move_uploaded_file($_FILES["imagefile"]["tmp_name"], $upload_path)) { $photo->setFileLoc($upload_path); // Reset thumbnail location in case new image does not need a thumbnail $photo->setThumbLoc(""); // Create thumbnail if ($info[0] > Photo::MAX_WIDTH) { $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($photo->getFileLoc()); $phpThumb->setParameter('w', Photo::MAX_WIDTH); $phpThumb->setParameter('config_output_format', 'jpeg'); if (!file_exists(dirname($thumbLoc))) { mkdir(dirname($thumbLoc)); } if ($phpThumb->GenerateThumbnail() && $phpThumb->RenderToFile($thumbLoc)) { $photo->setThumbLoc($thumbLoc); $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($photo->getFileLoc()); $phpThumb->setParameter('h', Photo::SMALL_THUMB_HEIGHT); $phpThumb->setParameter('config_output_format', 'jpeg'); $phpThumb->GenerateThumbnail(); } else { if (file_exists($photo->getFileLoc())) { unlink($photo->getFileLoc()); } $form_errors["imagefile"] = "Image larger than " . Photo::MAX_WIDTH . "x" . Photo::MAX_HEIGHT . " and thumbnail generation failed"; } } } else { $form_errors["imagefile"] = "File could not be moved"; } } if (empty($form_errors["imagefile"])) { if ($photoDAO->save($photo)) { $session->setMessage("Photo saved"); header("Location: edit_photo.php?id={$photo->getId()}"); return; } else { $session->setMessage("Photo not saved"); } } } else { if (empty($form_errors["id"])) { $photo = $photoDAO->load($form_values["id"]); } } } else { if (!empty($_GET)) { $form_values["id"] = isset($_GET["id"]) ? $_GET["id"] : ""; if (empty($form_values["id"])) { header("Location: " . BASE_URL); return; } else { $photo = $photoDAO->load($form_values["id"]); if ($photo) { $form_values["id"] = $photo->getId(); $form_values["albumid"] = $photo->getAlbumId(); $form_values["title"] = $photo->getTitle(); $form_values["description"] = $photo->getDescription(); } } } } $album_array = $albumDAO->all(); $this->template->render(array("title" => "Edit Photo", "session" => $session, "main_page" => "edit_photo_tpl.php", "photo" => $photo, "form_values" => $form_values, "form_errors" => $form_errors, "album_array" => $album_array)); }