From 5822652db2cd176a34e74f47a22e7aac56a9d4f7 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 13 Jun 2019 18:43:41 +0200 Subject: [PATCH] wip #2810 @5 --- app/Models/Product.php | 135 +++++++++++++++++++---------------- app/Models/Producttype.php | 22 +++--- app/Models/Specification.php | 13 +--- update | 4 +- 4 files changed, 84 insertions(+), 90 deletions(-) diff --git a/app/Models/Product.php b/app/Models/Product.php index 850cae8..034711b 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -34,71 +34,11 @@ class Product extends CubistMagicModel $this->addField(['name' => 'product_type', 'label' => 'Type de produit', 'type' => 'SelectFromModel', - 'model' => 'App\Models\ProductType', + 'optionsmodel' => 'App\Models\ProductType', 'column' => true, 'tab' => 'Spécifications']); - $specifications = Specification::all(); - $types = ProductType::all(); - foreach ($specifications as $spec) { - $in = []; - foreach ($types as $type) { - foreach ($type->specifications as $rel_specification) { - if ($spec->id == $rel_specification->id) { - $in[] = $type->id; - break; - } - } - foreach ($type->filters as $rel_specification) { - if ($spec->id == $rel_specification->id) { - $in[] = $type->id; - break; - } - } - $in = array_unique($in); - } - if (!count($in)) { - continue; - } - $params = ['tab' => 'Spécifications', - 'name' => 's_' . Str::snake($spec->name), - 'label' => $spec->name, - 'fake' => true, - 'store_in' => 'specifications', - 'when' => ['product_type' => $in], - ]; - - if ($spec->prefix) { - $params['prefix'] = $spec->prefix; - } - if ($spec->unit) { - $params['suffix'] = $spec->unit; - } - - if ($spec->type == 'numeric') { - $params['type'] = 'Number'; - } else if ($spec->type == 'range') { - $params['type'] = 'Range'; - } else if ($spec->type == 'text') { - $params['type'] = 'Text'; - } else if ($spec->type == 'numeric_list') { - $params['type'] = 'Table'; - $params['entity_singular'] = 'valeur'; - $params['columns'] = ['value' => 'Value']; - } else { - $params['type'] = 'SelectFromArray'; - $options = []; - if (is_string($spec->options)) { - $decoded = json_decode($spec->options); - } - foreach ($decoded as $option) { - $options[] = $option->name; - } - $params['options'] = $options; - } - $this->addField($params); - } - + $this->addSpecifications(); $this->addField(['name' => 'slug', 'type' => 'Slug', @@ -172,4 +112,75 @@ class Product extends CubistMagicModel 'tab' => 'Textes']); } + + public function addSpecifications() + { + + $specifications = Specification::all(); + $types = ProductType::all(); + foreach ($specifications as $spec) { + $in = []; + foreach ($types as $type) { + $lists = [$type->specifications, $type->filters]; + foreach ($lists as $list) { + if (!is_array($list)) { + continue; + } + foreach ($list as $rel_specification) { + if ($spec->id == $rel_specification) { + if (!in_array($type->getIdValue(), $in)) { + $in[] = $type->getIdValue(); + } + break; + } + } + } + $in = array_unique($in); + } + if (!count($in)) { + continue; + } + $params = ['tab' => 'Spécifications', + 'name' => 's_' . Str::snake($spec->name), + 'label' => $spec->name, + 'fake' => true, + 'store_in' => 'specifications', + 'when' => ['product_type' => $in], + ]; + + if ($spec->prefix) { + $params['prefix'] = $spec->prefix; + } + if ($spec->unit) { + $params['suffix'] = $spec->unit; + } + + if ($spec->type == 'numeric') { + $params['type'] = 'Number'; + } else if ($spec->type == 'range') { + $params['type'] = 'Range'; + } else if ($spec->type == 'text') { + $params['type'] = 'Text'; + } else if ($spec->type == 'numeric_list') { + $params['type'] = 'Table'; + $params['entity_singular'] = 'valeur'; + $params['columns'] = ['value' => 'Value']; + } else { + $params['type'] = 'SelectFromArray'; + $options = []; + if (is_string($spec->options)) { + $decoded = json_decode($spec->options); + } + if (is_array($decoded)) { + foreach ($decoded as $option) { + $options[] = $option->name; + } + + } + $params['options'] = $options; + } + $this->addField($params); + } + + } } diff --git a/app/Models/Producttype.php b/app/Models/Producttype.php index 5834b9f..4838b42 100644 --- a/app/Models/Producttype.php +++ b/app/Models/Producttype.php @@ -38,23 +38,17 @@ class ProductType extends CubistMagicModel $this->addField(['name' => 'specifications', 'label' => 'Spécifications', - 'type' => 'SelectFromModelMultiple', - 'model' => "App\Models\Specification", + 'type' => 'SelectFromModel', + 'optionsmodel' => "App\Models\Specification", + 'order' => true, + 'multiple' => true, ]); $this->addField(['name' => 'filters', 'label' => 'Spécifications utilisées comme filtre', - 'type' => 'SelectFromModelMultiple', - 'model' => 'App\Models\Specification']); - } - - public function specifications() - { - return $this->relationship('specifications'); - } - - public function filters() - { - return $this->relationship('filters'); + 'type' => 'SelectFromModel', + 'optionsmodel' => 'App\Models\Specification', + 'order' => true, + 'multiple' => true]); } } diff --git a/app/Models/Specification.php b/app/Models/Specification.php index 95f3b35..d1317f4 100644 --- a/app/Models/Specification.php +++ b/app/Models/Specification.php @@ -36,7 +36,7 @@ class Specification extends CubistMagicModel $this->addField(['name' => 'options', 'label' => 'Options', 'type' => 'Table', - 'entity_singular' => 'option', // used on the "Add X" button + 'entity_singular' => 'option', 'columns' => [ 'name' => 'Name', ], @@ -65,15 +65,4 @@ class Specification extends CubistMagicModel 'column'=>true, ]); } - - public function product_type_filters() - { - return $this->belongsToMany('App\Models\ProductType', 'catalog_product_types_filters_rel_btm'); - } - - public function product_type_specifications() - { - return $this->belongsToMany('App\Models\ProductType', 'catalog_product_types_specifications_rel_btm'); - } - } diff --git a/update b/update index 2f35509..e20cbd6 100644 --- a/update +++ b/update @@ -3,7 +3,7 @@ echo `composer update -v --no-progress --profile --prefer-dist`; #`php artisan vendor:publish --provider="CubistBackpackServiceProvider"`; // Database migrations -`php artisan vendor:publish --tag=migrations`; -`php artisan migrate`; +//`php artisan vendor:publish --tag=migrations`; +//`php artisan migrate`; #`composer dump-autoload`; `php artisan cubist:magic:generate`; -- 2.39.5