PHP Class MeioUploadBehavior, MeioUpload

Inheritance: extends ModelBehavior
ファイルを表示 Open project: jrbasso/MeioUpload

Public Properties

Property Type Description
$__fields array The array that saves the $options for the behavior
$__filesToRemove array Array of files to be removed on the afterSave callback
$_imageTypes array Array of all possible images that can be converted to thumbnails
$defaultOptions array The default options for the behavior
$defaultValidations array Default validations
$patterns array Patterns of reserved words
$replacements array Words to replace the patterns of reserved words

Public Methods

Method Description
__construct ( ) Constructor
_addError ( string $msg ) : void Adds an error, legacy from the component
_arrayMerge ( array $arr, array $ins ) : array Merges two arrays recursively
_copyFileFromTemp ( string $tmpName, string $saveAs, integer $filePermission ) : mixed Copies file from temporary directory to final destination
_createFolders ( string $dir, string $thumbDir, array $thumbsizes, integer $folderPermission ) : void Creates thumbnail folders if they do not already exist
_createThumbnail ( object &$model, string $source, string $target, string $fieldName, array $params = [] ) : void Function to create Thumbnail images
_createThumbnails ( object &$model, array $data, string $fieldName, $saveAs, string $ext, array $options ) : void Create all the thumbnails
_deleteFiles ( object &$model, string $field, string $filename, stirng $dir ) : boolean Delete the $filename inside the $dir and the thumbnails.
_deleteFilesList ( object &$model ) : void Deletes the files marked to be deleted in the save method.
_fixName ( object &$model, string $fieldName, boolean $checkFile = true ) : void Removes the bad characters from the $filename and replace reserved words. It updates the $model->data.
_getThumbnailName ( string $saveAs, string $dir, string $thumbDir, string $key, string $fieldToSaveAs, string $sub = null ) : string Generate the name for the thumbnail If a 'normal' thumbnail is set, then it will overwrite the original file
_includeDefaultReplacement ( string $default ) : void Include a pattern of reserved word based on a filename, and it's replacement.
_markForDeletion ( object &$model, string $fieldName, array $data, strng $default, string $thumbDir ) : void Marks files for deletion in the beforeSave() callback
_nullifyDataFields ( object &$model, string $fieldName, array $data, array $options ) : array Sets as NULL data from $data Useful when deleting a file.
_removeOriginal ( string $saveAs ) : boolean Remove original file
_replaceTokens ( object &$model, string $string, string $fieldName, array $tokens = [] ) : string Replaces some tokens. {ModelName} to the underscore version of the model name {fieldName} to the field name, {DS}. / or \ to DS constant value.
_setFileToRemove ( object &$model, sting $fieldName, string $thumbDir ) : void Set a file to be removed in afterSave() callback
_setupValidation ( object &$model, string $fieldName, array $options ) : void Sets the validation for each field, based on the options.
_sizeToBytes ( string $size ) : integer Convert a size value to bytes. For example: 2 MB to 2097152.
_splitFilenameAndExt ( string $filename ) : array Splits a filename in two parts: the name and the extension. Returns an array with it respectively.
_unsetDataFields ( string $modelName, string $fieldName, array $data, array $options ) : array Unsets data from $data Useful for no-db upload
_uploadCheckSize ( object &$model, array &$data, string $type ) : boolean Check generic to size of image
_uploadFile ( object &$model, array $data = null ) : mixed Uploads the files
afterDelete ( Model $model ) : void After delete (callback)
afterSave ( Model $model, $created ) : void After save (callback)
beforeDelete ( Model $model, $cascade = true ) : boolean Deletes all files associated with the record beforing delete it.
beforeSave ( Model $model ) : boolean Initializes the upload
beforeValidate ( Model $model ) : boolean Sets the validation rules for each field.
regenerateThumbnails ( Model $model ) Regenerate all the thumbnails, for all fields that have thumbSizes, for records in the table. (used, eg, when you want to add or change thumbnail sizes) This will overwrite existing thumbnails, when they exist, so a backup of thumbnails should be done before hand.
setup ( Model $model, array $settings = [] ) : void Setup the behavior. It stores a reference to the model, merges the default options with the options for each field, and setup the validation rules.
upload ( object &$model, array $data ) : boolean Performs a manual upload
uploadCheckDir ( object &$model, array $data ) : boolean Checks if the folder exists or can be created or writable.
uploadCheckEmpty ( object &$model, array $data ) : boolean Checks if the filename is not empty.
uploadCheckFieldName ( object &$model, array $data ) : boolean Checks if the field was declared in the MeioUpload Behavior setup
uploadCheckHttpPost ( object &$model, array $data ) : boolean Checks if the file is uploaded via HTTP POST
uploadCheckInvalidExt ( object &$model, array $data ) : boolean Checks if the file has an allowed extension.
uploadCheckInvalidMime ( object &$model, array $data ) : boolean Checks if the file is of an allowed mime-type.
uploadCheckMaxHeight ( object &$model, array $data ) : boolean Checks if the max height is allowed
uploadCheckMaxSize ( object &$model, array $data ) : boolean Checks if the file isn't bigger then the max file size option.
uploadCheckMaxWidth ( object &$model, array $data ) : boolean Checks if the max width is allowed
uploadCheckMinHeight ( object &$model, array $data ) : boolean Checks if the min height is allowed
uploadCheckMinWidth ( object &$model, array $data ) : boolean Checks if the min width is allowed
uploadCheckUploadError ( object &$model, array $data ) : boolean Checks if ocurred erros in the upload.

