improve histories order since User entity

This commit is contained in:
Mathieu Jaumotte 2023-10-04 12:47:23 +02:00
parent c526973475
commit 0b6b25fd95
3 changed files with 45 additions and 11 deletions

View File

@ -28,16 +28,18 @@ class UserJobScopeHistoriesController extends AbstractController
*/ */
public function indexAction(User $user): Response public function indexAction(User $user): Response
{ {
$jobHistories = $user->getUserJobHistories(); $jobHistories = $user->getUserJobHistoriesOrdered();
$scopeHistories = $user->getMainScopeHistories(); $scopeHistories = $user->getMainScopeHistoriesOrdered();
return new Response( return new Response(
$this->engine->render('@ChillMain/User/history.html.twig', [ $this->engine->render(
'@ChillMain/User/history.html.twig',
[
'user' => $user, 'user' => $user,
'jobHistories' => $jobHistories, 'jobHistories' => $jobHistories,
'scopeHistories' => $scopeHistories, 'scopeHistories' => $scopeHistories,
] ]
)); )
);
} }
} }

View File

@ -283,6 +283,22 @@ class User implements UserInterface, \Stringable
return $this->scopeHistories; return $this->scopeHistories;
} }
/**
* @return ArrayCollection|UserScopeHistory[]
*/
public function getMainScopeHistoriesOrdered(): ArrayCollection
{
$scopeHistories = $this->getMainScopeHistories();
$sortedScopeHistories = $scopeHistories->toArray();
usort($sortedScopeHistories, function ($a, $b) {
return $a->getStartDate() < $b->getStartDate() ? 1 : -1;
});
return new ArrayCollection($sortedScopeHistories);
}
/** /**
* @return string * @return string
*/ */
@ -321,6 +337,22 @@ class User implements UserInterface, \Stringable
return $this->jobHistories; return $this->jobHistories;
} }
/**
* @return ArrayCollection|UserJobHistory[]
*/
public function getUserJobHistoriesOrdered(): ArrayCollection
{
$jobHistories = $this->getUserJobHistories();
$sortedJobHistories = $jobHistories->toArray();
usort($sortedJobHistories, function ($a, $b) {
return $a->getStartDate() < $b->getStartDate() ? 1 : -1;
});
return new ArrayCollection($sortedJobHistories);
}
/** /**
* @return string * @return string
*/ */

View File

@ -16,7 +16,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for entity in jobHistories|reverse %} {% for entity in jobHistories %}
<tr> <tr>
<td>{{ entity.startDate|format_datetime('medium') }}</td> <td>{{ entity.startDate|format_datetime('medium') }}</td>
<td> <td>
@ -48,7 +48,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for entity in scopeHistories|reverse %} {% for entity in scopeHistories %}
<tr> <tr>
<td>{{ entity.startDate|format_datetime('medium') }}</td> <td>{{ entity.startDate|format_datetime('medium') }}</td>
<td> <td>