Admin producto y mirar mapa

This commit is contained in:
2020-05-27 13:39:14 -04:00
parent 31f308f5c7
commit 24c74d2887
9 changed files with 294 additions and 41 deletions

View File

@ -5,6 +5,7 @@ use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\Blade as View;
use Carbon\Carbon;
class Productos {
public function __invoke(Request $request, Response $response, View $view, Container $container): Response {
@ -60,6 +61,66 @@ class Productos {
}
public function do_edit(Request $request, Response $response, Container $container, $producto): Response {
$post = $request->getParsedBody();
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$id = $producto;
$productos = json_decode(trim(file_get_contents($filename)));
$producto = $productos[$producto];
$changed = false;
$fields = ['nombre', 'segmento', 'direccion', 'comuna', 'ciudad', 'bono', 'rentabilidad', 'estado',
'cuota', 'unidades', 'modelos', 'descripcion', 'entrega'];
foreach ($fields as $field) {
if (!isset($producto->$field) or $post[$field] != $producto->$field) {
$producto->$field = $post[$field];
$changed = true;
}
}
$valor = number_format($post['valor'], 0, ',', '.');
if (!isset($producto->valor) or $producto->valor != $valor) {
$producto->valor = $valor;
$changed = true;
}
$tamaño = $post['tamaño_min'] . ' - ' . $post['tamaño_max'] . ' m²';
if (!isset($producto->tamaño) or $producto->tamaño != $tamaño) {
$producto->tamaño = $tamaño;
$changed = true;
}
$status1 = false;
if ($changed) {
$productos[$id] = $producto;
$status1 = (file_put_contents($filename, json_encode($productos, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
}
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'destacados.json'
]);
$destacados = json_decode(trim(file_get_contents($filename)));
$changed = false;
if (isset($post['destacado']) and $post['destacado'] == 'on') {
if (array_search($id, $destacados) === false) {
$destacados []= $id;
sort($destacados);
$changed = true;
}
} else {
if (($i = array_search($id, $destacados)) !== false) {
unset($destacados[$i]);
$destacados = array_values($destacados);
$changed = true;
}
}
$status2 = false;
if ($changed) {
$status2 = (file_put_contents($filename, json_encode($destacados)) !== false);
}
return $response
->withHeader('Location', implode('/', [$container->get('urls')->admin, 'productos']));
}
public function add(Request $request, Response $response, View $view, Container $container): Response {
$filename = implode(DIRECTORY_SEPARATOR, [
@ -71,17 +132,172 @@ class Productos {
}
public function do_add(Request $request, Response $response, Container $container): Response {
$post = $request->getParsedBody();
$producto = (object) [];
$fields = ['nombre', 'segmento', 'direccion', 'comuna', 'ciudad', 'bono', 'rentabilidad', 'estado',
'cuota', 'unidades', 'modelos', 'descripcion', 'entrega'];
foreach ($fields as $field) {
$producto->$field = $post[$field];
}
$valor = number_format($post['valor'], 0, ',', '.');
$producto->valor = $valor;
$tamaño = $post['tamaño_min'] . ' - ' . $post['tamaño_max'] . ' m²';
$producto->tamaño = $tamaño;
$f = Carbon::today();
$producto->publicacion = implode(' ', [
$f->day,
'de',
ucfirst($f->locale('es')->isoFormat('MMMM')) . ',',
$f->year
]);
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$productos = json_decode(trim(file_get_contents($filename)));
$productos []= $producto;
file_put_contents($filename, json_encode($productos, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES));
if (isset($post['destacado']) and $post['destacado'] == 'on') {
$id = count($productos) - 1;
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'destacados.json'
]);
$destacados = json_decode(trim(file_get_contents($filename)));
$destacados []= $id;
sort($destacados);
file_put_contents($filename, json_encode($destacados));
}
return $response
->withHeader('Location', implode('/', [$container->get('urls')->admin, 'productos']));
}
public function delete(Request $request, Response $response, Container $container): Response {
$post = $request->getParsedBody();
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$id = $post['id'];
$productos = json_decode(trim(file_get_contents($filename)));
$producto = $productos[$id];
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
unset($productos[$id]);
$status = false;
$status |= (file_put_contents($filename, json_encode($productos, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
if (file_exists($folder)) {
$status |= rmdir($folder);
}
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'destacados.json'
]);
$destacados = json_decode(trim(file_get_contents($filename)));
if (($i = array_search($id, $destacados)) !== false) {
unset($destacados[$i]);
$destacados = array_values($destacados);
$status |= (file_put_contents($filename, json_encode($destacados)) !== false);
}
$output = [
'information' => $post,
'estado' => $status
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
public function add_image(Request $request, Response $response, Container $container, $producto): Response {
$post = $request->getParsedBody();
$files = $request->getUploadedFiles();
$file = $files['imagen'];
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$id = $producto;
$productos = json_decode(trim(file_get_contents($filename)));
$producto = $productos[$producto];
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
if (!file_exists($folder)) {
mkdir($folder);
}
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
$file->getClientFilename()
]);
$file->moveTo($filename);
$output = [
'informacion' => $id,
'estado' => file_exists($filename)
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
public function set_video(Request $request, Response $response, Container $container, $producto): Response {
$post = $request->getParsedBody();
$files = $request->getUploadedFiles();
die();
$file = $files['video'];
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
}
public function delete_image(Request $request, Response $response, Container $container, $producto): Response {
$post = $request->getParsedBody();
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$id = $producto;
$productos = json_decode(trim(file_get_contents($filename)));
$producto = $productos[$producto];
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
$post['imagen']
]);
$status = unlink($filename);
$output = [
'estado' => $status
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
public function delete_video(Request $request, Response $response, Container $container, $producto): Response {
$post = $request->getParsedBody();
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
}
}

View File

@ -40,14 +40,16 @@ class Productos {
'images',
mb_strtolower($producto->nombre)
]);
$files = new \DirectoryIterator($folder);
$producto->images = [];
if (file_exists($folder)) {
$files = new \DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$producto->images []= $file->getFilename();
}
}
return $view->render($response, 'productos.show', compact('producto'));
}
public function segmento(Request $request, Response $response, Container $container, $segmento): Response {
@ -112,6 +114,23 @@ class Productos {
$producto = $productos[$producto];
$producto->destacado = $destacado;
$producto->id = $id;
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
$producto->imagen = 'default.jpg';
if (file_exists($folder)) {
$files = new \DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$producto->imagen = implode('/', [mb_strtolower($producto->nombre), $file->getFilename()]);
break;
}
}
return $view->render($response, 'productos.ficha', compact('producto'));
}
}

View File

Before

Width:  |  Height:  |  Size: 992 KiB

After

Width:  |  Height:  |  Size: 992 KiB

View File

@ -6,6 +6,7 @@ $app->group('/productos', function($app) {
$app->get('[/]', [Productos::class, 'add']);
$app->post('[/]', [Productos::class, 'do_add']);
});
$app->post('/delete', [Productos::class, 'delete']);
$app->get('[/]', Productos::class);
});
$app->group('/producto/{producto}', function($app) {

View File

@ -22,6 +22,10 @@
<label>Comuna</label>
<input type="text" name="comuna" value="{{$producto->comuna ?? ''}}" />
</div>
<div class="field">
<label>Ciudad</label>
<input type="text" name="ciudad" value="{{$producto->ciudad ?? ''}}" />
</div>
</div>
</div>
<div class="column">
@ -101,7 +105,7 @@
<div class="fields">
<div class="field">
<label>Tamaño M&iacute;nimo</label>
<input type="text" name="tamaño" value="{{(isset($producto->tamaño)) ? explode(' - ', rtrim($producto->tamaño, ' m²'))[0] : ''}}" />
<input type="text" name="tamaño_min" value="{{(isset($producto->tamaño)) ? explode(' - ', rtrim($producto->tamaño, ' m²'))[0] : ''}}" />
</div>
<div class="field">
<label>Tamaño M&aacute;ximo</label>
@ -122,6 +126,8 @@
</div>
<div id="imagenes" class="ui list"></div>
</div>
<?php
/*
<div class="column">
<div class="field">
<label>Video</label>
@ -138,6 +144,7 @@
@endif
</div>
</div>
*/?>
</div>
<br />
<button class="ui button">GUARDAR</button>
@ -192,9 +199,13 @@
text: {
months: months.long,
monthsShort: months.short
},
formatInput: false,
onChange: function(a, b) {
$(this).find('input').val(('0' + (a.getMonth() + 1)).slice(-2) + '/' + a.getFullYear())
}
})
var entrega = new Date('20{{implode('-', array_reverse(explode('/', $producto->entrega)))}}-01T01:00')
var entrega = new Date('{{str_pad(implode('-', array_reverse(explode('/', $producto->entrega))), 7, '20', STR_PAD_LEFT)}}-01T01:00')
$('.calendar').calendar('set date', entrega)
$('.checkbox').checkbox()
@if ($producto->destacado)

