GetAllViewHelper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Members;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\Domain\Repository\MemberRepository;
  5. use Opentalent\OtCore\Exception\ApiRequestException;
  6. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  7. /**
  8. * This view helper give access to an array named according to the 'as' variable
  9. * and which contains the members of the structure, eventually grouped by instruments
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:members.getAll as="members"
  14. * organizationId="{settings.organizationId}">
  15. * <f:debug>{members}</f:debug>
  16. * </ot:members.getAll>
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetAllViewHelper extends OtAbstractViewHelper {
  21. use TemplateVariableViewHelperTrait;
  22. /**
  23. * >> Required to prevent typo3 to escape the html output
  24. * @var boolean
  25. */
  26. protected $escapeOutput = false;
  27. /**
  28. * @var MemberRepository
  29. *
  30. */
  31. protected $memberRepository;
  32. /**
  33. * -- This method is expected by Fluid --
  34. * Declares the viewhelper's parameters
  35. */
  36. public function initializeArguments()
  37. {
  38. $this->registerArgument(
  39. 'as',
  40. 'string',
  41. 'Name of the returned array',
  42. true
  43. );
  44. $this->registerArgument(
  45. 'organizationId',
  46. 'integer',
  47. 'Id of the current structure',
  48. true
  49. );
  50. $this->registerArgument(
  51. 'groupByInstruments',
  52. 'bool',
  53. 'If true, returns an array of the form [instrument=>[members]],
  54. else the returned array is simply [members]',
  55. false,
  56. false
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Fluid --
  61. * Renders the content as html
  62. *
  63. * @return string
  64. * @throws ApiRequestException
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $organizationId = $this->arguments['organizationId'];
  71. $groupByInstruments = $this->arguments['groupByInstruments'];
  72. try {
  73. // Get members of the structure
  74. $collection = $this->memberRepository->findByOrganizationId($organizationId);
  75. $members = $collection->getMembers();
  76. } catch (ApiRequestException $e) {
  77. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  78. $members = [];
  79. }
  80. // Sort by instrument (conductor first), then alphabetically by name
  81. usort($members,
  82. function($a, $b) {
  83. if ($a->getInstrument() == 'CONDUCTOR') {
  84. return -1;
  85. } else if ($b->getInstrument() == 'CONDUCTOR') {
  86. return 1;
  87. } else if ($a->getInstrument() != $b->getInstrument()) {
  88. return strcmp($a->getInstrument(), $b->getInstrument());
  89. } else {
  90. return strcmp($a->getName(), $b->getName());
  91. }
  92. }
  93. );
  94. if ($groupByInstruments && !empty($members)) {
  95. // Instruments to display in first place (next will be sorted alphabetically)
  96. $stack1 = ['CONDUCTOR' => []];
  97. $stack2 = [];
  98. foreach ($members as $member) {
  99. // If that Instrument exist in stack1: put it in this one
  100. if (array_key_exists($member->getInstrument(), $stack1)) {
  101. array_push($stack1[$member->getInstrument()], $member);
  102. } else {
  103. // Create the new array if needed in stack2, then put the member in it
  104. if (!array_key_exists($member->getInstrument(), $stack2)) {
  105. $stack2[$member->getInstrument()] = [];
  106. }
  107. array_push($stack2[$member->getInstrument()], $member);
  108. }
  109. }
  110. // remove empty instruments in stack 1
  111. $stack1 = array_filter($stack1);
  112. // sort by instrument stack2
  113. ksort($stack2);
  114. $members = array_merge($stack1, $stack2);
  115. }
  116. $variables = [$as => $members];
  117. return $this->renderChildrenWithVariables($variables);
  118. }
  119. /**
  120. * @param MemberRepository $memberRepository
  121. */
  122. public function injectMemberRepository(MemberRepository $memberRepository)
  123. {
  124. $this->memberRepository = $memberRepository;
  125. }
  126. }