Skip to content

Commit

Permalink
CSV download/generation added.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylephillips committed Nov 20, 2016
1 parent 90560ad commit a9f98aa
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions app/Services/CSVDownload/HistoryCsvDownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace SimpleLocator\Services\CSVDownload;

use SimpleLocator\Repositories\SearchHistoryRepository;
use League\Csv\Writer;

class HistoryCsvDownload
{
Expand All @@ -19,12 +20,48 @@ public function __construct()
{
$this->search_history_repo = new SearchHistoryRepository;
$this->getResults();
$this->generateCsv();
}

/**
* Get the Search History based on post params
*/
private function getResults()
{
var_dump($_POST);
$this->results = $this->search_history_repo->setSearchHistory('POST');
var_dump($this->results); die();
}

/**
* Generate the CSV
*/
private function generateCsv()
{
$csv = Writer::createFromFileObject(new \SplTempFileObject());

// Header Row
$csv->insertOne([
__('Time', 'wpsimplelocator'),
__('User IP', 'wpsimplelocator'),
__('Search Latitude', 'wpsimplelocator'),
__('Search Longitude', 'wpsimplelocator'),
__('Search Term', 'wpsimplelocator'),
__('Search Term Formatted', 'wpsimplelocator'),
__('Distance', 'wpsimplelocator')
]);

foreach ( $this->results as $result ){
$csv->insertOne([
$result->time,
$result->user_ip,
$result->search_lat,
$result->search_lng,
$result->search_term,
$result->search_term_formatted,
$result->distance
]);
}

$filename = __('location-search-history', 'wpsimplelocator') . '.csv';
$csv->output($filename);
}
}

0 comments on commit a9f98aa

Please sign in to comment.