lychee/php/Modules/Album.php

751 lines
22 KiB
PHP
Raw Normal View History

2014-04-02 20:14:20 +00:00
<?php
namespace Lychee\Modules;
2014-04-02 20:14:20 +00:00
2016-01-30 22:24:08 +00:00
use ZipArchive;
final class Album {
2014-04-02 20:14:20 +00:00
2016-01-30 19:22:28 +00:00
private $albumIDs = null;
2014-04-02 20:14:20 +00:00
public function __construct($albumIDs) {
2014-04-02 20:14:20 +00:00
// Init vars
$this->albumIDs = $albumIDs;
2014-04-02 20:14:20 +00:00
return true;
}
public function add($title = 'Untitled') {
2014-04-02 20:14:20 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-02 20:14:20 +00:00
// Properties
$public = 0;
$visible = 1;
// Database
$sysstamp = time();
$query = Database::prepare(Database::get(), "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
2014-04-02 20:14:20 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-04-02 20:14:20 +00:00
if ($result===false) return false;
return Database::get()->insert_id;
2014-04-02 20:14:20 +00:00
}
public static function prepareData(array $data) {
// This function requires the following album-attributes and turns them
// into a front-end friendly format: id, title, public, sysstamp, password
// Note that some attributes remain unchanged
// Init
$album = null;
// Set unchanged attributes
$album['id'] = $data['id'];
$album['title'] = $data['title'];
$album['public'] = $data['public'];
// Additional attributes
// Only part of $album when available
if (isset($data['description'])) $album['description'] = $data['description'];
if (isset($data['visible'])) $album['visible'] = $data['visible'];
if (isset($data['downloadable'])) $album['downloadable'] = $data['downloadable'];
// Parse date
$album['sysdate'] = date('F Y', $data['sysstamp']);
// Parse password
$album['password'] = ($data['password']=='' ? '0' : '1');
// Parse thumbs or set default value
$album['thumbs'] = (isset($data['thumbs']) ? explode(',', $data['thumbs']) : array());
return $album;
}
public function get() {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
// Get album information
2014-04-27 12:40:54 +00:00
switch ($this->albumIDs) {
case 'f':
$return['public'] = '0';
$query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE star = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
break;
case 's':
$return['public'] = '0';
$query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE public = 1 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
break;
case 'r':
$return['public'] = '0';
$query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
break;
case '0':
$return['public'] = '0';
$query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = 0 " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
break;
default:
$query = Database::prepare(Database::get(), "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$return = $albums->fetch_assoc();
$return = Album::prepareData($return);
$query = Database::prepare(Database::get(), "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
break;
}
// Get photos
$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$previousPhotoID = '';
if ($photos===false) Response::error('Could not get photos of album from database!');
2014-04-27 12:40:54 +00:00
while ($photo = $photos->fetch_assoc()) {
// Turn data from the database into a front-end friendly format
$photo = Photo::prepareData($photo);
// 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'];
// Add to return
$return['content'][$photo['id']] = $photo;
}
if ($photos->num_rows===0) {
// Album empty
$return['content'] = false;
} else {
// Enable next and previous for the first and last photo
$lastElement = end($return['content']);
$lastElementId = $lastElement['id'];
$firstElement = reset($return['content']);
$firstElementId = $firstElement['id'];
if ($lastElementId!==$firstElementId) {
$return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
$return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
}
}
$return['id'] = $this->albumIDs;
$return['num'] = $photos->num_rows;
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
return $return;
}
public function getAll($public = true) {
2014-04-02 20:14:20 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-02 20:14:20 +00:00
// Initialize return var
2015-02-27 19:37:24 +00:00
$return = array(
'smartalbums' => null,
'albums' => null,
'num' => 0
2015-02-27 19:37:24 +00:00
);
// Get SmartAlbums
2015-02-27 19:37:24 +00:00
if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
2014-04-02 20:14:20 +00:00
// Albums query
if ($public===false) $query = Database::prepare(Database::get(), 'SELECT id, title, public, sysstamp, password FROM ? ' . Settings::get()['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
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));
2014-04-02 20:14:20 +00:00
// Execute query
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
2016-01-31 14:44:54 +00:00
if ($albums===false) Response::error('Could not get albums from database!');
2014-04-02 20:14:20 +00:00
// For each album
2014-04-02 20:14:20 +00:00
while ($album = $albums->fetch_assoc()) {
// Turn data from the database into a front-end friendly format
$album = Album::prepareData($album);
2014-04-02 20:14:20 +00:00
// Thumbs
if (($public===true&&$album['password']==='0')||
($public===false)) {
// Execute query
$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']));
$thumbs = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($thumbs===false) Response::error('Could not get thumbs of album from database!');
2016-01-31 14:44:54 +00:00
// For each thumb
$k = 0;
while ($thumb = $thumbs->fetch_object()) {
$album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
$k++;
}
2014-04-02 20:14:20 +00:00
}
// Add to return
$return['albums'][] = $album;
2014-04-02 20:14:20 +00:00
}
// Num of albums
2014-04-02 20:14:20 +00:00
$return['num'] = $albums->num_rows;
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-09-17 21:11:02 +00:00
2014-04-02 20:14:20 +00:00
return $return;
}
private function getSmartInfo() {
// Initialize return var
2015-02-27 19:37:24 +00:00
$return = array(
'unsorted' => null,
'public' => null,
'starred' => null,
'recent' => null
2015-02-27 19:37:24 +00:00
);
/**
* Unsorted
*/
$query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE album = 0 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
$unsorted = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$i = 0;
if ($unsorted===false) Response::error('Could not get unsorted photos from database!');
$return['unsorted'] = array(
'thumbs' => array(),
'num' => $unsorted->num_rows
);
while($row = $unsorted->fetch_object()) {
if ($i<3) {
$return['unsorted']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
$i++;
} else break;
}
/**
* Starred
*/
$query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE star = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
$starred = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$i = 0;
if ($starred===false) Response::error('Could not get starred photos from database!');
$return['starred'] = array(
'thumbs' => array(),
'num' => $starred->num_rows
);
2015-02-27 19:37:24 +00:00
while($row3 = $starred->fetch_object()) {
if ($i<3) {
$return['starred']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
$i++;
} else break;
}
/**
* Public
*/
$query = Database::prepare(Database::get(), 'SELECT thumbUrl FROM ? WHERE public = 1 ' . Settings::get()['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
$public = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$i = 0;
if ($public===false) Response::error('Could not get public photos from database!');
$return['public'] = array(
'thumbs' => array(),
'num' => $public->num_rows
);
2015-02-27 19:37:24 +00:00
while($row2 = $public->fetch_object()) {
if ($i<3) {
$return['public']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
$i++;
} else break;
}
/**
* Recent
*/
$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));
$recent = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
$i = 0;
if ($recent===false) Response::error('Could not get recent photos from database!');
$return['recent'] = array(
'thumbs' => array(),
'num' => $recent->num_rows
);
while($row3 = $recent->fetch_object()) {
if ($i<3) {
$return['recent']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
$i++;
} else break;
}
// Return SmartAlbums
return $return;
}
public function getArchive() {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
// Illicit chars
$badChars = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
);
// Photos query
switch($this->albumIDs) {
case 's':
$photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
$zipTitle = 'Public';
break;
case 'f':
$photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
$zipTitle = 'Starred';
break;
case 'r':
$photos = Database::prepare(Database::get(), 'SELECT title, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY checksum', array(LYCHEE_TABLE_PHOTOS));
$zipTitle = 'Recent';
break;
default:
$photos = Database::prepare(Database::get(), "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
$zipTitle = 'Unsorted';
}
// Get title from database when album is not a SmartAlbum
2014-08-29 17:27:09 +00:00
if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
$query = Database::prepare(Database::get(), "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$album = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($album===false) Response::error('Could not get album from database!');
// Get album object
$album = $album->fetch_object();
// Photo not found
if ($album===null) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not find specified album');
Response::error('Could not find specified album!');
}
// Set title
$zipTitle = $album->title;
2014-08-29 17:27:09 +00:00
}
// Escape title
$zipTitle = str_replace($badChars, '', $zipTitle);
2014-04-13 12:08:18 +00:00
$filename = LYCHEE_DATA . $zipTitle . '.zip';
// Create zip
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive');
return false;
}
// Execute query
$photos = Database::execute(Database::get(), $photos, __METHOD__, __LINE__);
if ($album===null) Response::error('Could not get photos from database!');
// Check if album empty
if ($photos->num_rows==0) {
Log::error(Database::get(), __METHOD__, __LINE__, 'Could not create ZipArchive without images');
Response::error('Could not create ZipArchive without images!');
}
2014-04-12 16:00:11 +00:00
// Parse each path
$files = array();
while ($photo = $photos->fetch_object()) {
// Parse url
2014-04-13 12:08:18 +00:00
$photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
// Parse title
$photo->title = str_replace($badChars, '', $photo->title);
if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
2014-04-12 16:00:11 +00:00
// Check if readable
if (!@is_readable($photo->url)) continue;
// Get extension of image
2014-05-20 10:44:08 +00:00
$extension = getExtension($photo->url);
// Set title for photo
$zipFileName = $zipTitle . '/' . $photo->title . $extension;
// Check for duplicates
if (!empty($files)) {
$i = 1;
while (in_array($zipFileName, $files)) {
// Set new title for photo
2014-05-20 10:44:08 +00:00
$zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
$i++;
}
}
2014-04-12 16:00:11 +00:00
// Add to array
$files[] = $zipFileName;
2014-04-12 16:00:11 +00:00
// Add photo to zip
$zip->addFile($photo->url, $zipFileName);
2014-04-12 16:00:11 +00:00
}
// Finish zip
$zip->close();
// Send zip
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
2014-04-26 17:37:02 +00:00
header("Content-Length: " . filesize($filename));
readfile($filename);
// Delete zip
unlink($filename);
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
return true;
}
2014-04-03 16:38:58 +00:00
public function setTitle($title = 'Untitled') {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-04-03 16:38:58 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-03 16:38:58 +00:00
// Execute query
$query = Database::prepare(Database::get(), "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
2014-04-03 16:38:58 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-04-03 16:38:58 +00:00
if ($result===false) return false;
2014-04-03 16:38:58 +00:00
return true;
}
public function setDescription($description = '') {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-04-03 16:38:58 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-03 16:38:58 +00:00
// Execute query
$query = Database::prepare(Database::get(), "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
2014-04-03 16:38:58 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-04-03 16:38:58 +00:00
if ($result===false) return false;
2014-04-03 16:38:58 +00:00
return true;
}
public function getPublic() {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
// Execute query
$query = Database::prepare(Database::get(), "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($albums===false) return false;
// Get album object
$album = $albums->fetch_object();
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
if ($album->public==1) return true;
return false;
}
2014-08-17 18:22:46 +00:00
public function getDownloadable() {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-08-17 18:22:46 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-08-17 18:22:46 +00:00
2014-08-29 17:27:09 +00:00
if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
2014-08-17 18:22:46 +00:00
// Execute query
$query = Database::prepare(Database::get(), "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($albums===false) return false;
// Get album object
$album = $albums->fetch_object();
2014-08-17 18:22:46 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-08-17 18:22:46 +00:00
if ($album->downloadable==1) return true;
return false;
}
public function setPublic($public, $password, $visible, $downloadable) {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
// Convert values
$public = ($public==='1' ? 1 : 0);
$visible = ($visible==='1' ? 1 : 0);
$downloadable = ($downloadable==='1' ? 1 : 0);
// Set public
$query = Database::prepare(Database::get(), "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($result===false) return false;
// Reset permissions for photos
if ($public===1) {
$query = Database::prepare(Database::get(), "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($result===false) return false;
}
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
// Set password
if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
return true;
}
private function setPassword($password) {
2014-04-03 16:38:58 +00:00
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-04-03 16:38:58 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-03 16:38:58 +00:00
2014-04-21 12:18:13 +00:00
if (strlen($password)>0) {
// Get hashed password
$password = getHashedString($password);
// Set hashed password
// Do not prepare $password because it is hashed and save
// Preparing (escaping) the password would destroy the hash
$query = Database::prepare(Database::get(), "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
2014-04-21 12:18:13 +00:00
} else {
2014-04-21 12:18:13 +00:00
// Unset password
$query = Database::prepare(Database::get(), "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
2014-04-21 12:18:13 +00:00
}
2014-04-03 16:38:58 +00:00
// Execute query
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-04-03 16:38:58 +00:00
if ($result===false) return false;
2014-04-03 16:38:58 +00:00
return true;
}
public function checkPassword($password) {
2014-04-04 15:18:59 +00:00
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-04-04 15:18:59 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-04 15:18:59 +00:00
// Execute query
$query = Database::prepare(Database::get(), "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($albums===false) return false;
// Get album object
$album = $albums->fetch_object();
2014-04-04 15:18:59 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2014-04-04 15:18:59 +00:00
// Check if password is correct
if ($album->password=='') return true;
if ($album->password===crypt($password, $album->password)) return true;
return false;
2014-04-04 15:18:59 +00:00
}
2014-04-04 15:18:59 +00:00
2015-05-05 20:06:54 +00:00
public function merge() {
2014-04-04 15:18:59 +00:00
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
2014-04-04 15:18:59 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
2014-04-04 15:18:59 +00:00
// Convert to array
2015-05-05 20:06:54 +00:00
$albumIDs = explode(',', $this->albumIDs);
2014-04-04 15:18:59 +00:00
// Get first albumID
$albumID = array_splice($albumIDs, 0, 1);
$albumID = $albumID[0];
$query = Database::prepare(Database::get(), "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($result===false) return false;
// $albumIDs contains all IDs without the first albumID
// Convert to string
2015-05-05 20:06:54 +00:00
$filteredIDs = implode(',', $albumIDs);
$query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
2014-04-04 15:18:59 +00:00
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
if ($result===false) return false;
2014-04-04 15:18:59 +00:00
return true;
}
2015-05-05 20:06:54 +00:00
public function delete() {
// Check dependencies
Validator::required(isset($this->albumIDs), __METHOD__);
// Call plugins
Plugins::get()->activate(__METHOD__, 0, func_get_args());
// Init vars
$photoIDs = array();
// Execute query
$query = Database::prepare(Database::get(), "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
$photos = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
if ($photos===false) return false;
2015-05-05 20:06:54 +00:00
// Only delete photos when albums contain photos
if ($photos->num_rows>0) {
// Add each id to photoIDs
while ($row = $photos->fetch_object()) $photoIDs[] = $row->id;
// Convert photoIDs to a string
$photoIDs = implode(',', $photoIDs);
// Delete all photos
$photo = new Photo($photoIDs);
if ($photo->delete()!==true) return false;
}
// Delete albums
$query = Database::prepare(Database::get(), "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
// Call plugins
Plugins::get()->activate(__METHOD__, 1, func_get_args());
2015-05-05 20:06:54 +00:00
if ($result===false) return false;
return true;
2015-05-05 20:06:54 +00:00
}
2014-04-28 08:17:26 +00:00
}
2016-01-31 14:53:44 +00:00
?>