コード例 #1
0
 /**
  * Run method with main page logic
  * 
  * Populate template and display form for creating a new album entry. For POST request,
  * validate form data and save information to 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;
     }
     $albumDAO = AlbumDAO::getInstance();
     $album = null;
     $form_errors = array();
     $form_values = array("title" => "");
     if (!empty($_POST)) {
         $form_values["title"] = isset($_POST["title"]) ? trim($_POST["title"]) : "";
         if (empty($form_values["title"])) {
             $form_errors["title"] = "No title specified";
         }
         if (empty($form_errors)) {
             $album = new Album();
             $album->setTitle($form_values["title"]);
             if ($albumDAO->insert($album)) {
                 $session->setMessage("Album saved");
                 header("Location: edit_album.php?id={$album->id}");
                 return;
             } else {
                 $session->setMessage("Album not saved");
             }
         }
     }
     $this->template->render(array("title" => "Create Album", "session" => $session, "main_page" => "create_album_tpl.php", "album" => $album, "form_values" => $form_values, "form_errors" => $form_errors));
 }
コード例 #2
0
 /**
  * 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));
 }
コード例 #3
0
 /**
  * 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"));
 }
コード例 #4
0
 /**
  * Run method with main page logic
  * 
  * Populate template and display form for editing an album entry. For POST requests,
  * check user credentials, check if album 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;
     }
     $albumDAO = AlbumDAO::getInstance();
     $album = null;
     $form_errors = array();
     $form_values = array("id" => "", "title" => "");
     if (!empty($_POST)) {
         $form_values["id"] = isset($_POST["id"]) && is_numeric($_POST["id"]) ? intval($_POST["id"]) : "";
         $form_values["title"] = isset($_POST["title"]) ? trim($_POST["title"]) : "";
         if (empty($form_values["id"])) {
             $form_errors["id"] = "No id specified";
         }
         if (empty($form_values["title"])) {
             $form_errors["title"] = "No title specified";
         }
         if (empty($form_errors)) {
             $album = $albumDAO->load($form_values["id"]);
             if ($album) {
                 $album->setTitle($form_values["title"]);
                 if ($albumDAO->save($album)) {
                     $session->setMessage("Album saved");
                     header("Location: edit_album.php?id={$album->id}");
                     return;
                 } else {
                     $session->setMessage("Album not saved");
                 }
             }
         } else {
             if (empty($form_errors["id"])) {
                 $album = $albumDAO->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 {
                 $album = $albumDAO->load($form_values["id"]);
                 // Album does not exist. Pass null to template
                 if (!$album) {
                 } else {
                     $form_values["id"] = $album->getId();
                     $form_values["title"] = $album->getTitle();
                 }
             }
         }
     }
     $this->template->render(array("title" => "Edit Album", "session" => $session, "main_page" => "edit_album_tpl.php", "album" => $album, "form_values" => $form_values, "form_errors" => $form_errors));
 }
コード例 #5
0
 /**
  * Run method with main page logic
  * 
  * Read in albums from the database. Displays an interface to administer album data
  * for allowing bulk deletion of albums, deletion of a single
  * album and links to edit and view each album entry. Pagination enabled.
  * Available to admins only
  * @access public
  */
 public function run()
 {
     $PAGINATION_LIMIT = 10;
     $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;
     }
     $page = isset($_GET["page"]) && is_numeric($_GET["page"]) ? $_GET["page"] : 1;
     if ($page < 1) {
         $page = 1;
     }
     $action = isset($_GET["action"]) ? trim($_GET["action"]) : "";
     $albumDAO = AlbumDAO::getInstance();
     $album_array = $paginator_page = null;
     $content_title = "";
     // Check for POST request and necessary data for deletion
     if (!empty($_POST) && !empty($_POST["ids"]) && !empty($_POST["action"])) {
         $action = isset($_POST["action"]) ? trim($_POST["action"]) : "";
         if (!strcmp($action, "delete") == 0) {
             header("Location: " . BASE_URL);
             return;
         }
         $status = $albumDAO->deleteByIds($_POST["ids"]);
         if ($status) {
             $session->setMessage("Selected pages deleted");
             header("Location: {$_SERVER["PHP_SELF"]}");
             return;
         } else {
             $session->setMessage("Deletion failed", Session::MESSAGE_ERROR);
             header("Location: {$_SERVER["PHP_SELF"]}");
             return;
         }
     } else {
         if (strcmp($action, "delete") == 0 && !empty($_GET["ids"])) {
             $content_title = "Delete Album";
             $album_array = $albumDAO->allByIds($_GET["ids"]);
         } else {
             if (strcmp($action, "delete") == 0) {
             } else {
                 $count = $albumDAO->count();
                 $paginator = new Paginator($count, $PAGINATION_LIMIT);
                 $paginator_page = $paginator->getPage($page);
                 $album_array = $albumDAO->all(array("limit" => $paginator_page));
             }
         }
     }
     $this->template->render(array("title" => "Admin - Album Options", "main_page" => "album_options_tpl.php", "session" => $session, "album_array" => $album_array, "paginator_page" => $paginator_page, "action" => $action, "content_title" => $content_title));
 }
