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

boolean (opens new window)

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

null (opens new window)

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

null (opens new window)

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

Craft\AssetOperationResponseModel

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

Returns

Craft\AssetOperationResponseModel

Signature

public Craft\AssetOperationResponseModel deleteFiles ( $fileIds, $deleteFile = true )

# deleteFolderById()

Signature

public Craft\AssetOperationResponseModel deleteFolderById ( $folderId )

# deleteFolderRecord()

Delete a folder record by id.

View source (opens new window)

Arguments

  • $folderId

Returns

boolean (opens new window)

Signature

public boolean deleteFolderRecord ( $folderId )

# findFile()

Finds the first file that matches the given criteria.

View source (opens new window)

Arguments

  • $criteria (mixed)

Returns

Craft\AssetFileModel, null (opens new window)

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

Craft\AssetFolderModel, null (opens new window)

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

array (opens new window)

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

Returns

array (opens new window)

Signature

public array getAllDescendantFolders ( Craft\AssetFolderModel $parentFolder )

# getFileById()

Signature

public Craft\AssetFileModel, null getFileById ( $fileId, $localeId = null )

# getFilesBySourceId()

Returns all top-level files in a source.

View source (opens new window)

Arguments

Returns

array (opens new window)

Signature

public array getFilesBySourceId ( $sourceId, $indexBy = null )

# getFolderById()

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

array (opens new window)

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

Returns

array (opens new window)

Signature

public array getFolderTreeBySourceIds ( array $allowedSourceIds, array $additionalCriteria = [] )

# getRootFolderBySourceId()

Returns the root folder for a given source ID.

View source (opens new window)

Arguments

Returns

Craft\AssetFolderModel, null (opens new window)

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

integer (opens new window)

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

integer (opens new window)

Signature

public integer getTotalFolders ( $criteria )

# getUrlForFile()

Signature

public string getUrlForFile ( Craft\AssetFileModel $file, $transform = null )

# getUserFolder()

Get the users Folder model.

View source (opens new window)

Arguments

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

Craft\AssetOperationResponseModel

Signature

public Craft\AssetOperationResponseModel insertFileByLocalPath ( $localPath, $fileName, $folderId, $conflictResolution = null )

# isMergeInProgress()

Returns true, if a file is in the process os being merged.

View source (opens new window)

Returns

boolean (opens new window)

Signature

public boolean isMergeInProgress ( )

# moveFiles()

Move or rename files.

View source (opens new window)

Arguments

Returns

boolean (opens new window), Craft\AssetOperationResponseModel

Throws

Signature

public boolean, Craft\AssetOperationResponseModel moveFiles ( $fileIds, $folderId, $filename = '', $actions = [] )

# moveFolder()

Move a folder.

View source (opens new window)

Arguments

  • $folderId
  • $newParentId
  • $action

Returns

Craft\AssetOperationResponseModel

Signature

public Craft\AssetOperationResponseModel moveFolder ( $folderId, $newParentId, $action )

# onBeforeDeleteAsset()

Fires an 'onBeforeDeleteAsset' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onBeforeDeleteAsset ( Craft\Event $event )

# onBeforeReplaceFile()

Fires an 'onBeforeReplaceFile' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onBeforeReplaceFile ( Craft\Event $event )

# onBeforeSaveAsset()

Fires an 'onBeforeSaveAsset' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onBeforeSaveAsset ( Craft\Event $event )

# onBeforeUploadAsset()

Fires an 'onBeforeUploadAsset' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onBeforeUploadAsset ( Craft\Event $event )

# onDeleteAsset()

Fires an 'onDeleteAsset' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onDeleteAsset ( Craft\Event $event )

# onReplaceFile()

Fires an 'onReplaceFile' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

Signature

public null onReplaceFile ( Craft\Event $event )

# onSaveAsset()

Fires an 'onSaveAsset' event.

View source (opens new window)

Arguments

Returns

null (opens new window)

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

Returns

null (opens new window)

Signature

public null onSaveFileContent ( Craft\Event $event )

# renameFile()

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()

Signature

public boolean storeFile ( Craft\AssetFileModel $file )

# storeFolder()

Store a folder by model and return the id.

View source (opens new window)

Arguments

Returns

mixed

Signature

public mixed storeFolder ( Craft\AssetFolderModel $folder )

# uploadFile()

View source (opens new window)

Arguments

Returns

Craft\AssetOperationResponseModel

Signature

public Craft\AssetOperationResponseModel uploadFile ( $folderId, $userResponse = '', $theNewFileId = 0, $fileName = '' )