Skip to content

Commit

Permalink
Merge pull request #69 from lilt/3.x-development
Browse files Browse the repository at this point in the history
Release 3.2.0
  • Loading branch information
hadomskyi authored Nov 22, 2022
2 parents edec717 + 6f3c523 commit 8283c13
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.2.0 - 2022-11-20
### Added
- Option to enable target entries on translations publish
- Option to enable slug copy from source entries to target

## 3.1.2 - 2022-11-17
### Fixed
- Multiple drafts apply issue for different sites
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lilt/craft-lilt-plugin",
"description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.",
"type": "craft-plugin",
"version": "3.1.2",
"version": "3.2.0",
"keywords": [
"craft",
"cms",
Expand Down
22 changes: 22 additions & 0 deletions src/controllers/PostConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public function actionInvoke(): Response
$connectorApiUrlRecord->value = $request->getBodyParam('connectorApiUrl');
$connectorApiUrlRecord->save();

# enableEntriesForTargetSites
$enableEntriesForTargetSites = SettingRecord::findOne(['name' => 'enable_entries_for_target_sites']);
if (!$enableEntriesForTargetSites) {
$enableEntriesForTargetSites = new SettingRecord(['name' => 'enable_entries_for_target_sites']);
}
$enableEntriesForTargetSites->value = (int) $request->getBodyParam('enableEntriesForTargetSites');
$enableEntriesForTargetSites->save();

# copyEntriesSlugFromSourceToTarget
$copyEntriesSlugFromSourceToTarget = SettingRecord::findOne(
['name' => 'copy_entries_slug_from_source_to_target']
);
if (!$copyEntriesSlugFromSourceToTarget) {
$copyEntriesSlugFromSourceToTarget = new SettingRecord(
['name' => 'copy_entries_slug_from_source_to_target']
);
}
$copyEntriesSlugFromSourceToTarget->value = (int) $request->getBodyParam(
'copyEntriesSlugFromSourceToTarget'
);
$copyEntriesSlugFromSourceToTarget->save();

$liltConfigDisabled = true;
if (!empty($connectorApiKey) && !empty($connectorApiUrl)) {
//is token valid
Expand Down
12 changes: 8 additions & 4 deletions src/services/handlers/CopySourceTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use lilthq\craftliltplugin\services\handlers\commands\CreateDraftCommand;
use Throwable;
use yii\base\Exception;
use yii\db\StaleObjectException;
Expand Down Expand Up @@ -46,10 +47,13 @@ public function __invoke(Job $job): void
foreach ($job->getTargetSiteIds() as $targetSiteId) {
//Create draft with & update all values to source element
$drafts[$targetSiteId] = Craftliltplugin::getInstance()->createDraftHandler->create(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId
new CreateDraftCommand(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
)
);

$contents[$targetSiteId] = Craftliltplugin::getInstance()->elementTranslatableContentProvider->provide(
Expand Down
33 changes: 25 additions & 8 deletions src/services/handlers/CreateDraftHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\datetime\DateTime;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
use lilthq\craftliltplugin\records\SettingRecord;
use lilthq\craftliltplugin\services\handlers\commands\CreateDraftCommand;
use Throwable;
use yii\base\Exception;

Expand All @@ -29,11 +31,13 @@ class CreateDraftHandler
* @throws ElementNotFoundException
*/
public function create(
ElementInterface $element,
string $jobTitle,
int $sourceSiteId,
int $targetSiteId
CreateDraftCommand $command
): ElementInterface {
$element = $command->getElement();
$jobTitle = $command->getJobTitle();
$sourceSiteId = $command->getSourceSiteId();
$targetSiteId = $command->getTargetSiteId();

/**
* Element will be created from original one, we can't create draft from draft
* @var Entry $createFrom
Expand Down Expand Up @@ -87,10 +91,26 @@ public function create(
$draft->mergingCanonicalChanges = true;
$draft->afterPropagate(false);

$copyEntriesSlugFromSourceToTarget = SettingRecord::findOne(
['name' => 'copy_entries_slug_from_source_to_target']
);
$isCopySlugEnabled = (bool) ($copyEntriesSlugFromSourceToTarget->value ?? false);

if ($isCopySlugEnabled) {
$draft->slug = $element->slug;
}

Craft::$app->elements->saveElement($draft);

$this->markFieldsAsChanged($draft);

$attributes = ['title'];

if ($isCopySlugEnabled) {
$attributes[] = 'slug';
}
$this->upsertChangedAttributes($draft, $attributes);

return $draft;
}

Expand All @@ -109,7 +129,6 @@ private function markFieldsAsChanged(ElementInterface $element): void
|| get_class($field) === CraftliltpluginParameters::BENF_NEO_FIELD
|| get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_SUPER_TABLE
) {

/**
* @var ElementQuery $matrixBlockQuery
*/
Expand All @@ -128,12 +147,10 @@ private function markFieldsAsChanged(ElementInterface $element): void
}

$this->upsertChangedFields($element, $field);
$this->upsertChangedAttributes($element);
}
}



/**
* @throws \yii\db\Exception
*/
Expand Down Expand Up @@ -163,7 +180,7 @@ private function upsertChangedFields(ElementInterface $element, FieldInterface $
);
}

private function upsertChangedAttributes(ElementInterface $element, array $attributes = ['title']): void
private function upsertChangedAttributes(ElementInterface $element, array $attributes): void
{
$userId = Craft::$app->getUser()->getId();
$timestamp = Db::prepareDateForDb(new DateTime());
Expand Down
9 changes: 9 additions & 0 deletions src/services/handlers/PublishDraftHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Craft;
use craft\services\Drafts as DraftRepository;
use lilthq\craftliltplugin\records\SettingRecord;
use Throwable;

class PublishDraftHandler
Expand All @@ -36,6 +37,14 @@ public function __invoke(int $draftId, int $targetSiteId): void
return;
}

$enableEntriesForTargetSitesRecord = SettingRecord::findOne(['name' => 'enable_entries_for_target_sites']);
$enableEntriesForTargetSites = (bool) ($enableEntriesForTargetSitesRecord->value
?? false);

if ($enableEntriesForTargetSites && !$draftElement->getEnabledForSite($targetSiteId)) {
$draftElement->setEnabledForSite([$targetSiteId => true]);
}

$this->draftRepository->applyDraft($draftElement);
}
}
12 changes: 8 additions & 4 deletions src/services/handlers/SendJobToLiltConnectorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use lilthq\craftliltplugin\modules\FetchJobStatusFromConnector;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use lilthq\craftliltplugin\services\handlers\commands\CreateDraftCommand;
use Throwable;
use yii\base\Exception;
use yii\db\StaleObjectException;
Expand Down Expand Up @@ -70,10 +71,13 @@ public function __invoke(Job $job): void
foreach ($job->getTargetSiteIds() as $targetSiteId) {
//Create draft with & update all values to source element
$draft = Craftliltplugin::getInstance()->createDraftHandler->create(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId
new CreateDraftCommand(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
)
);

$content = Craftliltplugin::getInstance()->elementTranslatableContentProvider->provide(
Expand Down
60 changes: 60 additions & 0 deletions src/services/handlers/commands/CreateDraftCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* @link https://github.com/lilt
* @copyright Copyright (c) 2022 Lilt Devs
*/

declare(strict_types=1);

namespace lilthq\craftliltplugin\services\handlers\commands;

use craft\base\ElementInterface;

class CreateDraftCommand
{
private $element;
private $jobTitle;
private $sourceSiteId;
private $targetSiteId;
private $flow;

public function __construct(
ElementInterface $element,
string $jobTitle,
int $sourceSiteId,
int $targetSiteId,
string $flow
) {
$this->element = $element;
$this->jobTitle = $jobTitle;
$this->sourceSiteId = $sourceSiteId;
$this->targetSiteId = $targetSiteId;
$this->flow = $flow;
}

public function getElement(): ElementInterface
{
return $this->element;
}

public function getJobTitle(): string
{
return $this->jobTitle;
}

public function getSourceSiteId(): int
{
return $this->sourceSiteId;
}

public function getTargetSiteId(): int
{
return $this->targetSiteId;
}

public function getFlow(): string
{
return $this->flow;
}
}
22 changes: 22 additions & 0 deletions src/templates/_components/utilities/configuration.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@
disabled: liltConfigDisabled
}) }}

