2020-04-29 17:15:54 +00:00
< ? php
namespace App\Http\Controllers ;
2020-05-13 21:47:51 +00:00
use App\Http\Requests\ProfileSave ;
use Illuminate\Support\Facades\Log ;
use App\Profile ;
2020-06-26 23:32:33 +00:00
use App\User ;
use App\Facades\IP ;
use Carbon\Carbon ;
2020-04-29 17:15:54 +00:00
use Illuminate\Http\Request ;
2020-05-13 21:47:51 +00:00
use Illuminate\Support\Facades\Auth ;
2020-06-26 23:32:33 +00:00
use Spatie\Permission\Models\Role ;
2020-04-29 17:15:54 +00:00
class ProfileController extends Controller
{
2020-06-27 18:15:33 +00:00
public function index ()
{
return view ( 'dashboard.user.directory' )
-> with ( 'users' , User :: with ( 'profile' , 'bans' ) -> paginate ( 9 ));
}
2020-05-13 21:47:51 +00:00
public function showProfile ()
{
2020-06-26 23:32:33 +00:00
$socialLinks = Auth :: user () -> profile -> socialLinks ? ? " [] " ;
$socialMediaProfiles = json_decode ( $socialLinks , true );
2020-05-13 21:47:51 +00:00
return view ( 'dashboard.user.profile.userprofile' )
-> with ([
'profile' => Auth :: user () -> profile ,
'github' => $socialMediaProfiles [ 'links' ][ 'github' ] ? ? 'UpdateMe' ,
'twitter' => $socialMediaProfiles [ 'links' ][ 'twitter' ] ? ? 'UpdateMe' ,
'insta' => $socialMediaProfiles [ 'links' ][ 'insta' ] ? ? 'UpdateMe' ,
'discord' => $socialMediaProfiles [ 'links' ][ 'discord' ] ? ? 'UpdateMe#12345' ,
]);
}
2020-04-30 21:53:57 +00:00
2020-06-26 23:32:33 +00:00
// Route model binding
public function showSingleProfile ( Request $request , User $user )
2020-04-30 21:53:57 +00:00
{
2020-05-13 21:47:51 +00:00
2020-06-26 23:32:33 +00:00
$socialMediaProfiles = json_decode ( $user -> profile -> socialLinks , true );
$createdDate = Carbon :: parse ( $user -> created_at );
$systemRoles = Role :: all () -> pluck ( 'name' ) -> all ();
$userRoles = $user -> roles -> pluck ( 'name' ) -> all ();
$roleList = [];
foreach ( $systemRoles as $role )
{
if ( in_array ( $role , $userRoles ))
{
$roleList [ $role ] = true ;
}
else
{
$roleList [ $role ] = false ;
}
}
if ( Auth :: user () -> is ( $user ) || Auth :: user () -> can ( 'profiles.view.others' ))
{
return view ( 'dashboard.user.profile.displayprofile' )
-> with ([
'profile' => $user -> profile ,
'github' => $socialMediaProfiles [ 'links' ][ 'github' ] ? ? 'UpdateMe' ,
'twitter' => $socialMediaProfiles [ 'links' ][ 'twitter' ] ? ? 'UpdateMe' ,
'insta' => $socialMediaProfiles [ 'links' ][ 'insta' ] ? ? 'UpdateMe' ,
'discord' => $socialMediaProfiles [ 'links' ][ 'discord' ] ? ? 'UpdateMe#12345' ,
'since' => $createdDate -> englishMonth . " " . $createdDate -> year ,
'ipInfo' => IP :: lookup ( $user -> originalIP ),
'roles' => $roleList
]);
}
else
{
abort ( 403 , 'You cannot view someone else\'s profile.' );
}
}
public function saveProfile ( ProfileSave $request )
{
// TODO: Switch to route model binding
$profile = User :: find ( Auth :: user () -> id ) -> profile ;
2020-05-13 21:47:51 +00:00
$social = [];
if ( ! is_null ( $profile ))
{
switch ( $request -> avatarPref )
{
case 'MOJANG' :
$avatarPref = 'crafatar' ;
break ;
case 'GRAVATAR' :
$avatarPref = strtolower ( $request -> avatarPref );
break ;
}
$social [ 'links' ][ 'github' ] = $request -> socialGithub ;
$social [ 'links' ][ 'twitter' ] = $request -> socialTwitter ;
$social [ 'links' ][ 'insta' ] = $request -> socialInsta ;
$social [ 'links' ][ 'discord' ] = $request -> socialDiscord ;
$profile -> profileShortBio = $request -> shortBio ;
$profile -> profileAboutMe = $request -> aboutMe ;
$profile -> avatarPreference = $avatarPref ;
$profile -> socialLinks = json_encode ( $social );
2020-06-26 23:32:33 +00:00
$newProfile = $profile -> save ();
2020-05-13 21:47:51 +00:00
$request -> session () -> flash ( 'success' , 'Profile settings saved successfully.' );
}
else
{
$gm = 'Guru Meditation #' . rand ( 0 , 1000 );
Log :: alert ( '[GURU MEDITATION]: Could not find profile for authenticated user ' . Auth :: user () -> name . 'whilst trying to update it! Please verify that profiles are being created automatically during signup.' ,
[
'uuid' => Auth :: user () -> uuid ,
'timestamp' => now (),
'route' => $request -> route () -> getName (),
'gmcode' => $gm // If this error is reported, the GM code, denoting a severe error, will help us find this entry in the logs
]);
$request -> session () -> flash ( 'error' , 'A technical error has occurred whilst trying to save your profile. Incident details have been recorded. Please report this incident to administrators with the following case number: ' . $gm );
}
2020-04-30 21:53:57 +00:00
2020-05-13 21:47:51 +00:00
return redirect () -> back ();
2020-04-30 21:53:57 +00:00
}
2020-04-29 17:15:54 +00:00
}