コード例 #6
0
 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;
     }
     $albumDAO = AlbumDAO::getInstance();
     $delete_album = $album_array = $move_album = null;
     $form_errors = array();
     $form_values = array("id" => "", "album_move" => -1);
     if (!empty($_POST)) {
         $id = isset($_POST["id"]) ? trim($_POST["id"]) : "";
         $album_move = isset($_POST["album_move"]) && is_numeric($_POST["album_move"]) ? intval($_POST["album_move"]) : -1;
         if (empty($id)) {
             header("Location: " . BASE_URL);
             return;
         }
         if ($album_move > 0) {
             $move_album = $albumDAO->load($album_move);
         }
         // Move photos to different category
         if (is_numeric($id) && $move_album) {
             $delete_album = $albumDAO->load($id);
             if ($delete_album) {
                 $album_photos = $photoDAO->loadByAlbum($delete_album);
                 if ($albumDAO->delete($delete_album)) {
                     if ($album_photos) {
                         $photoDAO->moveByAlbumId($delete_album, $move_album);
                     }
                     $session->setMessage("Album deleted and photos moved");
                     header("Location: " . BASE_URL);
                     return;
                 } else {
                     $session->setMessage("Could not delete album", Session::MESSAGE_ERROR);
                 }
             }
         } else {
             if (is_numeric($id) && $album_move == 0) {
                 $delete_album = $albumDAO->load($id);
                 if ($delete_album) {
                     $album_photos = $photoDAO->loadByAlbum($delete_album);
                     if ($albumDAO->delete($delete_album)) {
                         if ($album_photos) {
                             $photo_ids = array();
                             foreach ($album_photos as $photo) {
                                 $photo_ids[] = $photo->getId();
                             }
                             $photoDAO->deleteByIds($photo_ids);
                         }
                         $session->setMessage("Album and photos deleted");
                         header("Location: " . BASE_URL);
                         return;
                     } else {
                         $session->setMessage("Could not delete album", Session::MESSAGE_ERROR);
                     }
                 }
             } else {
                 if (is_numeric($id)) {
                     $delete_album = $albumDAO->load($id);
                     if ($delete_album) {
                         if ($albumDAO->delete($delete_album)) {
                             $session->setMessage("Album deleted");
                             header("Location: " . BASE_URL);
                             return;
                         } else {
                             $session->setMessage("Could not delete album", 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_album = $albumDAO->load($id);
                     if ($delete_album) {
                         $form_values["id"] = $delete_album->getId();
                         $album_array = $albumDAO->allExclude($delete_album);
                         //print_r ($album_array);
                         //print_r ($delete_album);
                     }
                 }
             }
         } else {
             header("Location: " . BASE_URL);
             return;
         }
     }
     $this->template->render(array("title" => "Admin - Delete Album", "main_page" => "delete_album_tpl.php", "user" => $user, "session" => $session, "delete_album" => $delete_album, "form_errors" => $form_errors, "form_values" => $form_values, "album_array" => $album_array));
 }
コード例 #7
0
 /**
  * Parse the options array for limit clauses and order by clauses. The valid keys and value types are specified below.
  * limit - Page object. Will take values from a Paginator Page object and
  * set LIMIT and OFFSET portions of database query accordingly
  *
  * joins - bool. If true, an INNER JOIN will be done to retrieve the
  * Album associated with the page
  * 
  * order - string. Concatenate string with ORDER BY operator.
  * Will add table name to field if only associated with current table.
  * @access private
  * @param array &$options
  */
 protected function parseOptions(&$options)
 {
     if (!is_array($options)) {
         throw new InvalidArgumentException("Options for a database access function must be in an array");
     }
     if (array_key_exists("limit", $options) && $options["limit"] instanceof Page) {
         $this->query_limit .= $this->getLimitClause($options["limit"]);
     }
     if (array_key_exists("joins", $options) && $options["joins"] == true) {
         $albumDAO = AlbumDAO::getInstance();
         $this->query_select .= ", " . $albumDAO->buildColumnString();
         $this->query_joins .= " INNER JOIN (" . $albumDAO->getTableName() . ") ON (" . $albumDAO->getTableName() . ".id = " . $this->getTableName() . ".albumId) ";
         $this->select_columns = array_merge($this->select_columns, $albumDAO->buildColumnArray());
         $this->joins = true;
     }
     if (array_key_exists("order", $options) && is_string($options["order"])) {
         // Reference to album member
         if (strpos($options["order"], ".") === false) {
             $this->query_order = "ORDER BY " . $this->tableName . "." . $options["order"];
         } else {
             if (strpos($options["order"], "albums.") === 0 && $this->joins) {
                 $this->query_order = "ORDER BY " . $options["order"];
             } else {
                 throw new InvalidArgumentException("Invalid configuration for order option");
             }
         }
     }
 }
コード例 #8
0
 /**
  * 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));
 }