middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$user = request()->user();
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');
}
if (request()->ajax()) {
$subscription = PackageSubscription::activeSubscription($user->id);
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();
}
return DataTables::of($forms)
->addColumn(
'action',
function (Form $form) use ($subscription, $user) {
$action = '';
if (!empty($form->schema)) {
$action = '
';
}
$action .= '
';
if (auth()->user()->hasRole(RoleEnum::SUPERVISOR->value) || auth()->user()->id === $form->created_by) {
$action .= '';
}
if (auth()->user()->hasRole(RoleEnum::SUPERVISOR->value) || auth()->user()->id === $form->created_by) {
$action .= '';
}
if (auth()->user()->hasRole(RoleEnum::SUPERVISOR->value) || auth()->user()->id === $form->created_by) {
$action .= '';
}
$action .= '
';
if (auth()->user()->hasRole(RoleEnum::SUPERVISOR->value) || auth()->user()->id === $form->created_by) {
$action .= '
';
}
if (auth()->user()->hasRole(RoleEnum::SUPERVISOR->value) || auth()->user()->id === $form->created_by) {
$action .= '
';
}
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 .= '
(' . (__('messages.form_is_incomplete')) . ')';
}
return $html;
})
->removeColumn('id')
->rawColumns(['action', 'created_at', 'data_count', 'name'])
->make(true);
}
//Count forms
$form_count = $forms->count();
//Count templates.
$template_count = Form::where('created_by', $user->id)
->where('is_template', 1)
->count();
//Count submissions.
$submission_count = Form::join('form_data as fd', 'forms.id', '=', 'fd.form_id')
->where('is_template', 0)
->when(auth()->user()->hasRole(RoleEnum::USER->value), function (Builder $builder) use ($user) {
$builder->where('created_by', $user->id);
})
->count();
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')
->where(function ($query) use ($user_id) {
$query->where('is_template', 1)
->where('created_by', $user_id)
->orWhere('is_global_template', 1);
})
->groupBy('id');
return DataTables::of($forms)
->addColumn('action', function ($row) {
$action = '
';
if (!$row->is_global_template || auth()->user()->can('superadmin')) {
$action .= '
';
}
return $action;
})
->editColumn('is_global_template', function ($row) {
if (auth()->user()->can('superadmin')) {
$checked = $row->is_global_template ? 'checked' : '';
$html = '