✨ Ajoute des vues
This commit is contained in:
parent
b80bde6b1d
commit
0b50e75fa2
99
app/Deal.php
Normal file
99
app/Deal.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use function GuzzleHttp\json_decode;
|
||||
use Shikiryu\LBCReposter\Categories;
|
||||
|
||||
class Deal
|
||||
{
|
||||
/** @var Folder */
|
||||
protected $folder;
|
||||
/** @var array */
|
||||
protected $data;
|
||||
/** @var string[] */
|
||||
protected $images = [];
|
||||
/** @var int */
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Deal constructor.
|
||||
* @param $folder
|
||||
* @param int $id
|
||||
*/
|
||||
public function __construct($folder, $id)
|
||||
{
|
||||
$this->folder = $folder;
|
||||
$this->id = $id;
|
||||
foreach (new \DirectoryIterator(sprintf('%s/%s/%s', config('app.deals_dire'), $folder, $id)) as $file) {
|
||||
if ($file->getExtension() === 'json') {
|
||||
$content = file_get_contents($file->getPathname());
|
||||
$this->data = json_decode($content, true);
|
||||
} elseif ($file->getExtension() === 'jpg') {
|
||||
$data = file_get_contents($file->getPathname());
|
||||
$base64 = 'data:image/jpg;base64,' . base64_encode($data);
|
||||
$this->images[] = $base64;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getDateCreation(): \DateTime
|
||||
{
|
||||
$date_creation = \DateTime::createFromFormat('Y-m-d H:i:s.u', $this->data['datecreation']['date']);
|
||||
return $date_creation;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \string[]
|
||||
*/
|
||||
public function getImages(): array
|
||||
{
|
||||
return $this->images;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->data['subject'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->data['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getPrice(): float
|
||||
{
|
||||
return $this->data['price'];
|
||||
}
|
||||
|
||||
public function getCategory(): string
|
||||
{
|
||||
return Categories::$categories[$this->data['category']];
|
||||
}
|
||||
|
||||
}
|
11
app/Folder.php
Normal file
11
app/Folder.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
|
||||
class Folder
|
||||
{
|
||||
const FOLDER_NEW = 'new';
|
||||
const FOLDER_BACKUP = 'backup';
|
||||
const FOLDER_CURRENT = 'list';
|
||||
}
|
@ -2,14 +2,15 @@
|
||||
|
||||
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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@ -25,4 +26,27 @@ class HomeController extends Controller
|
||||
{
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,8 @@ return [
|
||||
|
||||
'name' => env('APP_NAME', 'Laravel'),
|
||||
|
||||
'deals_dir' => env('DEALS_DIR', storage_path('deals')),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Application Environment
|
||||
|
@ -15,6 +15,17 @@
|
||||
@endif
|
||||
|
||||
You are logged in!
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="<?php echo route('deals.list', ['type' => 'all']); ?>">Les deals</a>
|
||||
<ul>
|
||||
<li> <a href="<?php echo route('deals.list', ['type' => \App\Folder::FOLDER_CURRENT]); ?>">En cours</a></li>
|
||||
<li> <a href="<?php echo route('deals.list', ['type' => \App\Folder::FOLDER_NEW]); ?>">En attente d'ajout</a></li>
|
||||
<li> <a href="<?php echo route('deals.list', ['type' => \App\Folder::FOLDER_BACKUP]); ?>">Sauvegardés</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
47
resources/views/list.blade.php
Executable file
47
resources/views/list.blade.php
Executable file
@ -0,0 +1,47 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-8 col-md-offset-2">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Dashboard</div>
|
||||
|
||||
<div class="panel-body">
|
||||
@if (session('status'))
|
||||
<div class="alert alert-success">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Image</th>
|
||||
<th>Titre</th>
|
||||
<th>Date</th>
|
||||
<th>Options</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>@foreach($deals as $deal)
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
</td>
|
||||
<td>
|
||||
{{$deal->getSubject()}}
|
||||
</td>
|
||||
<td>
|
||||
{{$deal->getDateCreation()->format('d/m/Y')}}
|
||||
</td>
|
||||
<td>
|
||||
<a href="<?php echo route('deals.view', ['type' => $type, 'id' => $deal->getId()]); ?>">Voir</a>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach</tbody>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
30
resources/views/view.blade.php
Executable file
30
resources/views/view.blade.php
Executable file
@ -0,0 +1,30 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-8 col-md-offset-2">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Dashboard</div>
|
||||
|
||||
<div class="panel-body">
|
||||
@if (session('status'))
|
||||
<div class="alert alert-success">
|
||||
{{ session('status') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<h1>{{ $deal->getSubject() }}</h1>
|
||||
<h2>{{ $deal->getCategory() }}</h2>
|
||||
<p>id : <strong>{{ $deal->getId() }}</strong> − proposée le <strong>{{$deal->getDateCreation()->format('d/m/Y')}}</strong> à <strong>{{ $deal->getPrice() }} €</strong></p>
|
||||
<p>{{ $deal->getBody() }}</p>
|
||||
<ul>@foreach($deal->getImages() as $image)
|
||||
<li><img src="{{ $image }}"/></li>
|
||||
@endforeach</ul>
|
||||
</div>
|
||||
<div class="panel-footer"><a href="<?php echo route('deals.list', ['type' => $type]); ?>">Revenir à la liste</a></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
@ -11,14 +11,12 @@
|
||||
|
|
||||
*/
|
||||
|
||||
Route::get('/', function () {
|
||||
return view('welcome');
|
||||
});
|
||||
Route::redirect('/', 'home');
|
||||
|
||||
Auth::routes();
|
||||
|
||||
Route::get('/home', 'HomeController@index')->name('home');
|
||||
Route::redirect('/', 'deals.list');
|
||||
|
||||
Auth::routes();
|
||||
Route::get('/deals/list/{type}', 'HomeController@list')->name('deals.list');
|
||||
|
||||
Route::get('/home', 'HomeController@index')->name('home');
|
||||
Route::get('/deals/view/{type}/{id}', 'HomeController@view')->name('deals.view');
|
2
storage/deals/backup/.gitignore
vendored
Executable file
2
storage/deals/backup/.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
2
storage/deals/list/.gitignore
vendored
Executable file
2
storage/deals/list/.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
2
storage/deals/new/.gitignore
vendored
Executable file
2
storage/deals/new/.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
Loading…
Reference in New Issue
Block a user