ldc-com-app-bo/app/Http/Controllers/HomeController.php

63 lines
2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Contacts;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$version = Setting::where('key', 'version')->first();
return view('home', ['version' => $version->value]);
}
public function newVersion(Request $request) {
$version = Setting::where('key', 'version')->first();
$version->value = $version->value + 1;
$version->save();
return redirect()->route('home');
}
/**
* Récupérer les contacts (csv) et les fichiers
*/
public function getContacts() {
// generate csv
$csv_file = 'contact.csv';
$csvExporter = new \Laracsv\Export();
$contacts = Contacts::all();
$csvExporter->beforeEach(function ($contact) {
$contact->phone = "$contact->phone";
});
$csvExporter->getWriter()->setDelimiter(';');
$csvExporter->getWriter()->setEnclosure('"');
$csvExporter->getWriter()->setOutputBOM(\League\Csv\Reader::BOM_UTF8);
$csvExporter->build($contacts, ['owner_name' => 'Propriétaire', 'email' => 'Email', 'type' => 'Type du contact', 'other' => 'Type autre', 'network' => 'Réseau commercial', 'name' => 'Nom', 'firstname' => 'Prénom', 'office' => 'Poste', 'company' => 'Entreprise', 'address' => 'Adresse', 'phone' => 'Téléphone', 'notes' => 'Notes']);
Storage::disk('public')->put($csv_file, $csvExporter->getWriter()->getContent());
return response()->download(storage_path('app/public/') . "$csv_file", 'contacts.csv', ['Content-Type' => 'text/csv; charset=utf-8'])->deleteFileAfterSend();
}
}