Gallery.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class Gallery extends Controller {
  3. public function Index(IAlbumRepository $repo) {
  4. $albums=$repo->GetAlbums();
  5. return new View("Gallery/index.view",array("albums"=>$albums));
  6. }
  7. public function View($url) {
  8. $album=new Album($url,true);
  9. Breadcrumbs::Add($album->AlbumTitle, "");
  10. return new View("Gallery/view.view",array("album"=>$album));
  11. }
  12. public function Manage(IAlbumRepository $albumRepo, IImageRepository $imageRepo, $errors) {
  13. $vars=array();
  14. Breadcrumbs::Add("Manage", "");
  15. $vars["albums"]=$albumRepo->GetAlbums(true,true,true);
  16. if (isset($errors))
  17. $vars["errors"]=$errors;
  18. $vars['images']=$imageRepo->GetImagesByAlbum(0);
  19. if(isset($image))
  20. $vars['image']=$image;
  21. else
  22. $vars['image']=new Image();
  23. return new View("Gallery/manage.view",$vars);
  24. }
  25. public function Move(IAlbumRepository $albumRepo, $selectedImages, $targetAlbumId){
  26. if ($selectedImages!=null){
  27. foreach ($selectedImages as $imgId){
  28. $image=new Image($imgId);
  29. $image->AlbumId=$targetAlbumId;
  30. $image->Save();
  31. }
  32. }
  33. $albums=$albumRepo->GetAlbums(true,true,true);
  34. return json_encode($albums);
  35. }
  36. public function DeleteImages(IAlbumRepository $albumRepo, IImageRepository $imageRepo, $selectedImages){
  37. $imageRepo->DeleteImages($selectedImages);
  38. $albums=$albumRepo->GetAlbums(true,true,true);
  39. return json_encode($albums);
  40. }
  41. public function CreateAlbum($title,$description) {
  42. Breadcrumbs::Add("Manage", "");
  43. Breadcrumbs::Add("Create Album", "");
  44. $album=new Album();
  45. if (isset($title))
  46. $album->AlbumTitle=$title;
  47. if (isset($description))
  48. $album->AlbumDescription=$description;
  49. if ($album->AlbumTitle!="" && $album->AlbumDescription!=""){
  50. $album->Save();
  51. header("location:/gallery/manage");
  52. return;
  53. }
  54. return new View("Gallery/create_album.view",array("album"=>$album));
  55. }
  56. public function Upload($imageTitle,$imageDesc) {
  57. if (!isset($_FILES['imageFile'])){
  58. header("location: /gallery/manage/");
  59. return;
  60. }
  61. $errors=array();
  62. $filename=$_FILES['imageFile']['name'];
  63. $tempFile=$_FILES['imageFile']['tmp_name'];
  64. if ($imageTitle=="")
  65. $errors[]="The image doesn't have a title";
  66. if ($_FILES['imageFile']['error'])
  67. $errors[]=Utils::FileUploadErrorToMessage($_FILES['imageFile']['error']);
  68. if (count($errors)==0) {
  69. if(!Image::IsValidType(pathinfo($filename,PATHINFO_EXTENSION)))
  70. $errors[]="File is of an invalid type";
  71. if (getimagesize($tempFile)===false)
  72. $errors[]="File is not an image";
  73. }
  74. $image=new Image($filename,$tempFile);
  75. $image->ImageTitle=$imageTitle;
  76. $image->ImageDescription=$imageDesc;
  77. if (count($errors)==0){
  78. $image->Save();
  79. header("location: /gallery/manage/");
  80. return;
  81. }
  82. return $this->Manage(array("errors"=>$errors,"image"=>$image));
  83. }
  84. public function JsonLoadAlbum($albumId) {
  85. $repo=new ImageRepository();
  86. $json='[';
  87. $images=$repo->GetImagesByAlbum($albumId);
  88. foreach ($images as $image)
  89. $json.='{"ImageId":"'.$image->ImageId.'","ImageTitle":"'.$image->ImageTitle.'","Path":"'.$image->Path.'","ThumbnailPath":"'.$image->ThumbnailPath.'"},';
  90. $json=trim($json,',');
  91. $json.=']';
  92. return $json;
  93. //return json_encode($images);
  94. }
  95. }