File tree Expand file tree Collapse file tree 5 files changed +38
-8
lines changed
Expand file tree Collapse file tree 5 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,7 @@ public function editAction(Request $request, Menu $menu)
162162 if ($ form ->isSubmitted () && $ form ->isValid ()) {
163163 /** @var Menu $menu */
164164 $ menu = $ form ->getData ();
165+ $ menu ->assignPositions ();
165166 $ this ->em ->persist ($ menu );
166167 $ this ->em ->flush ();
167168
Original file line number Diff line number Diff line change @@ -178,4 +178,15 @@ public function __toString()
178178 {
179179 return (string ) $ this ->id ;
180180 }
181+
182+ public function assignPositions (): void
183+ {
184+ $ rootItems = $ this ->getItemsByParent ();
185+ $ position = 0 ;
186+ foreach ($ rootItems as $ item ) {
187+ $ item ->setPosition ($ position );
188+ $ item ->assignPositions ();
189+ ++$ position ;
190+ }
191+ }
181192}
Original file line number Diff line number Diff line change 1313namespace Novactive \EzMenuManagerBundle \Entity ;
1414
1515use Doctrine \Common \Collections \ArrayCollection ;
16+ use Doctrine \Common \Collections \Criteria ;
1617use Doctrine \ORM \Mapping as ORM ;
1718use Novactive \EzMenuManager \Traits \IdentityTrait ;
1819
@@ -203,7 +204,10 @@ public function hasChildrens(): bool
203204 */
204205 public function getChildrens ()
205206 {
206- return $ this ->childrens ;
207+ $ criteria = new Criteria ();
208+ $ criteria ->orderBy (['position ' => Criteria::ASC ]);
209+
210+ return $ this ->childrens ->matching ($ criteria );
207211 }
208212
209213 /**
@@ -303,7 +307,20 @@ public function __toString()
303307 public function update (array $ properties ): void
304308 {
305309 foreach ($ properties as $ property => $ value ) {
306- $ this ->$ property = $ value ;
310+ if ($ this ->$ property !== $ value ) {
311+ $ this ->$ property = $ value ;
312+ }
313+ }
314+ }
315+
316+ public function assignPositions (): void
317+ {
318+ $ childrens = $ this ->getChildrens ();
319+ $ position = 0 ;
320+ foreach ($ childrens as $ child ) {
321+ $ child ->setPosition ($ position );
322+ $ child ->assignPositions ();
323+ ++$ position ;
307324 }
308325 }
309326}
Original file line number Diff line number Diff line change @@ -78,7 +78,7 @@ public function reverseTransform($value)
7878 }
7979 $ parent = $ menuItemRepo ->find ($ hashItem ['parentId ' ]);
8080 if ($ hashItem ['parentId ' ] && $ parent ) {
81- $ menuItem -> setParent ( $ parent );
81+ $ parent -> addChildren ( $ menuItem );
8282 }
8383 $ menuItem ->setMenu ($ menuRepo ->find ($ hashItem ['menuId ' ]));
8484 $ menuItem ->setPosition ($ hashItem ['position ' ] ?? 0 );
Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ public function fromHash($hash): ?MenuItem
7676 'name ' => $ hash ['name ' ] ?? false ,
7777 'url ' => $ hash ['url ' ] ?? false ,
7878 'target ' => $ hash ['target ' ] ?? false ,
79- 'position ' => $ hash ['position ' ] ?? 0 ,
79+ 'position ' => ( int ) $ hash ['position ' ] ?? 0 ,
8080 ];
8181 $ menuItem ->update (array_filter ($ updateData ));
8282
@@ -85,11 +85,12 @@ public function fromHash($hash): ?MenuItem
8585 $ menuItem ->setOption ($ option , $ value );
8686 }
8787
88- if (isset ($ hash ['parentId ' ]) && $ hash ['parentId ' ]) {
88+ $ currentParent = $ menuItem ->getParent ();
89+ if ($ currentParent && (!isset ($ hash ['parentId ' ]) || null === $ hash ['parentId ' ])) {
90+ $ currentParent ->removeChildren ($ menuItem );
91+ } elseif ($ hash ['parentId ' ] && (!$ currentParent || $ currentParent ->getId () !== $ hash ['parentId ' ])) {
8992 $ parent = $ menuItemRepo ->find ($ hash ['parentId ' ]);
90- $ menuItem ->setParent ($ parent );
91- } else {
92- $ menuItem ->setParent (null );
93+ $ parent ->addChildren ($ menuItem );
9394 }
9495
9596 if (isset ($ hash ['menuId ' ])) {
You can’t perform that action at this time.
0 commit comments