Skip to content

Commit

Permalink
Fixes + new features
Browse files Browse the repository at this point in the history
* Fix PHPUnit for PHP 5.3
* Enhanced image support (PDF export)
* BC break in execute() Datagrid class
  • Loading branch information
ThaDafinser committed Aug 22, 2013
1 parent f304c89 commit e433b1a
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/ZfcDatagrid/Column/Action/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function toHtml ()

$attributes = implode(' ', $attributes);

return '<a href="' . $this->getLink() . '" ' . $attributes . '>' . $this->getLabel() . '</a>';
return '<a href="' . $this->getLink() . '" title="' . $this->getTitle() . '" ' . $attributes . '>' . $this->getLabel() . '</a>';
}
}
3 changes: 0 additions & 3 deletions src/ZfcDatagrid/DataSource/DataSourceInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php
namespace ZfcDatagrid\DataSource;

use ZfcDatagrid\Filter;
use ZfcDatagrid\Column\AbstractColumn;

interface DataSourceInterface
{

Expand Down
1 change: 0 additions & 1 deletion src/ZfcDatagrid/DataSource/ZendSelect.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace ZfcDatagrid\DataSource;

use ZfcDatagrid\Filter;
use ZfcDatagrid\Column;
use Zend\Paginator\Adapter\DbSelect as PaginatorAdapter;
use Zend\Db\Sql;
Expand Down
174 changes: 126 additions & 48 deletions src/ZfcDatagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ class Datagrid implements ServiceLocatorAwareInterface

protected $isInit = false;

protected $isExecuted = false;
protected $isDataLoaded = false;

protected $isRendered = false;

protected $forceRenderer;

Expand Down Expand Up @@ -270,11 +272,21 @@ public function getCache()
return $this->cache;
}

/**
* Set the cache id
*
* @param string $id
*/
public function setCacheId($id)
{
$this->cacheId = (string) $id;
}