Method Details

__construct() public method

Constructor
public __construct ( )

_addError() public method

Adds an error, legacy from the component
public _addError ( string $msg ) : void
$msg string error message
return void

_arrayMerge() public method

These values will remain intact even if the passed options were shorter. Solved that with array_splice to keep intact the previous indexes (already merged)
public _arrayMerge ( array $arr, array $ins ) : array
$arr array
$ins array
return array

_copyFileFromTemp() public method

Copies file from temporary directory to final destination
public _copyFileFromTemp ( string $tmpName, string $saveAs, integer $filePermission ) : mixed
$tmpName string full path to temporary file
$saveAs string full path to move the file to
$filePermission integer octal value of created file permission
return mixed true is successful, error message if not

_createFolders() public method

Creates thumbnail folders if they do not already exist
public _createFolders ( string $dir, string $thumbDir, array $thumbsizes, integer $folderPermission ) : void
$dir string Path to uploads
$thumbDir string Path to thumbnails
$thumbsizes array
$folderPermission integer octal value of created folder permission
return void

_createThumbnail() public method

Function to create Thumbnail images
public _createThumbnail ( object &$model, string $source, string $target, string $fieldName, array $params = [] ) : void
$model object Reference to model
$source string File name (without path)
$target string File name (without path)
$fieldName string
$params array
return void

_createThumbnails() public method

Create all the thumbnails
public _createThumbnails ( object &$model, array $data, string $fieldName, $saveAs, string $ext, array $options ) : void
$model object Reference to model
$data array
$fieldName string
$ext string
$options array
return void

_deleteFiles() public method

Returns true if the file is deleted and false otherwise.
public _deleteFiles ( object &$model, string $field, string $filename, stirng $dir ) : boolean
$model object Reference to model
$field string
$filename string
$dir stirng
return boolean

_deleteFilesList() public method

A file can be marked to be deleted if it is overwriten by another or if the user mark it to be deleted.
public _deleteFilesList ( object &$model ) : void
$model object Reference to model
return void

_fixName() public method

Removes the bad characters from the $filename and replace reserved words. It updates the $model->data.
public _fixName ( object &$model, string $fieldName, boolean $checkFile = true ) : void
$model object Reference to model
$fieldName string
$checkFile boolean
return void

_getThumbnailName() public method

Generate the name for the thumbnail If a 'normal' thumbnail is set, then it will overwrite the original file
public _getThumbnailName ( string $saveAs, string $dir, string $thumbDir, string $key, string $fieldToSaveAs, string $sub = null ) : string
$saveAs string name for original file
$dir string directory for all uploads
$thumbDir string Path to thumbnails
$key string thumbnail size
$fieldToSaveAs string field in model to save as
$sub string substring to append to directory for naming
return string

_includeDefaultReplacement() public method

Include a pattern of reserved word based on a filename, and it's replacement.
public _includeDefaultReplacement ( string $default ) : void
$default string
return void

_markForDeletion() public method

Marks files for deletion in the beforeSave() callback
public _markForDeletion ( object &$model, string $fieldName, array $data, strng $default, string $thumbDir ) : void
$model object Reference to model
$fieldName string name of field that holds a reference to the file
$data array
$default strng
$thumbDir string Thumb directory
return void

_nullifyDataFields() public method

Sets as NULL data from $data Useful when deleting a file.
public _nullifyDataFields ( object &$model, string $fieldName, array $data, array $options ) : array
$model object Reference to model
$fieldName string name of field that holds a reference to the file
$data array
$options array
return array

_removeOriginal() public method

Remove original file
public _removeOriginal ( string $saveAs ) : boolean
$saveAs string
return boolean

_replaceTokens() public method

Replaces some tokens. {ModelName} to the underscore version of the model name {fieldName} to the field name, {DS}. / or \ to DS constant value.
public _replaceTokens ( object &$model, string $string, string $fieldName, array $tokens = [] ) : string
$model object Reference to model
$string string
$fieldName string
$tokens array
return string

_setFileToRemove() public method

