web/app/Http/Controllers/HomeController.php
2017-11-13 20:51:16 +01:00

67 lines
1.9 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers;
use App\Deal;
use Illuminate\Http\Request;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
public function list($type)
{
$deals = [];
$folder_path = sprintf('%s/%s', config('app.deals_dir'), $type);
if (file_exists($folder_path)) {
foreach (new \RecursiveDirectoryIterator($folder_path) as $folder) {
if (file_exists($folder->getPathname().'/data.json')) {
$deals[] = new Deal($folder_path, $folder->getBasename());
}
}
}
return view('list', ['deals' => $deals, 'type' => $type]);
}
public function view($type, $id)
{
$folder_path = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
if (file_exists($folder_path)) {
$deal = new Deal(sprintf('%s/%s', config('app.deals_dir'), $type), $id);
}
return view('view', ['deal' => $deal, 'type' => $type]);
}
public function delete($type, $id)
{
$result = false;
$folder = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
if (file_exists($folder)) {
$files = array_diff(scandir($folder), ['.','..']);
foreach ($files as $file) {
(is_dir(sprintf('%s/%s', $folder, $file))) ? delete_folder(sprintf('%s/%s', $folder, $file)) : unlink(sprintf('%s/%s', $folder, $file));
}
$result = rmdir($folder);
}
return redirect()->route('deals.list', ['type' => $type])->with('status', ($result ? 'Deal supprimé': 'ERREUR'));
}
}