2020-06-08 13:08:46 +00:00
< ? php
namespace App\Http\Controllers ;
2020-06-22 19:08:09 +00:00
use App\Helpers\Detector ;
2020-08-19 08:01:22 +00:00
use App\ResourceCategory ;
use App\ResourceExtension ;
2020-08-10 09:06:27 +00:00
use Illuminate\Database\Eloquent\Collection ;
2020-06-08 13:08:46 +00:00
use Illuminate\Http\Request ;
2020-06-23 04:42:43 +00:00
use Illuminate\Http\Response ;
2020-06-10 06:25:21 +00:00
use App\User ;
2020-08-06 13:31:38 +00:00
use Illuminate\Support\Facades\App ;
2020-06-10 06:25:21 +00:00
use Illuminate\Support\Facades\Auth ;
2020-07-07 17:29:21 +00:00
use Illuminate\Support\Facades\DB ;
2020-07-27 11:09:51 +00:00
use Illuminate\Support\Facades\Hash ;
2020-06-22 12:53:00 +00:00
use Illuminate\Support\Facades\Log ;
2020-07-29 11:33:11 +00:00
use mysql_xdevapi\Table ;
2020-06-30 10:08:31 +00:00
use Spatie\Permission\Models\Role ;
2020-06-10 06:25:21 +00:00
2020-06-08 13:08:46 +00:00
class UserController extends Controller
{
2020-06-15 06:59:15 +00:00
public function __construct ()
{
2020-09-02 09:40:36 +00:00
$this -> middleware ([ " auth " ]) -> only ([ " accountupdate " , " accountedit " , " accounteditpass " , " account " , " logout " ]);
2020-08-31 07:36:10 +00:00
2020-06-29 12:28:09 +00:00
$this -> middleware ([ " guest " ]) -> only ( " login " );
2020-08-07 06:51:05 +00:00
$this -> middleware ([ " lang " ]) -> except ([ " login " , " showLogin " , " showForgot " , " forgot " ]);
2020-06-29 12:28:09 +00:00
2020-08-31 07:36:10 +00:00
$this -> middleware ([ " check.auth:user.show " ]) -> only ( " show " , " index " );
2020-06-29 12:28:09 +00:00
$this -> middleware ([ " check.auth:user.create " ]) -> only ( " create " );
2020-08-03 07:39:32 +00:00
$this -> middleware ([ " check.auth:user.edit " ]) -> only ([ " edit " , " update " ]);
2020-06-29 12:28:09 +00:00
$this -> middleware ([ " check.auth:user.delete " ]) -> only ( " delete " );
2020-06-15 06:59:15 +00:00
}
2020-06-08 13:08:46 +00:00
/**
* Display a listing of the resource .
*
2020-06-10 06:25:21 +00:00
* @ param Request $request
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
2020-06-10 06:25:21 +00:00
public function index ( Request $request )
2020-06-08 13:08:46 +00:00
{
2020-08-03 11:14:58 +00:00
//$users = User::query()->paginate($request->query("page", 20));
2020-08-03 11:21:47 +00:00
$users = User :: query () -> orderBy ( 'id' , 'asc' ) -> paginate ( 20 );
2020-06-10 06:25:21 +00:00
2020-06-23 04:42:43 +00:00
return Response :: detect ( " users.index " , [ " users " => $users ]);
2020-06-08 13:08:46 +00:00
}
/**
* Show the form for creating a new resource .
*
2020-06-10 06:25:21 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function create ()
{
2020-06-30 10:08:31 +00:00
$roles = Role :: all ();
return Response :: detect ( " users.create " , [ 'roles' => $roles ]);
2020-06-08 13:08:46 +00:00
}
/**
* Store a newly created resource in storage .
*
* @ param \Illuminate\Http\Request $request
2020-06-10 06:25:21 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function store ( Request $request )
{
2020-06-10 06:25:21 +00:00
$data = $request -> validate ([
" name_first " => " required|max:255 " ,
" name_last " => " required|max:255 " ,
2020-08-04 07:34:56 +00:00
" email " => " required " ,
2020-06-10 06:25:21 +00:00
" password " => " required|max:60 " ,
2020-08-17 12:42:58 +00:00
" phone " => " required|max:8|min:8 " ,
2020-06-10 06:25:21 +00:00
]);
2020-08-26 06:56:18 +00:00
2020-08-04 07:34:56 +00:00
$allUsersEmail = User :: query () -> where ( 'email' , '=' , $request -> email ) -> get ();
$allUsersPhone = User :: query () -> where ( 'phone' , '=' , $request -> phone ) -> get ();
if ( $request -> password != $request -> password2 )
2020-08-04 06:04:54 +00:00
return redirect () -> route ( 'users.create' ) -> with ( 'error#notsamepass' , '<p><b>Der stod ikke det samme i `Password` & `Confirm Password`!</b></p>' );
2020-08-04 07:34:56 +00:00
elseif ( count ( $allUsersEmail ) > 0 )
return redirect () -> route ( 'users.create' ) -> with ( 'EmailExists' , '<p><b>Der findes allerede en bruger med den Email!</b></p>' );
elseif ( count ( $allUsersPhone ) > 0 )
return redirect () -> route ( 'users.create' ) -> with ( 'PhoneExists' , '<p><b>Der findes allerede en bruger med det Telefon Nr.!</b></p>' );
2020-08-03 08:36:57 +00:00
$roles = $request -> input ( " roles " , [ " User " ]);
2020-07-28 08:22:45 +00:00
2020-06-10 06:25:21 +00:00
$user = new User ( $data );
2020-06-22 12:53:00 +00:00
2020-08-03 08:36:57 +00:00
$user -> assignRole ( $roles );
2020-06-10 06:25:21 +00:00
$user -> save ();
2020-06-22 12:53:00 +00:00
2020-08-03 08:36:57 +00:00
return redirect () -> route ( 'users.index' );
2020-06-08 13:08:46 +00:00
}
/**
* Display the specified resource .
*
* @ param int $id
2020-06-10 06:25:21 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function show ( $id )
{
2020-06-10 06:25:21 +00:00
$user = User :: find ( $id );
2020-06-25 08:34:10 +00:00
return Response :: detect ( " users.show " , [
2020-06-10 06:25:21 +00:00
" user " => $user
]);
2020-06-08 13:08:46 +00:00
}
/**
* Show the form for editing the specified resource .
*
* @ param int $id
2020-06-10 06:25:21 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function edit ( $id )
{
2020-06-30 10:11:18 +00:00
$roles = Role :: all ();
2020-06-10 06:25:21 +00:00
$user = User :: find ( $id );
2020-06-24 06:53:07 +00:00
return Response :: detect ( " users.edit " , [
2020-06-30 10:11:18 +00:00
" user " => $user ,
" roles " => $roles ,
2020-06-10 06:25:21 +00:00
]);
2020-06-08 13:08:46 +00:00
}
/**
* Update the specified resource in storage .
*
* @ param \Illuminate\Http\Request $request
* @ param int $id
2020-06-10 10:30:44 +00:00
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function update ( Request $request , $id )
{
2020-07-30 13:25:37 +00:00
$user = User :: find ( $id );
2020-06-29 07:08:49 +00:00
2020-08-26 06:56:18 +00:00
if ( $request -> file ( " resource " )) {
$user -> resource_id = ResourceController :: store ( $request ) -> id ;
}
$user -> save ();
2020-08-04 07:34:56 +00:00
$allUsersEmail = User :: query () -> where ( 'email' , '=' , $request -> email ) -> where ( 'id' , '!=' , $id ) -> get ();
$allUsersPhone = User :: query () -> where ( 'phone' , '=' , $request -> phone ) -> where ( 'id' , '!=' , $id ) -> get ();
2020-07-30 13:25:37 +00:00
if ( $id === Auth :: id () || auth () -> user () -> hasPermissionTo ( " user.edit " )) {
2020-08-03 06:33:11 +00:00
if ( $request -> roles != null && auth () -> user () -> hasPermissionTo ( " user.edit " )) { //You can only edit roles on the admin site, so if there is an input roles, then update user info and edit roles
2020-08-04 07:34:56 +00:00
if ( count ( $allUsersEmail ) > 0 )
2020-08-07 08:51:10 +00:00
return redirect () -> route ( 'users.edit' , [ 'user' => $id ]) -> with ( 'EmailExists' , '<p><b>Der findes allerede en bruger med den Email!</b></p>' );
2020-08-04 07:34:56 +00:00
elseif ( count ( $allUsersPhone ) > 0 )
return redirect () -> route ( 'users.edit' , [ 'user' => $id ]) -> with ( 'PhoneExists' , '<p><b>Der findes allerede en bruger med det Telefon Nr.!</b></p>' );
elseif ( $request -> password != " " ) { // If you have edited the password, then run this
2020-07-31 09:14:24 +00:00
if ( $request -> password == $request -> password2 ) { // If the password is the same as confirm password, then update everything from user
$data = $request -> all ();
/** @var User $user */
$user -> update ( $data );
} else // Else go back with an error message
2020-08-07 08:51:10 +00:00
return redirect () -> route ( " users.edit " , [ 'user' => $id ]) -> with ( 'error#notsamepass' , '<p><b>Der stod ikke det samme i `Password` & `Bekræft Adgangskode`!</b></p>' );
2020-07-31 09:14:24 +00:00
}
else { // If you haven't touched the password, then update everything else but password
$data = $request -> only ([ 'name_first' , 'name_last' , 'email' , 'phone' , 'roles' ]);
/** @var User $user */
$user -> update ( $data );
}
2020-07-01 08:13:49 +00:00
$user -> roles () -> detach ();
$user -> forgetCachedPermissions ();
2020-06-30 11:22:02 +00:00
2020-07-30 13:25:37 +00:00
foreach ( $request -> roles as $role ) {
2020-07-01 08:13:49 +00:00
$user -> assignRole ( $role );
}
2020-06-30 11:22:02 +00:00
}
2020-07-30 13:25:37 +00:00
}
2020-06-29 07:08:49 +00:00
$users = User :: query () -> paginate ( 20 );
2020-08-06 11:35:07 +00:00
return redirect () -> route ( " users.index " );
2020-06-08 13:08:46 +00:00
}
/**
* Remove the specified resource from storage .
*
* @ param int $id
2020-07-30 13:25:37 +00:00
* @ return \Illuminate\Http\RedirectResponse
2020-06-08 13:08:46 +00:00
*/
public function destroy ( $id )
{
2020-07-30 13:25:37 +00:00
if ( Auth :: user () -> hasPermissionTo ( " user.delete " )) {
2020-06-10 10:30:44 +00:00
$user = User :: find ( $id );
$user -> delete ();
2020-07-30 13:25:37 +00:00
}
2020-06-10 10:30:44 +00:00
2020-06-29 07:08:49 +00:00
return redirect () -> route ( " users.index " );
2020-06-08 13:08:46 +00:00
}
2020-06-10 06:25:21 +00:00
/*******************************************/
/* Authentication */
/*******************************************/
2020-06-16 06:13:24 +00:00
public function showLogin () {
2020-07-30 06:20:30 +00:00
if ( Auth :: user () == null )
return Response :: detect ( " users.login " );
else
return redirect () -> back ();
2020-06-16 06:13:24 +00:00
}
2020-06-10 06:25:21 +00:00
public function login ( Request $request ) {
$data = $request -> only ( " email " , " password " );
2020-08-11 11:05:30 +00:00
$remember = $request -> rememberpassword ;
2020-06-10 06:25:21 +00:00
2020-08-11 11:05:30 +00:00
if ( Auth :: attempt ( $data , $remember )) {
2020-06-29 12:28:09 +00:00
return redirect () -> route ( " root.index " );
2020-06-10 06:25:21 +00:00
}
2020-08-10 10:44:20 +00:00
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return redirect () -> back ( 303 ) -> with ( 'error#wrongcredentials' , '<p class="text-center text-white"><b>Email eller Password matchede ikke!</b></p>' );
if ( request () -> cookie ( 'languagesSetting' ) == " en " )
return redirect () -> back ( 303 ) -> with ( 'error#wrongcredentials' , '<p class="text-center text-white"><b>Wrong Email or Password!</b></p>' );
2020-08-10 10:29:38 +00:00
return redirect () -> back ( 303 ) -> with ( 'error#wrongcredentials' , '<p class="text-center text-white"><b>Email eller Password matchede ikke!</b></p>' );
2020-06-10 10:30:44 +00:00
}
public function logout ( Request $request ) {
Auth :: logout ();
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return redirect () -> route ( " users.login " ) -> with ( 'success#loggedout' , '<p class="text-center text-white"><b>Du er hermed logget ud!</b></p>' );
if ( request () -> cookie ( 'languagesSetting' ) == " en " )
return redirect () -> route ( " users.login " ) -> with ( 'success#loggedout' , '<p class="text-center text-white"><b>You are logged out!</b></p>' );
2020-08-10 10:29:38 +00:00
return redirect () -> route ( " users.login " ) -> with ( 'success#loggedout' , '<p class="text-center text-white"><b>Du er hermed logget ud!</b></p>' );
2020-06-10 06:25:21 +00:00
}
2020-06-30 08:19:01 +00:00
/*******************************************/
/* Forgot password */
/*******************************************/
public function showForgot (){
return Response :: detect ( 'users.forgot' );
}
2020-06-30 08:35:09 +00:00
public function forgot ( Request $request ){
$user = User :: query () -> where ( 'email' , '=' , $request -> email ) -> first ();
2020-06-30 08:19:01 +00:00
2020-06-30 08:35:09 +00:00
if ( $user == null ){
2020-08-07 08:51:10 +00:00
return redirect () -> back () -> with ( 'errornosuchuser' , '<p class="text-center text-white"><b>Denne email findes ikke i systemet!</b></p>' );
2020-06-30 08:35:09 +00:00
}
//Send email
2020-08-04 08:59:45 +00:00
$email = $user -> email ;
$pswd = " " ;
//Generate password
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
$n = 6 ;
$randomString = '' ;
for ( $i = 0 ; $i < $n ; $i ++ ) {
$index = rand ( 0 , strlen ( $characters ) - 1 );
$randomString .= $characters [ $index ];
}
$pswd = $randomString ;
$user -> setPasswordAttribute ( $pswd );
$user -> update ();
$subject = " SDE Skolehjem reset password " ;
2020-08-04 11:21:56 +00:00
$msg = " Hej " . $user -> name_first . " " . $user -> name_last . " . \n \n Din adgangskode er nu: " . $pswd ;
2020-08-04 08:59:45 +00:00
mail ( $email , $subject , $msg );
2020-06-30 08:35:09 +00:00
return redirect () -> route ( 'users.login' );
2020-06-30 08:19:01 +00:00
}
2020-06-30 12:40:46 +00:00
2020-08-12 08:46:36 +00:00
/*******************************************/
/* Account */
/*******************************************/
2020-06-30 12:40:46 +00:00
public function account ()
{
return Response :: detect ( " users.account " );
}
public function accountedit ()
{
return Response :: detect ( " users.edit " );
}
2020-07-01 08:13:49 +00:00
2020-08-12 08:46:36 +00:00
public function accounteditpic ()
{
return Response :: detect ( " users.editpic " );
}
2020-07-01 08:13:49 +00:00
public function accounteditpass ()
{
return Response :: detect ( " users.editpass " );
}
2020-07-01 09:46:29 +00:00
2020-08-03 07:39:32 +00:00
/**
* Update the specified resource in storage .
*
* @ param \Illuminate\Http\Request $request
2020-08-12 08:46:36 +00:00
* @ return \Illuminate\Http\RedirectResponse
2020-08-03 07:39:32 +00:00
*/
public function accountupdate ( Request $request )
{
2020-08-12 08:46:36 +00:00
/** @var User $user */
2020-08-03 07:39:32 +00:00
$user = User :: find ( Auth :: id ());
if ( $request -> input ( 'password' ) != null ) { // If you're editing the password
$data = $request -> only ([ 'name_first' , 'name_last' , 'email' , 'phone' , 'password' ]);
if ( $request -> input ( 'password' ) != $request -> input ( 'confirmpassword' )) { // If new password and new password confirm is not the same, go back with fail message.
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#notsamepass' , '<p class="text-center"><b>Der stod ikke det samme i Nyt Password & Confirm Password!</b></p>' );
2020-08-07 08:51:10 +00:00
elseif ( request () -> cookie ( 'languagesSetting' ) == " en " )
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#notsamepass' , '<p class="text-center"><b>New Password & Confirm Password was not the same!</b></p>' );
2020-08-10 10:29:38 +00:00
else
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#notsamepass' , '<p class="text-center"><b>Der stod ikke det samme i Nyt Password & Confirm Password!</b></p>' );
2020-08-03 07:39:32 +00:00
} elseif ( ! Hash :: check ( $request -> input ( 'oldpassword' ), $user -> password )) { // If the written current password and current password in DB is not the same, go back with fail message.
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#oldpass' , '<p class="text-center"><b>Det indtastede password i Nuværende Password er ikke dit nuværende password!</b></p>' );
2020-08-07 08:51:10 +00:00
elseif ( request () -> cookie ( 'languagesSetting' ) == " en " )
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#oldpass' , '<p class="text-center"><b>The entered Current Password was not the same as your current password!</b></p>' );
2020-08-10 10:29:38 +00:00
else
2020-08-25 12:48:52 +00:00
return redirect () -> route ( " users.accounteditpass " ) -> with ( 'error#oldpass' , '<p class="text-center"><b>Det indtastede password i Nuværende Password er ikke dit nuværende password!</b></p>' );
2020-08-03 07:39:32 +00:00
} else { // If new password and current password is the same AND current written and current DB password is the same. Then update and logout.
/** @var User $user */
$user -> update ( $data );
Auth :: logout ();
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return redirect () -> route ( " users.login " ) -> with ( 'success#passwordchange' , '<p class="text-center text-white"><b>Dit password er hermed ændret!</b></p>' );
elseif ( request () -> cookie ( 'languagesSetting' ) == " en " )
return redirect () -> route ( " users.login " ) -> with ( 'success#passwordchange' , '<p class="text-center text-white"><b>Your password was changed successfully!</b></p>' );
2020-08-10 10:29:38 +00:00
else
return redirect () -> route ( " users.login " ) -> with ( 'success#passwordchange' , '<p class="text-center text-white"><b>Dit password er hermed ændret!</b></p>' );
2020-08-03 07:39:32 +00:00
}
2020-08-12 08:46:36 +00:00
} else if ( $request -> file ( " resource " )) { // Else if you're editing the profile pic
2020-08-19 08:01:22 +00:00
$resource = ResourceController :: storeMime ( $request , ResourceCategory :: query () -> where ( " name " , " = " , " image " ) -> first ());
2020-08-12 08:46:36 +00:00
2020-08-19 08:01:22 +00:00
if ( $resource !== null ) {
$user -> update ([ " resource_id " => $resource -> id ]);
}
else {
return redirect () -> route ( " users.accounteditpic " );
}
2020-08-12 09:15:08 +00:00
2020-08-12 08:46:36 +00:00
return redirect () -> route ( " users.account " );
2020-08-03 07:39:32 +00:00
} else { // Else if you're not editing the password but anything else (Email, Phone Number). Then update user.
$data = $request -> only ([ 'email' , 'phone' ]);
$user -> update ( $data );
2020-08-07 08:51:10 +00:00
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return redirect () -> route ( " users.account " ) -> with ( 'success#credentialschanged' , '<p class="text-center"><b>Dine oplysninger er hermed ændret!</b></p>' );
elseif ( request () -> cookie ( 'languagesSetting' ) == " en " )
return redirect () -> route ( " users.account " ) -> with ( 'success#credentialschanged' , '<p class="text-center"><b>Your information has changed!</b></p>' );
2020-08-10 10:29:38 +00:00
else
return redirect () -> route ( " users.account " ) -> with ( 'success#credentialschanged' , '<p class="text-center"><b>Dine oplysninger er hermed ændret!</b></p>' );
2020-08-03 07:39:32 +00:00
}
$users = User :: query () -> paginate ( 20 );
2020-08-06 11:35:07 +00:00
return redirect () -> route ( " users.index " );
2020-08-03 07:39:32 +00:00
}
2020-07-01 09:46:29 +00:00
2020-08-14 08:17:52 +00:00
public function createajax () {
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$User = User :: query () -> where ( 'id' , '=' , auth () -> user () -> id );
// If you are in the Event, then remove yourself.
if ( count ( $User -> get ()) > 0 ) {
// If not, then it keeps going and saves and shows a success message
$User -> update ([ " wants_emails " => true ]);
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return '<p class="text-center"><b>Du får nu mails, når der kommer nyheder!</b></p>' ;
if ( request () -> cookie ( 'languagesSetting' ) == " en " )
return " <p class='text-center'><b>You'll now receive a mail when there's news!</b></p> " ;
return '<p class="text-center"><b>Du får nu mails, når der kommer nyheder!</b></p>' ;
}
}
public function createajaxcancel () {
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$User = User :: query () -> where ( 'id' , '=' , auth () -> user () -> id );
// If you are in the Event, then remove yourself.
if ( count ( $User -> get ()) > 0 ) {
// If not, then it keeps going and saves and shows a success message
$User -> update ([ " wants_emails " => false ]);
if ( request () -> cookie ( 'languagesSetting' ) == " dk " )
return '<p class="text-center"><b>Du får ikke mails mere, når kommer nyheder!</b></p>' ;
if ( request () -> cookie ( 'languagesSetting' ) == " en " )
return " <p class='text-center'><b>You'll no longer receive a mail when there's news!</b></p> " ;
return '<p class="text-center"><b>Du får ikke mails mere, når kommer nyheder!</b></p>' ;
}
}
2020-08-12 08:46:36 +00:00
/*******************************************/
/* Search and settings */
/*******************************************/
2020-07-27 14:03:49 +00:00
public function search ( Request $request ){
if ( $request -> ajax ()){
$output = " <tr> " .
" <th>Fornavn</th> " .
" <th>Efternavn</th> " .
" <th>Email</th> " .
" <th>Tlf nr</th> " .
" <th>Rolle(r)</th> " .
2020-08-24 12:09:03 +00:00
" <th style= \" width: 1em; \" ><img class= \" w-100 \" src= " . asset ( '/images/icons/pencil.svg' ) . " alt= \" Update \" ></th> " .
2020-08-24 08:01:18 +00:00
" <th style= \" width: 1em; \" ><img class= \" w-100 \" src= " . asset ( '/images/icons/trashcan.svg' ) . " alt= \" Delete \" ></th> " .
2020-07-27 14:03:49 +00:00
" </tr> " ;
2020-07-29 07:49:54 +00:00
2020-07-29 11:33:11 +00:00
//Join user and roles tables
//$users = User::query()->where('Roles', 'LIKE',$request->search.'%')->with(Spatie\Permission\Models\Role)->get();
2020-07-29 07:49:54 +00:00
//filter search
if ( $request -> isCheck === " navn " )
$users = User :: query () -> where ( 'name_first' , 'LIKE' , $request -> search . '%' ) -> get ();
elseif ( $request -> isCheck === " efternavn " )
$users = User :: query () -> where ( 'name_last' , 'LIKE' , $request -> search . '%' ) -> get ();
elseif ( $request -> isCheck === " email " )
$users = User :: query () -> where ( 'email' , 'LIKE' , $request -> search . '%' ) -> get ();
elseif ( $request -> isCheck === " telefon " )
$users = User :: query () -> where ( 'phone' , 'LIKE' , $request -> search . '%' ) -> get ();
2020-07-29 11:33:11 +00:00
elseif ( $request -> isCheck === " telefon " )
$users = User :: query () -> where ( 'phone' , 'LIKE' , $request -> search . '%' ) -> get ();
2020-07-30 09:02:41 +00:00
elseif ( $request -> isCheck ) {
$roles = Role :: query () -> where ( 'name' , 'LIKE' , $request -> search . '%' ) -> get ();
$usersTEMP = User :: all ();
$users = [];
foreach ( $usersTEMP as $user ) {
foreach ( $roles as $role ) {
if ( $user -> hasRole ( $role )) {
array_push ( $users , $user );
2020-08-03 11:21:47 +00:00
break 1 ;
2020-07-30 09:02:41 +00:00
}
}
}
if ( $request -> search == " " )
$users = User :: all ();
} else {
$usersTEMP = User :: query ()
-> where ( 'name_first' , 'LIKE' , $request -> search . '%' )
-> orWhere ( 'name_last' , 'LIKE' , $request -> search . '%' )
-> orWhere ( 'phone' , 'LIKE' , $request -> search . '%' )
-> orWhere ( 'email' , 'LIKE' , $request -> search . '%' )
2020-07-29 11:33:11 +00:00
-> get ();
2020-07-30 09:02:41 +00:00
$roles = Role :: query () -> where ( 'name' , 'LIKE' , $request -> search . '%' ) -> get ();
2020-08-10 09:06:27 +00:00
$users = new Collection ();
2020-07-30 09:02:41 +00:00
$allUsers = User :: all ();
//For hver user i systemet, check om han har en af de roller der matcher søgeordet
foreach ( $allUsers as $user ) {
foreach ( $roles as $role ) {
if ( $user -> hasRole ( $role )) {
2020-08-10 09:06:27 +00:00
$users -> add ( $user );
break 1 ;
2020-07-30 09:02:41 +00:00
}
}
}
2020-08-10 09:06:27 +00:00
2020-07-30 09:02:41 +00:00
//For hver user der har data, der matcher søgeordet, check om useren allerede er i resultatet, hvis ikke så tilføj ham
foreach ( $usersTEMP as $userTEMP ){
$isInUsers = false ;
foreach ( $users as $user ) {
if ( $userTEMP == $user ) {
$isInUsers = true ;
break 2 ;
}
}
if ( $isInUsers == false )
2020-08-10 09:06:27 +00:00
$users -> add ( $userTEMP );
2020-07-30 09:02:41 +00:00
}
2020-07-27 14:03:49 +00:00
2020-08-10 09:06:27 +00:00
if ( $request -> search == " " )
$users = User :: all ();
$unique = $users -> flatten () -> unique ( 'phone' );
$users = $unique -> values () -> all ();
2020-07-30 09:02:41 +00:00
}
2020-07-29 07:49:54 +00:00
2020-07-27 14:03:49 +00:00
if ( count ( $users ) !== 0 ){
foreach ( $users as $key => $user ){
2020-07-28 12:23:54 +00:00
2020-07-27 14:03:49 +00:00
$roles = null ;
$rolesString = null ;
foreach ( User :: all () as $usr ) {
if ( $usr -> id == $user -> id )
$roles = $usr -> roles ;
}
for ( $i = 0 ; $i < count ( $roles ); $i ++ ) {
if ( count ( $roles ) - 1 != $i ) {
$rolesString .= $roles [ $i ] -> name . " , " ;
} else {
$rolesString = $roles [ $i ] -> name ;
}
}
$output .= '<tr>' .
'<td>' . $user -> name_first . '</td>' .
'<td>' . $user -> name_last . '</td>' .
'<td>' . $user -> email . '</td>' .
'<td>' . $user -> phone . '</td>' .
2020-07-30 09:02:41 +00:00
'<td>' . $rolesString . '</td>' .
2020-07-27 14:03:49 +00:00
'<td><a href="' . route ( " users.edit " , [ " user " => $user -> id ]) . '"><img class="w-100" src="' . asset ( '/images/icons/pencil-dark.svg' ) . '" alt="Update"></a></td>' .
'<td><form method="post" action="' . route ( " users.destroy " , [ " user " => $user -> id ]) . '" class="w-100 nostyle">' .
csrf_field () .
method_field ( " delete " ) .
'<button class="w-100 nostyle" onclick="return confirm(\'Are you sure you want to delete?\');" type="submit"><img class="w-100 cursor-pointer" src="' . asset ( '/images/icons/trashcan-dark.svg' ) . '" alt="Delete"></button>' .
'</form>' .
'</td>' .
'</tr>' ;
}
2020-07-28 12:23:54 +00:00
} else {
$output .= '<tr>' .
'<td>Intet match</td>' .
'<td></td>' .
'<td></td>' .
'<td></td>' .
'<td></td>' .
'<td></td>' .
'<td></td>' .
'</tr>' ;
2020-07-27 14:03:49 +00:00
}
return Response ( $output );
}
}
2020-08-06 13:31:38 +00:00
2020-08-12 08:46:36 +00:00
public function setLanguages ( Request $request ){
2020-08-06 13:31:38 +00:00
if ( $request -> ajax ()){
if ( $request -> lang === " en " ){
$response = new Response ( 'Set Cookie' );
$response -> withCookie ( cookie ( 'languagesSetting' , $request -> lang , 5259488 ));
return $response ;
} elseif ( $request -> lang === " dk " ){
$response = new Response ( 'Set Cookie' );
$response -> withCookie ( cookie ( 'languagesSetting' , $request -> lang , 5259488 ));
return $response ;
}
}
}
2020-08-10 12:49:00 +00:00
public function setDarkMode ( Request $request ){
if ( $request -> ajax ()){
if ( $request -> darkmode === " dark " ){
$response = new Response ( 'dark' );
$response -> withCookie ( cookie ( 'mode' , $request -> darkmode , 5259488 ));
return $response ;
} elseif ( $request -> darkmode === " light " ){
$response = new Response ( 'light' );
$response -> withCookie ( cookie ( 'mode' , $request -> darkmode , 5259488 ));
return $response ;
}
}
}
2020-08-17 06:23:48 +00:00
public function nameCheck ( Request $request ){
2020-08-17 13:10:44 +00:00
$usersemail = User :: query () -> where ( 'email' , 'LIKE' , $request -> mailCheck ) -> get ();
$usersphone = User :: query () -> where ( 'phone' , '=' , $request -> phoneCheck ) -> get ();
if ( count ( $usersemail ) > 0 && count ( $usersphone ) > 0 ) {
2020-08-18 06:16:25 +00:00
return 'both' ;
2020-08-17 06:23:48 +00:00
}
2020-08-17 13:10:44 +00:00
elseif ( count ( $usersemail ) > 0 && $request -> mailCheck !== NULL ){
2020-08-18 06:16:25 +00:00
return 'email' ;
2020-08-17 13:10:44 +00:00
}
elseif ( count ( $usersphone ) > 0 && $request -> phoneCheck !== NULL ){
2020-08-18 06:16:25 +00:00
return 'phone' ;
}
}
public function nameCheckUpdate ( Request $request ){
$usersemail = User :: query () -> where ( 'email' , 'LIKE' , $request -> mailCheck ) -> where ( 'id' , '!=' , $request -> id ) -> get ();
$usersphone = User :: query () -> where ( 'phone' , '=' , $request -> phoneCheck ) -> where ( 'id' , '!=' , $request -> id ) -> get ();
if ( count ( $usersemail ) > 0 && count ( $usersphone ) > 0 ) {
return 'both' ;
}
elseif ( count ( $usersemail ) > 0 && $request -> mailCheck !== NULL ){
return 'email' ;
}
elseif ( count ( $usersphone ) > 0 && $request -> phoneCheck !== NULL ){
return 'phone' ;
2020-08-17 13:10:44 +00:00
}
2020-08-17 06:23:48 +00:00
}
2020-06-08 13:08:46 +00:00
}