GetAllCaViewHelper.php 4.8 KB

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