GetAllCaViewHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Members;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\MemberRepository;
  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 which contains the CA members of the structure classed by role
  12. *
  13. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  14. *
  15. * <ot:members.getAllCa as="members"
  16. * organizationId="{settings.organizationId}">
  17. * <f:debug>{members}</f:debug>
  18. * </ot:members.getAllCa>
  19. *
  20. * @package Opentalent\OtTemplating\ViewHelpers
  21. */
  22. class GetAllCaViewHelper 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 MemberRepository
  32. *
  33. */
  34. protected $memberRepository;
  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. 'groupByMissions',
  55. 'bool',
  56. 'If true, returns an array of the form [mission=>[members]],
  57. else the returned array is simply [members]',
  58. false,
  59. false
  60. );
  61. }
  62. /**
  63. * -- This method is expected by Fluid --
  64. * Renders the content as html
  65. *
  66. * @return string
  67. * @throws \GuzzleHttp\Exception\GuzzleException
  68. * @throws \Opentalent\OtTemplating\Exception\ApiRequestException
  69. */
  70. public function render()
  71. {
  72. // Get current settings
  73. $as = $this->arguments['as'];
  74. $organizationId = $this->arguments['organizationId'];
  75. $groupByMissions = $this->arguments['groupByMissions'];
  76. // Missions to display (sorted)
  77. $missions = [
  78. 'PRESIDENT',
  79. 'HONORARY_PRESIDENT',
  80. 'VICE_PRESIDENT',
  81. 'VICE_PRESIDENT_OF_HONOR',
  82. 'PRESIDENT_ASSISTANT',
  83. 'HOUR_PRESIDENT',
  84. 'SECRETARY',
  85. 'ASSISTANT_SECRETARY',
  86. 'TREASURER',
  87. 'TREASURER_ASSISTANT',
  88. 'MEMBER_OF_THE_BOARD',
  89. 'MEMBER_OF_BOARD_OF_HONOR',
  90. 'ACTIVE_COOPTED_BOARD_MEMBER',
  91. 'ACTIVE_MEMBER_OF_THE_CA',
  92. 'HONORARY_MEMBER',
  93. 'YOUTH_REPRESENTATIVE'
  94. ];
  95. // Get members of the structure (only CA members)
  96. try {
  97. $members = $this->memberRepository->findByOrganizationId($organizationId, true);
  98. } catch (ApiRequestException $e) {
  99. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  100. $members = [];
  101. }
  102. $members = array_filter($members, function($m) use ($missions) {
  103. return array_search($m->getMission(), $missions) !== false;
  104. });
  105. // Sort by roles, then alphabetically by name
  106. usort($members,
  107. function($a, $b) use ($missions) {
  108. if ($a->getMission() != $b->getMission()) {
  109. $ia = array_search($a->getMission(), $missions);
  110. $ib = array_search($b->getMission(), $missions);
  111. return $ia - $ib;
  112. } else {
  113. return strcmp($a->getName(), $b->getName());
  114. }
  115. }
  116. );
  117. if ($groupByMissions && !empty($members)) {
  118. // Missions to display (sorted)
  119. $membersByMission = [];
  120. foreach ($missions as $mission) {
  121. $membersByMission[$mission] = [];
  122. }
  123. // Put members into their categories
  124. foreach ($members as $member) {
  125. if (array_key_exists($member->getMission(), $membersByMission)) {
  126. array_push($membersByMission[$member->getMission()], $member);
  127. }
  128. }
  129. // Remove empty sections
  130. $members = array_filter($membersByMission);
  131. }
  132. $variables = [$as => $members];
  133. return $this->renderChildrenWithVariables($variables);
  134. }
  135. /**
  136. * @param MemberRepository $memberRepository
  137. */
  138. public function injectMemberRepository(MemberRepository $memberRepository)
  139. {
  140. $this->memberRepository = $memberRepository;
  141. }
  142. }