Browse Source

Code simplifications #340

Tobias Reich 9 years ago
parent
commit
e67eca81ae
2 changed files with 38 additions and 30 deletions
  1. 3 1
      php/access/Admin.php
  2. 35 29
      php/modules/Album.php

+ 3 - 1
php/access/Admin.php

@@ -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

+ 35 - 29
php/modules/Album.php

@@ -676,7 +676,7 @@ class Album extends Module {
 
 	}
 
-	public function delete() {
+	public function merge() {
 
 		# Check dependencies
 		self::dependencies(isset($this->database, $this->albumIDs));
@@ -684,29 +684,30 @@ class Album extends Module {
 		# Call plugins
 		$this->plugins(__METHOD__, 0, func_get_args());
 
-		# Init vars
-		$error = false;
+		# Convert to array
+		$albumIDs = explode(',', $this->albumIDs);
 
-		# Execute query
-		$query	= Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
-		$photos = $this->database->query($query);
+		# Get first albumID
+		$albumID = array_splice($albumIDs, 0, 1)[0];
 
-		# For each album delete photo
-		while ($row = $photos->fetch_object()) {
-
-			$photo = new Photo($this->database, $this->plugins, null, $row->id);
-			if (!$photo->delete($row->id)) $error = true;
+		$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;
 		}
 
-		# Delete albums
-		$query	= Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
+		# $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 ($error) return false;
 		if (!$result) {
 			Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
 			return false;
@@ -715,7 +716,7 @@ class Album extends Module {
 
 	}
 
-	public function merge() {
+	public function delete() {
 
 		# Check dependencies
 		self::dependencies(isset($this->database, $this->albumIDs));
@@ -723,30 +724,35 @@ class Album extends Module {
 		# Call plugins
 		$this->plugins(__METHOD__, 0, func_get_args());
 
-		$albumIDs          = explode(',', $this->albumIDs);
-		$albumID           = array_splice($albumIDs, 0, 1)[0];
+		# Init vars
+		$error = false;
 
-		$inQuery = implode(',', array_fill(0, count($albumIDs), '?'));
-		$data    = array_merge(array(LYCHEE_TABLE_PHOTOS, $albumID), $albumIDs);
+		# Execute query
+		$query	= Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
+		$photos = $this->database->query($query);
 
-		$merge_query   = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN ($inQuery)", $data);
-		$merge_result  = $this->database->query($merge_query);
+		# For each album delete photo
+		while ($row = $photos->fetch_object()) {
+
+			$photo = new Photo($this->database, $this->plugins, null, $row->id);
+			if (!$photo->delete($row->id)) $error = true;
 
-		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);
+		# Delete albums
+		$query	= Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
+		$result	= $this->database->query($query);
 
-		if (!$delete_result) {
+		# Call plugins
+		$this->plugins(__METHOD__, 1, func_get_args());
+
+		if ($error) return false;
+		if (!$result) {
 			Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
 			return false;
 		}
-
 		return true;
+
 	}
 
 }