177 lines
5.8 KiB
PHP
177 lines
5.8 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()
|
|
{
|
|
//Check authentication and languages settings
|
|
$this->middleware([ "auth" ]);
|
|
$this->middleware([ "lang" ]);
|
|
|
|
//The middleware is being run just before the pages is getting loaded.
|
|
|
|
//We use this middleware to chek if a user has the right permission/authentication to view the pages
|
|
$this->middleware([ "check.auth:contact.show" ])->only("show", "index");
|
|
$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
|
|
*/
|
|
|
|
//Controller for the contact index pages, all backend work for the contact index pages is done here.
|
|
public function index(Request $request)
|
|
{
|
|
//We use the Contact model to query all information from the contact form in the database
|
|
$contact = Contact::query()->get();
|
|
|
|
/*
|
|
Here we return/pass the contact variable containing all the database information to the
|
|
contacts.index pages as a parameter. The "contacts" in green text is the name you have to use
|
|
on the index pages to get the data from the database, you can name it what ever you want, but its best
|
|
best practice to give it a describing name.
|
|
*/
|
|
return Response::detect("contacts.index", [ "contacts" => $contact]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
|
|
/*The create controller, all it has to do is redirect you to the contacts create pages*/
|
|
public function create()
|
|
{
|
|
return Response::detect("contacts.create");
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
|
|
//The store controller is used to store user data.
|
|
public function store(Request $request)
|
|
{
|
|
//Validate that the user gave a contactname, title, email, phone and make sure its required and only contains max 255.
|
|
$requestContact = $request->validate([
|
|
"contactname" => "required|max:255",
|
|
"title" => "required|max:255",
|
|
"email" => "required|max:255",
|
|
"phone" => "max:255",
|
|
]);
|
|
|
|
//Make a new instance of the contact modal and pass $requestContact to be saved in the database
|
|
$contact = new Contact($requestContact);
|
|
//Set phonetimes to what the user entered, we dont validate thats why it has its on "save" function .
|
|
$contact->phonetimes = $request->phonetimes;
|
|
//Call the save function.
|
|
$saved = $contact->save();
|
|
|
|
//If it couldn't save redirect to the contacts.store view
|
|
if(!$saved){
|
|
return redirect()->route("contacts.store");
|
|
}else{
|
|
/*If it did get saved query all information from the database and redirect to contacts.index with the parameter
|
|
contacts
|
|
*/
|
|
$contact = Contact::query()->get();
|
|
return redirect()->route("contacts.index", ['contacts' => $contact]);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
|
|
//The show controller is used to show a single entity from the database based on the id its get
|
|
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
|
|
*/
|
|
|
|
/*Edit controller finds all the data on the specific id passed to it, finds that entity from the database and
|
|
that match with the id and redirect you to the edit pages.
|
|
*/
|
|
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
|
|
*/
|
|
|
|
//Update controller
|
|
public function update(Request $request, $id)
|
|
{
|
|
//request all the data from the database
|
|
$data = $request->all();
|
|
//Find the entity in the database that match the id
|
|
$contact = Contact::find($id);
|
|
//Call the update function
|
|
$contact->update($data);
|
|
//Call the save function
|
|
$saved = $contact->save();
|
|
|
|
//If it couldn't save redirect to the contacts.update view
|
|
if(!$saved){
|
|
return redirect()->route("contacts.update", [ "contacts" => $contact ]);
|
|
}else{
|
|
/*If it did get saved query all information from the database and redirect to contacts.index with the parameter
|
|
contacts
|
|
*/
|
|
$contact = Contact::query()->get();
|
|
return redirect()->route("contacts.index", ['contacts' => $contact]);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @throws \Exception
|
|
*/
|
|
//The destroy controller finds the id and calls a delete function, after that it redirects to contact index.
|
|
public function destroy($id)
|
|
{
|
|
$contact = Contact::find($id);
|
|
$contact->delete();
|
|
return redirect()->route("contacts.index");
|
|
}
|
|
}
|