Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
a7548229
Commit
a7548229
authored
Nov 16, 2014
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Save child items recursively
parent
90fcd7a0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
14 deletions
+41
-14
MenuService.php
Services/MenuService.php
+41
-14
No files found.
Services/MenuService.php
View file @
a7548229
...
...
@@ -4,33 +4,60 @@ use Modules\Menu\Repositories\MenuItemRepository;
class
MenuService
{
/**
* Current Menu Item being looped over
* @var
*/
protected
$menuItem
;
/**
* @var MenuItemRepository
*/
private
$menuItem
;
private
$menuItem
Repository
;
public
function
__construct
(
MenuItemRepository
$menuItem
)
{
$this
->
menuItem
=
$menuItem
;
$this
->
menuItem
Repository
=
$menuItem
;
}
public
function
handle
(
$item
,
$position
)
{
// Find menu item : $menuItem['id'] ->setRoot
$menuItem
=
$this
->
menuItem
->
find
(
$item
[
'id'
]);
$menuItem
->
position
=
$position
;
$menuItem
->
save
();
$menuItem
->
makeRoot
();
$this
->
menuItem
=
$this
->
menuItemRepository
->
find
(
$item
[
'id'
]);
$this
->
savePosition
(
$this
->
menuItem
,
$position
);
$this
->
menuItem
->
makeRoot
();
// If hasChildren ? set parent ˆ (recursive)
if
(
isset
(
$item
[
'children'
]))
{
foreach
(
$item
[
'children'
]
as
$childPosition
=>
$childItem
)
{
$childMenuItem
=
$this
->
menuItem
->
find
(
$childItem
[
'id'
]);
$childMenuItem
->
position
=
$childPosition
;
$childMenuItem
->
save
();
$childMenuItem
->
makeChildOf
(
$menuItem
);
}
if
(
$this
->
hasChildren
(
$item
))
{
$this
->
setChildrenRecursively
(
$item
,
$this
->
menuItem
);
}
}
/**
* @param $item
* @return bool
*/
private
function
hasChildren
(
$item
)
{
return
isset
(
$item
[
'children'
]);
}
private
function
setChildrenRecursively
(
$item
,
$parent
)
{
foreach
(
$item
[
'children'
]
as
$childPosition
=>
$childItem
)
{
$childMenuItem
=
$this
->
menuItemRepository
->
find
(
$childItem
[
'id'
]);
$this
->
savePosition
(
$childMenuItem
,
$childPosition
);
$childMenuItem
->
makeChildOf
(
$parent
);
if
(
$this
->
hasChildren
(
$childItem
))
$this
->
setChildrenRecursively
(
$childItem
,
$childMenuItem
);
}
// If no more children finish the recursive call
}
/**
* @param $item
* @param $position
*/
private
function
savePosition
(
$item
,
$position
)
{
$item
->
position
=
$position
;
$item
->
save
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment