GetChildrenViewHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\OrganizationRepository;
  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 returns the organization children structures as an ApiPagedCollection
  11. *
  12. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  13. *
  14. * <ot:organizations.getChildren as="organization"
  15. * organizationId="1">
  16. * <f:debug>{organization}</f:debug>
  17. * </ot:organizations.getChildren>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class GetChildrenViewHelper extends AbstractViewHelper implements LoggerAwareInterface {
  22. use LoggerAwareTrait;
  23. use TemplateVariableViewHelperTrait;
  24. /**
  25. * >> Required to prevent typo3 to escape the html output
  26. * @var boolean
  27. */
  28. protected $escapeOutput = false;
  29. /**
  30. * @var OrganizationRepository
  31. *
  32. */
  33. protected $organizationRepository;
  34. /**
  35. * -- This method is expected by Fluid --
  36. * Declares the viewhelper's parameters
  37. */
  38. public function initializeArguments()
  39. {
  40. $this->registerArgument(
  41. 'as',
  42. 'string',
  43. 'Name of the returned array',
  44. true
  45. );
  46. $this->registerArgument(
  47. 'organizationId',
  48. 'integer',
  49. 'Id of the organization',
  50. true
  51. );
  52. $this->registerArgument(
  53. 'page',
  54. 'integer',
  55. 'Page number',
  56. false,
  57. 1
  58. );
  59. }
  60. /**
  61. * -- This method is expected by Fluid --
  62. * Renders the content as html
  63. *
  64. * @return string
  65. */
  66. public function render()
  67. {
  68. $as = $this->arguments['as'];
  69. $organizationId = $this->arguments['organizationId'];
  70. $page = $this->arguments['page'];
  71. $searchParams = [];
  72. if($_REQUEST['search-loc']) {
  73. $searchParams['where'] = $_REQUEST['search-loc'];
  74. }
  75. try {
  76. $organizations = $this->organizationRepository->findChildrenById(
  77. $organizationId,
  78. $searchParams,
  79. $page
  80. );
  81. } catch (ApiRequestException $e) {
  82. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  83. $organizations = [];
  84. }
  85. $variables = [
  86. $as => $organizations
  87. ];
  88. return $this->renderChildrenWithVariables($variables);
  89. }
  90. /**
  91. * @param OrganizationRepository $organizationRepository
  92. */
  93. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  94. {
  95. $this->organizationRepository = $organizationRepository;
  96. }
  97. }