📄 galleryresources.class.php
字号:
<?php /** * \defgroup Gallery * * The pLog Gallery module encapsulates all the logic necessary for: * * - Dealing with files and their places in disk * - Dealing with albums, which are virtual groups of disks * - Automatic generation of thumbnails and medium-sized thumbnails, according to our configuration * - Automatic extraction of metadata from a set of supported formats. This is achieved * via the getID3 library. */ lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresource.class.php" ); lt_include( PLOG_CLASS_PATH."class/gallery/galleryconstants.php" ); lt_include( PLOG_CLASS_PATH.'class/dao/daocacheconstants.properties.php' ); /** * \ingroup Gallery * * database access for GalleryResource objects. Provides methods for adding, retrieving, updating and removing * resources from the database * * @see Model * @see GalleryResource */ class GalleryResources extends Model { var $albums; /** * maps extensions to resource types */ var $_extensionToType = Array( "jpg" => GALLERY_RESOURCE_IMAGE, "jpeg" => GALLERY_RESOURCE_IMAGE, "png" => GALLERY_RESOURCE_IMAGE, "gif" => GALLERY_RESOURCE_IMAGE, "bmp" => GALLERY_RESOURCE_IMAGE, "mp3" => GALLERY_RESOURCE_SOUND, "mp2" => GALLERY_RESOURCE_SOUND, "wav" => GALLERY_RESOURCE_SOUND, "au" => GALLERY_RESOURCE_SOUND, "aac" => GALLERY_RESOURCE_SOUND, "mp4" => GALLERY_RESOURCE_SOUND, "wma" => GALLERY_RESOURCE_SOUND, "ogg" => GALLERY_RESOURCE_SOUND, "mod" => GALLERY_RESOURCE_SOUND, "mid" => GALLERY_RESOURCE_SOUND, "midi" => GALLERY_RESOURCE_SOUND, "avi" => GALLERY_RESOURCE_VIDEO, "mpg" => GALLERY_RESOURCE_VIDEO, "mpeg" => GALLERY_RESOURCE_VIDEO, "wmv" => GALLERY_RESOURCE_VIDEO, //"asf" => GALLERY_RESOURCE_VIDEO, "mov" => GALLERY_RESOURCE_VIDEO, "divx" => GALLERY_RESOURCE_VIDEO, "rm" => GALLERY_RESOURCE_VIDEO, "swf" => GALLERY_RESOURCE_VIDEO, "qt" => GALLERY_RESOURCE_VIDEO, "pdf" => GALLERY_RESOURCE_DOCUMENT, "zip" => GALLERY_RESOURCE_ZIP ); function GalleryResources() { $this->Model(); $this->table = $this->getPrefix()."gallery_resources"; } /** * Fetches GalleryResource objects from the database * * @param resourceId The id of the resource we'd like to fetch * @param ownerId Optional, the id of the owner * @param albumId Optional, the id of the album to which this resoruce should belong * @return Returns a GalleryResource object representing the resource */ function getResource( $resourceId, $ownerId = -1, $albumId = -1 ) { $resource = $this->get( "id", $resourceId, CACHE_RESOURCES ); if( !$resource ) return false; if( !$this->check( $resource, $ownerId, $albumId )) return false; return $resource; } /** * given a resource id, tries to find the next one in the sequence * * @param resource A GalleryResource object that represents the resource whose next * object we'd like to load * @return Returns a GalleryResource object representing the next resource, or false * if there was no next resource */ function getNextResource( $resource ) { $prefix = $this->getPrefix(); $albumId = $resource->getAlbumId(); $date = $resource->getDate(); $id = $resource->getId(); $query = "SELECT id, owner_id, album_id, description, date, flags, resource_type, file_path, file_name, metadata, thumbnail_format, properties, file_size FROM {$prefix}gallery_resources WHERE album_id = '$albumId' AND date >= '$date' AND id > $id ORDER BY date ASC,id ASC LIMIT 1"; $result = $this->Execute( $query ); if( !$result ) return false; if( $result->RecordCount() == 0 ){ $result->Close(); return false; } $row = $result->FetchRow(); $result->Close(); $resource = $this->mapRow( $row ); $this->_cache->setData( $resource->getId(), CACHE_RESOURCES, $resource ); $this->_cache->setData( $resource->getFileName(), CACHE_RESOURCES_BY_NAME, $resource ); return $resource; } /** * given a resource id, tries to find the previus one in the sequence * * @param resource A GalleryResource object that represents the resource whose next * object we'd like to load * @return Returns a GalleryResource object representing the previous resource, or false * if there was no previous resource */ function getPreviousResource( $resource ) { $prefix = $this->getPrefix(); $albumId = $resource->getAlbumId(); $date = $resource->getDate(); $id = $resource->getId(); $query = "SELECT id, owner_id, album_id, description, date, flags, resource_type, file_path, file_name, metadata, thumbnail_format, properties, file_size FROM {$prefix}gallery_resources WHERE album_id = '$albumId' AND date <= '$date' AND id < $id ORDER BY date DESC,id DESC LIMIT 1"; $result = $this->Execute( $query ); if( !$result ) return false; if( $result->RecordCount() == 0 ){ $result->Close(); return false; } $row = $result->FetchRow(); $result->Close(); $resource = $this->mapRow( $row ); $this->_cache->setData( $resource->getId(), CACHE_RESOURCES, $resource ); $this->_cache->setData( $resource->getFileName(), CACHE_RESOURCES_BY_NAME, $resource ); return $resource; } /** * Returns all the resources that belong to a blog * * @param blogId The blog to which the resources belong, use -1 to indicate any blog/owner * @param albumId Filters by album * @param page * @param itemsPerPage * @param searchTerms * @return Returns an array of GalleryResource objects with all * the resources that match the given conditions, or empty * if none could be found. */ function getUserResources( $ownerId, $albumId = GALLERY_NO_ALBUM, $resourceType = GALLERY_RESOURCE_ANY, $searchTerms = "", $page = DEFAULT_PAGING_ENABLED, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) { $resources = Array(); $query = "SELECT id FROM ".$this->getPrefix()."gallery_resources WHERE "; if( $ownerId != -1 ) $query .= " owner_id = '".Db::qstr($ownerId)."' AND"; if( $albumId != GALLERY_NO_ALBUM ) $query .= " album_id = '".Db::qstr($albumId)."' AND"; if( $resourceType != GALLERY_RESOURCE_ANY ) $query .= " resource_type = '".Db::qstr($resourceType)."' AND"; if( $searchTerms != "" ) $query .= " (".$this->getSearchConditions( $searchTerms ).")"; // just in case if for any reason the string ends with "AND" $query = trim( $query ); if( substr( $query, -3, 3 ) == "AND" ) $query = substr( $query, 0, strlen( $query ) - 3 ); $result = $this->Execute( $query, $page, $itemsPerPage ); if( !$result ) return $resources; while( $row = $result->FetchRow()) { // use the primary key to retrieve the items via the cache $resources[] = $resource = $this->get( "id", $row["id"], CACHE_RESOURCES ); } return $resources; } /** * @private */ function check( $resource, $ownerId = -1, $albumId = GALLERY_NO_ALBUM, $resourceType = GALLERY_RESOURCE_ANY ) { if( $ownerId != -1 && $ownerId != '_all_' ) { if( $resource->getOwnerId() != $ownerId ) { return false; } } if( $albumId != GALLERY_NO_ALBUM ) { if( $resource->getAlbumId() != $albumId ) { return false; } } if( $resourceType != GALLERY_RESOURCE_ANY ) { if( $resource->getResourceType() != $resourceType ) return false; } return( true ); } /** * returns the number of items given certain conditions * * @param ownerId The id of the user whose amount of albums we'd like to check * @param albumId Optional, the id of the album, in case we'd only like to know the number of resources in a certain album. * use the constant GALLERY_NO_ALBUM to disable this parameter * @param resourceType An additional filter parameter, so that we can only count a certain type of resources. * Defaults to the constant GALLERY_RESOURCE_ANY * @param searchTerms * @see getUserResources * @return the total number of items */ function getNumUserResources( $ownerId, $albumId = GALLERY_NO_ALBUM, $resourceType = GALLERY_RESOURCE_ANY, $searchTerms = "" ) { $prefix = $this->getPrefix(); $table = "{$prefix}gallery_resources"; $cond = ""; if( $ownerId != -1 ) $cond = "owner_id = '".Db::qstr( $ownerId )."'"; else $cond = "owner_id = owner_id"; if( $albumId > GALLERY_NO_ALBUM ) $cond .= "AND album_id = '".Db::qstr($albumId)."'"; if( $resourceType > GALLERY_RESOURCE_ANY ) $cond .= " AND resource_type = '".Db::qstr($resourceType)."'"; if( $searchTerms != "" ) { $searchParams = $this->getSearchConditions( $searchTerms ); $cond .= " AND (".$searchParams.")"; } // return the number of items return( $this->getNumItems( $table, $cond )); } /** * Adds a row related to a resource to the database. You should usually use * GalleryResources::addResource() or GalleryResources::addResourceFromDisk(), which are more * suitable and will do most of the job for you. * * @param ownerId * @param albumId * @param description * @param flags * @param resourceType * @param filePath * @param fileName * @param metadata * @return a valid resource id or false otherwise * @see addResource */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -