From 4923c6c67d3e79e6df4e2e5726b8728c1e817971 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 3 Jun 2020 23:54:15 -0400 Subject: [PATCH] FIX: varias observaciones --- common/Controller/Web/Admin/Productos.php | 204 +----------------- resources/routes/web/admin/productos.php | 1 + resources/views/admin/producto.blade.php | 113 ++++------ resources/views/admin/productos/add.blade.php | 93 +++----- src/Productos/Oficina.php | 3 +- 5 files changed, 73 insertions(+), 341 deletions(-) diff --git a/common/Controller/Web/Admin/Productos.php b/common/Controller/Web/Admin/Productos.php index dd45cb3..74f5cdb 100644 --- a/common/Controller/Web/Admin/Productos.php +++ b/common/Controller/Web/Admin/Productos.php @@ -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(); diff --git a/resources/routes/web/admin/productos.php b/resources/routes/web/admin/productos.php index 3f82273..e129afa 100644 --- a/resources/routes/web/admin/productos.php +++ b/resources/routes/web/admin/productos.php @@ -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']); diff --git a/resources/views/admin/producto.blade.php b/resources/views/admin/producto.blade.php index 4fe89f3..0fdf423 100644 --- a/resources/views/admin/producto.blade.php +++ b/resources/views/admin/producto.blade.php @@ -31,7 +31,7 @@
-
@@ -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,44 +164,50 @@ @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( + $('
').attr('class', 'ui calendar').append( + $('').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() } - }) - }) - $('.trash.video').attr('cursor', 'pointer').click(() => { - var url = '{{$urls->admin}}/producto/{{$producto->id}}/video/delete' - $.post(url, {}, (data) => { - if (data.estado) { - window.location.reload() - } - }) + $(this).find('input').val(('0' + (a.getMonth() + 1)).slice(-2) + '/' + a.getFullYear()) + } }) + 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) { - current.remove() - current = next - next = next.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 = $('
').attr('class', 'column').append( @@ -241,6 +219,7 @@ ) end.before(campo) }) + producto.linkCalendar() }) } } diff --git a/resources/views/admin/productos/add.blade.php b/resources/views/admin/productos/add.blade.php index e973650..5b2edf2 100644 --- a/resources/views/admin/productos/add.blade.php +++ b/resources/views/admin/productos/add.blade.php @@ -58,75 +58,14 @@
- @foreach ($properties as $property) -
+
+
+
- - + +
- @endforeach - -
-
- - -

@@ -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 = $('
').attr('class', 'column').append( $('
').attr('class', 'field').append( @@ -188,6 +142,7 @@ $('').attr('type', 'text').attr('name', el.name) ) ) + end.before(campo) }) }) } diff --git a/src/Productos/Oficina.php b/src/Productos/Oficina.php index a5ca965..d0d0f6f 100644 --- a/src/Productos/Oficina.php +++ b/src/Productos/Oficina.php @@ -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' ], [