Browse Source

Moved Album::getAll into a separated file

Tobias Reich 8 years ago
parent
commit
bad7819ff6
6 changed files with 213 additions and 185 deletions
  1. 9 4
      php/Access/Admin.php
  2. 9 4
      php/Access/Guest.php
  3. 0 173
      php/Modules/Album.php
  4. 191 0
      php/Modules/Albums.php
  5. 1 1
      src/scripts/albums.js
  6. 3 3
      src/scripts/contextMenu.js

+ 9 - 4
php/Access/Admin.php

@@ -3,6 +3,7 @@
 namespace Lychee\Access;
 
 use Lychee\Modules\Album;
+use Lychee\Modules\Albums;
 use Lychee\Modules\Import;
 use Lychee\Modules\Photo;
 use Lychee\Modules\Response;
@@ -16,8 +17,10 @@ final class Admin extends Access {
 
 		switch ($fn) {
 
+			// Albums functions
+			case 'Albums::get':             self::getAlbumsAction(); break;
+
 			// Album functions
-			case 'Album::getAll':           self::getAlbumsAction(); break;
 			case 'Album::get':              self::getAlbumAction(); break;
 			case 'Album::add':              self::addAlbumAction(); break;
 			case 'Album::setTitle':         self::setAlbumTitleAction(); break;
@@ -65,15 +68,17 @@ final class Admin extends Access {
 
 	}
 
-	// Album functions
+	// Albums functions
 
 	private static function getAlbumsAction() {
 
-		$album = new Album(null);
-		Response::json($album->getAll(false));
+		$albums = new Albums();
+		Response::json($albums->get(false));
 
 	}
 
+	// Album functions
+
 	private static function getAlbumAction() {
 
 		Validator::required(isset($_POST['albumID']), __METHOD__);

+ 9 - 4
php/Access/Guest.php

@@ -3,6 +3,7 @@
 namespace Lychee\Access;
 
 use Lychee\Modules\Album;
+use Lychee\Modules\Albums;
 use Lychee\Modules\Photo;
 use Lychee\Modules\Response;
 use Lychee\Modules\Session;
@@ -14,8 +15,10 @@ final class Guest extends Access {
 
 		switch ($fn) {
 
+			// Albums functions
+			case 'Albums::get':       self::getAlbumsAction(); break;
+
 			// Album functions
-			case 'Album::getAll':     self::getAlbumsAction(); break;
 			case 'Album::get':        self::getAlbumAction(); break;
 			case 'Album::getPublic':  self::checkAlbumAccessAction(); break;
 
@@ -37,15 +40,17 @@ final class Guest extends Access {
 
 	}
 
-	// Album functions
+	// Albums functions
 
 	private static function getAlbumsAction() {
 
-		$album = new Album(null);
-		Response::json($album->getAll(true));
+		$albums = new Albums();
+		Response::json($albums->get(true));
 
 	}
 
+	// Album functions
+
 	private static function getAlbumAction() {
 
 		Validator::required(isset($_POST['albumID'], $_POST['password']), __METHOD__);

+ 0 - 173
php/Modules/Album.php

@@ -180,179 +180,6 @@ final class Album {
 
 	}
 
-	/**
-	 * @return array|false Returns an array of albums or false on failure.
-	 */
-	public function getAll($public = true) {
-
-		// Call plugins
-		Plugins::get()->activate(__METHOD__, 0, func_get_args());
-
-		// Initialize return var
-		$return = array(
-			'smartalbums' => null,
-			'albums'      => null,
-			'num'         => 0
-		);
-
-		// Get SmartAlbums
-		if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
-
-		// 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));
-
-		// Execute query
-		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
-
-		if ($albums===false) return false;
-
-		// For each album
-		while ($album = $albums->fetch_assoc()) {
-
-			// Turn data from the database into a front-end friendly format
-			$album = Album::prepareData($album);
-
-			// 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) return false;
-
-					// For each thumb
-					$k = 0;
-					while ($thumb = $thumbs->fetch_object()) {
-						$album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
-						$k++;
-					}
-
-			}
-
-			// Add to return
-			$return['albums'][] = $album;
-
-		}
-
-		// Num of albums
-		$return['num'] = $albums->num_rows;
-
-		// Call plugins
-		Plugins::get()->activate(__METHOD__, 1, func_get_args());
-
-		return $return;
-
-	}
-
-	/**
-	 * @return array|false Returns an array of smart albums or false on failure.
-	 */
-	private function getSmartInfo() {
-
-		// Initialize return var
-		$return = array(
-			'unsorted' => null,
-			'public'   => null,
-			'starred'  => null,
-			'recent'   => null
-		);
-
-		/**
-		 * 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) return false;
-
-		$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) return false;
-
-		$return['starred'] = array(
-			'thumbs' => array(),
-			'num'    => $starred->num_rows
-		);
-
-		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) return false;
-
-		$return['public'] = array(
-			'thumbs' => array(),
-			'num'    => $public->num_rows
-		);
-
-		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) return false;
-
-		$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;
-
-	}
-
 	/**
 	 * Starts a download of an album.
 	 * @return resource|boolean Sends a ZIP-file or returns false on failure.

+ 191 - 0
php/Modules/Albums.php

@@ -0,0 +1,191 @@
+<?php
+
+namespace Lychee\Modules;
+
+final class Albums {
+
+	/**
+	 * @return boolean Returns true when successful.
+	 */
+	public function __construct() {
+
+		return true;
+
+	}
+
+	/**
+	 * @return array|false Returns an array of albums or false on failure.
+	 */
+	public function get($public = true) {
+
+		// Call plugins
+		Plugins::get()->activate(__METHOD__, 0, func_get_args());
+
+		// Initialize return var
+		$return = array(
+			'smartalbums' => null,
+			'albums'      => null,
+			'num'         => 0
+		);
+
+		// Get SmartAlbums
+		if ($public===false) $return['smartalbums'] = $this->getSmartAlbums();
+
+		// 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));
+
+		// Execute query
+		$albums = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
+
+		if ($albums===false) return false;
+
+		// For each album
+		while ($album = $albums->fetch_assoc()) {
+
+			// Turn data from the database into a front-end friendly format
+			$album = Album::prepareData($album);
+
+			// 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) return false;
+
+					// For each thumb
+					$k = 0;
+					while ($thumb = $thumbs->fetch_object()) {
+						$album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
+						$k++;
+					}
+
+			}
+
+			// Add to return
+			$return['albums'][] = $album;
+
+		}
+
+		// Num of albums
+		$return['num'] = $albums->num_rows;
+
+		// Call plugins
+		Plugins::get()->activate(__METHOD__, 1, func_get_args());
+
+		return $return;
+
+	}
+
+	/**
+	 * @return array|false Returns an array of smart albums or false on failure.
+	 */
+	private function getSmartAlbums() {
+
+		// Initialize return var
+		$return = array(
+			'unsorted' => null,
+			'public'   => null,
+			'starred'  => null,
+			'recent'   => null
+		);
+
+		/**
+		 * 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) return false;
+
+		$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) return false;
+
+		$return['starred'] = array(
+			'thumbs' => array(),
+			'num'    => $starred->num_rows
+		);
+
+		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) return false;
+
+		$return['public'] = array(
+			'thumbs' => array(),
+			'num'    => $public->num_rows
+		);
+
+		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) return false;
+
+		$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;
+
+	}
+
+}
+
+?>

+ 1 - 1
src/scripts/albums.js

@@ -17,7 +17,7 @@ albums.load = function() {
 
 	if (albums.json===null) {
 
-		api.post('Album::getAll', {}, function(data) {
+		api.post('Albums::get', {}, function(data) {
 
 			let waitTime = 0
 

+ 3 - 3
src/scripts/contextMenu.js

@@ -90,7 +90,7 @@ contextMenu.albumMulti = function(albumIDs, e) {
 
 contextMenu.albumTitle = function(albumID, e) {
 
-	api.post('Album::getAll', {}, function(data) {
+	api.post('Albums::get', {}, function(data) {
 
 		let items = []
 
@@ -121,7 +121,7 @@ contextMenu.albumTitle = function(albumID, e) {
 
 contextMenu.mergeAlbum = function(albumID, e) {
 
-	api.post('Album::getAll', {}, function(data) {
+	api.post('Albums::get', {}, function(data) {
 
 		let items = []
 
@@ -238,7 +238,7 @@ contextMenu.move = function(photoIDs, e) {
 
 	let items = []
 
-	api.post('Album::getAll', {}, function(data) {
+	api.post('Albums::get', {}, function(data) {
 
 		if (data.num===0) {