search.php 1.9 KB

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