File: D:/HostingSpaces/SBogers84/zuiderbos.nl/app/Komma/Search/SearchController.php
<?php
namespace Komma\Search;
use Carbon\Carbon;
use Illuminate\Support\Facades\Response;
use Komma\Kms\Schools\School;
use Komma\Pages\PageService;
use Komma\Search\Models\SearchResults;
class SearchController extends \BaseController
{
protected $pageService;
protected $searchService;
public function __construct(PageService $pageService, SearchService $searchService)
{
parent::__construct();
$this->pageService = $pageService;
$this->searchService = $searchService;
}
public function show($school)
{
$page = $this->pageService->getPageByRoute($school);
$page->root = $page;
$page->school = School::where('type', $page->code_name)
->first();
//get all page links
$links = $this->pageService->getAllRoutes();
// Get search results
$results = $this->searchService->getResults(\Input::get('q', null), $page->school);
return \View::make('layouts.pages.search')
->with('page', $page)
->with('results', $results)
->with('links', $links);
}
public function searchPages()
{
$this->searchService->updateSearchTable();
}
/**
* Creates a temporary folder in app/storage if it does not exist. Clears it if it exists
*
* @param $folder
* @return string
*/
public static function createOrEmptyTempFolder($folder)
{
$folder = storage_path().DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR;
if (! file_exists($folder)) {
if (! mkdir($folder)) {
throw new \RuntimeException('The temporary folder to temporary store the search results in could not be created. Please contact your website builder. '.$folder);
}
} else {
self::rrmdir($folder, false);
}
return $folder;
}
/**
* Recursively deletes a the contents in a directory and if you specify the boolean argument to true the directory itself too
*
* @param $dir
* @param bool $deleteDir
*/
public static function rrmdir($dir, $deleteDir = true)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir.DIRECTORY_SEPARATOR.$object) == 'dir') {
self::rrmdir($dir.DIRECTORY_SEPARATOR.$object);
} else {
if (! unlink($dir.DIRECTORY_SEPARATOR.$object)) {
throw new \RuntimeException('Could not empty the temporary folder to store the exported results in. Please contact your website builder '.$dir.'/'.$object);
}
}
}
}
reset($objects);
if ($deleteDir) {
rmdir($dir);
}
}
}
/**
* Exports the search queries to csv
*
* @param null $dateFrom
* @param null $dateTrough
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public static function exportSearchQueries($dateFrom = null, $dateTrough = null)
{
$folder = self::createOrEmptyTempFolder('searchresults');
$filename = 'zoekresultaten.csv';
if (! $dateFrom || ! $dateTrough) {
$results = SearchResults::all();
} else {
$dateFrom = Carbon::createFromFormat('d-m-Y', $dateFrom);
$dateTrough = Carbon::createFromFormat('d-m-Y', $dateTrough);
$results = SearchResults::whereBetween('updated_at', [$dateFrom, $dateTrough])->get();
}
//Create the csv array to export
$headers = ['Zoekopdracht', 'Aantal keer gezocht', 'Opgeleverde resultaten'];
$csvArray = [];
//Add search results to a csv array
$results->each(function ($result) use (&$csvArray) {
$csvArray[] = [$result->query, $result->results, $result->created_at->toDateTimeString()]; //created at is a Carbon instance
});
//Refine search results for exporting
$termColumn = 0;
$quantityColumn = 1;
$numberOfTimesSearchedForColumn = 1; //Export array only
$exportArray = []; //Export array will hold non duplicate values that exist in the $csvArray. Each row is an array formatted like this: search term, search amount, date searched
foreach ($csvArray as $rowNumber => $row) {
//Check if the search term already exists in the export array
$foundAtRow = false;
foreach ($exportArray as $exportRowNumber => $exportRow) {
if (strtolower($exportRow[$termColumn]) == strtolower($row[$termColumn])) {
$foundAtRow = $exportRowNumber;
break;
}
}
if ($foundAtRow) {
//Term found already. Just increment search amount
$exportArray[$foundAtRow][$numberOfTimesSearchedForColumn]++;
} else {
//Term not found already. Insert is as a new term
$exportArray[] = [
$row[$termColumn],
1,
$row[$quantityColumn],
];
}
}
$fileHandler = fopen($folder.$filename, 'w+');
fputcsv($fileHandler, $headers, ';');
foreach ($exportArray as $row) {
// foreach($csvArray as $row) {
fputcsv($fileHandler, $row, ';');
}
fclose($fileHandler);
return Response::download($folder.$filename, $filename);
}
}