<div class="field">
{{ forms.checkbox({
label: 'Enable entries for target site on translation publish',
name: 'enableEntriesForTargetSites',
id: 'enableEntriesForTargetSites',
checked: enableEntriesForTargetSites,
errors: model is defined? model.getErrors('enableEntriesForTargetSites') : [],
disabled: liltConfigDisabled
}) }}
</div>

<div class="field">
{{ forms.checkbox({
label: 'Copy slug from source entries to target',
name: 'copyEntriesSlugFromSourceToTarget',
id: 'copyEntriesSlugFromSourceToTarget',
checked: copyEntriesSlugFromSourceToTarget,
errors: model is defined? model.getErrors('copyEntriesSlugFromSourceToTarget') : [],
disabled: liltConfigDisabled
}) }}
</div>

<div class="buttons">
<button type="submit" class="btn submit">{{ 'Update configuration'|t('craft-lilt-plugin') }}</button>
<div class="utility-status"></div>
Expand Down
11 changes: 11 additions & 0 deletions src/utilities/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public static function contentHtml(): string
$connectorApiUrl = $connectorApiUrlRecord->value
?? \LiltConnectorSDK\Configuration::getDefaultConfiguration()->getHost();

$enableEntriesForTargetSitesRecord = SettingRecord::findOne(['name' => 'enable_entries_for_target_sites']);
$enableEntriesForTargetSites = (bool) ($enableEntriesForTargetSitesRecord->value
?? false);

