lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Component/DataTable/Column/Column.php line 126

Open in your IDE?
  1. <?php
  2. /**
  3. * Column
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Component\DataTable\Column;
  8. use Closure;
  9. use Exception;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use VisualMedia\LisaBundle\Helper\EntityHelper;
  13. use VisualMedia\LisaBundle\Helper\Helper;
  14. use VisualMedia\LisaBundle\Component\Interfaces\ColumnInterface;
  15. use VisualMedia\LisaBundle\Component\Interfaces\DataTableInterface;
  16. use VisualMedia\LisaBundle\Component\Interfaces\EntityInterface;
  17. use VisualMedia\LisaBundle\Component\Interfaces\ManagerInterface;
  18. /**
  19. * Column
  20. */
  21. class Column implements ColumnInterface
  22. {
  23. /**
  24. * Request
  25. * @var Request
  26. */
  27. protected $request;
  28. /**
  29. * Translator
  30. * @var TranslatorInterface
  31. */
  32. protected $translator;
  33. /**
  34. * DataTable
  35. * @var iDataTable
  36. */
  37. protected $datatable;
  38. /**
  39. * Title
  40. * @var string
  41. */
  42. public $title;
  43. /**
  44. * Content
  45. * @var string|function
  46. */
  47. protected $content;
  48. /**
  49. * Ordering
  50. * @var string|array
  51. *
  52. * true = orderable
  53. * false = not orderable
  54. * array = order by [ 'field' => 'ASC|DESC' ]
  55. */
  56. public $ordering = false;
  57. /**
  58. * Header Template
  59. * @var string
  60. */
  61. public $headerTemplate = self::DEFAULT_HEADER_TEMPLATE;
  62. /**
  63. * Body Template
  64. * @var string
  65. */
  66. public $bodyTemplate = self::DEFAULT_BODY_TEMPLATE;
  67. /**
  68. * Footer Template
  69. * @var string
  70. */
  71. public $footerTemplate = self::DEFAULT_FOOTER_TEMPLATE;
  72. /**
  73. * Constructor
  74. *
  75. * @param RequestStack $requestStack
  76. * @param TranslatorInterface $translator
  77. */
  78. public function __construct(RequestStack $requestStack, TranslatorInterface $translator)
  79. {
  80. $this->request = $requestStack->getMasterRequest();
  81. $this->translator = $translator;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function initialize(DataTableInterface $datatable, $title = null, $content = null, $ordering = false): ColumnInterface
  87. {
  88. $this->datatable = $datatable;
  89. $this->title = $this->translator->trans($title, [], $this->datatable->getTranslationDomain());
  90. $this->content = $content;
  91. $queryOrderBy = $this->request->query->get(static::QUERY_ORDER_BY);
  92. $queryOrderDir = $this->request->query->get(static::QUERY_ORDER_DIR);
  93. if ($ordering === true && $this->getContent() === $queryOrderBy && $queryOrderBy !== null) {
  94. $this->ordering = array(
  95. static::QUERY_ORDER_BY => $queryOrderBy,
  96. static::QUERY_ORDER_DIR => $queryOrderDir,
  97. );
  98. }
  99. else {
  100. $this->ordering = $ordering;
  101. }
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getContent(EntityInterface $row = null)
  108. {
  109. $content = $this->content;
  110. if ($row !== null) {
  111. if ($content === null) {
  112. return '';
  113. }
  114. elseif ($content instanceof Closure) {
  115. return $content($row);
  116. }
  117. elseif (method_exists($row, $getter = EntityHelper::getter($content))) {
  118. if (is_object($result = $row->{ $getter }())) {
  119. if (method_exists($result, '__toString')) {
  120. return (string)$result;
  121. }
  122. }
  123. return $result;
  124. }
  125. elseif (property_exists($row, $content)) {
  126. return $row->{ $content };
  127. }
  128. else {
  129. throw new Exception($this->translator->trans('datatable.column.not_found', array(
  130. '%content%' => $content,
  131. '%class%' => get_class($row),
  132. ), 'exception'));
  133. }
  134. }
  135. else if ($content instanceof Closure) {
  136. return $content();
  137. }
  138. return $content;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function getNewOrderDir()
  144. {
  145. if (isset($this->ordering[static::QUERY_ORDER_DIR])) {
  146. return $this->ordering[static::QUERY_ORDER_DIR] === ManagerInterface::ORDER_DIR_ASC ? ManagerInterface::ORDER_DIR_DESC : ManagerInterface::ORDER_DIR_ASC;
  147. }
  148. else {
  149. return ManagerInterface::ORDER_DIR_ASC;
  150. }
  151. }
  152. }