AssetsService

Type
Class
Namespace
Craft
Inherits
Craft\AssetsService » Craft\BaseApplicationComponent » CApplicationComponent » CComponent
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

Public Properties

PropertyDescription
$behaviorsarray – The behaviors that should be attached to this component.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
canGetProperty()Determines whether a property can be read.
canSetProperty()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()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
evaluateExpression()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()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()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()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()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()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

Arguments

  • $folderId
  • $action

Returns

boolean

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

Arguments

  • $fileIds
  • $permission

Returns

null

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

Arguments

  • $folderIds
  • $permission

Returns

null

Throws

Signature

public null checkPermissionByFolderIds ( $folderIds, $permission )

createFolder()

Create a folder by it's parent id and a folder name.

View source

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

Arguments

  • $fileIds (array)
  • $deleteFile (boolean) – Should the file be deleted along the record. Defaults to true.

Returns

Craft\AssetOperationResponseModel

Signature

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

deleteFolderById()

Deletes a folder by its ID.

View source

Arguments

Returns

Craft\AssetOperationResponseModel

Throws

Signature

public Craft\AssetOperationResponseModel deleteFolderById ( $folderId )

deleteFolderRecord()

Delete a folder record by id.

View source

Arguments

  • $folderId

Returns

boolean

Signature

public boolean deleteFolderRecord ( $folderId )

findFile()

Finds the first file that matches the given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

Craft\AssetFileModel, null

Signature

public Craft\AssetFileModel, null findFile ( $criteria = null )

findFolder()

Finds the first folder that matches a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

Craft\AssetFolderModel, null

Signature

public Craft\AssetFolderModel, null findFolder ( $criteria = null )

findFolders()

Finds folders that match a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

array

Signature

public array findFolders ( $criteria = null )

getAllDescendantFolders()

Returns all of the folders that are descendants of a given folder.

View source

Arguments

Returns

array

Signature

public array getAllDescendantFolders ( Craft\AssetFolderModel $parentFolder )

getFileById()

Returns a file by its ID.

View source

Arguments

Returns

Craft\AssetFileModel, null

Signature

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

getFilesBySourceId()

Returns all top-level files in a source.

View source

Arguments

Returns

array

Signature

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

getFolderById()

Returns a folder by its ID.

View source

Arguments

Returns

Craft\AssetFolderModel, null

Signature

public Craft\AssetFolderModel, null getFolderById ( $folderId )

getFolderTreeByFolderId()

Get the folder tree for Assets by a folder id.

View source

Arguments

  • $folderId

Returns

array

Signature

public array getFolderTreeByFolderId ( $folderId )

getFolderTreeBySourceIds()

Get the folder tree for Assets by source ids, optionally filtered by additional criteria

View source

Arguments

  • $allowedSourceIds (array) – An array of allowed source ids
  • $additionalCriteria (array) – An array of additional criteria

Returns

array

Signature

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

getRootFolderBySourceId()

Returns the root folder for a given source ID.

View source

Arguments

Returns

Craft\AssetFolderModel, null

Signature

public Craft\AssetFolderModel, null getRootFolderBySourceId ( $sourceId )

getTotalFiles()

Gets the total number of files that match a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

integer

Signature

public integer getTotalFiles ( $criteria = null )

getTotalFolders()

Gets the total number of folders that match a given criteria.

View source

Arguments

  • $criteria (mixed)

Returns

integer

Signature

public integer getTotalFolders ( $criteria )

getUrlForFile()

Get URL for a file.

View source

Arguments

Returns

string

Signature

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

getUserFolder()

Get the users Folder model.

View source

Arguments

Returns

Craft\AssetFolderModel, null

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

Arguments

  • $localPath (string) – The local path to the file.
  • $fileName (string) – The name that the file should be given when saved in the asset folder.
  • $folderId (integer) – The ID of the folder that the file should be saved into.
  • $conflictResolution (string) – 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

Returns

boolean

Signature

public boolean isMergeInProgress ( )

moveFiles()

Move or rename files.

View source

Arguments

  • $fileIds
  • $folderId
  • $filename (string) – If this is a rename operation or not.
  • $actions (array) – Actions to take in case of a conflict.

Returns

boolean, Craft\AssetOperationResponseModel

Throws

Signature

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

moveFolder()

Move a folder.

View source

Arguments

  • $folderId
  • $newParentId
  • $action

Returns

Craft\AssetOperationResponseModel

Signature

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

onBeforeDeleteAsset()

Fires an 'onBeforeDeleteAsset' event.

View source

Arguments

Returns

null

Signature

public null onBeforeDeleteAsset ( Craft\Event $event )

onBeforeReplaceFile()

Fires an 'onBeforeReplaceFile' event.

View source

Arguments

Returns

null

Signature

public null onBeforeReplaceFile ( Craft\Event $event )

onBeforeSaveAsset()

Fires an 'onBeforeSaveAsset' event.

View source

Arguments

Returns

null

Signature

public null onBeforeSaveAsset ( Craft\Event $event )

onBeforeUploadAsset()

Fires an 'onBeforeUploadAsset' event.

View source

Arguments

Returns

null

Signature

public null onBeforeUploadAsset ( Craft\Event $event )

onDeleteAsset()

Fires an 'onDeleteAsset' event.

View source

Arguments

Returns

null

Signature

public null onDeleteAsset ( Craft\Event $event )

onReplaceFile()

Fires an 'onReplaceFile' event.

View source

Arguments

Returns

null

Signature

public null onReplaceFile ( Craft\Event $event )

onSaveAsset()

Fires an 'onSaveAsset' event.

View source

Arguments

Returns

null

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

Arguments

Returns

null

Signature

public null onSaveFileContent ( Craft\Event $event )

renameFile()

View source

Arguments

Returns

boolean, 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

Arguments

  • $folderId
  • $newName

Returns

Craft\AssetOperationResponseModel

Throws

Signature

public Craft\AssetOperationResponseModel renameFolder ( $folderId, $newName )

storeFile()

Saves the record for an asset.

View source

Arguments

Returns

boolean

Throws

Signature

public boolean storeFile ( Craft\AssetFileModel $file )

storeFolder()

Store a folder by model and return the id.

View source

Arguments

Returns

mixed

Signature

public mixed storeFolder ( Craft\AssetFolderModel $folder )

uploadFile()

View source

Arguments

  • $folderId (integer) – The Id of the folder the file is being uploaded to.
  • $userResponse (string) – User response regarding filename conflict.
  • $theNewFileId (integer) – The new file ID that has triggered the conflict.
  • $fileName (string) – The filename that is in the conflict.

Returns

Craft\AssetOperationResponseModel

Signature

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