Skip to content

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

Notifications You must be signed in to change notification settings

cikorka/CakePHP-Batch

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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']; ?>&nbsp;</td>
	<td><?php echo $post['Post']['user_id']; ?>&nbsp;</td>
	<td><?php echo $post['Post']['published']; ?>&nbsp;</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()?>

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.

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,
	));
}
?>
  1. actions: Array of actions upon which this component will act upon.
  2. defaults: Holds pagination defaults for controller actions. (syntax is array('Model' => array('key' => 'value'))
  3. fieldFormatting: Fields which will replace the regular syntax in where i.e. field = ‘value’
  4. formOptionsDatetime: Formatting for datetime fields (unused)
  5. paginatorParams: Paginator params sent in the URL
  6. parsed: Used to tell whether the data options have been parsed
  7. redirect: Used to tell whether to redirect so the url includes filter data
  8. useTime: Used to tell whether time should be used in the filtering
  9. separator: Separator to use between fields in a date input
  10. rangeSeparator: Separator to use between dates in a date range
  11. url: Url variable used in paginate helper (syntax is array('url' => $url));
  12. whitelist: Array of fields and models for which this component may filter
  13. cascade: Record deletion cascades
  14. callbacks: Record update/deletion triggers callbacks
  15. security: Add form buttons to the SecurityComponent::ignoreFields attribute. Resolves conflicts with security component

Todo

  1. Better code commenting – Done, left to help enforce the habit
  2. Support Datetime Done
  3. Support URL redirects and parsing Done
  4. Refactor datetime filtering for ranges Done
  5. Allow the action to be configurable Done
  6. Support jQuery Datepicker Outside scope
  7. Support Router prefixes, plugins, and named parameters in a “scope” instead of “actions” key.
  8. Expand hasMany and hasAndBelongsToMany support. Refactor behavior to conform with established practices.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%