lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Component/DataTable/Action.php line 94

Open in your IDE?
  1. <?php
  2. /**
  3. * Action
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Component\DataTable;
  8. use Closure;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use VisualMedia\LisaBundle\Component\Interfaces\ActionInterface;
  11. use VisualMedia\LisaBundle\Component\Interfaces\DataTableInterface;
  12. use VisualMedia\LisaBundle\Component\Interfaces\EntityInterface;
  13. use VisualMedia\LisaBundle\Helper\Helper;
  14. /**
  15. * Action
  16. */
  17. class Action implements ActionInterface
  18. {
  19. /**
  20. * Translator
  21. * @var TranslatorInterface
  22. */
  23. protected $translator;
  24. /**
  25. * DataTable
  26. * @var iDataTable
  27. */
  28. protected $datatable;
  29. /**
  30. * Title
  31. * @var string
  32. */
  33. protected $title;
  34. /**
  35. * Path
  36. * @var string
  37. */
  38. protected $path;
  39. /**
  40. * Target
  41. * @var string
  42. */
  43. public $target;
  44. /**
  45. * Confirm
  46. * @var string
  47. */
  48. protected $confirm;
  49. /**
  50. * actionTitle
  51. * @var string
  52. */
  53. protected $actionTitle;
  54. /**
  55. * Constructor
  56. *
  57. * @param TranslatorInterface $translator
  58. */
  59. public function __construct(TranslatorInterface $translator)
  60. {
  61. $this->translator = $translator;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function initialize(DataTableInterface $datatable, $title, $path = '', $target = null, $confirm = null, $actionTitle = null) : ActionInterface
  67. {
  68. $this->datatable = $datatable;
  69. $this->title = $title;
  70. $this->path = $path;
  71. $this->target = $target;
  72. $this->confirm = $confirm;
  73. $this->actionTitle = $actionTitle;
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getTitle(EntityInterface $row = null): ?string
  80. {
  81. $title = $this->title;
  82. if ($row !== null) {
  83. if (is_callable($title)) {
  84. $title = $title($row);
  85. }
  86. }
  87. if (!preg_match(Helper::phpRegex(Helper::REGEX_HTML), $title)) {
  88. $title = $this->translator->trans($title, [], $this->datatable->getTranslationDomain());
  89. }
  90. return $title;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function getPath(EntityInterface $row = null): ?string
  96. {
  97. $path = $this->path;
  98. if ($row !== null) {
  99. if (is_callable($path)) {
  100. return $path($row);
  101. }
  102. }
  103. return $path;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function getTarget(EntityInterface $row = null): ?string
  109. {
  110. $target = $this->target;
  111. if ($row !== null) {
  112. if (is_callable($target)) {
  113. return $target($row);
  114. }
  115. }
  116. return $target;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function getConfirm(): ?string
  122. {
  123. if ($this->confirm !== null) {
  124. return $this->translator->trans($this->confirm, [], $this->datatable->getTranslationDomain());
  125. }
  126. return null;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getActionTitle(): ?string
  132. {
  133. return $this->actionTitle;
  134. }
  135. }