CakePHP Batch (and filter) Plugin
Paginates Filtered Records and allows Batch deletion and updating of records
Background
The Batch plugin is an extension of Jose Diaz-Gonzalez’s Filter plugin, which is something of a fork of James Fairhurst’s Filter Component, which is in turn a fork by Maciej Grajcarek, which is ITSELF a fork from Nik Chankov’s code. Chad Jablonski then added RANGE support with a few bug fixes. jmroth pointed out a pretty bad redirect issue and “fixed it”. This also contains a view helper made by Matt Curry.
This also uses a behavior adapted from work by Brenton to allow for HasAndBelongsToMany and HasMany relationships. Disabled for now as the behavior isn’t working.
Installation
Download
- Clone/Submodule/Download from github into
/plugins/batch
Include Component
Include the component in your controller (AppController or otherwise)
var $components = array('Batch.Batch');
Note: The plugin by default only works with the ‘index’ action. If you want it to support more actions (such as ‘admin_index’) you must pass the option (see below)
Setup the form
<?php echo $this->Batch->create('Post')?>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('subject');?></th>
<th><?php echo $this->Paginator->sort('user_id');?></th>
<th><?php echo $this->Paginator->sort('published');?></th>
<th class="actions"><?php __('Actions');?></th>
</tr>
<?php
echo $this->Batch->filter(array('subject', 'user_id', 'published'));
foreach ($posts as $post):
?>
<tr>
<td><?php echo $post['Post']['subject']; ?> </td>
<td><?php echo $post['Post']['user_id']; ?> </td>
<td><?php echo $post['Post']['published']; ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View', true), array('action' => 'view', $post['Post']['id']), array('class' => 'view')); ?>
<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $post['Post']['id']), array('class' => 'edit')); ?>
<?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $post['Post']['id']), array('class' => 'delete'), sprintf(__('Are you sure you want to delete # %s?', true), $post['Post']['id'])); ?>
<?php echo $this->Batch->checkbox($post['Post']['id']); ?>
</td>
</tr>
<?php
endforeach;
echo $this->Batch->batch(array(null, 'user_id', 'published'));
?>
</table>
<?php echo $this->Batch->end()?>
CSS / JS (Optional)
Include theSome nifty functionality is supplied with the assets:
var $this->Html->script('/batch/js/jquery')
var $this->Html->css('/batch/css/batch')
The Javascript relies on jquery but the files are small so feel free to customize them how you see fit.
Advanced Usage
Initialization Tips
These different initialization options are combined in the setup array. Defaults are shown below.
<?php
class PostsController extends AppController {
var $name = 'Posts';
var $components = array('Batch.Batch' => array(
'actions' => array('index'), // You need to add admin_index manually
'defaults' => array(),
'fieldFormatting' => array(
'string' => "LIKE '%%%s%%'",
'text' => "LIKE '%%%s%%'",
'datetime' => "LIKE '%%%s%%'"
),
'formOptionsDatetime' => array(),
'paginatorParams' => array(
'page',
'sort',
'direction',
'limit'
),
'parsed' => false,
'redirect' => false,
'useTime' => false,
'separator' => '/',
'rangeSeparator' => '-',
'url' => array(),
'whitelist' => array(),
'cascade' => true,
'callbacks' => false,
'security' => true,
));
}
?>
- actions: Array of actions upon which this component will act upon.
- defaults: Holds pagination defaults for controller actions. (syntax is
array('Model' => array('key' => 'value')
) - fieldFormatting: Fields which will replace the regular syntax in where i.e. field = ‘value’
- formOptionsDatetime: Formatting for datetime fields (unused)
- paginatorParams: Paginator params sent in the URL
- parsed: Used to tell whether the data options have been parsed
- redirect: Used to tell whether to redirect so the url includes filter data
- useTime: Used to tell whether time should be used in the filtering
- separator: Separator to use between fields in a date input
- rangeSeparator: Separator to use between dates in a date range
- url: Url variable used in paginate helper (syntax is
array('url' => $url)
); - whitelist: Array of fields and models for which this component may filter
- cascade: Record deletion cascades
- callbacks: Record update/deletion triggers callbacks
- security: Add form buttons to the SecurityComponent::ignoreFields attribute. Resolves conflicts with security component
Todo
- Better code commenting – Done, left to help enforce the habit
Support DatetimeDoneSupport URL redirects and parsingDoneRefactor datetime filtering for rangesDoneAllow the action to be configurableDoneSupport jQuery DatepickerOutside scope- Support Router prefixes, plugins, and named parameters in a “scope” instead of “actions” key.
- Expand hasMany and hasAndBelongsToMany support. Refactor behavior to conform with established practices.