Gallery.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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", "/gallery/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(){
  57. Breadcrumbs::Add("Manage", "/gallery/manage/");
  58. Breadcrumbs::Add("Upload", "");
  59. return new View("Gallery/upload.view", array("maxUploadSize"=>Utils::GetMaxUploadSize()));
  60. }
  61. public function UploadImages($title, $description){
  62. if (count($title)==0)
  63. return;
  64. $allErrors=array();
  65. $uploadedImages=array();
  66. $files=$_FILES['files'];
  67. foreach ($files['tmp_name'] as $index=>$tempFile) {
  68. $filename=$files['name'][$index];
  69. $imageTitle=$title[$index];
  70. $errors=array();
  71. if ($imageTitle=="")
  72. $errors[]="The image $filename doesn't have a title";
  73. if ($files['error'][$index])
  74. $errors[]=Utils::FileUploadErrorToMessage($files['error'][$index]);
  75. if (count($errors)==0) {
  76. if(!Image::IsValidType(pathinfo($filename,PATHINFO_EXTENSION)))
  77. $errors[]="File is of an invalid type";
  78. if (getimagesize($tempFile)===false)
  79. $errors[]="File is not an image";
  80. }
  81. if (count($errors)>0){
  82. $allErrors=array_merge($allErrors, $errors);
  83. continue;
  84. }
  85. $uploadedImages[]=$index;
  86. $image=new Image($filename,$tempFile);
  87. $image->ImageTitle=$imageTitle;
  88. $image->ImageDescription=$description[$index];
  89. $image->Save();
  90. }
  91. return json_encode(array("errors"=>$allErrors, "uploaded"=>$uploadedImages));
  92. }
  93. }