GetAllViewHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\EventRepository;
  5. use Opentalent\OtTemplating\Exception\ApiRequestException;
  6. use Psr\Log\LoggerAwareInterface;
  7. use Psr\Log\LoggerAwareTrait;
  8. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  9. /**
  10. * This view helper give access to an array named according to the 'as' variable
  11. * and containing all the upcoming events of the structure
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * <ot:events.getAll as="events"
  16. * organizationId="{settings.organizationId}">
  17. * <f:debug>{events}</f:debug>
  18. * </ot:events.getAll>
  19. *
  20. * @package Opentalent\OtTemplating\ViewHelpers
  21. */
  22. class GetAllViewHelper extends AbstractViewHelper implements LoggerAwareInterface {
  23. use LoggerAwareTrait;
  24. use TemplateVariableViewHelperTrait;
  25. /**
  26. * >> Required to prevent typo3 to escape the html output
  27. * @var boolean
  28. */
  29. protected $escapeOutput = false;
  30. /**
  31. * @var EventRepository
  32. *
  33. */
  34. protected $eventRepository;
  35. /**
  36. * -- This method is expected by Fluid --
  37. * Declares the viewhelper's parameters
  38. */
  39. public function initializeArguments()
  40. {
  41. $this->registerArgument(
  42. 'as',
  43. 'string',
  44. 'Name of the returned array',
  45. true
  46. );
  47. $this->registerArgument(
  48. 'organizationId',
  49. 'integer',
  50. 'Id of the current structure',
  51. true
  52. );
  53. $this->registerArgument(
  54. 'fromChildren',
  55. 'bool',
  56. 'Get events from children instead',
  57. false,
  58. 0
  59. );
  60. }
  61. /**
  62. * -- This method is expected by Fluid --
  63. * Renders the content as html
  64. *
  65. * @return string
  66. * @throws ApiRequestException
  67. */
  68. public function render()
  69. {
  70. // Get current settings
  71. $as = $this->arguments['as'];
  72. $organizationId = $this->arguments['organizationId'];
  73. $fromChildren = $this->arguments['fromChildren'];
  74. $searchParams = [];
  75. if($_REQUEST['search-loc']) {
  76. $searchParams['where'] = $_REQUEST['search-loc'];
  77. }
  78. if($_REQUEST['search-name']) {
  79. $searchParams['what'] = $_REQUEST['search-name'];
  80. }
  81. if($_REQUEST['search-datestart']) {
  82. $dateStart = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-datestart']);
  83. $searchParams['datetimeStart'] = $dateStart->format('d-m-Y');
  84. }
  85. if($_REQUEST['search-dateend']) {
  86. $dateEnd = \DateTime::createFromFormat('d/m/Y', $_REQUEST['search-dateend']);
  87. $searchParams['datetimeEnd'] = $dateEnd->format('d-m-Y');
  88. }
  89. if ($fromChildren) {
  90. $searchParams['children'] = '1';
  91. }
  92. try {
  93. $events = $this->eventRepository->searchBy($organizationId, $searchParams);
  94. } catch (ApiRequestException $e) {
  95. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  96. $events = [];
  97. }
  98. $variables = [$as => $events];
  99. return $this->renderChildrenWithVariables($variables);
  100. }
  101. /**
  102. * @param EventRepository $eventRepository
  103. */
  104. public function injectEventRepository(EventRepository $eventRepository)
  105. {
  106. $this->eventRepository = $eventRepository;
  107. }
  108. }