PHP Class CrudComponent, Platform-Crud-Plugin

Scaffolding on steroids! :) Copyright 2010-2012, Nodes ApS. (http://www.nodesagency.com/) Licensed under The MIT License Redistributions of files must retain the above copyright notice.
See also: http://book.cakephp.org/2.0/en/controllers/components.html#Component
Inheritance: extends Component
显示文件 Open project: nodesagency/Platform-Crud-Plugin Class Usage Examples

Public Properties

Property Type Description
$components array Reference to a Session component
$settings array validateId ID Argument validation - by default it will inspect your model's primary key and based on it's data type either use integer or uuid validation. Can be disabled by setting it to "false". Supports "integer" and "uuid" configuration By default it's configuration is NULL, which means "auto detect" eventPrefix All emitted events will be prefixed with this property value secureDelete delete() can only be called with the HTTP DELETE verb, not POST when true. If set to false HTTP POST is also acceptable actions contains an array of controller methods this component should offer implementation for. The actions is used for actionMap, viewMap and findMethodMap to change behavior of CrudComponent By default no actions are enabled translations is the settings for the translations Event, responsible for the text used in flash messages see TranslationsEvent::$_defaults the full list of options relatedList is a map of the controller action and the whether it should fetch associations lists to be used in select boxes. An array as value means it is enabled and represent the list of model associations to be fetched saveAllOptions Raw array passed as 2nd argument to saveAll() in add and edit method If you configure a key with your action name, it will override the default settings. This is useful for adding fieldList to enhance security in saveAll. actionMap A map of the controller action and what CRUD action we should call. By default it supports non-prefix and admin_ prefixed routes viewMap A map of the controller action and the view to render By default it supports non-prefix and admin_ prefixed routes findMethodMap The default find method for reading data listenerClassMap List of internal-name => ${plugin}.${class} listeners that will be bound automatically in Crud. By default translations and related model events are bound. Events will always assume to be in the Controller/Event folder

Protected Properties

Property Type Description
$_action string The current controller action
$_controller Controller Reference to the current controller
$_eventManager CakeEventManager Reference to the current event manager
$_listeners array List of listener objects attached to Crud
$_model Model Cached property for the current Controller::modelClass instance
$_modelName string Cached property for Controller::modelClass
$_request CakeRequest Reference to the current request

Public Methods

Method Description
__construct ( ComponentCollection $collection, array $settings = [] ) Constructor
config ( mixed $key = null, mixed $value = null ) : TranslationsEvent Generic config method
disableAction ( string $action ) : void Disable a CRUD action
enableAction ( string $action ) : void Enable a CRUD action
executeAction ( string $controllerAction = null, $args = [] ) : mixed Execute a Crud action
getIdFromRequest ( ) : string Helper method to get the passed ID to an action
getListener ( string $name, $create = true ) : CrudBaseEvent Get a single event class
initialize ( Controller $controller ) : void Make sure to update the list of known controller methods before startup is called
isActionMapped ( string | null $action = null ) : boolean Check if a CRUD action has been mapped (aka should be handled by CRUD component)
mapAction ( string $action, string $type, boolean $enable = true ) : void Map action to a internal request type
mapActionView ( string | array $action, string $view = null ) : void Map the view file to use for a controller action
mapFindMethod ( string $action, strign $method ) : void Map a controller action to a Model::find($method)
on ( string | array $events, callback $callback ) : void Attaches an event listener function to the controller for Crud Events
trigger ( string $eventName, array $data = [] ) : CrudSubject Triggers a Crud event by creating a new subject and filling it with $data if $data is an instance of CrudSubject it will be reused as the subject object for this event.

Protected Methods

Method Description
_addAction ( ) : void Generic add action
_deleteAction ( string $id = null ) : void Generic delete action
_detectPrimaryKeyFieldType ( ) : string Automatically detect primary key data type for _validateId()
_editAction ( string $id = null ) : void Generic edit action
_getFindMethod ( string | null $action = null, string | null $default = null ) : string Get the model find method for a current controller action
_getResourceName ( ) : string Return the human name of the model
_getSaveAllOptions ( string | null $action = null ) : array Build options for saveAll
_getSubject ( array $additional = [] ) : CrudSubject Create a CakeEvent subject with the required properties
_indexAction ( ) : void Generic index action
_loadListener ( string $name ) : void Load a single event class attached to Crud
_loadListeners ( ) : void Load all event classes attached to Crud
_redirect ( CrudSubject $subject, array | null $url = null ) : void Called for all redirects inside CRUD
_setFlash ( string $type ) : void Wrapper for Session::setFlash
_setModelProperties ( ) : void Set internal model properties from the controller
_validateId ( mixed $id ) : boolean Is the passed ID valid ?
_viewAction ( string $id = null ) : void Generic view action

Method Details

__construct() public method

Constructor
public __construct ( ComponentCollection $collection, array $settings = [] )
$collection ComponentCollection A ComponentCollection this component can use to lazy load its components
$settings array Array of configuration settings.

_addAction() protected method

Triggers the following callbacks - Crud.init - Crud.beforeSave - Crud.afterSave - Crud.beforeRender
protected _addAction ( ) : void
return void

_deleteAction() protected method

Triggers the following callbacks - beforeFind - recordNotFound - beforeDelete - afterDelete
protected _deleteAction ( string $id = null ) : void
$id string
return void

_detectPrimaryKeyFieldType() protected method

Binary or string with length of 36 chars will be detected as UUID If the primary key is a number, integer validation will be used If no reliable detection can be made, no validation will be made
protected _detectPrimaryKeyFieldType ( ) : string
return string

_editAction() protected method

Triggers the following callbacks - Crud.init - Crud.beforeSave - Crud.afterSave - Crud.beforeFind - Crud.recordNotFound - Crud.afterFind - Crud.beforeRender
protected _editAction ( string $id = null ) : void
$id string
return void

_getFindMethod() protected method

Get the model find method for a current controller action
protected _getFindMethod ( string | null $action = null, string | null $default = null ) : string
$action string | null The controller action
$default string | null The default find method in case it haven't been mapped
return string The find method used in ->_model->find($method)

_getResourceName() protected method

By default it uses Inflector::humanize, but can be changed using the "name" configuration property
protected _getResourceName ( ) : string
return string

_getSaveAllOptions() protected method

Merges defaults + any custom options for the specific action
protected _getSaveAllOptions ( string | null $action = null ) : array
$action string | null
return array

_getSubject() protected method

Create a CakeEvent subject with the required properties
protected _getSubject ( array $additional = [] ) : CrudSubject
$additional array Additional properties for the subject
return CrudSubject

_indexAction() protected method

Triggers the following callbacks - Crud.init - Crud.beforePaginate - Crud.afterPaginate - Crud.beforeRender
protected _indexAction ( ) : void
return void

_loadListener() protected method

Load a single event class attached to Crud
protected _loadListener ( string $name ) : void
$name string
return void

_loadListeners() protected method

Load all event classes attached to Crud
protected _loadListeners ( ) : void
return void

_redirect() protected method

Called for all redirects inside CRUD
protected _redirect ( CrudSubject $subject, array | null $url = null ) : void
$subject CrudSubject
$url array | null
return void

_setFlash() protected method

Wrapper for Session::setFlash
protected _setFlash ( string $type ) : void
$type string Message type
return void

_setModelProperties() protected method

Set internal model properties from the controller
protected _setModelProperties ( ) : void
return void

_validateId() protected method

By default we assume you want to validate an numeric string like a normal incremental ids from MySQL Change the validateId settings key to "uuid" for UUID check instead
protected _validateId ( mixed $id ) : boolean
$id mixed
return boolean

_viewAction() protected method

Triggers the following callbacks - Crud.init - Crud.beforeFind - Crud.recordNotFound - Crud.afterFind - Crud.beforeRender
protected _viewAction ( string $id = null ) : void
$id string
return void

config() public method

If $key is an array and $value is empty, $key will be merged directly with $this->_config If $key is a string it will be passed into Hash::insert
public config ( mixed $key = null, mixed $value = null ) : TranslationsEvent
$key mixed
$value mixed
return TranslationsEvent

disableAction() public method

Disable a CRUD action
public disableAction ( string $action ) : void
$action string The action to disable
return void

enableAction() public method

Enable a CRUD action
public enableAction ( string $action ) : void
$action string The action to enable
return void

executeAction() public method

Execute a Crud action
public executeAction ( string $controllerAction = null, $args = [] ) : mixed
$controllerAction string Override the controller action to execute as
return mixed void, or a CakeResponse object

getIdFromRequest() public method

Helper method to get the passed ID to an action
public getIdFromRequest ( ) : string
return string

getListener() public method

Get a single event class
public getListener ( string $name, $create = true ) : CrudBaseEvent
$name string
return CrudBaseEvent

initialize() public method

The reason for this is that if we don't, the Auth component won't execute any callbacks on the controller like isAuthorized
public initialize ( Controller $controller ) : void
$controller Controller
return void

isActionMapped() public method

Check if a CRUD action has been mapped (aka should be handled by CRUD component)
public isActionMapped ( string | null $action = null ) : boolean
$action string | null If null, use the current action
return boolean

mapAction() public method

Map action to a internal request type
public mapAction ( string $action, string $type, boolean $enable = true ) : void
$action string The Controller action to fake
$type string one of the CRUD events (index, add, edit, delete, view)
$enable boolean Should the mapping be enabled right away?
return void

mapActionView() public method

To map multiple action views in one go pass an array as first argument and no second argument
public mapActionView ( string | array $action, string $view = null ) : void
$action string | array
$view string
return void

mapFindMethod() public method

Map a controller action to a Model::find($method)
public mapFindMethod ( string $action, strign $method ) : void
$action string
$method strign
return void

on() public method

Attaches an event listener function to the controller for Crud Events
public on ( string | array $events, callback $callback ) : void
$events string | array Name of the Crud Event you want to attach to controller
$callback callback callable method or closure to be executed on event
return void

trigger() public method

If Event listeners return a CakeResponse object, the this method will throw an exception and fill a 'response' property on it with a reference to the response object.
public trigger ( string $eventName, array $data = [] ) : CrudSubject
$eventName string
$data array
return CrudSubject

Property Details

$_action protected_oe property

The current controller action
protected string $_action
return string

$_controller protected_oe property

Reference to the current controller
protected Controller $_controller
return Controller

$_eventManager protected_oe property

Reference to the current event manager
protected CakeEventManager $_eventManager
return CakeEventManager

$_listeners protected_oe property

List of listener objects attached to Crud
protected array $_listeners
return array

$_model protected_oe property

Cached property for the current Controller::modelClass instance
protected Model $_model
return Model

$_modelName protected_oe property

Cached property for Controller::modelClass
protected string $_modelName
return string

$_request protected_oe property

Reference to the current request
protected CakeRequest $_request
return CakeRequest

$components public_oe property

Reference to a Session component
public array $components
return array

$settings public_oe property

validateId ID Argument validation - by default it will inspect your model's primary key and based on it's data type either use integer or uuid validation. Can be disabled by setting it to "false". Supports "integer" and "uuid" configuration By default it's configuration is NULL, which means "auto detect" eventPrefix All emitted events will be prefixed with this property value secureDelete delete() can only be called with the HTTP DELETE verb, not POST when true. If set to false HTTP POST is also acceptable actions contains an array of controller methods this component should offer implementation for. The actions is used for actionMap, viewMap and findMethodMap to change behavior of CrudComponent By default no actions are enabled translations is the settings for the translations Event, responsible for the text used in flash messages see TranslationsEvent::$_defaults the full list of options relatedList is a map of the controller action and the whether it should fetch associations lists to be used in select boxes. An array as value means it is enabled and represent the list of model associations to be fetched saveAllOptions Raw array passed as 2nd argument to saveAll() in add and edit method If you configure a key with your action name, it will override the default settings. This is useful for adding fieldList to enhance security in saveAll. actionMap A map of the controller action and what CRUD action we should call. By default it supports non-prefix and admin_ prefixed routes viewMap A map of the controller action and the view to render By default it supports non-prefix and admin_ prefixed routes findMethodMap The default find method for reading data listenerClassMap List of internal-name => ${plugin}.${class} listeners that will be bound automatically in Crud. By default translations and related model events are bound. Events will always assume to be in the Controller/Event folder
public array $settings
return array