GetChildrenViewHelper.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  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. }
  53. /**
  54. * -- This method is expected by Fluid --
  55. * Renders the content as html
  56. *
  57. * @return string
  58. * @throws ApiRequestException
  59. */
  60. public function render()
  61. {
  62. $as = $this->arguments['as'];
  63. $organizationId = $this->arguments['organizationId'];
  64. $searchParams = [];
  65. if($_REQUEST['search-loc']) {
  66. $searchParams['where'] = $_REQUEST['search-loc'];
  67. }
  68. try {
  69. $organizations = $this->organizationRepository->findChildrenById($organizationId, $searchParams);
  70. } catch (ApiRequestException $e) {
  71. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  72. $organizations = [];
  73. }
  74. $variables = [$as => $organizations];
  75. return $this->renderChildrenWithVariables($variables);
  76. }
  77. /**
  78. * @param OrganizationRepository $organizationRepository
  79. */
  80. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  81. {
  82. $this->organizationRepository = $organizationRepository;
  83. }
  84. }