File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Debtors/DebtorController.php
<?php
namespace KommaApp\Debtors;
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
use App\Events\SearchDebtors;
use App\Events\ShowDebtorDetail;
use KommaApp\Core\CoreController;
use KommaApp\Debtors\DebtorService;
use Symfony\Component\Console\Input\Input;
class DebtorController extends CoreController
{
private $debtorService;
public function __construct(DebtorService $debtorService)
{
//Authentication, check if the user is logged in
$this->middleware('auth:customer');
$this->debtorService = $debtorService;
}
/**
* This method will show the search form
*
* @return mixed
*/
public function search()
{
$this->debtorService->showRobotOnFirstVisit();
$message = (object)[
'status' => false,
'type' => 0,
];
if(\Session::has('myFlashMessage')){
$message->status = true;
$message->type = \Session::get('myFlashMessage');
\Session::forget('myFlashMessage');
}
return \View::make('debtors.search')->with('message', $message);
}
public function results()
{
//Check if there is an encryptedSearchField
if ($encryptedSearchField = \Session::get('encSearchField')) {
//If true, show the results for the decrypted term
return $this->showResultsFor(\Crypt::decrypt($encryptedSearchField));
}
return $this->search();
}
/**
* This method will search the debtors based on the form input
*
* @return mixed
*/
public function processSearch()
{
//Validate the searchForm
if ($messages = $this->debtorService->validateSearchForm(\Input::all())) return \Redirect::back()->withInput()->withErrors($messages);
$searchTerm = \Input::get('search_field');
//Valid search term
if (!$this->debtorService->validateSearchTerm($searchTerm)) {
return \Redirect::back()
->withInput()
->withErrors(['not_specific' => \Lang::get('validation.not_specific')]);
}
//show the detail page for the searchTerm
return $this->showResultsFor($searchTerm, true);
}
/**
* This will show a detail page of a debtor
*
* @param $code
* @return mixed
*/
public function detail($code)
{
//Check if we can get a debtor by the given hash, if not trhow error
if (!$debtor = $this->debtorService->getDebtorByHash($code)) \App::abort('404', \Lang::get('debtors.malformed'));
//Fire the ShowDebtorDetail event
\Event::fire(new ShowDebtorDetail($debtor));
//Debtor found, return detail page
return \View::make('debtors.detail')->withDebtor($debtor);
}
/**
* Show debtors based on a given searchTerm
*
* @param $searchTerm
* @return mixed
*/
public function showResultsFor($searchTerm)
{
//Search the debtors with the search-field
$debtors = $this->debtorService->search($searchTerm);
//Fire the SearchDebtors event, when it is an new search
if(\Input::has('search_field'))
\Event::fire(new SearchDebtors($searchTerm, $debtors));
//No debtors found, return to the main page with a message
if ($debtors->count() == 0) {
return \Redirect::route('debtors.search')
->withInput()
->withErrors(['no_debtors_found' => \Lang::get('validation.no_debtors_found', ['search_term' => \Input::get('search_field')])]);
}
\Session::set('encSearchField', \Crypt::encrypt($searchTerm));
//Set the searchTerm to the search_field
\Input::merge(['search_field' => $searchTerm]);
//Debtors found, return the debtor result page
return \View::make('debtors.result')->withDebtors($debtors)->withInput(\Input::all());
}
}