search.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. use Lychee\Modules\Album;
  3. use Lychee\Modules\Database;
  4. use Lychee\Modules\Photo;
  5. use Lychee\Modules\Settings;
  6. /**
  7. * @return array|false Returns an array with albums and photos.
  8. */
  9. function search($term) {
  10. // Initialize return var
  11. $return = array(
  12. 'photos' => null,
  13. 'albums' => null,
  14. 'hash' => ''
  15. );
  16. /**
  17. * Photos
  18. */
  19. $query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url, medium FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%' OR tags LIKE '%?%'", array(LYCHEE_TABLE_PHOTOS, $term, $term, $term));
  20. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  21. if ($result===false) return false;
  22. while($photo = $result->fetch_assoc()) {
  23. $photo = Photo::prepareData($photo);
  24. $return['photos'][$photo['id']] = $photo;
  25. }
  26. /**
  27. * Albums
  28. */
  29. $query = Database::prepare(Database::get(), "SELECT id, title, public, sysstamp, password FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%'", array(LYCHEE_TABLE_ALBUMS, $term, $term));
  30. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  31. if ($result===false) return false;
  32. while($album = $result->fetch_assoc()) {
  33. // Turn data from the database into a front-end friendly format
  34. $album = Album::prepareData($album);
  35. // Thumbs
  36. $query = Database::prepare(Database::get(), "SELECT thumbUrl FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'] . " LIMIT 0, 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
  37. $thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  38. if ($thumbs===false) return false;
  39. // For each thumb
  40. $k = 0;
  41. while ($thumb = $thumbs->fetch_object()) {
  42. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  43. $k++;
  44. }
  45. // Add to return
  46. $return['albums'][$album['id']] = $album;
  47. }
  48. // Hash
  49. $return['hash'] = md5(json_encode($return));
  50. return $return;
  51. }
  52. ?>