88use Livewire \Component ;
99use Livewire \WithPagination ;
1010use Secondnetwork \Kompass \Models \Block ;
11- use Secondnetwork \Kompass \Models \Datafield ;
1211use Secondnetwork \Kompass \Models \Post ;
1312
1413class PostsTable extends Component
@@ -93,7 +92,7 @@ public function mount()
9392
9493 public function resetpost ()
9594 {
96- $ this ->mount ( $ this -> post_id );
95+ $ this ->resetpage ( );
9796 }
9897
9998 private function resultDate ()
@@ -140,7 +139,7 @@ public function addPost()
140139 {
141140 $ this ->validate ();
142141
143- $ slugNameURL = Str::slug ($ this ->title , '- ' , 'de ' ); //Convert Input to Str Slug
142+ $ slugNameURL = Str::slug ($ this ->title , '- ' , 'de ' ); // Convert Input to Str Slug
144143
145144 $ placeObj = new Post ;
146145
@@ -153,12 +152,12 @@ public function addPost()
153152 $ newSlug = Str::slug ($ newSlug , '- ' , 'de ' );
154153 $ checkSlug = $ placeObj ->whereSlug ($ newSlug )->exists ();
155154 if (! $ checkSlug ) {
156- $ newpostslug = $ newSlug ; //New Slug
155+ $ newpostslug = $ newSlug ; // New Slug
157156 break ;
158157 }
159158 }
160159 } else {
161- //Slug do not exists. Just use the selected Slug.
160+ // Slug do not exists. Just use the selected Slug.
162161 $ newpostslug = $ slugNameURL ;
163162 }
164163
@@ -182,7 +181,7 @@ public function clone($id)
182181
183182 $ newpost = $ post ->replicate ();
184183
185- $ slugNameURL = Str::slug ($ newpost ['title ' ], '- ' , 'de ' ); //Convert Input to Str Slug
184+ $ slugNameURL = Str::slug ($ newpost ['title ' ], '- ' , 'de ' ); // Convert Input to Str Slug
186185
187186 $ placeObj = new Post ;
188187
@@ -195,44 +194,80 @@ public function clone($id)
195194 $ newSlug = Str::slug ($ newSlug , '- ' , 'de ' );
196195 $ checkSlug = $ placeObj ->whereSlug ($ newSlug )->exists ();
197196 if (! $ checkSlug ) {
198- $ newpost ->slug = $ newSlug ; //New Slug
197+ $ newpost ->slug = $ newSlug ; // New Slug
199198 break ;
200199 }
201200 }
202201 } else {
203- //Slug do not exists. Just use the selected Slug.
202+ // Slug do not exists. Just use the selected Slug.
204203 $ newpost ->slug = $ slugNameURL ;
205204 }
206205 $ newpost ->status = 'draft ' ;
207206 $ newpost ->created_at = Carbon::now ();
208207
209208 $ newpost ->push ();
210209
211- $ blocksclone = Block::where ('post_id ' , $ id )->orderBy ('order ' , 'asc ' )->where ('subgroup ' , null )->with ('children ' )->get ();
210+ $ relationships = ['datafield ' , 'meta ' , 'children ' ];
211+ $ blocksclone = Block::where ('blockable_id ' , $ id )->where ('blockable_type ' , 'post ' )->with ($ relationships )->get ();
212212
213- $ blocksclone ->each (function ($ item , $ key ) use ($ newpost ): void {
214- $ altID = $ item ->id ;
213+ $ rootblock = $ blocksclone ->whereNull ('subgroup ' );
215214
215+ foreach ($ rootblock as $ item ) {
216+ $ blockcopy = $ item ->replicate ();
217+ $ blockcopy ->blockable_id = $ newpost ->id ;
218+ $ blockcopy ->push ();
219+
220+ if ($ itemMeta = $ item ->allMeta ) {
221+ $ mod = Block::find ($ blockcopy ->id );
222+ foreach ($ itemMeta as $ value ) {
223+ $ mod ->saveMeta ([
224+ $ value ->key => $ value ->value ,
225+ ]);
226+ }
227+ }
228+
229+ foreach ($ item ->datafield as $ itemdata ) {
230+ $ copydatablock = $ itemdata ->replicate ();
231+ $ copydatablock ->block_id = $ blockcopy ->id ;
232+ $ copydatablock ->push ();
233+ }
234+
235+ $ children = $ blocksclone ->where ('subgroup ' , $ item ->id );
236+ $ this ->cloneTree ($ children , $ blocksclone , $ newpost ->id , $ blockcopy ->id );
237+ }
238+
239+ $ this ->resetpage ();
240+ }
241+
242+ public function cloneTree ($ categories , $ allCategories , $ cloneid , $ parentId )
243+ {
244+ foreach ($ categories as $ item ) {
216245 $ copy = $ item ->replicate ();
246+ $ copy ->blockable_id = $ cloneid ;
247+ $ copy ->subgroup = $ parentId ;
248+ $ copy ->push ();
249+
250+ if ($ itemMeta = $ item ->allMeta ) {
251+ $ mod = Block::find ($ copy ->id );
217252
218- $ copy ->post_id = $ newpost ->id ;
219- $ copy ->save ();
220- if ($ copy ->children ) {
221- foreach ($ copy ->children as $ subgroup ) {
222- $ copygroup = $ subgroup ->replicate ();
223- $ copygroup ->post_id = $ newpost ->id ;
224- $ copygroup ->subgroup = $ copy ->id ;
225- $ copygroup ->save ();
253+ foreach ($ itemMeta as $ value ) {
254+ $ mod ->saveMeta ([
255+ $ value ->key => $ value ->value ,
256+ ]);
226257 }
227258 }
228259
229- $ fields = Datafield::where ('block_id ' , $ altID )->get ();
230- $ fields ->each (function ($ item , $ key ) use ($ copy ): void {
231- $ copyitem = $ item ->replicate ();
232- $ copyitem ->block_id = $ copy ->id ;
233- $ copyitem ->save ();
234- }, );
235- }, );
260+ foreach ($ item ->datafield as $ itemdata ) {
261+ $ copydatablock = $ itemdata ->replicate ();
262+ $ copydatablock ->block_id = $ copy ->id ;
263+ $ copydatablock ->push ();
264+ }
265+
266+ $ children = $ allCategories ->where ('subgroup ' , $ item ->id );
267+ if ($ children ->isNotEmpty ()) {
268+ $ this ->cloneTree ($ children , $ allCategories , $ cloneid , $ copy ->id );
269+ }
270+ }
236271 }
237272
238273 public function delete ()
0 commit comments