Skip to content

Commit

Permalink
Merge pull request #487 from kevinkoessl/42-google-translate
Browse files Browse the repository at this point in the history
#42 Google Translations for user generated content.
  • Loading branch information
Der-Alex authored Dec 5, 2019
2 parents 022cad7 + cb267d8 commit 344a536
Show file tree
Hide file tree
Showing 14 changed files with 615 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use Oforge\Engine\Modules\Core\Annotation\Endpoint\EndpointClass;
use Oforge\Engine\Modules\Core\Exceptions\ServiceNotFoundException;
use Oforge\Engine\Modules\I18n\Helper\I18N;
use Oforge\Engine\Modules\I18n\Models\Language;
use Oforge\Engine\Modules\I18n\Services\LanguageService;
use Oforge\Engine\Modules\Mailer\Services\MailService;
use Oforge\Engine\Modules\TemplateEngine\Extensions\Services\UrlService;
use ReflectionException;
Expand Down Expand Up @@ -244,12 +246,12 @@ public function processStepsAction(Request $request, Response $response, $args)
$mailService = Oforge()->Services()->get('mail');

if ($request->isPost()) {
Oforge()->Logger()->get('create')->info("process data ", $_POST);
Oforge()->Logger()->get('create')->info('process data ', $_POST);

$data = $formsService->processPostData($typeId);
try {
$processData = $formsService->parsePageData($data);
Oforge()->Logger()->get('create')->info("processed data ", $data);
Oforge()->Logger()->get('create')->info('processed data ', $data);

$insertionId = $createService->create($typeId, $user, $processData);

Expand Down Expand Up @@ -374,7 +376,7 @@ public function listingAction(Request $request, Response $response, $args)

$typeAttributes = $service->getInsertionTypeAttributeTree($type->getId());
$result['attributes'] = $typeAttributes;
$result["all_attributes"] = $service->getInsertionTypeAttributeMap();
$result['all_attributes'] = $service->getInsertionTypeAttributeMap();
$result['keys'] = [];
$result['typeId'] = $args['type'];
$result['type'] = $type->toArray(0);
Expand Down Expand Up @@ -550,7 +552,7 @@ public function detailAction(Request $request, Response $response, $args)
}
}

Oforge()->View()->assign(["insertion" => $insertion->toArray(3, ['user' => ['*', '!id']])]);
Oforge()->View()->assign(['insertion' => $insertion->toArray(3, ['user' => ['*', '!id']])]);

/**
* @var $service InsertionProfileService
Expand All @@ -559,11 +561,11 @@ public function detailAction(Request $request, Response $response, $args)

$profile = $service->get($insertion->getUser()->getId());
if (isset($profile)) {
Oforge()->View()->assign(["profile" => $profile->toArray()]);
Oforge()->View()->assign(['rofile' => $profile->toArray()]);
}

/** @var $insertionTypeService InsertionTypeService */
$insertionTypeService = Oforge()->Services()->get("insertion.type");
$insertionTypeService = Oforge()->Services()->get('insertion.type');

$typeAttributes = $insertionTypeService->getInsertionTypeAttributeTree($insertion->getInsertionType()->getId());
$insertionValues = [];
Expand All @@ -585,22 +587,32 @@ public function detailAction(Request $request, Response $response, $args)
if ($attribute['top'] == 'true') {
if (isset($insertionValues[$attribute['attributeKey']['id']])) {
$topValues[] = [
"name" => $attribute['attributeKey']['name'],
"type" => $attribute['attributeKey']['type'],
"filterType" => $attribute['attributeKey']['filterType'],
"attributeKey" => $attribute['attributeKey']['id'],
"value" => $insertionValues[$attribute['attributeKey']['id']],
'name' => $attribute['attributeKey']['name'],
'type' => $attribute['attributeKey']['type'],
'filterType' => $attribute['attributeKey']['filterType'],
'attributeKey' => $attribute['attributeKey']['id'],
'value' => $insertionValues[$attribute['attributeKey']['id']],
];
}
}
}
}

/** @var LanguageService $languageService */
$languageService = Oforge()->Services()->get('i18n.language');

