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
{
$jobHistories = $user->getUserJobHistories();
$scopeHistories = $user->getMainScopeHistories();
$jobHistories = $user->getUserJobHistoriesOrdered();
$scopeHistories = $user->getMainScopeHistoriesOrdered();
return new Response(
$this->engine->render('@ChillMain/User/history.html.twig', [
'user' => $user,
'jobHistories' => $jobHistories,
'scopeHistories' => $scopeHistories,
]
));
$this->engine->render(
'@ChillMain/User/history.html.twig',
[
'user' => $user,
'jobHistories' => $jobHistories,
'scopeHistories' => $scopeHistories,
]
)
);
}
}

View File

@ -283,6 +283,22 @@ class User implements UserInterface, \Stringable
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
*/
@ -321,6 +337,22 @@ class User implements UserInterface, \Stringable
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
*/

View File

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