Albums.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Albums {
  4. /**
  5. * @return boolean Returns true when successful.
  6. */
  7. public function __construct() {
  8. return true;
  9. }
  10. /**
  11. * @return array|false Returns an array of albums or false on failure.
  12. */
  13. public function get($public = true) {
  14. // Call plugins
  15. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  16. // Initialize return var
  17. $return = array(
  18. 'smartalbums' => null,
  19. 'albums' => null,
  20. 'num' => 0
  21. );
  22. // Get SmartAlbums
  23. if ($public===false) $return['smartalbums'] = $this->getSmartAlbums();
  24. // Albums query
  25. if ($public===false) $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  26. else $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0 ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  27. // Execute query
  28. $albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  29. if ($albums===false) return false;
  30. // For each album
  31. while ($album = $albums->fetch_assoc()) {
  32. // Turn data from the database into a front-end friendly format
  33. $album = Album::prepareData($album);
  34. // Thumbs
  35. if (($public===true&&$album['password']==='0')||
  36. ($public===false)) {
  37. // Execute query
  38. $query = Database::prepare(Database::get(), "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr(Settings::get()['sortingPhotos'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
  39. $thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  40. if ($thumbs===false) return false;
  41. // For each thumb
  42. $k = 0;
  43. while ($thumb = $thumbs->fetch_object()) {
  44. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  45. $k++;
  46. }
  47. }
  48. // Add to return
  49. $return['albums'][] = $album;
  50. }
  51. // Num of albums
  52. $return['num'] = $albums->num_rows;
  53. // Call plugins
  54. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  55. return $return;
  56. }
  57. /**
  58. * @return array|false Returns an array of smart albums or false on failure.
  59. */
  60. private function getSmartAlbums() {
  61. // Initialize return var
  62. $return = array(
  63. 'unsorted' => null,
  64. 'public' => null,
  65. 'starred' => null,
  66. 'recent' => null
  67. );
  68. /**
  69. * Unsorted
  70. */
  71. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE album = 0 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  72. $unsorted = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  73. $i = 0;
  74. if ($unsorted===false) return false;
  75. $return['unsorted'] = array(
  76. 'thumbs' => array(),
  77. 'num' => $unsorted->num_rows
  78. );
  79. while($row = $unsorted->fetch_object()) {
  80. if ($i<3) {
  81. $return['unsorted']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
  82. $i++;
  83. } else break;
  84. }
  85. /**
  86. * Starred
  87. */
  88. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE star = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  89. $starred = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  90. $i = 0;
  91. if ($starred===false) return false;
  92. $return['starred'] = array(
  93. 'thumbs' => array(),
  94. 'num' => $starred->num_rows
  95. );
  96. while($row3 = $starred->fetch_object()) {
  97. if ($i<3) {
  98. $return['starred']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  99. $i++;
  100. } else break;
  101. }
  102. /**
  103. * Public
  104. */
  105. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE public = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  106. $public = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  107. $i = 0;
  108. if ($public===false) return false;
  109. $return['public'] = array(
  110. 'thumbs' => array(),
  111. 'num' => $public->num_rows
  112. );
  113. while($row2 = $public->fetch_object()) {
  114. if ($i<3) {
  115. $return['public']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
  116. $i++;
  117. } else break;
  118. }
  119. /**
  120. * Recent
  121. */
  122. $query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  123. $recent = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  124. $i = 0;
  125. if ($recent===false) return false;
  126. $return['recent'] = array(
  127. 'thumbs' => array(),
  128. 'num' => $recent->num_rows
  129. );
  130. while($row3 = $recent->fetch_object()) {
  131. if ($i<3) {
  132. $return['recent']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  133. $i++;
  134. } else break;
  135. }
  136. // Return SmartAlbums
  137. return $return;
  138. }
  139. }
  140. ?>