/** @var Language[] $availableLanguages */
$availableLanguages = $languageService->list(['active' => true]);
foreach ($availableLanguages as &$language) {
$language = $language->getIso();
}
Oforge()->View()->assign([
"top_values" => $topValues,
"attributes" => $typeAttributes,
"all_attributes" => $insertionTypeService->getInsertionTypeAttributeMap(),
"insertion_values" => $insertionValues,
'top_values' => $topValues,
'attributes' => $typeAttributes,
'all_attributes' => $insertionTypeService->getInsertionTypeAttributeMap(),
'insertion_values' => $insertionValues,
'animations' => Oforge()->View()->Flash()->getData('animations'),
'languages' => $availableLanguages
]);
Oforge()->View()->Flash()->clearData('animations');

Expand Down Expand Up @@ -791,13 +803,13 @@ public function contactAction(Request $request, Response $response, $args)
*/
foreach ($insertion->getInsertionType()->getAttributes() as $attribute) {
if ($attribute->isTop()) {
foreach ($data["values"] as $value) {
if ($value["attributeKey"] == $attribute->getAttributeKey()->getId()) {
$data["topvalues"][] = [
"name" => $attribute->getAttributeKey()->getName(),
"type" => $attribute->getAttributeKey()->getType(),
"attributeKey" => $attribute->getAttributeKey()->getId(),
"value" => $value["value"],
foreach ($data['values'] as $value) {
if ($value['attributeKey'] == $attribute->getAttributeKey()->getId()) {
$data['topvalues'][] = [
'name' => $attribute->getAttributeKey()->getName(),
'type' => $attribute->getAttributeKey()->getType(),
'attributeKey' => $attribute->getAttributeKey()->getId(),
'value' => $value['value'],
];
}
}
Expand Down Expand Up @@ -863,19 +875,19 @@ public function reportAction(Request $request, Response $response, $args)
}

$data = $insertion->toArray(2);
$data["topvalues"] = [];
$data['topvalues'] = [];
/**
* @var $attribute InsertionTypeAttribute
*/
foreach ($insertion->getInsertionType()->getAttributes() as $attribute) {
if ($attribute->isTop()) {
foreach ($data["values"] as $value) {
if ($value["attributeKey"] == $attribute->getAttributeKey()->getId()) {
$data["topvalues"][] = [
"name" => $attribute->getAttributeKey()->getName(),
"type" => $attribute->getAttributeKey()->getType(),
"attributeKey" => $attribute->getAttributeKey()->getId(),
"value" => $value["value"],
foreach ($data['values'] as $value) {
if ($value['attributeKey'] == $attribute->getAttributeKey()->getId()) {
$data['topvalues'][] = [
'name' => $attribute->getAttributeKey()->getName(),
'type' => $attribute->getAttributeKey()->getType(),
'attributeKey' => $attribute->getAttributeKey()->getId(),
'value' => $value['value'],
];
}
}
Expand Down Expand Up @@ -930,8 +942,8 @@ public function findByIdAction(Request $request, Response $response)

if (!isset($insertion)) {
Oforge()->View()->Flash()->addMessage('error', I18N::translate('insertion_not_exist', [
'en' => "The provided ID does not belong to an insertion.",
'de' => "Zu der gegebenen ID existiert kein Inserat.",
'en' => 'The provided ID does not belong to an insertion.',
'de' => 'Zu der gegebenen ID existiert kein Inserat.',
]));

return $response->withRedirect('/');
Expand Down Expand Up @@ -960,7 +972,7 @@ public function findByIdAction(Request $request, Response $response)
*/
public function trackViewAction(Request $request, Response $response) {
if($request->isPost()) {
$id = $request->getParsedBody()["id"];
$id = $request->getParsedBody()['id'];
/** @var InsertionService $insertionService */
$insertionService = Oforge()->Services()->get('insertion');
$insertionService->countUpInsertionViews($id);
Expand Down
50 changes: 49 additions & 1 deletion Plugins/Insertion/Services/InsertionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct()
'default' => Insertion::class,
'insertionAttributeValue' => InsertionAttributeValue::class,
'language' => Language::class,
'content' => InsertionContent::class
]);
}

Expand Down Expand Up @@ -175,7 +176,8 @@ public function getInsertionByDays(DateTime $from, DateTime $to)
* @param $id
* @throws ORMException
*/
public function countUpInsertionViews($id) {
public function countUpInsertionViews($id)
{
/** @var Insertion $insertion */
$insertion = $this->repository()->find($id);
$insertion = $insertion->countUpViews();
Expand All @@ -198,4 +200,50 @@ public function getLatestInsertionOfUser($userId)
->setMaxResults(1);
return $queryBuilder->getQuery()->getOneOrNullResult();
}

/**
* @param string|int $id
* @param string $language
* @return object|null
* @throws ORMException
*/
public function getInsertionContentByLanguage($id, $language)
{
/** @var Language $language */
$language = $this->repository('language')->findOneBy(['iso' => $language]);
if (!isset($language)) {
return null;
}
/** @var InsertionContent $insertion */
return $insertionContent = $this->repository('content')->findOneBy(['insertion' => $id, 'language' => $language->getId()]);
}


/**
* @param array $args
* @return Insertion
* @throws ORMException
*/
public function addTranslatedInsertionContent($args = [])
{
/** @var Insertion $insertion */
$insertion = $this->repository()->find($args['id']);

/** @var InsertionContent[] $existingContent */
$content = new InsertionContent();
$insertion->setContent([$content]);

$content->setDescription($args['description']);
$content->setTitle($args['title']);
$content->setInsertion($insertion);

/** @var Language $language */
$language = $this->repository("language")->findOneBy(["iso" => $args['language']]);
$content->setLanguage($language);

$this->entityManager()->create($content, false);
$this->entityManager()->update($insertion);

return $insertion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@
{% include 'Plugins/Insertion/Frontend/FrontendInsertion/DetailAttributes.twig' %}
{% endblock insertion_detail__attributes %}

<div class="panel panel--product-description ">
<div class="description__head">
<h3 class="panel__header has--bottom-separator">{{ i18n('insertion_detail_description_title_' ~ insertion.insertionType.name, i18n('insertion_detail_description_title')) }}</h3>
<div class="language-switch"></div>
</div>
<div class="description__body">
{{ insertion.content[0].description|nl2br }}
</div>
<div class="description__view-counter">
<svg class="icon icon--small">
<use xlink:href="#preview"></use>
</svg>
<span class="counter">{{ "%04d"|format(insertion.views) }}</span>
{% block insertion_detail__description %}
<div class="panel panel--product-description ">
<div class="description__head">
<h3 class="panel__header has--bottom-separator">{{ i18n('insertion_detail_description_title_' ~ insertion.insertionType.name, i18n('insertion_detail_description_title')) }}</h3>
<div class="language-switch"></div>
</div>
<div class="description__body">
{{ insertion.content[0].description|nl2br }}
</div>
<div class="description__view-counter">
<svg class="icon icon--small">
<use xlink:href="#preview"></use>
</svg>
<span class="counter">{{ "%04d"|format(insertion.views) }}</span>
</div>
</div>
</div>
{% endblock insertion_detail__description %}

{% block insertion_detail__extended_content %}
{% endblock insertion_detail__extended_content %}
Expand Down
1 change: 1 addition & 0 deletions Plugins/Translation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keys/
57 changes: 57 additions & 0 deletions Plugins/Translation/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php


namespace Translation;


use Oforge\Engine\Modules\Core\Abstracts\AbstractBootstrap;
use Oforge\Engine\Modules\Core\Models\Config\ConfigType;
use Oforge\Engine\Modules\Core\Services\ConfigService;
use Oforge\Engine\Modules\I18n\Models\Language;
use Translation\Controller\Frontend\TranslationController;
use Translation\Services\TranslationService;

/**
* Class Bootstrap
*
* @package Oforge\Engine\Plugins\Translation
*/
class Bootstrap extends AbstractBootstrap
{
public function __construct()
{
$this->endpoints = [
TranslationController::class
];

$this->models = [
Language::class,
];

$this->services = [
'translation' => TranslationService::class
];

$this->dependencies = [
\Insertion\Bootstrap::class
];
}

public function install()
{
parent::install(); // TODO: Change the autogenerated stub

/** @var ConfigService $configService */
$configService = Oforge()->Services()->get('config');

$configService->add([
'name' => 'google_cloud_translation_api_key',
'type' => ConfigType::STRING,
'group' => 'google_cloud',
'default' => '',
'label' => 'config_google_cloud_translation_api_key',
'required' => true,
'order' => 0,
]);
}
}
Loading

0 comments on commit 344a536

Please sign in to comment.