Skip to content

Commit

Permalink
Merge pull request #408 from davidyell/flash-customisation
Browse files Browse the repository at this point in the history
Flash customisation
  • Loading branch information
jippi committed Mar 24, 2016
2 parents b1134af + 0f7621e commit 1c8560e
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 70 deletions.
5 changes: 3 additions & 2 deletions docs/_partials/events/after_bulk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Check Success

.. code-block:: phpinline
public function bulk($id) {
public function bulk($id)
{
$this->Crud->on('afterBulk', function(\Cake\Event\Event $event) {
if (!$event->subject()->success) {
$this->log("Bulk action failed");
$this->log("Bulk action failed");
}
});
Expand Down
5 changes: 3 additions & 2 deletions docs/_partials/events/after_delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Check Success

.. code-block:: phpinline
public function delete($id) {
public function delete($id)
{
$this->Crud->on('afterDelete', function(\Cake\Event\Event $event) {
if (!$event->subject()->success) {
$this->log("Delete failed for entity " . $event->subject()->id);
$this->log("Delete failed for entity " . $event->subject()->id);
}
});
Expand Down
3 changes: 2 additions & 1 deletion docs/_partials/events/after_find.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Logging the Found Item

.. code-block:: phpinline
public function delete($id) {
public function delete($id)
{
$this->Crud->on('afterFind', function(\Cake\Event\Event $event) {
$this->log("Found item: " . $event->subject()->entity->id . " in the database");
});
Expand Down
5 changes: 3 additions & 2 deletions docs/_partials/events/after_lookup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Modify the Result

.. code-block:: phpinline
public function lookup() {
public function lookup()
{
$this->Crud->on('afterLookup', function(\Cake\Event\Event $event) {
foreach ($event->subject()->entities as $entity) {
// $entity is an entity
// $entity is an entity
}
});
Expand Down
3 changes: 2 additions & 1 deletion docs/_partials/events/after_paginate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Modify the Result

.. code-block:: phpinline
public function index() {
public function index()
{
$this->Crud->on('afterPaginate', function(\Cake\Event\Event $event) {
foreach ($event->subject()->entities as $entity) {
// $entity is an entity
Expand Down
15 changes: 9 additions & 6 deletions docs/_partials/events/after_save.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Check Created Status

.. code-block:: phpinline
public function edit($id) {
public function edit($id)
{
$this->Crud->on('afterSave', function(\Cake\Event\Event $event) {
if ($event->subject()->created) {
$this->log("The entity was created");
Expand All @@ -36,7 +37,8 @@ Check Success Status

.. code-block:: phpinline
public function edit($id) {
public function edit($id)
{
$this->Crud->on('afterSave', function(\Cake\Event\Event $event) {
if ($event->subject()->success) {
$this->log("The entity was saved successfully");
Expand All @@ -53,11 +55,12 @@ Get Entity ID

.. code-block:: phpinline
public function add() {
public function add()
{
$this->Crud->on('afterSave', function(\Cake\Event\Event $event) {
if ($event->subject()->created) {
$this->log("The entity was created with id: " . $event->subject()->id);
}
if ($event->subject()->created) {
$this->log("The entity was created with id: " . $event->subject()->id);
}
});
return $this->Crud->execute();
Expand Down
3 changes: 2 additions & 1 deletion docs/_partials/events/before_bulk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Stop Bulk Action

.. code-block:: phpinline
public function bulk($id) {
public function bulk($id)
{
$this->Crud->on('beforeBulk', function(\Cake\Event\Event $event) {
// Stop the bulk event, the action will not continue
if ($event->subject()->item->author !== 'admin') {
Expand Down
3 changes: 2 additions & 1 deletion docs/_partials/events/before_delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Stop Delete

.. code-block:: phpinline
public function delete($id) {
public function delete($id)
{
$this->Crud->on('beforeDelete', function(\Cake\Event\Event $event) {
// Stop the delete event, the entity will not be deleted
if ($event->subject()->item->author !== 'admin') {
Expand Down
3 changes: 2 additions & 1 deletion docs/_partials/events/before_lookup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Add Conditions

.. code-block:: phpinline
public function lookup() {
public function lookup()
{
$this->Crud->on('beforeLookup', function(\Cake\Event\Event $event) {
$this->paginate['conditions']['is_active'] = true;
});
Expand Down
37 changes: 36 additions & 1 deletion docs/_partials/events/set_flash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,41 @@ The :ref:`Crud Subject <crud-subject>` contains the following keys:
All keys can be modified as you see fit, at the end of the event cycle they will be passed
directly to ``SessionComponent::setFlash``.

Defaults are stored in the ``messages`` configuration array for each action.
Defaults are stored in the ``messages`` configuration array for each :doc:`action </actions>`.

If you do not want to use this feature, simply stop the event by calling it's ``stopPropagation()`` method.

If you'd like to customise the flash messages that are used, perhaps you're using
`friendsofcake/bootstrap-ui <https://github.com/friendsofcake/bootstrap-ui>`_. It's actually quite simple to do, and can
be done as part of the component configuration or on the fly.

.. code-block:: phpinline
public function initialize()
{
$this->loadComponent('Crud.Crud', [
'actions' => [
'edit' => [
'className' => 'Crud.Edit',
'messages' => [
'success' => [
'params' => ['class' => 'alert alert-success alert-dismissible']
],
'error' => [
'params' => ['class' => 'alert alert-danger alert-dismissible']
]
],
]
]
]);
}
If you'd like to configure it on the fly you can use the eventManager to change the event subject as the event is emitted.

.. code-block:: phpinline
$this->eventManager()->on('Crud.setFlash', function (Event $event) {
if ($event->subject()->success) {
$event->subject()->params['class'] = 'alert alert-success alert-dismissible';
}
});
51 changes: 31 additions & 20 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ now begin the actual configuration of Crud.

.. code-block:: phpinline
class AppController extends \Cake\Controller\Controller {
class AppController extends \Cake\Controller\Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Crud.Crud');
}
}
At this time, the Crud Component is loaded, but we need to tell Crud which actions we want it to handle for us.
Expand Down Expand Up @@ -46,17 +47,20 @@ An example configuration for handling index actions looks like this.
'Crud.Index'
]
]);
}
}
An example of on the fly enabling an Crud action:

.. code-block:: phpinline
class AppController extends \Cake\Controller\Controller {
class AppController extends \Cake\Controller\Controller
{
public function beforeFilter(\Cake\Event\Event $event) {
$this->Crud->mapAction('index', 'Crud.Index');
public function beforeFilter(\Cake\Event\Event $event)
{
$this->Crud->mapAction('index', 'Crud.Index');
}
}
The examples above are functionally identical, and instructs Crud to handle the
Expand Down Expand Up @@ -98,20 +102,23 @@ A more verbose example now, where we'll change the view template that Crud will
]
]
]);
}
}
An example of on the fly enabling a Crud action with configuration

.. code-block:: phpinline
class AppController extends \Cake\Controller\Controller {
class AppController extends \Cake\Controller\Controller
{
public function beforeFilter(\Cake\Event\Event $event) {
$this->Crud->mapAction('index', [
'className' => 'Crud.Index',
'view' => 'my_index'
]);
public function beforeFilter(\Cake\Event\Event $event)
{
$this->Crud->mapAction('index', [
'className' => 'Crud.Index',
'view' => 'my_index'
]);
}
}
Disabling loaded actions
Expand Down Expand Up @@ -142,17 +149,20 @@ specific action in our ``PostsController``.
'Crud.Edit'
]
]);
}
}
.. code-block:: phpinline
class PostsController extends AppController {
public function beforeFilter(\Cake\Event\Event $event) {
parent::beforeFilter($event);
class PostsController extends AppController
{
$this->Crud->disable(['Edit', 'Delete']);
}
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
$this->Crud->disable(['Edit', 'Delete']);
}
}
Built-in actions
Expand Down Expand Up @@ -180,7 +190,6 @@ the ``className`` configuration key for an action, and Crud will use that one in
class AppController extends \Cake\Controller\Controller
{
use \Crud\Controller\ControllerTrait;
public function initialize()
Expand All @@ -193,6 +202,8 @@ the ``className`` configuration key for an action, and Crud will use that one in
'view' => ['className' => '\App\Crud\Action\MyViewAction']
]
]);
}
}
.. note::

Expand Down
1 change: 1 addition & 0 deletions docs/crud-subject.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ This is an example of the data passed in a ``beforeFind`` event subject.
$primaryKey = $event->subject()->id;
$table = $event->subject()->repository;
});
}
Find more examples in the :ref:`Core Crud Events <core-crud-events>` documentation, for the event you need.
23 changes: 14 additions & 9 deletions docs/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ automatically.

.. code-block:: php
<?php
namespace app\Controller;
class BlogsController extends AppController {
class BlogsController extends AppController
{
public function implementedEvents() {
public function implementedEvents()
{
return parent::implementedEvents() + ['Crud.beforeFind' => '_beforeFind'];
}
public function _beforeFind(\Cake\Event\Event $event) {
public function _beforeFind(\Cake\Event\Event $event)
{
}
}
.. note::
Expand All @@ -55,7 +56,8 @@ can be used

.. code-block:: phpinline
public function view($id) {
public function view($id)
{
$this->Crud->on('beforeFind', function(\Cake\Event\Event $event) {
// Will only execute for the view() action
});
Expand All @@ -72,17 +74,20 @@ The benefit of the controller method is that you can easily share it between two

.. code-block:: phpinline
public function view($id) {
public function view($id)
{
$this->Crud->on('beforeFind', [$this, '_beforeFind']);
return $this->Crud->execute();
}
public function admin_view($id) {
public function admin_view($id)
{
$this->Crud->on('beforeFind', [$this, '_beforeFind']);
return $this->Crud->execute();
}
public function _beforeFind(\Cake\Event\Event $event) {
public function _beforeFind(\Cake\Event\Event $event)
{
// Will execute for both view() and admin_view()
}
Expand Down
Loading

0 comments on commit 1c8560e

Please sign in to comment.