Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
export json & csv #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille37 committed May 22, 2015
1 parent 9b719e7 commit 23ff77d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
85 changes: 84 additions & 1 deletion app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,90 @@ public function rentsCount()
public function rentsFindInBBox( $swLat, $swLng, $neLat, $neLng)
{
$rents = Rent::bBox($swLat, $swLng, $neLat, $neLng)->get() ;
return response()->json($rents);
$headers = array('Content-type'=> 'application/json; charset=utf-8', 'charset'=>'utf-8');
return response()->json( $rents, 200, $headers, JSON_UNESCAPED_UNICODE );
}

public function rentsExport($format)
{

switch( $format )
{

case 'csv':

$rentprices = RentPrice::all();
$rentprices->load('rent');

$csv = '' ;
$csv .= self::rowheaders_2_csv($rentprices[0]->toArray())."\n";
foreach ($rentprices as $row) {
error_log( var_export($row->toArray(),true ));
$csv .= self::row_2_csv($row->toArray())."\n" ;
}

$filename = 'prixDesLoyers_'.date('Y-m-d-H-i-s');
$headers = array(
'Content-Type' => 'text/csv; charset=utf-8',
'charset'=>'utf-8',
'Content-Disposition' => 'attachment; filename="'.$filename.'.csv"',
);
return response( $csv, 200, $headers );
break;

case 'json':

$rents = Rent::all();
$rents->load('prices');
$headers = array('Content-type'=> 'application/json; charset=utf-8', 'charset'=>'utf-8');
return response()->json( $rents, 200, $headers, JSON_UNESCAPED_UNICODE );
break;

default:
abort(501,'Export format not supported');
break;
}
}

static function rowheaders_2_csv($row) {
$csv = '' ;
foreach( $row as $k=>$v )
{
if( $v instanceof Arrayable )
{
$csv .= self::rowheaders_2_csv( $v->toArray() );
}
else if( is_array($v))
{
$csv .= self::rowheaders_2_csv( $v );
}
else
{
$csv .= ';'.$k ;
}
}
return $csv ;
}

static function row_2_csv($row) {

$csv = '' ;
foreach( $row as $k=>$v )
{
if( $v instanceof Arrayable )
{
$csv .= self::row_2_csv( $v->toArray() );
}
else if( is_array($v))
{
$csv .= self::row_2_csv( $v );
}
else
{
$csv .= ';'.$v ;
}
}
return $csv ;
}

}
5 changes: 5 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function about()
return view('about');
}

public function export()
{
return view('export');
}

}
3 changes: 2 additions & 1 deletion app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
$app->get( '/', ['as'=>'Home', 'uses'=>'App\Http\Controllers\Controller@home'] );
$app->get( '/about', ['as'=>'About', 'uses'=>'App\Http\Controllers\Controller@about'] );
$app->get( '/export', ['as'=>'Export', 'uses'=>'App\Http\Controllers\Controller@export'] );

$app->group( [ 'prefix' => 'rent' ],
function ( Laravel\Lumen\Application $app )
Expand All @@ -42,6 +43,6 @@ function ( $app )
$app->get(
'rentsFindInBBox/{swLat:[0-9\.\-]+}/{swLng:[0-9\.\-]+}/{neLat:[0-9\.\-]+}/{neLng:[0-9\.\-]+}',
'App\Http\Controllers\ApiController@rentsFindInBBox' );

$app->get( 'rentsExport/{format:json|csv|ods}', 'App\Http\Controllers\ApiController@rentsExport' );
} );

22 changes: 22 additions & 0 deletions resources/views/export.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

@extends('layout')

@section('title', 'Exporter les données')

@section('content')

<h1>Export</h1>

<h2>Export des loyers</h2>

<p>Tous les logements avec leurs loyers au format :</p>
<ul>
<li><a href="/api/rentsExport/json">JSON</a></li>
</ul>

<p>Tous les loyers avec leur logement au format :</p>
<ul>
<li><a href="/api/rentsExport/csv">CSV</a></li>
</ul>

@stop
3 changes: 2 additions & 1 deletion resources/views/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
<div class="collapse navbar-collapse" id="navbar-items-collapse" >
<ul class="nav navbar-nav">
<li ><a href="/rent">Ajouter un loyer<span class="sr-only">(current)</span></a></li>
<li><a href="/export">Export</a></li>
<li><a href="/about">À propos</a></li>
</ul>
</ul>
</div>

</div>
Expand Down

0 comments on commit 23ff77d

Please sign in to comment.