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 = '';
while ($photo = $photos->fetch_assoc()) {
# Parse
$photo['sysdate'] = date('d F Y', substr($photo['id'], 0, -4));
$photo['previousPhoto'] = $previousPhotoID;
$photo['nextPhoto'] = '';
$photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
# Turn data from the database into a front-end friendly format
$photo = Photo::prepareData($photo);
# 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 previous and next photoID for navigation purposes
$photo['previousPhoto'] = $previousPhotoID;
$photo['nextPhoto'] = '';
# Set current photoID as nextPhoto of previous photo
if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
$previousPhotoID = $photo['id'];
@ -183,18 +177,19 @@ class Album extends Module {
$album['password'] = ($album['password']=='' ? '0' : '1');
# Thumbs
if (($public===true&&$album['password']==='0')||($public===false)) {
if (($public===true&&$album['password']==='0')||
($public===false)) {
# 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']));
$thumbs = $this->database->query($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']));
$thumbs = $this->database->query($query);
# For each thumb
$k = 0;
while ($thumb = $thumbs->fetch_object()) {
$album["thumb$k"] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
$k++;
}
# For each thumb
$k = 0;
while ($thumb = $thumbs->fetch_object()) {
$album["thumb$k"] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
$k++;
}
}

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) {
# Check dependencies

View File

@ -21,12 +21,13 @@ function search($database, $settings, $term) {
);
# 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);
while($row = $result->fetch_assoc()) {
$return['photos'][$row['id']] = $row;
$return['photos'][$row['id']]['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $row['thumbUrl'];
$return['photos'][$row['id']]['sysdate'] = date('d M. Y', substr($row['id'], 0, -4));
while($photo = $result->fetch_assoc()) {
$photo = Photo::prepareData($photo);
$return['photos'][$photo['id']] = $photo;
}
# Albums

View File

@ -124,7 +124,7 @@ build.photo = function(data) {
<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>`;
html += '</div>';