Self-hosted PHP photo gallery - your photos, your website, your property! Built on Laravel and Bootstrap 4, Blue Twilight is a private alternative to Flickr and Instagram. https://showmy.photos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

208 lines
6.3 KiB

  1. <?php
  2. namespace App\Http\Controllers\Gallery;
  3. use App\Album;
  4. use App\Facade\Theme;
  5. use App\Facade\UserConfig;
  6. use App\Helpers\DbHelper;
  7. use App\Http\Controllers\Controller;
  8. use App\User;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\App;
  11. use Illuminate\Support\Facades\DB;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class UserController extends Controller
  14. {
  15. public function settings(Request $request)
  16. {
  17. return Theme::render('gallery.user_settings', [
  18. 'user' => $this->getUser()
  19. ]);
  20. }
  21. public function show(Request $request, $idOrAlias)
  22. {
  23. // If a user has a profile alias set, their profile page cannot be accessed by the ID
  24. $user = User::where(DB::raw('COALESCE(profile_alias, id)'), strtolower($idOrAlias))->first();
  25. if (is_null($user))
  26. {
  27. App::abort(404);
  28. return null;
  29. }
  30. $this->authorizeForUser($this->getUser(), 'view', $user);
  31. $albums = $this->getAlbumsForUser($user);
  32. $albumIDs = $this->getAlbumIDsForUser($user);
  33. $cameras = $this->getCamerasUsedInAlbums($albumIDs);
  34. $activity = $this->getActivityDatesInAlbums($albumIDs);
  35. $daysInMonth = $this->getDaysInMonths();
  36. return Theme::render('gallery.user_profile', [
  37. 'activity_taken' => $this->constructActivityGrid($activity['taken']),
  38. 'activity_uploaded' => $this->constructActivityGrid($activity['uploaded']),
  39. 'albums' => $albums,
  40. 'cameras' => $cameras,
  41. 'month_days' => $daysInMonth,
  42. 'user' => $user
  43. ]);
  44. }
  45. private function constructActivityGrid(Collection $collection)
  46. {
  47. $results = [];
  48. $lastYearFrom = new \DateTime();
  49. $lastYearFrom->sub(new \DateInterval('P1Y'));
  50. $lastYearFrom->add(new \DateInterval('P1D'));
  51. $today = new \DateTime();
  52. $current = clone $lastYearFrom;
  53. while ($current < $today)
  54. {
  55. $year = intval($current->format('Y'));
  56. $month = intval($current->format('m'));
  57. $date = intval($current->format('d'));
  58. if (!isset($results[$year]))
  59. {
  60. $results[$year] = [];
  61. }
  62. if (!isset($results[$year][$month]))
  63. {
  64. $results[$year][$month] = [];
  65. }
  66. if (!isset($results[$year][$month][$date]))
  67. {
  68. $results[$year][$month][$date] = 0;
  69. }
  70. $current->add(new \DateInterval('P1D'));
  71. }
  72. // Now update the totals from the collection
  73. foreach ($collection as $photoInfo)
  74. {
  75. $date = \DateTime::createFromFormat('Y-m-d', $photoInfo->the_date);
  76. $year = intval($date->format('Y'));
  77. $month = intval($date->format('m'));
  78. $date = intval($date->format('d'));
  79. $results[$year][$month][$date] = $photoInfo->photos_count;
  80. }
  81. // Replace the month names
  82. foreach ($results as $year => &$months)
  83. {
  84. foreach ($months as $month => $dates)
  85. {
  86. $monthDate = \DateTime::createFromFormat('m', $month);
  87. $months[$monthDate->format('M')] = $dates;
  88. unset($months[$month]);
  89. }
  90. }
  91. return $results;
  92. }
  93. private function getActivityDatesInAlbums(array $albumIDs)
  94. {
  95. $createdAt = DB::table('photos')
  96. ->whereIn('album_id', $albumIDs)
  97. ->whereRaw(DB::raw('DATE(created_at) > DATE(DATE_SUB(NOW(), INTERVAL 1 year))'))
  98. ->select([
  99. DB::raw('DATE(created_at) AS the_date'),
  100. DB::raw('COUNT(photos.id) AS photos_count')
  101. ])
  102. ->groupBy(DB::raw('DATE(created_at)'))
  103. ->orderBy(DB::raw('DATE(created_at)'))
  104. ->get();
  105. $takenAt = DB::table('photos')
  106. ->whereIn('album_id', $albumIDs)
  107. ->whereRaw(DB::raw('DATE(taken_at) > DATE(DATE_SUB(NOW(), INTERVAL 1 year))'))
  108. ->select([
  109. DB::raw('DATE(taken_at) AS the_date'),
  110. DB::raw('COUNT(photos.id) AS photos_count')
  111. ])
  112. ->groupBy(DB::raw('DATE(taken_at)'))
  113. ->orderBy(DB::raw('DATE(taken_at)'))
  114. ->get();
  115. return ['uploaded' => $createdAt, 'taken' => $takenAt];
  116. }
  117. private function getAlbumsForUser(User $user)
  118. {
  119. return DbHelper::getAlbumsForCurrentUser_NonPaged()
  120. ->where('user_id', $user->id)
  121. ->paginate(UserConfig::get('items_per_page'));
  122. }
  123. private function getAlbumIDsForUser(User $user)
  124. {
  125. $results = [];
  126. $albums = DbHelper::getAlbumsForCurrentUser_NonPaged()
  127. ->where('user_id', $user->id)
  128. ->select('albums.id')
  129. ->get();
  130. foreach ($albums as $album)
  131. {
  132. $results[] = intval($album->id);
  133. }
  134. return $results;
  135. }
  136. private function getCamerasUsedInAlbums(array $albumIDs)
  137. {
  138. return DB::table('photos')
  139. ->whereIn('album_id', $albumIDs)
  140. ->where([
  141. ['camera_make', '!=', ''],
  142. ['camera_model', '!=', '']
  143. ])
  144. ->groupBy('camera_make', 'camera_model', 'camera_software')
  145. ->select('camera_make', 'camera_model', 'camera_software', DB::raw('count(*) as photo_count'))
  146. ->orderBy('photo_count', 'desc')
  147. ->orderBy('camera_make')
  148. ->orderBy('camera_model')
  149. ->orderBy('camera_software')
  150. ->get();
  151. }
  152. private function getDaysInMonths()
  153. {
  154. $results = [];
  155. $lastYearFrom = new \DateTime();
  156. $lastYearFrom->sub(new \DateInterval('P1Y'));
  157. $lastYearFrom->sub(new \DateInterval(sprintf('P%dD', $lastYearFrom->format('d') - 1)));
  158. $today = new \DateTime();
  159. $current = clone $lastYearFrom;
  160. while ($current < $today)
  161. {
  162. $year = intval($current->format('Y'));
  163. $month = intval($current->format('m'));
  164. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  165. $results[$year][$current->format('M')] = $daysInMonth;
  166. $current->add(new \DateInterval('P1M'));
  167. }
  168. return $results;
  169. }
  170. }