Set a file to be removed in afterSave() callback
public _setFileToRemove ( object &$model, sting $fieldName, string $thumbDir ) : void
$model object Reference to model
$fieldName sting
$thumbDir string Path to thumbnails
return void

_setupValidation() public method

Sets the validation for each field, based on the options.
public _setupValidation ( object &$model, string $fieldName, array $options ) : void
$model object Reference to model
$fieldName string
$options array
return void

_sizeToBytes() public method

Convert a size value to bytes. For example: 2 MB to 2097152.
public _sizeToBytes ( string $size ) : integer
$size string
return integer

_splitFilenameAndExt() public method

Splits a filename in two parts: the name and the extension. Returns an array with it respectively.
public _splitFilenameAndExt ( string $filename ) : array
$filename string
return array

_unsetDataFields() public method

Unsets data from $data Useful for no-db upload
public _unsetDataFields ( string $modelName, string $fieldName, array $data, array $options ) : array
$modelName string name of the Model
$fieldName string name of field that holds a reference to the file
$data array
$options array
return array

_uploadCheckSize() public method

Check generic to size of image
public _uploadCheckSize ( object &$model, array &$data, string $type ) : boolean
$model object Reference to model
$data array Reference to data
$type string Type of check: minHeight, minWidth, maxHeight, maxWidth
return boolean

_uploadFile() public method

Uploads the files
public _uploadFile ( object &$model, array $data = null ) : mixed
$model object Reference to model
$data array Optional Containing data to be saved
return mixed

afterDelete() public method

After delete (callback)
public afterDelete ( Model $model ) : void
$model Model Reference to model
return void

afterSave() public method

After save (callback)
public afterSave ( Model $model, $created ) : void
$model Model Reference to model
return void

beforeDelete() public method

Deletes all files associated with the record beforing delete it.
public beforeDelete ( Model $model, $cascade = true ) : boolean
$model Model Reference to model
return boolean Always true

beforeSave() public method

Initializes the upload
public beforeSave ( Model $model ) : boolean
$model Model Reference to model
return boolean Whether the upload completed

beforeValidate() public method

Sets the validation rules for each field.
public beforeValidate ( Model $model ) : boolean
$model Model Reference to model
return boolean Always true

regenerateThumbnails() public method

This will not delete any redundant thumbnails - redundant thumbnails can be deleted manually. From your controller, call this method like: $this->MyModel->regenerateThumbnails();
public regenerateThumbnails ( Model $model )
$model Model Reference to model

setup() public method

Setup the behavior. It stores a reference to the model, merges the default options with the options for each field, and setup the validation rules.
public setup ( Model $model, array $settings = [] ) : void
$model Model Reference to model
$settings array Settings (optional)
return void

upload() public method

Performs a manual upload
public upload ( object &$model, array $data ) : boolean
$model object Reference to model
$data array Array data to be saved
return boolean Whether the upload completed

uploadCheckDir() public method

Checks if the folder exists or can be created or writable.
public uploadCheckDir ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckEmpty() public method

Checks if the filename is not empty.
public uploadCheckEmpty ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckFieldName() public method

Checks if the field was declared in the MeioUpload Behavior setup
public uploadCheckFieldName ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckHttpPost() public method

Checks if the file is uploaded via HTTP POST
public uploadCheckHttpPost ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckInvalidExt() public method

Checks if the file has an allowed extension.
public uploadCheckInvalidExt ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckInvalidMime() public method

Checks if the file is of an allowed mime-type.
public uploadCheckInvalidMime ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckMaxHeight() public method

Checks if the max height is allowed
public uploadCheckMaxHeight ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckMaxSize() public method

Checks if the file isn't bigger then the max file size option.
public uploadCheckMaxSize ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckMaxWidth() public method

Checks if the max width is allowed
public uploadCheckMaxWidth ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckMinHeight() public method

Checks if the min height is allowed
public uploadCheckMinHeight ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckMinWidth() public method

Checks if the min width is allowed
public uploadCheckMinWidth ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

uploadCheckUploadError() public method

Checks if ocurred erros in the upload.
public uploadCheckUploadError ( object &$model, array $data ) : boolean
$model object Reference to model
$data array
return boolean

Property Details

$__fields public_oe property

The array that saves the $options for the behavior
public array $__fields
return array

$__filesToRemove public_oe property

Array of files to be removed on the afterSave callback
public array $__filesToRemove
return array

$_imageTypes public_oe property

Array of all possible images that can be converted to thumbnails
public array $_imageTypes
return array

$defaultOptions public_oe property

The default options for the behavior
public array $defaultOptions
return array

$defaultValidations public_oe property

Default validations
public array $defaultValidations
return array

$patterns public_oe property

Patterns of reserved words
public array $patterns
return array

$replacements public_oe property

Words to replace the patterns of reserved words
public array $replacements
return array