diff --git a/src/Column/Action.php b/src/Column/Action.php index ac8961f1..1bdf06c1 100644 --- a/src/Column/Action.php +++ b/src/Column/Action.php @@ -17,6 +17,7 @@ class Action extends Column { + use Traits\TButtonRenderer; use Traits\TButtonTryAddIcon; use Traits\TButtonText; use Traits\TLink; @@ -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( diff --git a/src/Column/Column.php b/src/Column/Column.php index cbeae4de..a3f10ffa 100644 --- a/src/Column/Column.php +++ b/src/Column/Column.php @@ -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 */ @@ -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 @@ -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 diff --git a/src/Toolbar/ToolbarButton.php b/src/Toolbar/ToolbarButton.php index f8a8e991..d9514aaf 100644 --- a/src/Toolbar/ToolbarButton.php +++ b/src/Toolbar/ToolbarButton.php @@ -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; /** @@ -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); diff --git a/src/Traits/TButtonRenderer.php b/src/Traits/TButtonRenderer.php new file mode 100644 index 00000000..fec50b3c --- /dev/null +++ b/src/Traits/TButtonRenderer.php @@ -0,0 +1,146 @@ + + * @author Pavel Janda + * @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]; + } +}