Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Memorekall Member New
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rekall
Memorekall Member New
Commits
7e6a8bc5
Commit
7e6a8bc5
authored
3 years ago
by
Sebastien
Browse files
Options
Downloads
Patches
Plain Diff
Fix capsule group filter on index page
parent
4753da59
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!100
Resolve "Filter by group n’apparait que sur la première page de la liste des capsules"
Pipeline
#1118
passed
3 years ago
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Controller/CapsuleController.php
+53
-23
53 additions, 23 deletions
src/Controller/CapsuleController.php
src/Form/FilterByGroupFormType.php
+5
-4
5 additions, 4 deletions
src/Form/FilterByGroupFormType.php
with
58 additions
and
27 deletions
src/Controller/CapsuleController.php
+
53
−
23
View file @
7e6a8bc5
...
...
@@ -13,6 +13,7 @@ use App\Repository\CapsuleRepository;
use
App\Builder\CapsuleBuilder
;
use
App\Form\CreateCapsuleFormType
;
use
Doctrine\ORM\EntityManagerInterface
;
use
Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination
;
use
Knp\Component\Pager\PaginatorInterface
;
use
Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
use
Symfony\Component\Filesystem\Filesystem
;
...
...
@@ -75,45 +76,53 @@ class CapsuleController extends AbstractController
$all_capsules
=
$current_user
->
getCapsules
()
->
toArray
();
$groups
=
$this
->
prependGroupAllToUserGroups
(
$current_user
);
$form
=
$this
->
createForm
(
FilterByGroupFormType
::
class
,
[
'groups'
=>
$groups
,
'method'
=>
'GET'
]);
$group_id
=
$request
->
query
->
getInt
(
'group'
,
Group
::
$GROUP_ALL_ID
);
$group
=
$this
->
getGroupByIdOrNull
(
$groups
,
$group_id
,
$groups
[
0
]);
// Retrieve group from GET data and update form data if required
$paginatorGroupName
=
$request
->
query
->
get
(
'group'
,
''
);
$forceForm
=
false
;
if
(
$paginatorGroupName
!=
''
)
{
$form
->
submit
([
'name'
=>
$paginatorGroupName
]);
$forceForm
=
true
;
}
else
{
$form
->
handleRequest
(
$request
);
}
$filter_by_group_form
=
$this
->
createForm
(
FilterByGroupFormType
::
class
,
[
'groups'
=>
$groups
,
'method'
=>
'GET'
,
'selected_group'
=>
$group
]
);
if
(
$forceForm
||
$form
->
isSubmitted
()
&&
$form
->
isValid
())
{
$group
=
$form
->
getData
()[
'name'
];
$filter_by_group_form
->
handleRequest
(
$request
);
if
(
$filter_by_group_form
->
isSubmitted
()
&&
$filter_by_group_form
->
isValid
())
{
$group
=
$filter_by_group_form
->
getData
()[
'name'
];
if
(
!
$group
instanceof
Group
)
{
throw
new
\Exception
(
'Group not found'
);
}
}
if
(
$group
->
getId
()
!==
Group
::
$GROUP_ALL_ID
)
{
if
(
$group
instanceof
Group
&&
$group
->
getId
()
!==
Group
::
$GROUP_ALL_ID
)
{
$all_capsules
=
$group
->
getCapsules
()
->
toArray
();
}
}
else
{
$group
=
null
;
}
$nb_elements_per_page
=
5
;
// Fix the current page.
// For example, the user is on the page 5 without filter and apply a filter that reduce the number of pages.
// We display the last page.
$current_page
=
min
(
[
intval
((
count
(
$all_capsules
)
/
$nb_elements_per_page
)
+
1
),
$request
->
query
->
getInt
(
'page'
,
1
)
]
);
$request
->
query
->
set
(
'page'
,
$current_page
);
// Pass group param to the paginator (it will be sent as GET data)
$capsules
=
$paginator
->
paginate
(
$all_capsules
,
$
request
->
query
->
getInt
(
'page'
,
1
)
,
5
$
current_page
,
$nb_elements_per_page
);
// Pass group param to the paginator (it will be sent as GET data)
if
(
$group
)
{
$capsules
->
setParam
(
'group'
,
$group
->
getName
());
if
(
$capsules
instanceof
SlidingPagination
)
{
$capsules
->
setParam
(
'group'
,
$group
->
getId
());
}
return
$this
->
renderForm
(
'capsule/index.html.twig'
,
[
'filterByGroupForm'
=>
$form
,
'filterByGroupForm'
=>
$
filter_by_group_
form
,
'capsules'
=>
$capsules
,
'current_user'
=>
$current_user
,
'legacy_url'
=>
$this
->
getParameter
(
'app.legacy_external_prefix'
),
...
...
@@ -387,4 +396,25 @@ class CapsuleController extends AbstractController
return
$groups
;
}
/**
* @param array<Group> $groups
* @param int|null $id
* @param Group $defaultGroup The returned group if current groups isn't found
* @return Group
*/
private
function
getGroupByIdOrNull
(
array
$groups
,
?int
$id
,
Group
$defaultGroup
):
Group
{
if
(
null
===
$id
)
{
return
$defaultGroup
;
}
foreach
(
$groups
as
$g
)
{
if
(
$g
->
getId
()
===
$id
)
{
return
$g
;
}
}
return
$defaultGroup
;
}
}
This diff is collapsed.
Click to expand it.
src/Form/FilterByGroupFormType.php
+
5
−
4
View file @
7e6a8bc5
...
...
@@ -15,7 +15,7 @@ class FilterByGroupFormType extends AbstractType
public
function
buildForm
(
FormBuilderInterface
$builder
,
array
$options
):
void
{
$groups
=
$options
[
'data'
][
'groups'
];
$selected_group
=
$options
[
'data'
][
'selected_group'
];
$builder
->
add
(
'name'
,
ChoiceType
::
class
,
[
'choices'
=>
$groups
,
...
...
@@ -24,9 +24,10 @@ class FilterByGroupFormType extends AbstractType
'choice_label'
=>
function
(
?Group
$entity
)
{
return
$entity
?
$entity
->
getName
()
:
''
;
},
'choice_value'
=>
'name'
,
'choice_name'
=>
'name'
,
'expanded'
=>
false
'choice_value'
=>
'id'
,
'choice_name'
=>
'id'
,
'expanded'
=>
false
,
'data'
=>
$selected_group
]);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment