Ekapp/skolehjem/app/Http/Controllers/ContactController.php

185 lines
5.7 KiB
PHP
Raw Normal View History

2020-06-29 06:50:50 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Contact;
2020-07-07 17:29:21 +00:00
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Context;
2020-07-27 11:24:35 +00:00
2020-06-29 06:50:50 +00:00
class ContactController extends Controller
{
2020-06-30 07:21:21 +00:00
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "check.auth:contact.list" ])->only("index");
$this->middleware([ "check.auth:contact.show" ])->only("show");
$this->middleware([ "check.auth:contact.create" ])->only("create", "store");
$this->middleware([ "check.auth:contact.edit" ])->only("edit", "update");
$this->middleware([ "check.auth:contact.delete" ])->only("delete");
}
2020-06-29 06:50:50 +00:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
2020-06-29 06:50:50 +00:00
{
$contact = Contact::query()->paginate($request->input("limit", 20));
2020-06-29 09:39:00 +00:00
return Response::detect("contacts.index", [ "contacts" => $contact]);
2020-06-29 06:50:50 +00:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
2020-06-29 08:41:27 +00:00
return Response::detect("contacts.create");
2020-06-29 08:30:09 +00:00
2020-06-29 06:50:50 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
2020-06-29 08:30:09 +00:00
$requestContact = $request->validate([
2020-07-27 11:34:45 +00:00
"contactname" => "required|max:255",
"title" => "required|max:255",
2020-06-29 08:37:37 +00:00
"email" => "required|max:255",
"phone" => "required|max:255",
2020-06-29 08:30:09 +00:00
]);
2020-06-29 08:41:27 +00:00
$contact = new Contact($requestContact);
$saved = $contact->save();
if(!$saved){
return Response::detect("contacts.store");
}else{
$contact = Contact::query()->paginate($request->input("limit", 20));
return Response::detect("contacts.index", ['contacts' => $contact]);
}
2020-06-29 08:30:09 +00:00
2020-06-29 06:50:50 +00:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return Response::detect("contacts.show", [ "contacts" => $id]);
2020-06-29 06:50:50 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
2020-06-29 10:36:07 +00:00
$contact = Contact::find($id);
return Response::detect("contacts.edit", ["contact" => $contact]);
2020-06-29 06:50:50 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$data = $request->all();
$contact = Contact::find($id);
$contact->update($data);
$saved = $contact->save();
if(!$saved){
return Response::detect("contacts.update", [ "contacts" => $contact ]);
}else{
$contact = Contact::query()->paginate($request->input("limit", 20));
return Response::detect("contacts.index", ['contacts' => $contact]);
}
2020-06-29 06:50:50 +00:00
}
/**
* Remove the specified resource from storage.
*
2020-06-29 11:18:58 +00:00
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
2020-06-29 06:50:50 +00:00
*/
2020-06-30 09:45:54 +00:00
public function destroy($id)
2020-06-29 06:50:50 +00:00
{
2020-06-30 09:45:54 +00:00
$contact = Contact::find($id);
$contact->delete();
return redirect()->route("contacts.index");
2020-06-29 06:50:50 +00:00
}
2020-07-07 17:29:21 +00:00
public function search(Request $request){
if($request->ajax()){
$output = "<tr>".
"<th>Kontakt Navn</th>".
"<th>Titel</th>".
"<th>E-mail</th>".
"<th>Tlf</th>".
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/pencil.svg\" alt=\"Update\"></th>".
"<th style=\"width: 1em;\"><img class=\"w-100\" src=\"http://127.0.0.1:8000/images/icons/trashcan.svg\" alt=\"Delete\"></th>".
"</tr>";
2020-07-28 07:23:28 +00:00
$users = DB::table('contacts')->where('contactname', 'LIKE',$request->search.'%')
->orWhere('title','LIKE', $request->search.'%')
2020-07-27 11:07:19 +00:00
->orWhere('phone','LIKE', $request->search.'%')
->orWhere('email','LIKE',$request->search. '%')->get();
2020-07-07 17:29:21 +00:00
if(count($users) !== 0){
2020-07-07 17:29:21 +00:00
foreach ($users as $key => $user){
$output.='<tr>'.
2020-07-28 07:23:28 +00:00
'<td>' . $user->contactname . '</td>'.
'<td>' . $user->title . '</td>'.
2020-07-27 11:07:19 +00:00
'<td>' . $user->email . '</td>'.
2020-07-07 17:29:21 +00:00
'<td>' . $user->phone .'</td>'.
'<td><a href="'. route("contacts.edit", [ "contact" => $user->id ]) . '"><img class="w-100" src="'. asset('/images/icons/pencil-dark.svg') . '" alt="Update"></a></td>'.
'<td><form method="post" action="' .route("contacts.destroy", [ "contact" => $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>'.
2020-07-07 17:29:21 +00:00
'</tr>';
}
2020-07-28 09:07:33 +00:00
}else{
$output.='<tr>'.
'<td>Din søgning matchede ikke nogen personer</td>'.
'</tr>';
2020-07-07 17:29:21 +00:00
}
return Response($output);
2020-07-07 17:29:21 +00:00
}
}
2020-06-29 06:50:50 +00:00
}