/**
* Get the cache id
*
* @return string
*/
public function getCacheId()
{
if ($this->cacheId === null) {
Expand Down Expand Up @@ -397,6 +409,11 @@ public function getDataSource()
return $this->dataSource;
}

/**
* Datasource defined?
*
* @return boolean
*/
public function hasDataSource()
{
if ($this->dataSource !== null) {
Expand Down Expand Up @@ -469,6 +486,11 @@ public function getParameters()
return $this->parameters;
}

/**
* Has parameters?
*
* @return boolean
*/
public function hasParameters()
{
if (count($this->getParameters()) > 0) {
Expand All @@ -478,6 +500,11 @@ public function hasParameters()
return false;
}

/**
* Set the base url
*
* @param string $url
*/
public function setUrl($url)
{
$this->url = $url;
Expand All @@ -492,11 +519,21 @@ public function getUrl()
return $this->url;
}

/**
* Set the export renderers (overwrite the config)
*
* @param array $renderers
*/
public function setExportRenderers(array $renderers = array())
{
$this->exportRenderers = $renderers;
}

/**
* Get the export renderers
*
* @return array
*/
public function getExportRenderers()
{
if ($this->exportRenderers === null) {
Expand Down Expand Up @@ -540,6 +577,10 @@ public function getColumnByUniqueId($id)
return null;
}

/**
*
* @param boolean $mode
*/
public function setUserFilterDisabled($mode = true)
{
$this->isUserFilterEnabled = (bool) ! $mode;
Expand Down Expand Up @@ -573,6 +614,10 @@ public function getRowClickAction()
return $this->rowClickAction;
}

/**
*
* @return boolean
*/
public function hasRowClickAction()
{
if (is_object($this->rowClickAction)) {
Expand All @@ -595,7 +640,37 @@ public function setRenderer($name = null)
}

/**
* Return the current renderer and give him some knowledge about the rest
* Get the current renderer name
*
* @return string
*/
public function getRendererName()
{
$options = $this->getOptions();
$parameterName = $options['generalParameterNames']['rendererType'];

if ($this->forceRenderer !== null) {
// A special renderer was given -> use is
$rendererName = $this->forceRenderer;
} else {
// DEFAULT
if ($this->getRequest() instanceof ConsoleRequest) {
$rendererName = $options['settings']['default']['renderer']['console'];
} else {
$rendererName = $options['settings']['default']['renderer']['http'];
}
}

// From request
if ($this->getRequest() instanceof HttpRequest && $this->getRequest()->getQuery($parameterName) != '') {
$rendererName = $this->getRequest()->getQuery($parameterName);
}

return $rendererName;
}

/**
* Return the current renderer
*
* @return \ZfcDatagrid\Renderer\AbstractRenderer
*/
Expand Down Expand Up @@ -623,6 +698,8 @@ public function getRenderer()
$renderer->setTitle($this->getTitle());
$renderer->setColumns($this->getColumns());
$renderer->setCacheId($this->getCacheId());
$renderer->setCacheData($this->getCache()
->getItem($this->getCacheId()));

$this->renderer = $renderer;
} else {
Expand All @@ -633,61 +710,32 @@ public function getRenderer()
return $this->renderer;
}

/**
* Get the current renderer name
*
* @return string
*/
public function getRendererName()
public function isDataLoaded()
{
$options = $this->getOptions();
$parameterName = $options['generalParameterNames']['rendererType'];

if ($this->forceRenderer !== null) {
// A special renderer was given -> use is
$rendererName = $this->forceRenderer;
} else {
// DEFAULT
if ($this->getRequest() instanceof ConsoleRequest) {
$rendererName = $options['settings']['default']['renderer']['console'];
} else {
$rendererName = $options['settings']['default']['renderer']['http'];
}
}

//From request
if ($this->getRequest() instanceof HttpRequest && $this->getRequest()->getQuery($parameterName) != '') {
$rendererName = $this->getRequest()->getQuery($parameterName);
}

return $rendererName;
return (bool) $this->isDataLoaded;
}

/**
* Prepare all variables for the view
* - title
* - data
* - grid
* - ...
* Load the data
*/
public function execute()
public function loadData()
{
if ($this->isDataLoaded === true) {
return true;
}

if ($this->isInit() !== true) {
throw new \Exception('The init() method has to be called, before you can call execute()!');
throw new \Exception('The init() method has to be called, before you can call loadData()!');
}

if ($this->hasDataSource() === false) {
throw new \Exception('No datasource defined! So no grid to display...');
throw new \Exception('No datasource defined! Please call "setDataSource()" first"');
}

/**
* Read cache
* Apply cache
*/
$renderer = $this->getRenderer();
$renderer->setCacheData($this->getCache()
->getItem($this->getCacheId()));

$this->isExecuted = true;

/**
* Step 1) Apply needed columns + filters + sort
Expand All @@ -714,6 +762,7 @@ public function execute()
$this->getDataSource()->addFilter($filter);
}
}

/**
* Save cache
*/
Expand Down Expand Up @@ -769,20 +818,49 @@ public function execute()
$prepareData->prepare();
$this->preparedData = $prepareData->getData();

$this->isDataLoaded = true;
}

/**
* @deprecated use render() instead!
*/
public function execute(){
if($this->isRendered() === false){
$this->render();
}
}
/**
* Render the grid
*/
public function render()
{
if($this->isDataLoaded() === false){
$this->loadData();
}

/**
* Step 4) Render the data to the defined output format (HTML, PDF...)
* - Styling the values based on column (and value)
*/
$renderer = $this->getRenderer();
$renderer->setTitle($this->getTitle());
$renderer->setPaginator($this->getPaginator());
$renderer->setData($this->getPreparedData());
$renderer->prepareViewModel($this);

$this->response = $renderer->execute();

$this->isRendered = true;
}

public function isExecuted()
/**
* Is already rendered?
*
* @return boolean
*/
public function isRendered()
{
return (bool) $this->isExecuted;
return (bool) $this->isRendered;
}

/**
Expand All @@ -793,7 +871,7 @@ public function isExecuted()
public function getPaginator()
{
if ($this->paginator === null) {
throw new \Exception('Paginator is only available, after the grid has been executed!');
throw new \Exception('Paginator is only available after calling "loadData()"');
}

return $this->paginator;
Expand Down Expand Up @@ -831,7 +909,7 @@ public function getToolbarTemplate()
public function setViewModel(ViewModel $viewModel)
{
if ($this->viewModel !== null) {
throw new \Exception('A viewModel is already set (did you already called execute()?)');
throw new \Exception('A viewModel is already set (did you already called render()?)');
}

$this->viewModel = $viewModel;
Expand All @@ -856,8 +934,8 @@ public function getViewModel()
*/
public function getResponse()
{
if (! $this->isExecuted()) {
$this->execute();
if (! $this->isRendered()) {
$this->render();
}

return $this->response;
Expand Down
Loading

0 comments on commit e433b1a

Please sign in to comment.