2023-09-21 12:45:08 +00:00
< ? php
namespace App\Http\Controllers ;
2023-09-21 14:20:06 +00:00
use App\Enums\User\RoleEnum ;
2023-09-21 12:45:08 +00:00
use App\Form ;
use App\Mail\TestEmail ;
use App\PackageSubscription ;
use App\UserForm ;
use Carbon\Carbon ;
use DB ;
2023-09-21 14:20:06 +00:00
use Illuminate\Database\Eloquent\Builder ;
2023-09-21 12:45:08 +00:00
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Mail ;
use Yajra\DataTables\Facades\DataTables ;
class HomeController extends Controller
{
/**
* Create a new controller instance .
*
* @ return void
*/
public function __construct ()
{
$this -> middleware ( 'auth' );
}
/**
* Show the application dashboard .
*
* @ return \Illuminate\Contracts\Support\Renderable
*/
public function index ()
{
$user = request () -> user ();
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value , 'web' )) {
$forms = Form :: query ()
-> withCount ( 'data' )
-> where ( 'is_template' , 0 )
-> get ();
} else {
$forms = Form :: query ()
-> withCount ( 'data' )
-> where ( 'is_template' , 0 )
-> where ( 'created_by' , $user -> id )
-> groupBy ( 'id' );
}
2023-09-21 12:45:08 +00:00
if ( request () -> ajax ()) {
$subscription = PackageSubscription :: activeSubscription ( $user -> id );
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value , 'web' )) {
$forms = Form :: query ()
-> withCount ( 'data' )
-> where ( 'is_template' , 0 )
-> get ();
} else {
$forms = Form :: query ()
-> withCount ( 'data' )
-> where ( 'is_template' , 0 )
-> where ( 'created_by' , $user -> id )
-> get ();
}
2023-09-21 12:45:08 +00:00
return DataTables :: of ( $forms )
2023-09-21 14:20:06 +00:00
-> addColumn (
'action' ,
function ( Form $form ) use ( $subscription , $user ) {
$action = '' ;
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if ( ! empty ( $form -> schema )) {
$action = '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'show' ], [ 'form' => $form -> slug ? : $form -> id ]) . '"' . '
2023-09-21 14:01:58 +00:00
target = " _blank "
2023-09-21 14:20:06 +00:00
class = " btn btn-sm btn-info m-1 " data - toggle = " tooltip " title = " ' . __('messages.view') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-eye " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
$action .= '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'edit' ], [ 'form' => $form -> id ]) . '"' . '
class = " btn btn-sm btn-warning m-1 " data - toggle = " tooltip " title = " ' . __('messages.edit') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-edit " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value ) || auth () -> user () -> id === $form -> created_by ) {
$action .= '<button type="button" data-href="' . action ([ \App\Http\Controllers\FormController :: class , 'destroy' ], [ 'form' => $form -> id ]) . '"' . ' class = " btn btn-sm btn-danger delete_form m-1 " data - toggle = " tooltip "
title = " ' . __('messages.delete') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-trash " aria - hidden = " true " ></ i >
</ button > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value ) || auth () -> user () -> id === $form -> created_by ) {
$action .= '<button type="button" data-href="' . action ([ \App\Http\Controllers\FormController :: class , 'copyForm' ], [ 'id' => $form -> id ]) . '"' . ' class = " btn btn-sm btn-primary copy_form m-1 " data - toggle = " tooltip "
title = " ' . __('messages.copy_this_form') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fas fa-copy " ></ i >
</ button > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value ) || auth () -> user () -> id === $form -> created_by ) {
$action .= '<button type="button" data-href="' . action ([ \App\Http\Controllers\FormController :: class , 'generateWidget' ], [ 'id' => $form -> id ]) . '"' . ' class = " btn btn-sm btn-info generate_widget m-1 " data - toggle = " tooltip "
title = " ' . __('messages.widget') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-random " aria - hidden = " true " ></ i >
</ button > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
$action .= '<a href="' . action ([ \App\Http\Controllers\FormDataController :: class , 'show' ], [ 'id' => $form -> id ]) . '"' . ' "
class = " btn btn-sm btn-success m-1 " data - toggle = " tooltip " title = " ' . __('messages.view_form_data') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-list " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value ) || auth () -> user () -> id === $form -> created_by ) {
$action .= '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'downloadCode' ], [ 'id' => $form -> id ]) . '"' . ' " class= " btn btn - sm btn - dark m - 1 " data-toggle= " tooltip "
title = " ' . __('messages.download_code') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fas fa-download " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value ) || auth () -> user () -> id === $form -> created_by ) {
$action .= '<a data-href="' . action ([ \App\Http\Controllers\FormController :: class , 'getCollab' ], [ 'id' => $form -> id ]) . '"' . 'class="btn btn-sm btn-primary m-1 collab_btn" data-toggle="tooltip" title="' . __ ( 'messages.collaborate' ) . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fas fa-handshake text-white " aria - hidden = " true " ></ i >
</ a > ' ;
}
2023-09-21 14:20:06 +00:00
return $action ;
}
)
-> editColumn ( 'created_at' , function ( $row ) {
$date_format = config ( 'constants.APP_DATE_FORMAT' );
if ( config ( 'constants.APP_TIME_FORMAT' ) == '12' ) {
$date_format .= ' h:i A' ;
} elseif ( config ( 'constants.APP_TIME_FORMAT' ) == '24' ) {
$date_format .= ' H:i' ;
} else {
$date_format = 'm/d/Y h:i A' ;
}
return ! empty ( $row -> created_at ) ? Carbon :: createFromTimestamp ( strtotime ( $row -> created_at )) -> format ( $date_format ) : null ;
})
-> editColumn ( 'data_count' , function ( $row ) {
return $row -> data_count ;
})
-> editColumn ( 'name' , function ( $row ) {
$html = $row -> name ;
if ( empty ( $row -> schema )) {
$html .= '<br><small class="text-danger">(' . ( __ ( 'messages.form_is_incomplete' )) . ')</small>' ;
}
return $html ;
})
-> removeColumn ( 'id' )
-> rawColumns ([ 'action' , 'created_at' , 'data_count' , 'name' ])
-> make ( true );
2023-09-21 12:45:08 +00:00
}
//Count forms
2023-09-21 14:20:06 +00:00
$form_count = $forms -> count ();
2023-09-21 12:45:08 +00:00
//Count templates.
$template_count = Form :: where ( 'created_by' , $user -> id )
2023-09-21 14:20:06 +00:00
-> where ( 'is_template' , 1 )
-> count ();
2023-09-21 12:45:08 +00:00
//Count submissions.
$submission_count = Form :: join ( 'form_data as fd' , 'forms.id' , '=' , 'fd.form_id' )
2023-09-21 14:20:06 +00:00
-> where ( 'is_template' , 0 )
-> when ( auth () -> user () -> hasRole ( RoleEnum :: USER -> value ), function ( Builder $builder ) use ( $user ) {
$builder -> where ( 'created_by' , $user -> id );
})
-> count ();
2023-09-21 12:45:08 +00:00
return view ( 'home' )
-> with ( compact ( 'form_count' , 'template_count' , 'submission_count' ));
}
/**
* Show Form Template
*
* @ return \Illuminate\Contracts\Support\Renderable
*/
public function getTemplate ()
{
if ( request () -> ajax ()) {
$user_id = request () -> user () -> id ;
$forms = Form :: select ( 'name' , 'description' , 'id' , 'slug' , 'is_global_template' )
2023-09-21 14:20:06 +00:00
-> where ( function ( $query ) use ( $user_id ) {
$query -> where ( 'is_template' , 1 )
-> where ( 'created_by' , $user_id )
-> orWhere ( 'is_global_template' , 1 );
})
-> groupBy ( 'id' );
2023-09-21 12:45:08 +00:00
return DataTables :: of ( $forms )
2023-09-21 14:20:06 +00:00
-> addColumn ( 'action' , function ( $row ) {
$action = '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'show' ], [ 'form' => $row -> slug ? : $row -> id ]) . '"' . '
2023-09-21 14:01:58 +00:00
target = " _blank "
2023-09-21 14:20:06 +00:00
class = " btn btn-sm btn-info m-1 " data - toggle = " tooltip " title = " ' . __('messages.view') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-eye " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
if ( ! $row -> is_global_template || auth () -> user () -> can ( 'superadmin' )) {
$action .= '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'edit' ], [ 'form' => $row -> id ]) . '"' . '
class = " btn btn-sm btn-warning m-1 " data - toggle = " tooltip " title = " ' . __('messages.edit') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-edit " aria - hidden = " true " ></ i >
</ a >
2023-09-21 14:20:06 +00:00
< button type = " button " data - href = " ' . action([ \ App \ Http \ Controllers \ FormController::class, 'destroy'], ['form' => $row->id ]) . ' " ' . ' class = " btn btn-sm btn-danger delete_template m-1 " data - toggle = " tooltip "
title = " ' . __('messages.delete') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-trash " aria - hidden = " true " ></ i >
</ button > ' ;
2023-09-21 14:20:06 +00:00
}
return $action ;
})
-> editColumn ( 'is_global_template' , function ( $row ) {
if ( auth () -> user () -> can ( 'superadmin' )) {
$checked = $row -> is_global_template ? 'checked' : '' ;
$html = ' < div class = " form-check " >
< input class = " form-check-input toggle_global_template " type = " checkbox " value = " 1 " ' . $checked . ' data - form_id = " ' . $row->id . ' " >
2023-09-21 12:45:08 +00:00
</ div > ' ;
2023-09-21 14:20:06 +00:00
return $html ;
}
})
-> editColumn ( 'name' , function ( $row ) {
$name = $row -> name ;
if ( $row -> is_global_template ) {
$name .= '<br><span class="badge badge-pill badge-info">' . __ ( 'messages.pre_made' ) . '</span>' ;
}
return $name ;
})
-> removeColumn ( 'id' )
-> rawColumns ([ 'action' , 'is_global_template' , 'name' ])
-> make ( true );
2023-09-21 12:45:08 +00:00
}
return view ( 'home' );
}
/**
* Tests if SMTP connection details is correct or not .
*
* @ return \Illuminate\Contracts\Support\Renderable
*/
public function testSMTP ()
{
try {
//Set the default config.
config ([
'mail.mailers.smtp.host' => request () -> host ,
'mail.mailers.smtp.port' => request () -> port ,
'mail.from.address' => request () -> from_address ,
'mail.from.name' => request () -> from_name ,
'mail.mailers.smtp.encryption' => request () -> encryption ,
'mail.mailers.smtp.username' => request () -> username ,
'mail.mailers.smtp.password' => request () -> password
]);
Mail :: to ( request () -> from_address )
2023-09-21 14:20:06 +00:00
-> send ( new TestEmail ());
2023-09-21 12:45:08 +00:00
return $this -> respondSuccess ();
} catch ( \Exception $e ) {
return $this -> respondWithError ( $e -> getMessage ());
}
}
/**
* Show assigned forms
*
* @ return \Illuminate\Contracts\Support\Renderable
*/
public function getAssignedForms ( Request $request )
{
if ( $request -> ajax ()) {
$forms = UserForm :: join ( 'forms' , 'user_forms.form_id' , '=' , 'forms.id' )
2023-09-21 14:20:06 +00:00
-> leftJoin ( 'users' , 'forms.created_by' , '=' , 'users.id' )
-> where ( 'user_forms.assigned_to' , \Auth :: id ())
-> select ( 'user_forms.permissions as permissions' , 'forms.name as name' , 'forms.description as description' , 'forms.id as form_id' , 'forms.created_at as created_at' , 'forms.slug as slug' , 'users.name as created_by' );
2023-09-21 12:45:08 +00:00
return DataTables :: of ( $forms )
2023-09-21 14:20:06 +00:00
-> addColumn (
'action' ,
function ( $row ) {
$action = '' ;
if (( ! empty ( $row -> permissions ) && in_array ( 'can_view_form' , $row -> permissions )) || auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value )) {
$action = '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'show' ], [ 'form' => $row -> slug ? : $row -> form_id ]) . '"' . '
2023-09-21 14:01:58 +00:00
target = " _blank "
2023-09-21 14:20:06 +00:00
class = " btn btn-sm btn-info m-1 " data - toggle = " tooltip " title = " ' . __('messages.view') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-eye " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if (( ! empty ( $row -> permissions ) && in_array ( 'can_design_form' , $row -> permissions )) || auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value )) {
$action .= '<a href="' . action ([ \App\Http\Controllers\FormController :: class , 'edit' ], [ 'form' => $row -> form_id ]) . '"' . '
class = " btn btn-sm btn-warning m-1 " data - toggle = " tooltip " title = " ' . __('messages.edit') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-edit " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if (( ! empty ( $row -> permissions ) && in_array ( 'can_view_data' , $row -> permissions )) || auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value )) {
$action .= '<a href="' . action ([ \App\Http\Controllers\FormDataController :: class , 'show' ], [ 'id' => $row -> form_id ]) . '"' . ' "
class = " btn btn-sm btn-success m-1 " data - toggle = " tooltip " title = " ' . __('messages.view_form_data') . ' " >
2023-09-21 12:45:08 +00:00
< i class = " fa fa-list " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 14:20:06 +00:00
}
2023-09-21 12:45:08 +00:00
2023-09-21 14:20:06 +00:00
if ( auth () -> user () -> hasRole ( RoleEnum :: SUPERVISOR -> value )) {
$action .= '<a href="' . action ([ \App\Http\Controllers\FormDataController :: class , 'getReport' ], [ 'id' => $row -> form_id ]) . '"' . ' "
target = " _blank "
class = " btn btn-sm btn-success m-1 " data - toggle = " tooltip " title = " ' . __('messages.report') . ' " >
< i class = " fas fa-chart-pie " aria - hidden = " true " ></ i >
</ a > ' ;
2023-09-21 12:45:08 +00:00
}
2023-09-21 14:20:06 +00:00
return $action ;
}
)
-> editColumn ( 'created_by' , function ( $row ) {
return ucfirst ( $row -> created_by );
})
-> removeColumn ([ 'id' , 'permissions' ])
-> rawColumns ([ 'action' , 'created_by' ])
-> make ( true );
2023-09-21 12:45:08 +00:00
}
}
}