Streamlined prepare process of photo data

This commit is contained in:
Tobias Reich 2015-03-12 00:11:16 +01:00
parent f35c146578
commit e8cc2e9192
4 changed files with 64 additions and 28 deletions

View File

@ -94,20 +94,14 @@ class Album extends Module {
$previousPhotoID = ''; $previousPhotoID = '';
while ($photo = $photos->fetch_assoc()) { while ($photo = $photos->fetch_assoc()) {
# Parse # Turn data from the database into a front-end friendly format
$photo['sysdate'] = date('d F Y', substr($photo['id'], 0, -4)); $photo = Photo::prepareData($photo);
# Set previous and next photoID for navigation purposes
$photo['previousPhoto'] = $previousPhotoID; $photo['previousPhoto'] = $previousPhotoID;
$photo['nextPhoto'] = ''; $photo['nextPhoto'] = '';
$photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
# Parse url
$photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
if (isset($photo['takestamp'])&&$photo['takestamp']!=='0') {
$photo['cameraDate'] = 1;
$photo['sysdate'] = date('d F Y', $photo['takestamp']);
}
# Set current photoID as nextPhoto of previous photo
if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id']; if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
$previousPhotoID = $photo['id']; $previousPhotoID = $photo['id'];
@ -183,7 +177,8 @@ class Album extends Module {
$album['password'] = ($album['password']=='' ? '0' : '1'); $album['password'] = ($album['password']=='' ? '0' : '1');
# Thumbs # Thumbs
if (($public===true&&$album['password']==='0')||($public===false)) { if (($public===true&&$album['password']==='0')||
($public===false)) {
# Execute query # Execute query
$query = Database::prepare($this->database, "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id'])); $query = Database::prepare($this->database, "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr($this->settings['sorting'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));

View File

@ -526,6 +526,46 @@ class Photo extends Module {
} }
public static function prepareData($data) {
# This function requires the following photo-attributes and turns them
# into a front-end friendly format: id, title, tags, public, star, album, thumbUrl, takestamp, url
# Note that some attributes remain unchanged
# Init
$photo = null;
# Set unchanged attribute
$photo['id'] = $data['id'];
$photo['title'] = $data['title'];
$photo['tags'] = $data['tags'];
$photo['public'] = $data['public'];
$photo['star'] = $data['star'];
$photo['album'] = $data['album'];
# Parse urls
$photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $data['thumbUrl'];
$photo['url'] = LYCHEE_URL_UPLOADS_BIG . $data['url'];
# Use takestamp as sysdate when possible
if (isset($data['takestamp'])&&$data['takestamp']!=='0') {
# Use takestamp
$photo['cameraDate'] = '1';
$photo['sysdate'] = date('d F Y', $data['takestamp']);
} else {
# Use sysstamp from the id
$photo['cameraDate'] = '0';
$photo['sysdate'] = date('d F Y', substr($data['id'], 0, -4));
}
return $photo;
}
public function get($albumID) { public function get($albumID) {
# Check dependencies # Check dependencies

View File

@ -21,12 +21,13 @@ function search($database, $settings, $term) {
); );
# Photos # Photos
$query = Database::prepare($database, "SELECT id, title, tags, public, star, album, thumbUrl FROM ? WHERE title LIKE '%?%' OR description LIKE '%?%' OR tags LIKE '%?%'", array(LYCHEE_TABLE_PHOTOS, $term, $term, $term)); $query = Database::prepare($database, "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));
$result = $database->query($query); $result = $database->query($query);
while($row = $result->fetch_assoc()) { while($photo = $result->fetch_assoc()) {
$return['photos'][$row['id']] = $row;
$return['photos'][$row['id']]['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $row['thumbUrl']; $photo = Photo::prepareData($photo);
$return['photos'][$row['id']]['sysdate'] = date('d M. Y', substr($row['id'], 0, -4)); $return['photos'][$photo['id']] = $photo;
} }
# Albums # Albums

View File

@ -124,7 +124,7 @@ build.photo = function(data) {
<h1 title='${ longTitle }'>${ title }</h1> <h1 title='${ longTitle }'>${ title }</h1>
` `
if (data.cameraDate===1) html += `<a><span title='Camera Date'>${ build.iconic('camera-slr') }</span>${ data.sysdate }</a>`; if (data.cameraDate==='1') html += `<a><span title='Camera Date'>${ build.iconic('camera-slr') }</span>${ data.sysdate }</a>`;
else html += `<a>${ data.sysdate }</a>`; else html += `<a>${ data.sysdate }</a>`;
html += '</div>'; html += '</div>';