Skip to content

Commit

Permalink
TButtonRenderer (so even ToolbarButton can be rendered by custom call…
Browse files Browse the repository at this point in the history
…back)
  • Loading branch information
paveljanda committed Jan 6, 2018
1 parent 1bf24dc commit ec26a25
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 133 deletions.
9 changes: 3 additions & 6 deletions src/Column/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class Action extends Column
{
use Traits\TButtonRenderer;
use Traits\TButtonTryAddIcon;
use Traits\TButtonText;
use Traits\TLink;
Expand Down Expand Up @@ -109,15 +110,11 @@ public function __construct(DataGrid $grid, $href, $name, $params)
*/
public function render(Row $row)
{
/**
* Renderer function may be used
*/
try {
// Renderer function may be used
return $this->useRenderer($row);
} catch (DataGridColumnRendererException $e) {
/**
* Do not use renderer
*/
// Do not use renderer
}

$link = $this->createLink(
Expand Down
126 changes: 1 addition & 125 deletions src/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@

abstract class Column extends FilterableColumn
{
use Traits\TButtonRenderer;
use Traits\TLink;

/**
* @var array
*/
protected $replacements = [];

/**
* @var Renderer|NULL
*/
protected $renderer;

/**
* @var string
*/
Expand Down Expand Up @@ -139,31 +130,6 @@ public function render(Row $row)
}


/**
* Try to render item with custom renderer
* @param Row $row
* @return mixed
*/
public function useRenderer(Row $row)
{
$renderer = $this->getRenderer();

if (!$renderer) {
throw new DataGridColumnRendererException;
}

if ($renderer->getConditionCallback()) {
if (!call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) {
throw new DataGridColumnRendererException;
}

return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
}

return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
}


/**
* Should be column values escaped in latte?
* @param bool $template_escaping
Expand Down Expand Up @@ -328,96 +294,6 @@ public function getName()
}


/**
* Set column replacements
* @param array $replacements
* @return Column
*/
public function setReplacement(array $replacements)
{
$this->replacements = $replacements;

return $this;
}


/**
* Tell whether columns has replacements
* @return bool
*/
public function hasReplacements()
{
return (bool) $this->replacements;
}


/**
* Apply replacements
* @param Row $row
* @return array
*/
public function applyReplacements(Row $row)
{
$value = $row->getValue($this->column);

if ((is_scalar($value) || $value === null) && isset($this->replacements[$value])) {
return [true, $this->replacements[$value]];
}

return [false, null];
}


/**
* Set renderer callback and (it may be optional - the condition callback will decide)
* @param callable $renderer
*/
public function setRenderer($renderer, $condition_callback = null)
{
if ($this->hasReplacements()) {
throw new DataGridException(
'Use either Column::setReplacement() or Column::setRenderer, not both.'
);
}

if (!is_callable($renderer)) {
throw new DataGridException(
'Renderer (method Column::setRenderer()) must be callable.'
);
}

if ($condition_callback != null&& !is_callable($condition_callback)) {
throw new DataGridException(
'Renderer (method Column::setRenderer()) must be callable.'
);
}

$this->renderer = new Renderer($renderer, $condition_callback);

return $this;
}


/**
* Set renderer callback just if condition is truthy
* @param callable $renderer
*/
public function setRendererOnCondition($renderer, $condition_callback)
{
return $this->setRenderer($renderer, $condition_callback);
}


/**
* Return custom renderer callback
* @return Renderer|null
*/
public function getRenderer()
{
return $this->renderer;
}


/**
* Column may have its own template
* @param string $template
Expand Down
13 changes: 11 additions & 2 deletions src/Toolbar/ToolbarButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

use Nette\Utils\Html;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
use Ublaboo\DataGrid\Traits;

class ToolbarButton
{
use Traits\TButtonTryAddIcon;
use Traits\TButtonIcon;
use Traits\TButtonClass;
use Traits\TButtonTitle;
use Traits\TButtonIcon;
use Traits\TButtonRenderer;
use Traits\TButtonText;
use Traits\TButtonTitle;
use Traits\TLink;

/**
Expand Down Expand Up @@ -63,6 +65,13 @@ public function __construct(DataGrid $grid, $href, $text, $params = [])
*/
public function renderButton()
{
try {
// Renderer function may be used
return $this->useRenderer();
} catch (DataGridColumnRendererException $e) {
// Do not use renderer
}

$link = $this->createLink($this->grid, $this->href, $this->params);

$a = Html::el('a')->href($link);
Expand Down
146 changes: 146 additions & 0 deletions src/Traits/TButtonRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

/**
* @copyright Copyright (c) 2015 ublaboo <[email protected]>
* @author Pavel Janda <[email protected]>
* @package Ublaboo
*/

namespace Ublaboo\DataGrid\Traits;

use Ublaboo\DataGrid\Column\Renderer;
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
use Ublaboo\DataGrid\Exception\DataGridException;
use Ublaboo\DataGrid\Row;

trait TButtonRenderer
{

/**
* @var Renderer|null
*/
protected $renderer;

/**
* @var array
*/
protected $replacements = [];


/**
* Try to render item with custom renderer
* @param Row|null $row
* @return mixed
* @throws DataGridColumnRendererException
*/
public function useRenderer(?Row $row = null)
{
$renderer = $this->getRenderer();
$args = $row ? [$row->getItem()] : [];

if (!$renderer) {
throw new DataGridColumnRendererException;
}

if ($renderer->getConditionCallback()) {
if (!call_user_func_array($renderer->getConditionCallback(), $args)) {
throw new DataGridColumnRendererException;
}

return call_user_func_array($renderer->getCallback(), $args);
}

return call_user_func_array($renderer->getCallback(), $args);
}


/**
* Set renderer callback and (it may be optional - the condition callback will decide)
* @param callable $renderer
* @throws DataGridException
*/
public function setRenderer($renderer, $condition_callback = null)
{
if ($this->hasReplacements()) {
throw new DataGridException(
'Use either Column::setReplacement() or Column::setRenderer, not both.'
);
}

if (!is_callable($renderer)) {
throw new DataGridException(
'Renderer (method Column::setRenderer()) must be callable.'
);
}

if ($condition_callback != null && !is_callable($condition_callback)) {
throw new DataGridException(
'Renderer (method Column::setRenderer()) must be callable.'
);
}

$this->renderer = new Renderer($renderer, $condition_callback);

return $this;
}


/**
* Set renderer callback just if condition is truthy
* @param callable $renderer
*/
public function setRendererOnCondition($renderer, $condition_callback)
{
return $this->setRenderer($renderer, $condition_callback);
}


/**
* Return custom renderer callback
* @return Renderer|null
*/
public function getRenderer()
{
return $this->renderer;
}


/**
* Set column replacements
* @param array $replacements
* @return Column
*/
public function setReplacement(array $replacements)
{
$this->replacements = $replacements;

return $this;
}


/**
* Tell whether columns has replacements
* @return bool
*/
public function hasReplacements()
{
return (bool) $this->replacements;
}


/**
* Apply replacements
* @param Row $row
* @return array
*/
public function applyReplacements(Row $row)
{
$value = $row->getValue($this->column);

if ((is_scalar($value) || $value === null) && isset($this->replacements[$value])) {
return [true, $this->replacements[$value]];
}

return [false, null];
}
}

0 comments on commit ec26a25

Please sign in to comment.