FIX: varias observaciones

This commit is contained in:
2020-06-03 23:54:15 -04:00
parent 5864efd24f
commit 4923c6c67d
5 changed files with 73 additions and 341 deletions

View File

@ -11,49 +11,10 @@ use ProVM\KI\Producto;
class Productos {
public function __invoke(Request $request, Response $response, View $view, ModelFactory $factory): Response {
/*$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$productos = json_decode(trim(file_get_contents($filename)));*/
$productos = $factory->find(Producto::class)->many();
return $view->render($response, 'admin.productos', compact('productos'));
}
public function edit(Request $request, Response $response, View $view, Container $container, ModelFactory $factory, $producto): Response {
/*$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'productos.json'
]);
$id = $producto;
$productos = json_decode(trim(file_get_contents($filename)));
$producto = $productos[$id];
$producto->id = $id;
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'destacados.json'
]);
$destacados = json_decode(trim(file_get_contents($filename)));
$destacado = false;
if (array_search($id, $destacados) !== false) {
$destacado = true;
}
$producto->destacado = $destacado;
$folder = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.public'),
'assets',
'images',
mb_strtolower($producto->nombre)
]);
$producto->images = [];
if (file_exists($folder)) {
$files = new \DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$producto->images []= $file->getFilename();
}
}*/
$producto = $factory->find(Producto::class)->where([['id', $producto]])->one();
$filename = implode(DIRECTORY_SEPARATOR, [
@ -66,33 +27,8 @@ class Productos {
}
public function do_edit(Request $request, Response $response, Container $container, ModelFactory $factory, $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];*/
$producto = $factory->find(Producto::class)->where([['id', $producto]])->one();
/*$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;
}*/
$f = Carbon::today();
$post['publicacion'] = $producto->publicacion ?? implode(' ', [
$f->day,
@ -103,37 +39,8 @@ class Productos {
$post['id'] = $producto->id;
$producto->map((object) $post);
/*$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);
}*/
$producto->save();
/*$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($producto->id, $destacados) === false) {
$destacados []= $producto->id;
sort($destacados);
$changed = true;
}
} else {
if (($i = array_search($producto->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']));
}
@ -148,33 +55,6 @@ class Productos {
public function do_add(Request $request, Response $response, Container $container, ModelFactory $factory): 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));*/
$f = Carbon::today();
$post['publicacion'] = implode(' ', [
$f->day,
@ -188,54 +68,11 @@ class Productos {
}
$producto->save();
/*if (isset($post['destacado']) and $post['destacado'] == 'on') {
//$id = count($productos) - 1;
$id = $producto->id;
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'destacados.json'
]);
$destacados = json_decode(trim(file_get_contents($filename)));
$destacados []= $proyecto->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, ModelFactory $factory): 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]);
$productos = array_values($productos);
$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);
}*/
$producto = $factory->find(Producto::class)->where([['id', $post['id']]])->one();
$status = $producto->delete();
@ -252,27 +89,6 @@ class Productos {
$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);*/
$producto = $factory->find(Producto::class)->where([['id', $producto]])->one();
$status = $producto->addImagen($file);
@ -299,24 +115,6 @@ class Productos {
}
public function delete_image(Request $request, Response $response, ModelFactory $factory, $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);*/
$producto = $factory->find(Producto::class)->where([['id', $producto]])->one();
$status = $producto->deleteImagen($post['imagen']);
@ -344,7 +142,7 @@ class Productos {
'ProVM',
'KI',
'Productos',
$post['segmento']
rtrim($post['segmento'], 's')
]);
$obj = new $class;
$properties = $obj->getProperties();

View File

@ -2,6 +2,7 @@
use ProVM\KI\Common\Controller\Web\Admin\Productos;
$app->group('/productos', function($app) {
$app->post('/campos', [Productos::class, 'fields']);
$app->group('/add', function($app) {
$app->get('[/]', [Productos::class, 'add']);
$app->post('[/]', [Productos::class, 'do_add']);

View File

@ -31,7 +31,7 @@
<div class="column">
<div class="field">
<label>Segmento</label>
<div class="ui selection dropdown">
<div class="ui selection dropdown" id="segmento">
<input type="hidden" name="segmento" />
<i class="dropdown icon"></i>
<div class="default text">Segmento</div>
@ -67,12 +67,14 @@
</div>
@endforeach
<span id="end_campos"></span>
<div class="row">
<div class="ten wide column">
<div class="field">
<label>Descripci&oacute;n</label>
<textarea rows="1" name="descripcion">{{$producto->descripcion ?? ''}}</textarea>
</div>
</div>
</div>
<div class="column">
<div class="field">
<label>Im&aacute;genes</label>
@ -80,25 +82,6 @@
</div>
<div id="imagenes" class="ui list"></div>
</div>
<?php
/*
<div class="column">
<div class="field">
<label>Video</label>
<input type="file" name="video" />
</div>
<div class="ui list">
@if (isset($producto->video))
<div class="item">
<i class="trash alternate outline icon video"></i>
<div class="content">
{{$producto->video}}
</div>
</div>
@endif
</div>
</div>
*/?>
</div>
<br />
<button class="ui button">GUARDAR</button>
@ -150,24 +133,13 @@
},
setup: () => {
producto.setMonths()
$('.selection.dropdown').dropdown()
$('.selection.dropdown').dropdown('set selected', '{{$producto->segmento}}')
$('.calendar').calendar({
type: 'month',
text: {
months: producto.months.long,
monthsShort: producto.months.short
},
formatInput: false,
onChange: function(a, b) {
if (typeof a == 'undefined') {
a = new Date()
}
$(this).find('input').val(('0' + (a.getMonth() + 1)).slice(-2) + '/' + a.getFullYear())
$('#segmento').dropdown({
onChange: () => {
producto.changeSegmento()
}
})
var entrega = new Date('{{str_pad(implode('-', array_reverse(explode('/', $producto->entrega))), 7, '20', STR_PAD_LEFT)}}-01T01:00')
$('.calendar').calendar('set date', entrega)
$('#segmento').dropdown('set selected', '{{$producto->segmento}}')
producto.linkCalendar()
$('.checkbox').checkbox()
@if ($producto->destacado())
$('.checkbox').checkbox('set checked')
@ -192,45 +164,51 @@
@foreach ($producto->imagenes() as $image)
producto.listImage('{{$image}}')
@endforeach
$("input[name='video']").change(function() {
var fData = new FormData()
fData.append('video', $("input[name='video']")[0].files[0])
var url = '{{$urls->admin}}/producto/{{$producto->id}}/video/set'
$.ajax({
url: url,
method: 'post',
data: fData,
contentType: false,
processData: false,
success: (data) => {
if (data.estado) {
window.location.reload()
},
linkCalendar: () => {
var div = $("input[name='entrega']").parent()
div.find('input').remove()
div.append(
$('<div></div>').attr('class', 'ui calendar').append(
$('<input />').attr('type', 'text').attr('name', 'entrega').attr('placeholder', 'Entrega')
)
)
$('.calendar').calendar({
type: 'month',
text: {
months: producto.months.long,
monthsShort: producto.months.short
},
formatInput: false,
onChange: function(a, b) {
if (typeof a == 'undefined') {
a = new Date()
}
$(this).find('input').val(('0' + (a.getMonth() + 1)).slice(-2) + '/' + a.getFullYear())
}
})
})
$('.trash.video').attr('cursor', 'pointer').click(() => {
var url = '{{$urls->admin}}/producto/{{$producto->id}}/video/delete'
$.post(url, {}, (data) => {
if (data.estado) {
window.location.reload()
}
})
})
var entrega = new Date('{{str_pad(implode('-', array_reverse(explode('/', $producto->entrega))), 7, '20', STR_PAD_LEFT)}}-01T01:00')
$('.calendar').calendar('set date', entrega)
},
changeSegmento: () => {
var segmento = $('#segmento').dropdown('get value')
if (segmento == '{{$producto->segmento}}') {
return
}
var url = '{{$urls->admin}}/productos/campos'
$.post(url, {segmento: segmento}, (data) => {
var div = $('#campos')
var end = $('#end_campos')
var current = div.next()
var next = current.next()
while (next != end) {
if (current[0] == end[0] || next[0] == end[0]) {
} else {
while (next[0] != end[0] || next.length == 0) {
current.remove()
current = next
next = next.next()
}
}
$.each(data.fields, (i, el) => {
var campo = $('<div></div>').attr('class', 'column').append(
$('<div></div>').attr('class', 'field').append(
@ -241,6 +219,7 @@
)
end.before(campo)
})
producto.linkCalendar()
})
}
}

View File

@ -58,70 +58,8 @@
</div>
</div>
<div id="campos"></div>
@foreach ($properties as $property)
<div class="column">
<div class="field">
<label>{{$property->label}}</label>
<input type="text" name="{{$property->name}}" />
</div>
</div>
@endforeach
<!-- <div class="column">
<div class="field">
<label>Bono Pie en UF</label>
<input type="text" name="bono" />
</div>
</div>
<div class="column">
<div class="field">
<label>Rentabilidad %</label>
<input type="text" name="rentabilidad" />
</div>
</div>
<div class="column">
<div class="field">
<label>Valor Cuota en UF</label>
<input type="text" name="cuota" />
</div>
</div>
<div class="column">
<div class="field">
<label>Entrega Estimada</label>
<div class="ui calendar">
<input type="text" name="entrega" placeholder="Entrega" />
</div>
</div>
</div>
<div class="column">
<div class="field">
<label>Estado</label>
<input type="text" name="estado" />
</div>
</div>
<div class="column">
<div class="field">
<label>Unidades</label>
<input type="text" name="unidades" />
</div>
</div>
<div class="column">
<div class="field">
<label>Modelos</label>
<input type="text" name="modelos" />
</div>
</div>
<div class="column">
<div class="fields">
<div class="field">
<label>Tamaño M&iacute;nimo</label>
<input type="text" name="tamaño_min" />
</div>
<div class="field">
<label>Tamaño M&aacute;ximo</label>
<input type="text" name="tamaño_max" />
</div>
</div>
</div> -->
<div id="end_campos"></div>
<div class="row">
<div class="ten wide column">
<div class="field">
<label>Descripci&oacute;n</label>
@ -129,6 +67,7 @@
</div>
</div>
</div>
</div>
<br />
<button class="ui button">AGREGAR</button>
</form>
@ -161,12 +100,16 @@
},
setup: () => {
producto.setMonths()
$('#segmento').dropdown()
$('#segmento').dropdown({
onChange: () => {
producto.changeSegmento()
}
})
$('.calendar').calendar({
type: 'month',
text: {
months: months.long,
monthsShort: months.short
months: producto.months.long,
monthsShort: producto.months.short
},
formatInput: false,
onChange: function(a) {
@ -180,6 +123,17 @@
var url = '{{$urls->admin}}/productos/campos'
$.post(url, {segmento: segmento}, (data) => {
var div = $('#campos')
var end = $('#end_campos')
var current = div.next()
var next = current.next()
if (current[0] == end[0] || next[0] == end[0]) {
} else {
while (next[0] != end[0] || next.length == 0) {
current.remove()
current = next
next = next.next()
}
}
$.each(data.fields, (i, el) => {
var campo = $('<div></div>').attr('class', 'column').append(
$('<div></div>').attr('class', 'field').append(
@ -188,6 +142,7 @@
$('<input />').attr('type', 'text').attr('name', el.name)
)
)
end.before(campo)
})
})
}

View File

@ -5,7 +5,6 @@ use ProVM\KI\Producto;
class Oficina extends Producto {
protected $properties = [
'arriendo',
'm2',
'baños',
'privados',
@ -25,7 +24,7 @@ class Oficina extends Producto {
$properties = [
[
'label' => 'Valor Arriendo',
'name' => 'arriendo',
'name' => 'valor',
'suffix' => ' UF'
],
[