File: /var/www/vhost/disk-apps/sigedo-demo.allup.com.co/app/Http/Controllers/InstitutionsController.php
<?php
namespace App\Http\Controllers;
use App\Sector;
use App\Position;
use App\Department;
use App\Institution;
use App\AcademicCharacter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class InstitutionsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
abort_unless(\Gate::allows('institution_access'), 403);
$institutions = Institution::with(['sector','academic_character','city','position'])->orderBy('active', 'desc')->orderBy('name', 'asc')->get();
return view('institutions.list', compact('institutions'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
abort_unless(\Gate::allows('institution_create'), 403);
$sectors = Sector::where('active', true)->get();
$departments = Department::where('active', true)->get();
$positions = Position::where('active', true)->get();
$academicCharacters = AcademicCharacter::where('active', true)->get();
return view('institutions.create', compact('sectors','departments','positions','academicCharacters'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
abort_unless(\Gate::allows('institution_create'), 403);
if (!Institution::where([['code', $request->input('code')]])->first()) {
if ($data = Institution::create($request->all())) {
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('created_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_creating'), "data" => null));
}
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('code')]), "data" => null));
}
}
/**
* Display the specified resource.
*
* @param int $institution_id
* @return \Illuminate\Http\Response
*/
public function show($institution_id)
{
abort_unless(\Gate::allows('institution_show'), 403);
return redirect()->back();
}
/**
* Show the form for editing the specified resource.
*
* @param int $institution_id
* @return \Illuminate\Http\Response
*/
public function edit($institution_id)
{
abort_unless(\Gate::allows('institution_edit'), 403);
$institution = Institution::findOrFail($institution_id);
$sectors = Sector::where('active', true)->get();
$departments = Department::where('active', true)->get();
$positions = Position::where('active', true)->get();
$academicCharacters = AcademicCharacter::where('active', true)->get();
return view('institutions.edit', compact('institution','sectors','departments','positions','academicCharacters'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $institution_id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $institution_id)
{
abort_unless(\Gate::allows('institution_edit'), 403);
if (!Institution::where([['id', '!=', $institution_id], ['code', $request->input('code')]])->first()) {
if (Institution::where('id', $institution_id)->update(array_slice($request->all(), 1))) {
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('updated_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_updating'), "data" => null));
}
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('code')]), "username" => null));
}
}
/**
* Remove the specified resource from storage.
*
* @param int $institution_id
* @return \Illuminate\Http\Response
*/
public function destroy($institution_id)
{
abort_unless(\Gate::allows('institution_destroy'), 403);
try {
if (Institution::where('id', $institution_id)->delete()) {
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('deleted_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_removing'), "data" => null));
}
} catch (\Illuminate\Database\QueryException $e) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('delete_relation_data'), "data" => null));
}
}
}