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

204 lines
6.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Contact;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Context;
class ContactController extends Controller
{
public function __construct()
{
$this->middleware([ "auth" ]);
$this->middleware([ "lang" ]);
$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");
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$contact = Contact::query()->paginate($request->input("limit", 20));
return Response::detect("contacts.index", [ "contacts" => $contact]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return Response::detect("contacts.create");
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$requestContact = $request->validate([
"contactname" => "required|max:255",
"title" => "required|max:255",
"email" => "required|max:255",
"phone" => "max:255",
]);
$contact = new Contact($requestContact);
$contact->phonetimes = $request->phonetimes;
$saved = $contact->save();
if(!$saved){
return redirect()->route("contacts.store");
}else{
$contact = Contact::query()->paginate($request->input("limit", 20));
return redirect()->route("contacts.index", ['contacts' => $contact]);
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return Response::detect("contacts.show", [ "contacts" => $id]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$contact = Contact::find($id);
return Response::detect("contacts.edit", ["contact" => $contact]);
}
/**
* 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 redirect()->route("contacts.update", [ "contacts" => $contact ]);
}else{
$contact = Contact::query()->paginate($request->input("limit", 20));
return redirect()->route("contacts.index", ['contacts' => $contact]);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function destroy($id)
{
$contact = Contact::find($id);
$contact->delete();
return redirect()->route("contacts.index");
}
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>";
if($request->isCheck === "navn")
$users = Contact::query()->where('contactname', 'LIKE',$request->search.'%')->get();
elseif ($request->isCheck === "titel")
$users = Contact::query()->where('title', 'LIKE',$request->search.'%')->get();
elseif ($request->isCheck === "email")
$users = Contact::query()->where('email', 'LIKE',$request->search.'%')->get();
elseif ($request->isCheck === "tf")
$users = Contact::query()->where('phone', 'LIKE',$request->search.'%')->get();
else
$users = DB::table('contacts')->where('contactname', 'LIKE',$request->search.'%')
->orWhere('title','LIKE', $request->search.'%')
->orWhere('phone','LIKE', $request->search.'%')
->orWhere('email','LIKE',$request->search. '%')->get();
if(count($users) !== 0){
foreach ($users as $key => $user){
$output.='<tr>'.
'<td>' . $user->contactname . '</td>'.
'<td>' . $user->title . '</td>'.
'<td>' . $user->email . '</td>'.
'<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>'.
'</tr>';
}
}else{
$output.='<tr>'.
'<td>Intet match</td>'.
'<td></td>'.
'<td></td>'.
'<td></td>'.
'<td></td>'.
'<td></td>'.
'</tr>';
}
return Response($output);
}
}
}