$copyEntriesSlugFromSourceToTargetRecord = SettingRecord::findOne(
['name' => 'copy_entries_slug_from_source_to_target']
);
$copyEntriesSlugFromSourceToTarget = (bool) ($copyEntriesSlugFromSourceToTargetRecord->value ?? false);

return Craft::$app->getView()->renderTemplate(
'craft-lilt-plugin/_components/utilities/configuration.twig',
[
Expand All @@ -88,6 +97,8 @@ public static function contentHtml(): string
'connectorApiUrl' => $connectorApiUrl,
'formActionUrl' => UrlHelper::cpUrl('craft-lilt-plugin/settings/lilt-configuration'),
'liltConfigDisabled' => $liltConfigDisabled,
'enableEntriesForTargetSites' => $enableEntriesForTargetSites,
'copyEntriesSlugFromSourceToTarget' => $copyEntriesSlugFromSourceToTarget,
]
);
}
Expand Down
16 changes: 10 additions & 6 deletions tests/_support/Helper/CraftLiltPluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use lilthq\craftliltplugin\records\I18NRecord;
use lilthq\craftliltplugin\records\JobRecord;
use lilthq\craftliltplugin\records\TranslationRecord;
use lilthq\craftliltplugin\services\handlers\commands\CreateDraftCommand;
use lilthq\craftliltplugin\services\handlers\commands\CreateJobCommand;
use yii\base\InvalidArgumentException;

Expand Down Expand Up @@ -47,10 +48,13 @@ public function createJobWithTranslations(array $data): array
->provide($element);
//Create draft with & update all values to source element
$drafts[$targetSiteId] = Craftliltplugin::getInstance()->createDraftHandler->create(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId
new CreateDraftCommand(
$element,
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
)
);
}

Expand Down Expand Up @@ -244,7 +248,7 @@ public function assertTranslationFailed(
$this->assertSame(TranslationRecord::STATUS_FAILED, $translation->status);
}

public function assertTranslationStatus(int $translationId, string $expectedStatus)
public function assertTranslationStatus(int $translationId, string $expectedStatus): void
{
$actualTranslation = TranslationRecord::findOne(
[
Expand All @@ -255,7 +259,7 @@ public function assertTranslationStatus(int $translationId, string $expectedStat
$this->assertSame($expectedStatus, $actualTranslation->status);
}

public function assertJobStatus(int $jobId, string $expectedStatus)
public function assertJobStatus(int $jobId, string $expectedStatus): void
{
$actualJob = JobRecord::findOne(
[
Expand Down

0 comments on commit 8283c13

Please sign in to comment.