CakePHP-Batch icon indicating copy to clipboard operation
CakePHP-Batch copied to clipboard

Search / Filter and Update / Delete in batches your paginated results

h1. CakePHP Batch (and filter) Plugin

Paginates Filtered Records and allows Batch deletion and updating of records

!http://deansofer.com/uploads/media/original-CakePHP_Batch_Plugin.jpg!

h2. Background

The Batch plugin is an extension of "Jose Diaz-Gonzalez's Filter plugin":http://github.com/josegonzalez/cakephp-filter-component, which is something of a fork of "James Fairhurst's Filter Component":http://www.jamesfairhurst.co.uk/posts/view/cakephp_filter_component/, which is in turn a fork by "Maciej Grajcarek":http://blog.uplevel.pl/index.php/2008/06/cakephp-12-filter-component/, which is ITSELF a fork from "Nik Chankov's code":http://nik.chankov.net/2008/03/01/filtering-component-for-your-tables/. "Chad Jablonski":http://github.com/cjab/cakephp-filter-plugin then added RANGE support with a few bug fixes. "jmroth":http://github.com/jmroth/cakephp-filter-plugin pointed out a pretty bad redirect issue and "fixed it". This also contains a view helper made by "Matt Curry":http://github.com/mcurry/cakephp-filter-component.

This also uses a behavior adapted from work by "Brenton":http://bakery.cakephp.org/articles/view/habtm-searching to allow for HasAndBelongsToMany and HasMany relationships. Disabled for now as the behavior isn't working.

h2. Installation

h3. Download

  • Clone/Submodule/Download from github into @/plugins/batch@

h3. 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)

h3. Setup the form

Batch->create('Post')?>	
Batch->filter(array('subject', 'user_id', 'published'));
foreach ($posts as $post):
?>
Batch->batch(array(null, 'user_id', 'published')); 
?>
Paginator->sort('subject');?> Paginator->sort('user_id');?> Paginator->sort('published');?>
      Html->link(__('View', true), array('action' => 'view', $post['Post']['id']), array('class' => 'view')); ?> Html->link(__('Edit', true), array('action' => 'edit', $post['Post']['id']), array('class' => 'edit')); ?> 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'])); ?> Batch->checkbox($post['Post']['id']); ?>
Batch->end()?>

h3. Include the CSS / JS (Optional)

Some 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.

h2. Advanced Usage

h3. Initialization Tips

These different initialization options are combined in the setup array. Defaults are shown below.

 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

h2. Todo

Better code commenting - Done, left to help enforce the habit

Support Datetime Done

Support URL redirects and parsing Done

Refactor datetime filtering for ranges Done

Allow the action to be configurable Done

Support jQuery Datepicker Outside 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.