Commit 96c98ab9 authored by Aleksey.Alekseev's avatar Aleksey.Alekseev
Browse files

Related Products Import v1.0

parent aa2fc6f5
......@@ -25,10 +25,18 @@ use Drupal\commerce_product\Entity\ProductAttributeValue;
*/
class SynhelperContentImport {
/**
* Global directory variable.
*
* @var string
*/
protected $directory;
/**
* Import Full Content.
*/
public function import(string $directory) {
$this->directory = $directory;
$this->importContacts($directory);
$this->importTaxonomy($directory);
$this->importNode($directory);
......@@ -42,7 +50,8 @@ class SynhelperContentImport {
* Import Contacts.
*/
public function importContacts(string $directory) {
$sourceContacts = new FileStorage($directory . '/contacts');
$this->directory = $directory;
$sourceContacts = new FileStorage($this->directory . '/contacts');
foreach ($sourceContacts->listAll() as $file) {
$data = $sourceContacts->read($file);
$path = \Drupal::service('path_alias.manager')->getPathByAlias('/contacts');
......@@ -66,7 +75,8 @@ class SynhelperContentImport {
* Import Taxonomy.
*/
public function importTaxonomy(string $directory) {
$sourceTaxonomy = new FileStorage($directory . '/taxonomy');
$this->directory = $directory;
$sourceTaxonomy = new FileStorage($this->directory . '/taxonomy');
foreach ($sourceTaxonomy->listAll() as $file) {
$data = $sourceTaxonomy->read($file);
if (!$this->checkVocabularyExistence($data['vid'])) {
......@@ -86,7 +96,8 @@ class SynhelperContentImport {
* add a new 'case' to existing ones.
*/
public function importNode(string $directory) {
$sourceNode = new FileStorage($directory . '/nodes');
$this->directory = $directory;
$sourceNode = new FileStorage($this->directory . '/nodes');
foreach ($sourceNode->listAll() as $file) {
$data = $sourceNode->read($file);
$storage = \Drupal::entityTypeManager()->getStorage('node');
......@@ -140,7 +151,8 @@ class SynhelperContentImport {
* Import Menu.
*/
public function importMenu(string $directory) {
$sourceMenu = new FileStorage($directory . '/menu');
$this->directory = $directory;
$sourceMenu = new FileStorage($this->directory . '/menu');
foreach ($sourceMenu->listAll() as $file) {
$data = $sourceMenu->read($file);
if ($menu = \Drupal::entityTypeManager()->getStorage('menu')->load($data['menu']['id'])) {
......@@ -164,13 +176,11 @@ class SynhelperContentImport {
* Import Products.
*/
public function importProducts(string $directory) {
$sourceProducts = new FileStorage($directory . '/products');
$this->directory = $directory;
$sourceProducts = new FileStorage($this->directory . '/products');
foreach ($sourceProducts->listAll() as $file) {
$product = $sourceProducts->read($file);
if (!empty($product['variations'])) {
$variations = $this->importVariations($product);
}
$this->createProduct($product, $variations);
$product_array = $sourceProducts->read($file);
$this->updateCreateProduct($product_array);
}
}
......@@ -178,7 +188,8 @@ class SynhelperContentImport {
* Import Blocks.
*/
public function importBlocks(string $directory) {
$sourceBlock = new FileStorage($directory . '/block');
$this->directory = $directory;
$sourceBlock = new FileStorage($this->directory . '/block');
foreach ($sourceBlock->listAll() as $file) {
$data = $sourceBlock->read($file);
$storage = \Drupal::entityTypeManager()->getStorage('block_content');
......@@ -212,7 +223,8 @@ class SynhelperContentImport {
* Set Synlanding form background.
*/
public function setSynLandingConfig(string $directory) {
$sourceSynLanding = new FileStorage($directory . '/synlanding');
$this->directory = $directory;
$sourceSynLanding = new FileStorage($this->directory . '/synlanding');
foreach ($sourceSynLanding->listAll() as $file) {
$data = $sourceSynLanding->read($file);
$image = $this->importImages($data['form_bg_path']);
......@@ -237,6 +249,132 @@ class SynhelperContentImport {
*
*/
/**
* Update or Create Products.
*/
private function updateCreateProduct(array $product_array) {
$created_product = $this->getProductFromBase($product_array['field_unique_name']);
if (!($created_product)) {
$this->createProduct($product_array, 'full_create');
}
elseif (!empty($product_array['related_products'])) {
$created_product_id = $created_product->id();
$this->setRelated($created_product_id, $product_array['related_products']);
}
}
/**
* Create product.
*/
private function createProduct(array $product_array, string $statement = 'full_create') {
if ($statement == 'full_create') {
$created_product_id = $this->procedureOfProductCreation($product_array)->id();
if (!empty($product_array['related_products'])) {
$this->setRelated($created_product_id, $product_array['related_products']);
}
}
elseif ($statement == 'only_as_related') {
$created_product = $this->getProductFromBase($product_array['field_unique_name']);
if ($created_product) {
return $created_product->id();
}
$created_product_id = $this->procedureOfProductCreation($product_array)->id();
}
return $created_product_id;
}
/**
* Procedure of creation.
*/
private function procedureOfProductCreation(array $product_array) {
if (!empty($product_array['variations'])) {
$variations = $this->importVariations($product_array);
}
$storage = \Drupal::entityTypeManager()->getStorage('commerce_product');
foreach ($product_array as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$n[$key] = $this->importImages($value);
break;
case 'field_attach':
$n[$key] = $this->importAttach($value);
break;
case 'field_catalog':
$n[$key] = $this->importTaxonomyField($value);
break;
case 'field_tx_options':
foreach ($value as $termName) {
$terms[] = $this->importTaxonomyField($termName);
}
$n[$key] = $terms;
break;
case 'field_paragraph':
$n[$key] = $this->importParagraph($value);
break;
case 'variations':
$n[$key] = $variations;
break;
case 'related_products':
break;
default:
$n[$key] = $value;
break;
}
}
$n['uid'] = 1;
$product = $storage->create($n);
$product->save();
return $product;
}
/**
* Search for created product.
*/
private function getProductFromBase(string $unique_name) {
$products = \Drupal::entityTypeManager()->getStorage('commerce_product')->loadByProperties(['field_unique_name' => $unique_name]);
$product = array_shift($products);
return $product ?? NULL;
}
/**
* Search for products_array in $this->directory/products.
*/
private function getProductFromFile(string $unique_name) {
$sourceProducts = new FileStorage($this->directory . '/products');
foreach ($sourceProducts->listAll() as $file) {
$product_array = $sourceProducts->read($file);
if ($product_array['field_unique_name'] == $unique_name) {
break;
}
}
return $product_array;
}
/**
* Create Related Products.
*/
private function setRelated(string $created_product_id, array $related_products) {
$storage = \Drupal::entityTypeManager()->getStorage('commerce_product');
$products_ids = [];
foreach ($related_products as $unique_name) {
$product_array = $this->getProductFromFile($unique_name);
if (!empty($product_array)) {
$products_ids[] = $this->createProduct($product_array, 'only_as_related');
}
}
$created_product = $storage->load($created_product_id);
$created_product->set("field_rf_product", $products_ids);
$created_product->save();
}
/**
* Imports product variations.
*/
......@@ -269,7 +407,7 @@ class SynhelperContentImport {
/**
* Import Attributes.
*/
public function importAttribute(array $fields, string $name) {
private function importAttribute(array $fields, string $name) {
$ids = $this->issetAttribute($fields, $name);
if (empty($ids)) {
$a = [
......@@ -311,7 +449,7 @@ class SynhelperContentImport {
/**
* Check already created attributes.
*/
public function issetAttribute(array $fields, string $name) {
private function issetAttribute(array $fields, string $name) {
$query = \Drupal::entityQuery('commerce_product_attribute_value');
if ($name == 'color') {
$query->condition('field_hex', $fields['field_hex']);
......@@ -322,50 +460,6 @@ class SynhelperContentImport {
return $query->execute();
}
/**
* Creates product.
*/
private function createProduct(array $data, array $variations = NULL) {
foreach ($data as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$n[$key] = $this->importImages($value);
break;
case 'field_attach':
$n[$key] = $this->importAttach($value);
break;
case 'field_catalog':
$n[$key] = $this->importTaxonomyField($value);
break;
case 'field_tx_options':
foreach ($value as $termName) {
$terms[] = $this->importTaxonomyField($termName);
}
$n[$key] = $terms;
break;
case 'field_paragraph':
$n[$key] = $this->importParagraph($value);
break;
case 'variations':
$n[$key] = $variations;
break;
default:
$n[$key] = $value;
break;
}
}
$n['uid'] = 1;
$product = Product::create($n);
$product->save();
}
/**
* Checks vocabulary for existence.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment