File: /home/clientsoftwares/www/advocate.clientsoftwares.com/app/Http/Controllers/DoctypeController.php
<?php
namespace App\Http\Controllers;
use App\Models\DocType;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator as FacadesValidator;
class DoctypeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (Auth::user()->can('manage doctype')) {
$types = DocType::where('created_by', Auth::user()->creatorId())->get();
return view('doctype.index', compact('types'));
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
if (Auth::user()->can('create doctype')) {
return view('doctype.create');
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if (Auth::user()->can('create doctype')) {
$validator = FacadesValidator::make(
$request->all(),
[
'name' => 'required',
]
);
if ($validator->fails()) {
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$type = new DocType();
$type['name'] = $request->name;
$type['description'] = $request->description;
$type['created_by'] = Auth::user()->creatorId();
$type->save();
return redirect()->route('doctype.index')->with('success', __('Document type successfully created.'));
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
if (Auth::user()->can('edit doctype')) {
$type = DocType::find($id);
return view('doctype.edit', compact('type'));
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
if (Auth::user()->can('edit doctype')) {
$type = DocType::find($id);
$validator = FacadesValidator::make(
$request->all(),
[
'name' => 'required',
]
);
if ($validator->fails()) {
$messages = $validator->getMessageBag();
return redirect()->back()->with('error', $messages->first());
}
$type['name'] = $request->name;
$type['description'] = $request->description;
$type->save();
return redirect()->route('doctype.index')->with('success', __('Document type successfully updated.'));
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if (Auth::user()->can('delete doctype')) {
$type = DocType::find($id);
if ($type) {
$type->delete();
}
return redirect()->route('doctype.index')->with('success', __('Document type successfully deleted.'));
} else {
return redirect()->back()->with('error', __('Permission Denied.'));
}
}
}