Code simplifications #340

This commit is contained in:
Tobias Reich 2015-05-05 22:06:54 +02:00
parent 9cd52b99c7
commit e67eca81ae
2 changed files with 43 additions and 35 deletions

View File

@ -22,7 +22,7 @@ class Admin extends Access {
case 'Album::setDescription': $this->setAlbumDescription(); break;
case 'Album::setPublic': $this->setAlbumPublic(); break;
case 'Album::delete': $this->deleteAlbum(); break;
case 'Album::merge': $this->mergeAlbums(); break;
case 'Album::merge': $this->mergeAlbums(); break;
# Photo functions
case 'Photo::get': $this->getPhoto(); break;
@ -125,9 +125,11 @@ class Admin extends Access {
}
private function mergeAlbums() {
Module::dependencies(isset($_POST['albumIDs']));
$album = new Album($this->database, $this->plugins, $this->settings, $_POST['albumIDs']);
echo $album->merge();
}
# Photo functions

View File

@ -676,6 +676,46 @@ class Album extends Module {
}
public function merge() {
# Check dependencies
self::dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
# Convert to array
$albumIDs = explode(',', $this->albumIDs);
# Get first albumID
$albumID = array_splice($albumIDs, 0, 1)[0];
$query = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
$result = $this->database->query($query);
if (!$result) {
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
return false;
}
# $albumIDs contains all IDs without the first albumID
# Convert to string
$filteredIDs = implode(',', $albumIDs);
$query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
$result = $this->database->query($query);
# Call plugins
$this->plugins(__METHOD__, 1, func_get_args());
if (!$result) {
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
return false;
}
return true;
}
public function delete() {
# Check dependencies
@ -715,40 +755,6 @@ class Album extends Module {
}
public function merge() {
# Check dependencies
self::dependencies(isset($this->database, $this->albumIDs));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
$albumIDs = explode(',', $this->albumIDs);
$albumID = array_splice($albumIDs, 0, 1)[0];
$inQuery = implode(',', array_fill(0, count($albumIDs), '?'));
$data = array_merge(array(LYCHEE_TABLE_PHOTOS, $albumID), $albumIDs);
$merge_query = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN ($inQuery)", $data);
$merge_result = $this->database->query($merge_query);
if (!$merge_result) {
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
return false;
}
$data = array_merge( array(LYCHEE_TABLE_ALBUMS), $albumIDs);
$delete_query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN ($inQuery)", $data);
$delete_result = $this->database->query($delete_query);
if (!$delete_result) {
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
return false;
}
return true;
}
}
?>