GridHelper - for easy grids in CakePHP
Been busy as hell and I’m afraid it will be like this for another month or so, but here is a quick one I felt like sharing because I hope it will be helpful to some.
Haven’t you ever wished to only have to write something like this in your views:
e($grid->create($results));Note: It’s still unfinished and I know I will have lots of revisions to it while working on this app, so if you have any suggestions/critics or better ways of doing it, please let me know.
/app/views/helpers/grid.php:
/**
* GridHelper - easily creating grids
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Jad Bitar
* @copyright Copyright 2007, loudbaking.com
*/
class GridHelper extends AppHelper
{
/**
* Field/Title inflections
*
* @var array
* @access public
*/
var $inflections = array(
'commission' => 'Earned',
'created' => 'Created on',
'email' => 'Email address',
'ip' => 'IP address',
'is_active' => 'Status',
'joined' => 'Date',
'ref_code' => 'Code',
'url' => 'URL address',
);
/**
* Defines how to format values in cells
*
* @var array
* @access public
*/
var $formats = array(
'date' => 'Y-m-d',
'currency' => 'USD',
'is_active' => array('Not active', 'Active'),
);
/**
* Dependent on the following helpers
*
* @var array
* @access public
*/
var $helpers = array('Html', 'Number', 'String', 'Time');
/**
* Patterns to use when creating the HTML
*
* @var array
* @access public
*/
var $tags = array(
'table' => '
$default = array(
'table' => array(
),
'headers' => array(),
'cells' => array(),
'before' => 'before',
'after' => 'after',
'empty' => 'No records found',
);
$headers = $this->Html->tableHeaders($this->_inflect(array_keys($records[0])));
$rows = '';
foreach ($records as $record)
{
$rows .= $this->Html->tableCells(array_values($this->_format($record)));
}
return sprintf($this->tags['table'], $headers, $rows);
} /** * Formats values in recordset * * @param array * @return array * @access private */ function _format($cols) { foreach($cols as $field => $value) { switch ($field) { case ‘created’: case ‘modified’: case ‘joined’: case ‘last_login’: $cols[$field] = $this->Time->format($this->formats[’date’], $value); break; case ‘is_active’: $cols[$field] = $this->formats[’is_active’][$value != 0]; break; case ‘email’: $cols[$field] = $this->String->obfuscate($value, 5); break; case ‘commission’: $cols[$field] = $this->Number->currency($value, $this->formats[’currency’]); break;
default:
;
} // switch
}
return $cols;
} /** * Transforms keys into titles * * @param array * @return array * @access private */ function _inflect($keys) { foreach ($keys as $k => $key) { if (!isset($this->inflections[$key])) { trigger_error(sprintf(__(’No inflection found for %s’, true), $key), E_USER_ERROR); } $keys[$k] = $this->inflections[$key]; } return $keys; } } As you can see from this code, it’s still pretty simplistic but the idea is to completely separate resultset preparation from rendering re-enforcing the fat models, skinny controllers by adding skinny views (maybe?).
If you think it could help you at the stage it is at, copy it here - WP seems to eat chunks of it when saving.
Trackbacks
Use this link to trackback from your own site.


I think to make this helper really reusable you have to get rid of the hard-coded column names. But at the moment I don’t have an idea how to do that…
Beautiful JadB, I was just thinking about putting together something similar.
@all: Thanks for dropping by.
@Daniel: What you mean by ‘getting rid of the col names’?
@Jad: See the _format() method, if I want to use this helper I would have to modify this method as the names used there are specific to your application. I hope you see what I mean
@Daniel: Ah! now I see what you mean.. Ya, I thought of that but haven’t tried implementing the solution yet. I believe callback methods like AppHelper::gridFieldName() would work great for that - will keep you guys updated, I have to finish it soon anyhow.