2020-06-29 06:50:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2020-06-29 08:07:42 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use App\Contact;
|
2020-07-07 17:29:21 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-07-01 07:50:37 +00:00
|
|
|
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
|
|
|
|
*/
|
2020-06-29 08:07:42 +00:00
|
|
|
public function index(Request $request)
|
2020-06-29 06:50:50 +00:00
|
|
|
{
|
2020-06-29 08:07:42 +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",
|
2020-06-29 09:14:07 +00:00
|
|
|
"phone" => "required|max:255",
|
2020-06-29 08:30:09 +00:00
|
|
|
]);
|
|
|
|
|
2020-06-29 08:41:27 +00:00
|
|
|
$contact = new Contact($requestContact);
|
2020-07-01 07:50:37 +00:00
|
|
|
$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)
|
|
|
|
{
|
2020-06-29 09:48:36 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-06-29 09:48:36 +00:00
|
|
|
|
|
|
|
$data = $request->all();
|
|
|
|
$contact = Contact::find($id);
|
|
|
|
$contact->update($data);
|
2020-07-01 07:50:37 +00:00
|
|
|
$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 09:48:36 +00:00
|
|
|
|
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();
|
2020-06-29 09:48:36 +00:00
|
|
|
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()){
|
2020-07-27 12:03:57 +00:00
|
|
|
$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
|
|
|
|
2020-07-27 12:03:57 +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>'.
|
2020-07-27 12:03:57 +00:00
|
|
|
'<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>'.
|
2020-07-28 12:23:54 +00:00
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
|
|
|
'<td></td>'.
|
2020-07-28 09:07:33 +00:00
|
|
|
'</tr>';
|
2020-07-07 17:29:21 +00:00
|
|
|
}
|
2020-07-27 12:03:57 +00:00
|
|
|
return Response($output);
|
2020-07-07 17:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-29 06:50:50 +00:00
|
|
|
}
|