Skip to content

Commit

Permalink
Rename update
Browse files Browse the repository at this point in the history
  • Loading branch information
pmusialmacopedia committed Jan 20, 2021
1 parent 073c61f commit 9fa39be
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 89 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "codehat/akeneo-translation",
"name": "piotrmus/akeneo-product-translation",
"type": "symfony-bundle",
"description": "Codehat translation extension for Akeneo",
"description": "Translation extension for Akeneo",
"version": "1.0.0",
"license": "OSL-3.0",
"authors": [
{
"name": "Codehat",
"name": "Piotr Musiał",
"homepage": "https://www.codehat.pl"
}
],
Expand All @@ -15,7 +16,7 @@
},
"autoload": {
"psr-4": {
"Codehat\\TranslationExtension\\": "src/"
"Piotrmus\\Translator\\": "src/"
}
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions src/CodehatTranslatorBundle.php

This file was deleted.

84 changes: 52 additions & 32 deletions src/Connector/Processor/MassEdit/TranslateAttributesProcessor.php
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
<?php


namespace Codehat\TranslationExtension\Connector\Processor\MassEdit;
namespace Piotrmus\Translator\Connector\Processor\MassEdit;


use Akeneo\Pim\Enrichment\Component\Product\Connector\Processor\MassEdit\AbstractProcessor;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface;
use Akeneo\Pim\Enrichment\Component\Product\Model\ValueInterface;
use Akeneo\Tool\Component\StorageUtils\Updater\PropertySetterInterface;
use Codehat\TranslationExtension\Translator\Language;
use Codehat\TranslationExtension\Translator\TranslatorInterface;
use Akeneo\Pim\Enrichment\Component\Product\Value\ScalarValue;
use Piotrmus\Translator\Translator\Language;
use Piotrmus\Translator\Translator\TranslatorInterface;

class TranslateAttributesProcessor extends AbstractProcessor
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var PropertySetterInterface
*/
private $propertySetter;

public function __construct(
TranslatorInterface $translator,
PropertySetterInterface $propertySetter
TranslatorInterface $translator
) {
$this->translator = $translator;
$this->propertySetter = $propertySetter;
}

/**
* @param ProductInterface|ProductModelInterface $item
* @return void
* @param ProductInterface|ProductModelInterface $product
* @return ProductInterface
*/
public function process($item): void
public function process($product): ProductInterface
{
$actions = $this->getConfiguredActions();
$action = $actions[0];
$this->translateAttributes($item, $action);
$product = $this->translateAttributes($product, $action);
return $product;
}

/**
* @param ProductInterface|ProductModelInterface $product
* @param array $action
*/
private function translateAttributes($product, array $action): void
private function translateAttributes($product, array $action): ProductInterface
{
$sourceScope = $action['sourceScope'];
$targetScope = $action['targetScope'];
$sourceLocale = new Language($action['sourceLocale']);
$targetLocale = new Language($action['targetLocale']);
$attributeCodes = $action['attributes'];
$sourceScope = $action['sourceChannel'];
$targetScope = $action['targetChannel'];
$sourceLocaleAkeneo = $action['sourceLocale'];
$targetLocaleAkeneo = $action['targetLocale'];
$sourceLocale = new Language(explode('_', $sourceLocaleAkeneo)[0]);
$targetLocale = new Language(explode('_', $targetLocaleAkeneo)[0]);
$attributeCodes = $action['translatedAttributes'];

foreach ($attributeCodes as $attributeCode) {
/** @var ValueInterface|null $attributeValue */
$attributeValue = $product->getValue($attributeCode, $sourceLocale, $sourceScope);
$attributeValue = $product->getValue($attributeCode, $sourceLocaleAkeneo, $sourceScope);

if ($attributeValue === null) {
continue;
Expand All @@ -69,16 +66,39 @@ private function translateAttributes($product, array $action): void
$sourceLocale,
$targetLocale
);

$this->propertySetter->setData(
$product,
$attributeCode,
$translatedValue,
[
'locale' => $targetLocale,
'scope' => $targetScope,
]
);

$this->replaceProductValue($product, $attributeCode, $targetLocaleAkeneo, $targetScope, $translatedValue);
}
return $product;
}

/**
* @param ProductInterface $product
* @param string $attributeCode
* @param $targetLocale
* @param $targetScope
* @param string $newValue
*/
private function replaceProductValue(
ProductInterface $product,
string $attributeCode,
$targetLocale,
$targetScope,
string $newValue
): void {
$targetAttributeValue = $product->getValue($attributeCode, $targetLocale, $targetScope);

if ($targetAttributeValue !== null) {
$product->removeValue($targetAttributeValue);
}

$product->addValue(
ScalarValue::scopableLocalizableValue(
$attributeCode,
$newValue,
$targetScope,
$targetLocale
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Codehat\TranslationExtension\DependencyInjection;
namespace Piotrmus\Translator\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class CodehatTranslationExtension extends Extension
class PiotrmusTranslatorExtension extends Extension
{
/**
* {@inheritdoc}
Expand Down
9 changes: 9 additions & 0 deletions src/PiotrmusTranslatorBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Piotrmus\Translator;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PiotrmusTranslatorBundle extends Bundle
{
}
13 changes: 13 additions & 0 deletions src/Resources/config/form_extensions/mass_edit/product.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extensions:
piotrmus-mass-product-edit-configure-add-comment:
module: piotrmus-translator/mass-edit/translate
parent: pim-mass-product-edit
position: 500
config:
title: pim_enrich.mass_edit.product.title
label: 'Translate Product'
labelCount: "{1}Add comment to <span class=\"AknFullPage-title--highlight\">1 product</span>|]1, Inf[Add comment to <span class=\"AknFullPage-title--highlight\">{{ itemsCount }} products</span>"
description: 'Add a comment for a set of products'
code: update_product_translations
jobInstanceCode: update_product_translations
icon: icon-compare
4 changes: 4 additions & 0 deletions src/Resources/config/requirejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config:
paths:
piotrmus-translator/mass-edit/translate: piotrmustranslator/js/mass-edit/translate
piotrmus-translator/template/mass-edit/translate: piotrmustranslator/templates/mass-edit/translate.html
44 changes: 29 additions & 15 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
services:
Codehat\TranslationExtension\Translator\GoogleTranslator:
Piotrmus\Translator\Translator\GoogleTranslator:
arguments:
- 'teste'
- '%env(GOOGLE_API_KEY)%'

Codehat\TranslationExtension\Connector\Processor\MassEdit\TranslateAttributesProcessor:
Piotrmus\Translator\Connector\Processor\MassEdit\TranslateAttributesProcessor:
arguments:
- '@Codehat\TranslationExtension\Translator\GoogleTranslator'
- '@pim_catalog.updater.property_setter'
- '@Piotrmus\Translator\Translator\GoogleTranslator'

codehat.job.translate:
class: '%pim_connector.job.simple_job.class%'
piotrmus.job.update_product_translations:
class: Akeneo\Tool\Component\Batch\Job\Job
arguments:
- 'translate_attributes'
- 'update_product_translations'
- '@event_dispatcher'
- '@akeneo_batch.job_repository'
- [ '@codehat.job.translate_attributes.step' ]
- [ '@piotrmus.step.update_product_translations.mass_edit' ]
tags:
- { name: akeneo_batch.job, connector: 'Codehat Translator', type: '%pim_enrich.job.mass_edit_type%' } ]
- { name: akeneo_batch.job, connector: '%pim_enrich.connector_name.mass_edit%', type: '%pim_enrich.job.mass_edit_type%' }

codehat.job.translate_attributes.step:
class: '%pim_connector.step.item_step.class%'
piotrmus.job.default_values_provider.translate_product:
class: Akeneo\Pim\Enrichment\Component\Product\Connector\Job\JobParameters\DefaultValueProvider\ProductMassEdit
arguments:
- [ 'update_product_translations' ]
tags:
- { name: akeneo_batch.job.job_parameters.default_values_provider }

piotrmus.job.constraint_collection_provider.translate_product:
class: Akeneo\Pim\Enrichment\Component\Product\Connector\Job\JobParameters\ConstraintCollectionProvider\ProductMassEdit
arguments:
- [ 'update_product_translations' ]
tags:
- { name: akeneo_batch.job.job_parameters.constraint_collection_provider }

piotrmus.step.update_product_translations.mass_edit:
class: Akeneo\Tool\Component\Batch\Step\ItemStep
arguments:
- 'perform'
- '@event_dispatcher'
- '@akeneo_batch.job_repository'
- '@pim_enrich.reader.database.product_and_product_model'
- '@Codehat\TranslationExtension\Connector\Processor\MassEdit\TranslateAttributesProcessor'
- '@pim_connector.writer.database.product'
- '@pim_enrich.reader.database.products_and_variant_products_of_product_models'
- '@Piotrmus\Translator\Connector\Processor\MassEdit\TranslateAttributesProcessor'
- '@pim_connector.writer.database.product'
- 10
101 changes: 101 additions & 0 deletions src/Resources/public/js/mass-edit/translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';

define([
'jquery',
'underscore',
'pim/mass-edit-form/product/operation',
'piotrmus-translator/template/mass-edit/translate',
'pim/fetcher-registry',
'pim/initselect2',
'pim/user-context',
],
function (
$,
_,
BaseOperation,
template,
FetcherRegistry,
initSelect2,
UserContext
) {
return BaseOperation.extend({
template: _.template(template),
events: {
'change select.form-field': 'setValue',
'change #translated-attributes': 'setAttributes',
},

render: function () {
let locales = FetcherRegistry.getFetcher('locale').fetchActivated();
let scopes = FetcherRegistry.getFetcher('channel').fetchAll();
let attributes = FetcherRegistry.getFetcher('attribute').search({
options: {
locale: UserContext.get('catalogLocale'),
limit: 1000
}
});
Promise.all([locales, scopes, attributes]).then(function (values) {
let locales = values[0];
let channels = values[1];
let attributes = values[2];

console.log(attributes.length);

attributes = attributes.filter(function (attribute) {
console.log(attribute);
return attribute.localizable;
})

let data = this.getFormData();
if (data.actions.length === 0) {
data.actions[0] = {
"sourceChannel": channels[0].code,
"sourceLocale": locales[0].code,
"targetChannel": channels[0].code,
"targetLocale": locales[0].code,
"translatedAttributes": [],
};
this.setData(data);
}

let model = this.getFormData();

this.$el.html(this.template({
locales: locales,
channels: channels,
attributes: attributes,
sourceChannel: model.actions[0].sourceChannel,
sourceLocale: model.actions[0].sourceLocale,
targetChannel: model.actions[0].targetChannel,
targetLocale: model.actions[0].targetLocale,
}));

$('.select2').each(function (key, select) {
$(select).select2();
if ($(select).attr('readonly')) {
$(select).select2('readonly', true);
}
})
}.bind(this));
return this;
},

setValue: function (event) {
this.setFormValue(event.target.name, event.target.value);
},

setAttributes: function (event) {
let data = this.getFormData();
data.actions[0].translatedAttributes = $(event.target).val();
this.setData(data);
},

setFormValue: function (key, value) {
let data = this.getFormData();
data.actions[0][key] = value;
console.log(data.actions[0]);
this.setData(data);
}
});
}
);
Loading

0 comments on commit 9fa39be

Please sign in to comment.