\Dewdrop\Admin\PageEditAbstract

DEPRECATED! Look at \Dewdrop\Fields\RowEditor instead.

An abstract page controller to make the standard edit page workflow easier to implement. When extending this class, the normal page controller workflow is altered in the following ways:

  • Your process method will only be called if the request is a POST
  • You'll have a \Dewdrop\Fields\Edit object available automatically
  • You'll have a \Zend\InputFilter\InputFilter object available
  • The findRowById() method makes it easy to get a row based on the query string

Prior to calling your page's process() method, EditAbstract will do a couple things that are very consistently needed for add/edit pages. First, it will skip your process() method completely if the request is not a POST, as mentioned above. Second, if the request is a POST, EditAbstract will automatically pass the data from POST to your fields and input filter objects.

That means by the time your process() method is running, you only need to concern yourself with a couple steps:

  1. Use the input filter to determine if the input from POST was valid.
  2. Apply any post-validation filtering you need (e.g. password hashing)
  3. Save DB rows objects as needed
  4. Set a redirect and/or success message on the supplied response object

The following is a simple example of applying the described tasks:


namespace Admin\MyComponent;

use Dewdrop\Admin\Page\EditAbstract;

class MyPage extends EditAbstract
{
    // ... your init method ...

    public function process($response)
    {
        if ($this->inputFilter->isValid()) {
            $this->row->save();

            $this->response
                ->setSuccessMessage('You saved it!  Wooohoooo!')
                ->setRedirectToAdminPage('Index');
        }
    }

    // ... your render method
}

Summary

Methods
Properties
Constants
__construct()
createStockPage()
init()
shouldProcess()
process()
render()
renderView()
url()
createResponseHelper()
getView()
setName()
getName()
findRowById()
getErrorsFromInputFilter()
fieldHasError()
getInputFilter()
getFields()
No public properties found
No constants found
inflectViewScriptName()
$component
$view
$request
$viewScriptPath
$fields
$inputFilter
N/A
No private methods found
$name
$fieldsWithErrors
N/A

Properties

$view

$view : \Dewdrop\View\View

A view object available for rendering. Generally, your page should not be rendering any output directly, but instead passing information from models to the view and then rendering the view.

Type

\Dewdrop\View\View

$request

$request : \Dewdrop\Request

An object representing the current HTTP request. The is primarily around to make it easier to test your pages by injecting POST and GET data into the request object.

Type

\Dewdrop\Request

$viewScriptPath

$viewScriptPath : string

The path where this page's view script should be found.

Type

string

$fields

$fields : \Dewdrop\Fields\Edit

The \Dewdrop\Fields\Edit object used to manage and validate DB fields

Type

\Dewdrop\Fields\Edit

$inputFilter

$inputFilter : \Zend\InputFilter\InputFilter

A \Zend\InputFilter\InputFilter object that can be used for filtering and validating any input accepted by this page. This input filter will be passed directly to the \Dewdrop\Fields\Edit object but you can also add your own input objects for non-Field-related input (e.g. credit card security codes, plaintext passwords, etc.) and handle their post-validation processing and filtering independently.

Type

\Zend\InputFilter\InputFilter

$name

$name : string

The name used in the URL to route to this page.

Type

string

$fieldsWithErrors

$fieldsWithErrors : array

We keep a list of fields with errors so that we can check our validation logic while testing.

Type

array

Methods

__construct()

__construct(\Dewdrop\Admin\Component\ComponentInterface  $component, \Dewdrop\Request  $request, string  $pageFile) 

Override the PageAbstract contructor so we can add a \Dewdrop\Fields\Edit object before proceeding to init().

Also, by default, the page will be configured to look for view scripts in the view-scripts sub-folder of its component.

Parameters

\Dewdrop\Admin\Component\ComponentInterface $component
\Dewdrop\Request $request
string $pageFile

The file in which the page class is defined.

createStockPage()

createStockPage(  $name) 

Parameters

$name

init()

init() : void

Create any resources that need to be accessible both for processing and rendering.

shouldProcess()

shouldProcess() : boolean

Only proceed to process() method if the request is a POST.

If the request is a POST, pass the POST data along to the fields object and the input filter.

Returns

boolean

process()

process(\Dewdrop\Admin\ResponseHelper\Standard  $response) 

Perform any processing or data manipulation needed before render.

A response helper object will be passed to this method to allow you to easily add success messages or redirects. This helper should be used to handle these kinds of actions so that you can easily test your page's code.

Parameters

\Dewdrop\Admin\ResponseHelper\Standard $response

render()

render() : mixed

Assign variables to your page's view and render the output.

Returns

mixed

renderView()

renderView() : string

You can call renderView() directly from your render() method. Or, if your render method produces no output itself, the component will call this method itself to automatically render your view script.

Returns

string

url()

url(string  $page, array  $params = array()) : string

As the component this page belongs to for a URL matching the provided page and query string parameters. This method should always be used for generating URLs in your components so that it will play nicely with various WP integration points like submenus.

Parameters

string $page
array $params

Returns

string

createResponseHelper()

createResponseHelper(callable  $redirector) : \Dewdrop\Admin\ResponseHelper\Standard

Create a response helper object for this page.

If your page would benefit from an alternative response helper with additional methods relevant to your use case, you can override this method and the helper will be injected into the page's process() method rather than the standard helper created in PageAbstract.

Parameters

callable $redirector

Returns

\Dewdrop\Admin\ResponseHelper\Standard

getView()

getView() : \Dewdrop\View\View

Get a reference to this page's view object.

Returns

\Dewdrop\View\View

setName()

setName(string  $name) : $this

Set the name used in the URL to route to this page.

Parameters

string $name

Returns

$this

getName()

getName() : string

Set the name used in the URL to route to this page.

Returns

string

findRowById()

findRowById(string  $modelClass) : \Dewdrop\Db\Row

Instantiate the provided model class and return a row for editing.

If the primary value(s) for the supplied model class are present in the query string, the matching row will be queried and returned. Otherwise, a new row will be returned.

This method will also use your supplied model class to generate a default title for the page in the style of the WP admin. For a new row, it will be "Add New {$model->getSingularTitle()}" and for an existing row "Edit {$model->getSingularTitle()}". If you'd like to override this default, you can do so in your render method by reassigning the "title" variable on your view.

Parameters

string $modelClass

Returns

\Dewdrop\Db\Row

getErrorsFromInputFilter()

getErrorsFromInputFilter() : array

Retrieve error messages from input filter.

If the specific input object generating the message is tied to a Field, we will prefix the field's label to the error message to make it easier to understand.

Returns

array

fieldHasError()

fieldHasError(string  $name) 

Check to see whether the supplied field control name is associated with any validation errors.

Parameters

string $name

getInputFilter()

getInputFilter() : \Zend\InputFilter\InputFilter

Get a reference to the page's input filter. Primarily used for testing.

Returns

\Zend\InputFilter\InputFilter

getFields()

getFields() : \Dewdrop\Fields\Edit

Get a reference to the page's fields. Primarily used for testing.

Returns

\Dewdrop\Fields\Edit

inflectViewScriptName()

inflectViewScriptName() : string

Determine the view script name for this page by inflecting the page class name to all lower case with the words separated by hyphens.

For example, the following class name:

Admin\MyComponent\Index

Would become:

index.phtml

Returns

string