2017-11-12 15:09:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-11-12 23:36:48 +01:00
|
|
|
use App\Deal;
|
2017-11-12 15:09:38 +01:00
|
|
|
use Illuminate\Http\Request;
|
2017-11-12 23:36:48 +01:00
|
|
|
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
2017-11-12 15:09:38 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
2017-11-12 23:36:48 +01:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
2017-11-13 20:51:16 +01:00
|
|
|
|
2017-11-20 12:52:52 +01:00
|
|
|
public function update(Request $request, $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);
|
|
|
|
}
|
|
|
|
if ($request->isMethod('post')) {
|
|
|
|
// TODO filter
|
2017-12-10 22:20:58 +01:00
|
|
|
|
|
|
|
$json_data = \json_decode(file_get_contents($folder_path . '/data.json'), true);
|
|
|
|
$json_data['subject'] = $request->subject;
|
|
|
|
$json_data['body'] = $request->body;
|
|
|
|
$json_data['category'] = $request->category;
|
|
|
|
$json_data['price'] = $request->price;
|
|
|
|
$result = file_put_contents($folder_path . '/data.json', \json_encode($json_data, JSON_UNESCAPED_UNICODE)) !== false;
|
|
|
|
// FIXME when API is updated
|
|
|
|
/*$updated_deal = \Shikiryu\LBCReposter\Deal::fromJSON($folder_path . '/data.json');
|
2017-11-20 15:29:48 +01:00
|
|
|
$result =
|
|
|
|
$updated_deal
|
|
|
|
->setSubject($request->subject)
|
|
|
|
->setBody($request->body)
|
|
|
|
->setCategory($request->category)
|
2017-12-10 22:20:58 +01:00
|
|
|
->setPrice($request->price);
|
|
|
|
->save(sprintf('%s/%s', config('app.deals_dir'), $type), false);*/
|
2017-11-20 12:52:52 +01:00
|
|
|
return redirect()->route('deals.list', ['type' => $type])->with('status', ($result ? 'Deal mis à jour': 'ERREUR'));
|
|
|
|
}
|
|
|
|
return view('update', ['deal' => $deal, 'type' => $type]);
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:51:16 +01:00
|
|
|
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'));
|
|
|
|
}
|
2017-11-12 15:09:38 +01:00
|
|
|
}
|