<?php
/**
* Action
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\LisaBundle\Component\DataTable;
use Closure;
use Symfony\Contracts\Translation\TranslatorInterface;
use VisualMedia\LisaBundle\Component\Interfaces\ActionInterface;
use VisualMedia\LisaBundle\Component\Interfaces\DataTableInterface;
use VisualMedia\LisaBundle\Component\Interfaces\EntityInterface;
use VisualMedia\LisaBundle\Helper\Helper;
/**
* Action
*/
class Action implements ActionInterface
{
/**
* Translator
* @var TranslatorInterface
*/
protected $translator;
/**
* DataTable
* @var iDataTable
*/
protected $datatable;
/**
* Title
* @var string
*/
protected $title;
/**
* Path
* @var string
*/
protected $path;
/**
* Target
* @var string
*/
public $target;
/**
* Confirm
* @var string
*/
protected $confirm;
/**
* actionTitle
* @var string
*/
protected $actionTitle;
/**
* Constructor
*
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
public function initialize(DataTableInterface $datatable, $title, $path = '', $target = null, $confirm = null, $actionTitle = null) : ActionInterface
{
$this->datatable = $datatable;
$this->title = $title;
$this->path = $path;
$this->target = $target;
$this->confirm = $confirm;
$this->actionTitle = $actionTitle;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTitle(EntityInterface $row = null): ?string
{
$title = $this->title;
if ($row !== null) {
if (is_callable($title)) {
$title = $title($row);
}
}
if (!preg_match(Helper::phpRegex(Helper::REGEX_HTML), $title)) {
$title = $this->translator->trans($title, [], $this->datatable->getTranslationDomain());
}
return $title;
}
/**
* {@inheritdoc}
*/
public function getPath(EntityInterface $row = null): ?string
{
$path = $this->path;
if ($row !== null) {
if (is_callable($path)) {
return $path($row);
}
}
return $path;
}
/**
* {@inheritdoc}
*/
public function getTarget(EntityInterface $row = null): ?string
{
$target = $this->target;
if ($row !== null) {
if (is_callable($target)) {
return $target($row);
}
}
return $target;
}
/**
* {@inheritdoc}
*/
public function getConfirm(): ?string
{
if ($this->confirm !== null) {
return $this->translator->trans($this->confirm, [], $this->datatable->getTranslationDomain());
}
return null;
}
/**
* {@inheritdoc}
*/
public function getActionTitle(): ?string
{
return $this->actionTitle;
}
}