View File

@ -22,6 +22,10 @@
<label>Comuna</label>
<input type="text" name="comuna" />
</div>
<div class="field">
<label>Ciudad</label>
<input type="text" name="ciudad" />
</div>
</div>
</div>
<div class="column">
@ -101,7 +105,7 @@
<div class="fields">
<div class="field">
<label>Tamaño M&iacute;nimo</label>
<input type="text" name="tamaño" />
<input type="text" name="tamaño_min" />
</div>
<div class="field">
<label>Tamaño M&aacute;ximo</label>
@ -150,6 +154,10 @@
text: {
months: months.long,
monthsShort: months.short
},
formatInput: false,
onChange: function(a) {
$(this).find('input').val(('0' + (a.getMonth() + 1)).slice(-2) + '/' + a.getFullYear())
}
})
$('.checkbox').checkbox()

View File

@ -2,7 +2,6 @@
<div class="ui top attached tabular menu">
<a class="active item" data-tab="informacion">Informaci&oacute;n</a>
<a class="item" data-tab="mapa">Ver Mapa</a>
<a class="item" data-tab="video">Video</a>
<a class="item" data-tab="estado">Estado del producto</a>
</div>
<div class="ui bottom attached basic segment active tab" data-tab="informacion">
@ -14,7 +13,7 @@
Precio:
</div>
<div class="column">
{{$producto->valor}} UF
{{$producto->valor ?? ''}} UF
</div>
</div>
<div class="row">
@ -22,7 +21,7 @@
Estado:
</div>
<div class="column">
{{$producto->estado}}
{{$producto->estado ?? ''}}
</div>
</div>
<div class="row">
@ -30,7 +29,7 @@
Tipo:
</div>
<div class="column">
{{$producto->segmento}}
{{$producto->segmento ?? ''}}
</div>
</div>
<div class="row">
@ -38,7 +37,7 @@
Ubicaci&oacute;n:
</div>
<div class="column">
{{$producto->comuna}}, {{$producto->ciudad}}
{{$producto->comuna ?? ''}}, {{$producto->ciudad ?? ''}}
</div>
</div>
<div class="row">
@ -46,7 +45,7 @@
Unidades:
</div>
<div class="column">
{{$producto->unidades}}
{{$producto->unidades ?? ''}}
</div>
</div>
<div class="row">
@ -54,7 +53,7 @@
Modelos:
</div>
<div class="column">
{{$producto->modelos}}
{{$producto->modelos ?? ''}}
</div>
</div>
<div class="row">
@ -62,25 +61,21 @@
Tama&ntilde;o
</div>
<div class="column">
{{$producto->tamaño}}
{{$producto->tamaño ?? ''}}
</div>
</div>
</div>
</div>
<div class="twelve wide column">
{{$producto->descripcion}}
{{$producto->descripcion ?? ''}}
</div>
</div>
</div>
<div class="ui bottom attached basic segment tab" data-tab="mapa">
<div class="ui fluid placeholder">
<div class="image"></div>
</div>
</div>
<div class="ui bottom attached basic segment tab" data-tab="video">
<div class="ui fluid placeholder">
<div class="image"></div>
</div>
<div class="ui embed" id="map" data-source="Google Maps"
data-url="https://maps.google.com/maps?hl=es&amp;amp;q={{(isset($producto->direccion)) ? str_replace(' ', '%20', implode(', ', [
$producto->direccion, $producto->comuna
])) : 'Santiago,%20Chile'}}&amp;amp;ie=UTF8&amp;amp;z=11&amp;amp;iwloc=B&amp;amp;output=embed"></div>
</div>
<div class="ui bottom attached basic segment tab" data-tab="estado">
<div class="ui fluid placeholder">
@ -103,6 +98,7 @@
<script type="text/javascript">
$(document).ready(() => {
$('#datos').find('.tabular .item').tab()
$('#map').embed()
})
</script>
@endpush

View File

@ -1,4 +1,5 @@
<div id="galeria">
@if (count($producto->images))
<div class="ui image" data-id="0">
<img src="{{$urls->images}}/{{mb_strtolower($producto->nombre)}}/{{$producto->imagen}}" />
</div>
@ -14,6 +15,7 @@
</div>
@endforeach
</div>
@endif
</div>
@push('scripts')

View File

@ -10,10 +10,10 @@
{{$producto->nombre}}
</span>
<span class="direccion">
{{$producto->direccion}}, {{$producto->comuna}}, {{$producto->ciudad}}
{{$producto->direccion ?? ''}}, {{$producto->comuna ?? ''}}, {{$producto->ciudad ?? ''}}
</span>
<div class="publicado">
Publicado el {{$producto->publicacion}}
Publicado el {{$producto->publicacion ?? ''}}
</div>
</div>
@include('productos.producto.galeria')