@@ -741,6 +741,113 @@ public function delProfilePicture()
741741 return back ();
742742 }
743743
744+ //Export user links
745+ public function exportLinks (request $ request )
746+ {
747+ $ userId = Auth::id ();
748+ $ user = User::find ($ userId );
749+ $ links = Link::where ('user_id ' , $ userId )->get ();
750+
751+ if (!$ user ) {
752+ // handle the case where the user is null
753+ return response ()->json (['message ' => 'User not found ' ], 404 );
754+ }
755+
756+ $ userData ['links ' ] = $ links ->toArray ();
757+
758+ $ fileName = 'links.json ' ;
759+ $ headers = [
760+ 'Content-Type ' => 'application/json ' ,
761+ 'Content-Disposition ' => 'attachment; filename=" ' .$ fileName .'" ' ,
762+ ];
763+ return response ()->json ($ userData , 200 , $ headers );
764+
765+ return back ();
766+ }
767+
768+ //Export all user data
769+ public function exportAll (request $ request )
770+ {
771+ $ userId = Auth::id ();
772+ $ user = User::find ($ userId );
773+ $ links = Link::where ('user_id ' , $ userId )->get ();
774+
775+ if (!$ user ) {
776+ // handle the case where the user is null
777+ return response ()->json (['message ' => 'User not found ' ], 404 );
778+ }
779+
780+ $ userData = $ user ->toArray ();
781+ $ userData ['links ' ] = $ links ->toArray ();
782+
783+ $ fileName = 'user_data.json ' ;
784+ $ headers = [
785+ 'Content-Type ' => 'application/json ' ,
786+ 'Content-Disposition ' => 'attachment; filename=" ' .$ fileName .'" ' ,
787+ ];
788+ return response ()->json ($ userData , 200 , $ headers );
789+
790+ return back ();
791+ }
792+
793+ //Import user data from file
794+ public function importData (Request $ request )
795+ {
796+ try {
797+ // Get the JSON data from the uploaded file
798+ if (!$ request ->hasFile ('import ' ) || !$ request ->file ('import ' )->isValid ()) {
799+ throw new \Exception ('File not uploaded or is faulty ' );
800+ }
801+ $ file = $ request ->file ('import ' );
802+ $ jsonString = $ file ->get ();
803+ $ userData = json_decode ($ jsonString , true );
804+
805+ // Update the authenticated user's profile data if defined in the JSON file
806+ $ user = auth ()->user ();
807+ if (isset ($ userData ['name ' ])) {
808+ $ user ->name = $ userData ['name ' ];
809+ }
810+ if (isset ($ userData ['littlelink_name ' ])) {
811+ $ user ->littlelink_name = $ userData ['littlelink_name ' ];
812+ }
813+ if (isset ($ userData ['littlelink_description ' ])) {
814+ $ user ->littlelink_description = $ userData ['littlelink_description ' ];
815+ }
816+ if (isset ($ userData ['image ' ])) {
817+ $ user ->image = $ userData ['image ' ];
818+ }
819+ $ user ->save ();
820+
821+ // Delete all links for the authenticated user
822+ Link::where ('user_id ' , $ user ->id )->delete ();
823+
824+ // Loop through each link in $userData and create a new link for the user
825+ foreach ($ userData ['links ' ] as $ linkData ) {
826+ $ newLink = new Link ();
827+
828+ // Copy over the link data from $linkData to $newLink
829+ $ newLink ->button_id = $ linkData ['button_id ' ];
830+ $ newLink ->link = $ linkData ['link ' ];
831+ $ newLink ->title = $ linkData ['title ' ];
832+ $ newLink ->order = $ linkData ['order ' ];
833+ $ newLink ->click_number = $ linkData ['click_number ' ];
834+ $ newLink ->up_link = $ linkData ['up_link ' ];
835+ $ newLink ->custom_css = $ linkData ['custom_css ' ];
836+ $ newLink ->custom_icon = $ linkData ['custom_icon ' ];
837+
838+ // Set the user ID to the current user's ID
839+ $ newLink ->user_id = $ user ->id ;
840+
841+ // Save the new link to the database
842+ $ newLink ->save ();
843+ }
844+
845+ return redirect ('studio/profile ' )->with ('success ' , 'Profile updated successfully! ' );
846+ } catch (\Exception $ e ) {
847+ return redirect ('studio/profile ' )->with ('error ' , 'An error occurred while updating your profile. ' );
848+ }
849+ }
850+
744851 //Edit/save page icons
745852 public function editIcons (request $ request )
746853 {
0 commit comments