From 080ed4553e4533df7978488560b50d051a3488a8 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 3 Jun 2020 21:59:11 -0400 Subject: [PATCH] Se agregan los campos por segmento --- common/Controller/Web/Admin/Productos.php | 32 ++-- common/Controller/Web/Productos.php | 3 +- provm/common/Factory/Model.php | 17 +- provm/common/Implementation/Model.php | 7 +- resources/views/admin/producto.blade.php | 85 ++++------ resources/views/admin/productos/add.blade.php | 47 ++++-- resources/views/productos/ficha.blade.php | 12 +- .../views/productos/producto/datos.blade.php | 61 +------- .../views/productos/producto/ficha.blade.php | 10 ++ src/Producto.php | 71 ++++----- src/Productos/Bodega.php | 61 ++++++++ src/Productos/Oficina.php | 67 ++++++++ src/Productos/Retail.php | 148 ++++++++++++++++++ src/Productos/Terreno.php | 39 +++++ 14 files changed, 486 insertions(+), 174 deletions(-) create mode 100644 resources/views/productos/producto/ficha.blade.php create mode 100644 src/Productos/Bodega.php create mode 100644 src/Productos/Oficina.php create mode 100644 src/Productos/Retail.php create mode 100644 src/Productos/Terreno.php diff --git a/common/Controller/Web/Admin/Productos.php b/common/Controller/Web/Admin/Productos.php index 11e4c0d..dd45cb3 100644 --- a/common/Controller/Web/Admin/Productos.php +++ b/common/Controller/Web/Admin/Productos.php @@ -61,7 +61,8 @@ class Productos { 'segmentos.json' ]); $segmentos = json_decode(trim(file_get_contents($filename))); - return $view->render($response, 'admin.producto', compact('producto', 'segmentos')); + $properties = $producto->getProperties(); + return $view->render($response, 'admin.producto', compact('producto', 'segmentos', 'properties')); } public function do_edit(Request $request, Response $response, Container $container, ModelFactory $factory, $producto): Response { $post = $request->getParsedBody(); @@ -92,15 +93,6 @@ class Productos { $producto->tamaño = $tamaño; $changed = true; }*/ - if (isset($post['destacado']) and $post['destacado'] == 'on') { - $producto->destacado(true); - } - $post['tamaños'] = implode(' ', [ - $post['tamaño_min'], - '-', - $post['tamaño_max'], - 'm²' - ]); $f = Carbon::today(); $post['publicacion'] = $producto->publicacion ?? implode(' ', [ $f->day, @@ -346,4 +338,24 @@ class Productos { ]); return $response; } + public function fields(Request $request, Response $response): Response { + $post = $request->getParsedBody(); + $class = implode("\\", [ + 'ProVM', + 'KI', + 'Productos', + $post['segmento'] + ]); + $obj = new $class; + $properties = $obj->getProperties(); + + $output = [ + 'information' => $post, + 'fields' => $properties + ]; + $response->getBody()->write(json_encode($output, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)); + return $response + ->withHeader('Content-Type', 'application/json') + ->withStatus(201); + } } diff --git a/common/Controller/Web/Productos.php b/common/Controller/Web/Productos.php index 00021af..0669da6 100644 --- a/common/Controller/Web/Productos.php +++ b/common/Controller/Web/Productos.php @@ -64,6 +64,7 @@ class Productos { } public function ficha(Request $request, Response $response, View $view, ModelFactory $factory, $producto): Response { $producto = $factory->find(Producto::class)->where([['id', $producto]])->one(); - return $view->render($response, 'productos.ficha', compact('producto')); + $properties = $producto->getFicha(); + return $view->render($response, 'productos.ficha', compact('producto', 'properties')); } } diff --git a/provm/common/Factory/Model.php b/provm/common/Factory/Model.php index 95d8118..30a194a 100644 --- a/provm/common/Factory/Model.php +++ b/provm/common/Factory/Model.php @@ -114,7 +114,15 @@ class Model { return $data; } protected function fillObject($data): BaseModel { - $obj = new $this->class; + $class = $this->class; + if (strpos($class, 'Producto') !== false) { + $segmento = str_replace(' ', '', $data->segmento); + $class = implode("\\", [ + $class . 's', + rtrim($segmento, 's') + ]); + } + $obj = new $class; $obj->map($data); $obj->setFactory($this); $obj->setImageFolder($this->image_folder); @@ -139,6 +147,13 @@ class Model { } public function create(string $class, $data) { + if (strpos($class, 'Producto') !== false) { + $segmento = str_replace(' ', '', $data->segmento); + $class = implode("\\", [ + $class . 's', + rtrim($segmento, 's') + ]); + } $obj = new $class; $obj->map($data); $obj->setFactory($this); diff --git a/provm/common/Implementation/Model.php b/provm/common/Implementation/Model.php index c9a1d98..56776ca 100644 --- a/provm/common/Implementation/Model.php +++ b/provm/common/Implementation/Model.php @@ -47,9 +47,14 @@ abstract class Model implements \JsonSerializable { protected function getFilename(): string { $data = explode("\\", get_called_class()); $folder = $this->factory->getFolder(); + $class = array_pop($data); + $productos = ['Bodega', 'Oficina', 'Retail', 'Terreno']; + if (array_search($class, $productos) !== false) { + $class = 'Producto'; + } return implode(DIRECTORY_SEPARATOR, [ $folder, - str_replace(' ', '_', mb_strtolower(array_pop($data))) . 's.json' + str_replace(' ', '_', mb_strtolower($class)) . 's.json' ]); } } diff --git a/resources/views/admin/producto.blade.php b/resources/views/admin/producto.blade.php index 3550f43..4fe89f3 100644 --- a/resources/views/admin/producto.blade.php +++ b/resources/views/admin/producto.blade.php @@ -49,32 +49,6 @@ -
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- -
- -
-
-
@@ -83,36 +57,16 @@
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
+ + @foreach ($properties as $property) +
- - -
-
- - + +
-
+ @endforeach +
@@ -263,6 +217,31 @@ } }) }) + }, + changeSegmento: () => { + var segmento = $('#segmento').dropdown('get value') + 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() + } + $.each(data.fields, (i, el) => { + var campo = $('
').attr('class', 'column').append( + $('
').attr('class', 'field').append( + $('').html(el.label) + ).append( + $('').attr('type', 'text').attr('name', el.name) + ) + ) + end.before(campo) + }) + }) } } $(document).ready(() => { diff --git a/resources/views/admin/productos/add.blade.php b/resources/views/admin/productos/add.blade.php index 324f355..e973650 100644 --- a/resources/views/admin/productos/add.blade.php +++ b/resources/views/admin/productos/add.blade.php @@ -31,7 +31,7 @@
-
+
+ +
+ +
+
+
+
+ @foreach ($properties as $property) +
+
+ + +
+
+ @endforeach +
@@ -152,7 +161,7 @@ }, setup: () => { producto.setMonths() - $('.selection.dropdown').dropdown() + $('#segmento').dropdown() $('.calendar').calendar({ type: 'month', text: { @@ -165,6 +174,22 @@ } }) $('.checkbox').checkbox() + }, + changeSegmento: () => { + var segmento = $('#segmento').dropdown('get value') + var url = '{{$urls->admin}}/productos/campos' + $.post(url, {segmento: segmento}, (data) => { + var div = $('#campos') + $.each(data.fields, (i, el) => { + var campo = $('
').attr('class', 'column').append( + $('
').attr('class', 'field').append( + $('').html(el.label) + ).append( + $('').attr('type', 'text').attr('name', el.name) + ) + ) + }) + }) } } $(document).ready(() => { diff --git a/resources/views/productos/ficha.blade.php b/resources/views/productos/ficha.blade.php index 4734eca..6352004 100644 --- a/resources/views/productos/ficha.blade.php +++ b/resources/views/productos/ficha.blade.php @@ -17,7 +17,15 @@
-
+ @foreach ($properties as $property) +
+ + {{$producto->{$property->name} }}{{$property->suffix}} + + {{$property->label}} +
+ @endforeach +