AssetsService
- Type
- Class
- Namespace
- Craft
- Inherits
- Craft\AssetsService » Craft\BaseApplicationComponent » CApplicationComponent (opens new window) » CComponent (opens new window)
- Implements
IApplicationComponent- Since
- 1.0
- Deprecated in
- This class will have several breaking changes in Craft 3.0.
Class AssetsService
See also http://craftcms.com
View source (opens new window)
# Public Properties
| Property | Description |
|---|---|
| $behaviors (opens new window) | array (opens new window) – The behaviors that should be attached to this component. |
# Public Methods
| Method | Description |
|---|---|
| __call() (opens new window) | Calls the named method which is not a class method. |
| __get() (opens new window) | Returns a property value, an event handler list or a behavior based on its name. |
| __isset() (opens new window) | Checks if a property value is null. |
| __set() (opens new window) | Sets value of a component property. |
| __unset() (opens new window) | Sets a component property to be null. |
| asa() (opens new window) | Returns the named behavior object. |
| attachBehavior() (opens new window) | Attaches a behavior to this component. |
| attachBehaviors() (opens new window) | Attaches a list of behaviors to the component. |
| attachEventHandler() (opens new window) | Attaches an event handler to an event. |
| canGetProperty() (opens new window) | Determines whether a property can be read. |
| canSetProperty() (opens new window) | Determines whether a property can be set. |
| canUserPerformAction() | Return true if user has permission to perform the action on the folder. |
| checkPermissionByFileIds() | Check for a permission on a source by a file id or an array of file ids. |
| checkPermissionByFolderIds() | Check for a permission on a source by a folder id or an array of folder ids. |
| createFolder() | Create a folder by it's parent id and a folder name. |
| deleteFiles() | Delete a list of files by an array of ids (or a single id). |
| deleteFolderById() | Deletes a folder by its ID. |
| deleteFolderRecord() | Delete a folder record by id. |
| detachBehavior() (opens new window) | Detaches a behavior from the component. |
| detachBehaviors() (opens new window) | Detaches all behaviors from the component. |
| detachEventHandler() (opens new window) | Detaches an existing event handler. |
| disableBehavior() (opens new window) | Disables an attached behavior. |
| disableBehaviors() (opens new window) | Disables all behaviors attached to this component. |
| enableBehavior() (opens new window) | Enables an attached behavior. |
| enableBehaviors() (opens new window) | Enables all behaviors attached to this component. |
| evaluateExpression() (opens new window) | Evaluates a PHP expression or callback under the context of this component. |
| findFile() | Finds the first file that matches the given criteria. |
| findFolder() | Finds the first folder that matches a given criteria. |
| findFolders() | Finds folders that match a given criteria. |
| getAllDescendantFolders() | Returns all of the folders that are descendants of a given folder. |
| getEventHandlers() (opens new window) | Returns the list of attached event handlers for an event. |
| getFileById() | Returns a file by its ID. |
| getFilesBySourceId() | Returns all top-level files in a source. |
| getFolderById() | Returns a folder by its ID. |
| getFolderTreeByFolderId() | Get the folder tree for Assets by a folder id. |
| getFolderTreeBySourceIds() | Get the folder tree for Assets by source ids, optionally filtered by additional criteria |
| getIsInitialized() (opens new window) | Checks if this application component has been initialized. |
| getRootFolderBySourceId() | Returns the root folder for a given source ID. |
| getTotalFiles() | Gets the total number of files that match a given criteria. |
| getTotalFolders() | Gets the total number of folders that match a given criteria. |
| getUrlForFile() | Get URL for a file. |
| getUserFolder() | Get the users Folder model. |
| hasEvent() (opens new window) | Determines whether an event is defined. |
| hasEventHandler() (opens new window) | Checks whether the named event has attached handlers. |
| hasProperty() (opens new window) | Determines whether a property is defined. |
| init() (opens new window) | Initializes the application component. |
| insertFileByLocalPath() | Saves a file into an asset folder. |
| isInitialized() | Checks if this application component has been initialized yet, or not. |
| isMergeInProgress() | Returns true, if a file is in the process os being merged. |
| moveFiles() | Move or rename files. |
| moveFolder() | Move a folder. |
| onBeforeDeleteAsset() | Fires an 'onBeforeDeleteAsset' event. |
| onBeforeReplaceFile() | Fires an 'onBeforeReplaceFile' event. |
| onBeforeSaveAsset() | Fires an 'onBeforeSaveAsset' event. |
| onBeforeUploadAsset() | Fires an 'onBeforeUploadAsset' event. |
| onDeleteAsset() | Fires an 'onDeleteAsset' event. |
| onReplaceFile() | Fires an 'onReplaceFile' event. |
| onSaveAsset() | Fires an 'onSaveAsset' event. |
| onSaveFileContent() | Fires an 'onSaveFileContent' event. |
| raiseEvent() (opens new window) | Raises an event. |
| renameFile() | |
| renameFolder() | Rename a folder by it's folder and a new name. |
| storeFile() | Saves the record for an asset. |
| storeFolder() | Store a folder by model and return the id. |
| uploadFile() |
# canUserPerformAction()
Return true if user has permission to perform the action on the folder.
View source (opens new window)
Arguments
$folderId$action
Returns
Signature
public boolean canUserPerformAction ( $folderId, $action )
# checkPermissionByFileIds()
Check for a permission on a source by a file id or an array of file ids.
View source (opens new window)
Arguments
$fileIds$permission
Returns
Throws
Signature
public null checkPermissionByFileIds ( $fileIds, $permission )
# checkPermissionByFolderIds()
Check for a permission on a source by a folder id or an array of folder ids.
View source (opens new window)
Arguments
$folderIds$permission
Returns
Throws
Signature
public null checkPermissionByFolderIds ( $folderIds, $permission )
# createFolder()
Create a folder by it's parent id and a folder name.
View source (opens new window)
Arguments
$parentId$folderName
Returns
Signature
public Craft\AssetOperationResponseModel createFolder ( $parentId, $folderName )
# deleteFiles()
Delete a list of files by an array of ids (or a single id).
View source (opens new window)
Arguments
$fileIds(array (opens new window))$deleteFile(boolean (opens new window)) – Should the file be deleted along the record. Defaults to true.
Returns
Signature
public Craft\AssetOperationResponseModel deleteFiles ( $fileIds, $deleteFile = true )
# deleteFolderById()
Deletes a folder by its ID.
View source (opens new window)
Arguments
$folderId(integer (opens new window))
Returns
Craft\AssetOperationResponseModel
Throws
Signature
public Craft\AssetOperationResponseModel deleteFolderById ( $folderId )
# deleteFolderRecord()
Signature
public boolean deleteFolderRecord ( $folderId )
# findFile()
Finds the first file that matches the given criteria.
View source (opens new window)
Arguments
$criteria(mixed)
Returns
Signature
public Craft\AssetFileModel, null findFile ( $criteria = null )
# findFolder()
Finds the first folder that matches a given criteria.
View source (opens new window)
Arguments
$criteria(mixed)
Returns
Signature
public Craft\AssetFolderModel, null findFolder ( $criteria = null )
# findFolders()
Finds folders that match a given criteria.
View source (opens new window)
Arguments
$criteria(mixed)
Returns
Signature
public array findFolders ( $criteria = null )
# getAllDescendantFolders()
Returns all of the folders that are descendants of a given folder.
View source (opens new window)
Arguments
$parentFolder(Craft\AssetFolderModel)
Returns
Signature
public array getAllDescendantFolders ( Craft\AssetFolderModel $parentFolder )
# getFileById()
Returns a file by its ID.
View source (opens new window)
Arguments
$fileId$localeId(string (opens new window), null (opens new window))
Returns
Signature
public Craft\AssetFileModel, null getFileById ( $fileId, $localeId = null )
# getFilesBySourceId()
Returns all top-level files in a source.
View source (opens new window)
Arguments
$sourceId(integer (opens new window))$indexBy(string (opens new window), null (opens new window))
Returns
Signature
public array getFilesBySourceId ( $sourceId, $indexBy = null )
# getFolderById()
Returns a folder by its ID.
View source (opens new window)
Arguments
$folderId(integer (opens new window))
Returns
Signature
public Craft\AssetFolderModel, null getFolderById ( $folderId )
# getFolderTreeByFolderId()
Get the folder tree for Assets by a folder id.
View source (opens new window)
Arguments
$folderId
Returns
Signature
public array getFolderTreeByFolderId ( $folderId )
# getFolderTreeBySourceIds()
Get the folder tree for Assets by source ids, optionally filtered by additional criteria
View source (opens new window)
Arguments
$allowedSourceIds(array (opens new window)) – An array of allowed source ids$additionalCriteria(array (opens new window)) – An array of additional criteria
Returns
Signature
public array getFolderTreeBySourceIds ( array $allowedSourceIds, array $additionalCriteria = [] )
# getRootFolderBySourceId()
Returns the root folder for a given source ID.
View source (opens new window)
Arguments
$sourceId(integer (opens new window))
Returns
Signature
public Craft\AssetFolderModel, null getRootFolderBySourceId ( $sourceId )
# getTotalFiles()
Gets the total number of files that match a given criteria.
View source (opens new window)
Arguments
$criteria(mixed)
Returns
Signature
public integer getTotalFiles ( $criteria = null )
# getTotalFolders()
Gets the total number of folders that match a given criteria.
View source (opens new window)
Arguments
$criteria(mixed)
Returns
Signature
public integer getTotalFolders ( $criteria )
# getUrlForFile()
Get URL for a file.
View source (opens new window)
Arguments
$file(Craft\AssetFileModel)$transform(string (opens new window))
Returns
Signature
public string getUrlForFile ( Craft\AssetFileModel $file, $transform = null )
# getUserFolder()
Get the users Folder model.
View source (opens new window)
Arguments
$userModel(Craft\UserModel)
Returns
Craft\AssetFolderModel, null (opens new window)
Throws
Signature
public Craft\AssetFolderModel, null getUserFolder ( Craft\UserModel $userModel = null )
# insertFileByLocalPath()
Saves a file into an asset folder. This can be used to store newly-uploaded files:
$uploadedFile = UploadedFile::getInstanceByName('photo');
$folderId = 10;
$response = craft()->assets->insertFileByLocalPath(
$uploadedFile->tempName,
$uploadedFile->name,
$folderId,
AssetConflictResolution::KeepBoth
);
if ($response->isSuccess())
{
$fileId = $response->getDataItem('fileId');
// ...
}
View source (opens new window)
Arguments
$localPath(string (opens new window)) – The local path to the file.$fileName(string (opens new window)) – The name that the file should be given when saved in the asset folder.$folderId(integer (opens new window)) – The ID of the folder that the file should be saved into.$conflictResolution(string (opens new window)) – What action should be taken in the event of a filename conflict, if any (AssetConflictResolution::KeepBoth,AssetConflictResolution::Replace, or `AssetConflictResolution::Cancel).
Returns
Signature
public Craft\AssetOperationResponseModel insertFileByLocalPath ( $localPath, $fileName, $folderId, $conflictResolution = null )
# isMergeInProgress()
Signature
public boolean isMergeInProgress ( )
# moveFiles()
Move or rename files.
View source (opens new window)
Arguments
$fileIds$folderId$filename(string (opens new window)) – If this is a rename operation or not.$actions(array (opens new window)) – Actions to take in case of a conflict.
Returns
boolean (opens new window), Craft\AssetOperationResponseModel
Throws
Signature
public boolean, Craft\AssetOperationResponseModel moveFiles ( $fileIds, $folderId, $filename = '', $actions = [] )
# moveFolder()
Signature
public Craft\AssetOperationResponseModel moveFolder ( $folderId, $newParentId, $action )
# onBeforeDeleteAsset()
Fires an 'onBeforeDeleteAsset' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onBeforeDeleteAsset ( Craft\Event $event )
# onBeforeReplaceFile()
Fires an 'onBeforeReplaceFile' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onBeforeReplaceFile ( Craft\Event $event )
# onBeforeSaveAsset()
Fires an 'onBeforeSaveAsset' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onBeforeSaveAsset ( Craft\Event $event )
# onBeforeUploadAsset()
Fires an 'onBeforeUploadAsset' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onBeforeUploadAsset ( Craft\Event $event )
# onDeleteAsset()
Fires an 'onDeleteAsset' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onDeleteAsset ( Craft\Event $event )
# onReplaceFile()
Fires an 'onReplaceFile' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onReplaceFile ( Craft\Event $event )
# onSaveAsset()
Signature
public null onSaveAsset ( Craft\Event $event )
# onSaveFileContent()
DEPRECATED
Deprecated Deprecated in 2.0. Use {@link onSaveAsset() assets.onSaveAsset} instead.
Fires an 'onSaveFileContent' event.
View source (opens new window)
Arguments
$event(Craft\Event)
Returns
Signature
public null onSaveFileContent ( Craft\Event $event )
# renameFile()
View source (opens new window)
Arguments
$file(Craft\AssetFileModel)$filename(string (opens new window))$action(string (opens new window)) – The action to take in case of a conflict.
Returns
boolean (opens new window), Craft\AssetOperationResponseModel
Signature
public boolean, Craft\AssetOperationResponseModel renameFile ( Craft\AssetFileModel $file, $filename, $action = '' )
# renameFolder()
Rename a folder by it's folder and a new name.
View source (opens new window)
Arguments
$folderId$newName
Returns
Craft\AssetOperationResponseModel
Throws
Signature
public Craft\AssetOperationResponseModel renameFolder ( $folderId, $newName )
# storeFile()
Saves the record for an asset.
View source (opens new window)
Arguments
$file(Craft\AssetFileModel)
Returns
Throws
Signature
public boolean storeFile ( Craft\AssetFileModel $file )
# storeFolder()
Store a folder by model and return the id.
View source (opens new window)
Arguments
$folder(Craft\AssetFolderModel)
Returns
mixed
Signature
public mixed storeFolder ( Craft\AssetFolderModel $folder )
# uploadFile()
View source (opens new window)
Arguments
$folderId(integer (opens new window)) – The Id of the folder the file is being uploaded to.$userResponse(string (opens new window)) – User response regarding filename conflict.$theNewFileId(integer (opens new window)) – The new file ID that has triggered the conflict.$fileName(string (opens new window)) – The filename that is in the conflict.
Returns
Signature
public Craft\AssetOperationResponseModel uploadFile ( $folderId, $userResponse = '', $theNewFileId = 0, $fileName = '' )