Skip to content

Commit 0ae4e27

Browse files
authored
Merge pull request #124 from lilt/4.x-development
Relase 4.2.2
2 parents 50b2631 + 60f3606 commit 0ae4e27

23 files changed

+133
-264
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

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

7+
## 4.2.2 - 2023-09-22
8+
### Fixed
9+
- Empty user id on draft creation
10+
- Nested blocks getting removed on current entry version when translations downloaded
11+
712
## 4.2.1 - 2023-09-05
813
### Fixed
914
- Download diagnostic data functionality

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "lilt/craft-lilt-plugin",
33
"description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.",
44
"type": "craft-plugin",
5-
"version": "4.2.1",
5+
"version": "4.2.2",
66
"keywords": [
77
"craft",
88
"cms",

e2e/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ install: clone
3030
backup-db:
3131
docker-compose exec -T app sh -c 'php craft db/backup'
3232

33+
refresh:
34+
docker-compose exec -T app sh -c 'rm -rf /app/vendor/lilt/craft-lilt-plugin'
35+
docker-compose exec -T app sh -c 'composer require --ignore-platform-reqs "lilt/craft-lilt-plugin":"^999.9.9"'
36+
3337
build:
3438
docker build -f happy-lager-main/Dockerfile happy-lager-main -t happy-lager
3539

e2e/happy-lager-override/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM craftcms/nginx:8.0
22

33
# switch to the root user to install mysql tools
44
USER root
5-
RUN apk add --no-cache mysql-client libpng libpng-dev libjpeg libjpeg-turbo freetype freetype-dev libjpeg-turbo-dev libzip-dev gd && docker-php-ext-configure gd --with-freetype --with-jpeg && apk del --no-cache libpng-dev freetype-dev libjpeg-turbo-dev
5+
RUN apk add --no-cache composer mysql-client libpng libpng-dev libjpeg libjpeg-turbo freetype freetype-dev libjpeg-turbo-dev libzip-dev gd && docker-php-ext-configure gd --with-freetype --with-jpeg && apk del --no-cache libpng-dev freetype-dev libjpeg-turbo-dev
66
USER www-data
77

88
# the user is `www-data`, so we copy the files using the user and group

src/controllers/job/AbstractJobController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use lilthq\craftliltplugin\Craftliltplugin;
1717
use lilthq\craftliltplugin\elements\Job;
1818
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
19+
use RuntimeException;
1920
use yii\base\InvalidConfigException;
21+
use yii\web\IdentityInterface;
2022
use yii\web\Response;
2123

2224
class AbstractJobController extends Controller
@@ -34,10 +36,20 @@ protected function convertRequestToJobModel(): Job
3436
$job->title = $bodyParams['title'];
3537
$job->sourceSiteId = (int)$bodyParams['sourceSite'];
3638
$job->versions = $bodyParams['versions'] ?? [];
37-
$job->authorId = !empty($bodyParams['author'][0]) ? (int)$bodyParams['author'][0] : null;
39+
$job->authorId = !empty($bodyParams['author'][0]) ? (int) $bodyParams['author'][0] : null;
3840
$job->translationWorkflow = $bodyParams['translationWorkflow'];
3941
$job->elementIds = json_decode($bodyParams['entries'], false) ?? [];
4042

43+
if (empty($job->authorId)) {
44+
$userIdentity = Craft::$app->getUser()->getIdentity();
45+
46+
if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) {
47+
throw new RuntimeException("can't create job: author id is empty");
48+
}
49+
50+
$job->authorId = (int) $userIdentity->getId();
51+
}
52+
4153
//TODO: due date not using right now
4254
//$job->dueDate = DateTimeHelper::toDateTime($this->request->getBodyParam('dueDate')) ?: null;
4355

src/controllers/job/GetJobCreateFormController.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Craft;
1717
use craft\helpers\UrlHelper;
1818
use lilthq\craftliltplugin\elements\Job;
19+
use RuntimeException;
1920
use yii\base\InvalidConfigException;
21+
use yii\web\IdentityInterface;
2022
use yii\web\Response;
2123

2224
class GetJobCreateFormController extends AbstractJobController
@@ -46,20 +48,16 @@ public function actionInvoke(): Response
4648
$job->sourceSiteId = (int) $sourceSiteId;
4749
}
4850

51+
$userIdentity = Craft::$app->getUser()->getIdentity();
52+
if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) {
53+
throw new RuntimeException("can't create job: author id is empty");
54+
}
55+
56+
$job->authorId = (int)$userIdentity->getId();
57+
4958
return $this->renderJobForm(
5059
$job,
5160
[
52-
/*'formActions' => [
53-
[
54-
"label" => "Create and continue editing",
55-
"redirect" => "{cpEditUrl}",
56-
"shortcut" => true,
57-
"retainScroll" => true,
58-
"eventData" => [
59-
"autosave" => false
60-
]
61-
]
62-
],*/
6361
'crumbs' => [
6462
[
6563
'label' => 'Lilt Plugin',
@@ -70,7 +68,6 @@ public function actionInvoke(): Response
7068
'url' => UrlHelper::cpUrl('admin/craft-lilt-plugin/jobs')
7169
],
7270
],
73-
'author' => Craft::$app->getUser()->getIdentity()
7471
]
7572
);
7673
}

src/modules/FetchTranslationFromConnector.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ public function execute($queue): void
8080
}
8181
$translationRecord->refresh();
8282

83+
if (empty($translationRecord->connectorTranslationId)) {
84+
Craft::error(
85+
sprintf(
86+
"Connector translation id is empty for translation:"
87+
. "%d source site: &d target site: %d lilt job: %d",
88+
$translationRecord->id,
89+
$translationRecord->sourceSiteId,
90+
$translationRecord->targetSiteId,
91+
$job->liltJobId,
92+
)
93+
);
94+
95+
$translationRecord->status = TranslationRecord::STATUS_FAILED;
96+
$translationRecord->save();
97+
98+
$mutex->release($mutexKey);
99+
$this->markAsDone($queue);
100+
101+
return;
102+
}
103+
83104
$translationFromConnector = Craftliltplugin::getInstance()->connectorTranslationRepository->findById(
84105
$translationRecord->connectorTranslationId
85106
);
@@ -98,8 +119,11 @@ public function execute($queue): void
98119
]
99120
);
100121

122+
Craftliltplugin::getInstance()->updateJobStatusHandler->update($job->id);
123+
101124
$mutex->release($mutexKey);
102125
$this->markAsDone($queue);
126+
103127
return;
104128
}
105129

@@ -226,17 +250,20 @@ private function isTranslationFailed($job, TranslationResponse $translationFromC
226250
*/
227251
private function isTranslationFinished($job, TranslationResponse $translationFromConnector): bool
228252
{
229-
return ($job->isInstantFlow() && $translationFromConnector->getStatus(
230-
) === TranslationResponse::STATUS_MT_COMPLETE)
231-
|| ($job->isVerifiedFlow() && $translationFromConnector->getStatus(
232-
) === TranslationResponse::STATUS_EXPORT_COMPLETE);
253+
return (
254+
$job->isInstantFlow()
255+
&& $translationFromConnector->getStatus() === TranslationResponse::STATUS_MT_COMPLETE)
256+
|| (
257+
$job->isVerifiedFlow()
258+
&& $translationFromConnector->getStatus() === TranslationResponse::STATUS_EXPORT_COMPLETE
259+
);
233260
}
234261

235262
public static function getDelay(string $flow = CraftliltpluginParameters::TRANSLATION_WORKFLOW_INSTANT): int
236263
{
237264
$envDelay = getenv('CRAFT_LILT_PLUGIN_QUEUE_DELAY_IN_SECONDS');
238265
if (!empty($envDelay) || $envDelay === '0') {
239-
return (int) $envDelay;
266+
return (int)$envDelay;
240267
}
241268

242269
return strtolower($flow) === strtolower(CraftliltpluginParameters::TRANSLATION_WORKFLOW_INSTANT) ?

src/modules/ManualJobSync.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use lilthq\craftliltplugin\Craftliltplugin;
2020
use lilthq\craftliltplugin\elements\Job;
2121
use lilthq\craftliltplugin\records\JobRecord;
22+
use lilthq\craftliltplugin\records\TranslationRecord;
2223

2324
class ManualJobSync extends BaseJob
2425
{
@@ -58,7 +59,7 @@ public function execute($queue): void
5859

5960
// Release all previously queued jobs for lilt plugin jobs
6061
foreach ($jobsInfo as $jobInfo) {
61-
$jobDetails = Craft::$app->getQueue()->getJobDetails((string) $jobInfo['id']);
62+
$jobDetails = Craft::$app->getQueue()->getJobDetails((string)$jobInfo['id']);
6263

6364
if ($jobDetails['status'] === Queue::STATUS_RESERVED) {
6465
//we don't need to do anything with job in progress
@@ -117,7 +118,7 @@ public function execute($queue): void
117118
'id' => $jobInfo['id'],
118119
], [], false);
119120

120-
$queue->retry((string) $jobInfo['id']);
121+
$queue->retry((string)$jobInfo['id']);
121122
$jobsInProgress[$queueJob->jobId] = $queueJob;
122123
} catch (Exception $ex) {
123124
Craft::error(
@@ -170,19 +171,39 @@ public function execute($queue): void
170171
}
171172

172173
if (!empty($jobRecord->liltJobId)) {
173-
// job is already on lilt side, we just need to fetch status again
174-
CraftHelpersQueue::push(
175-
(new FetchJobStatusFromConnector(
176-
[
177-
'jobId' => $jobRecord->id,
178-
'liltJobId' => $jobRecord->liltJobId,
179-
]
180-
)),
181-
FetchJobStatusFromConnector::PRIORITY,
182-
0
174+
$translationRecords = TranslationRecord::findAll(['jobId' => $jobRecord->id]);
175+
$connectorTranslationIds = array_map(
176+
static function (TranslationRecord $translationRecord) {
177+
return $translationRecord->connectorTranslationId;
178+
},
179+
$translationRecords
183180
);
184181

185-
continue;
182+
$translationStatuses = array_map(
183+
static function (TranslationRecord $translationRecord) {
184+
return $translationRecord->status;
185+
},
186+
$translationRecords
187+
);
188+
189+
if (
190+
!in_array(null, $connectorTranslationIds)
191+
&& !in_array(TranslationRecord::STATUS_FAILED, $translationStatuses)
192+
) {
193+
// job is already on lilt side, we just need to fetch status again
194+
CraftHelpersQueue::push(
195+
(new FetchJobStatusFromConnector(
196+
[
197+
'jobId' => $jobRecord->id,
198+
'liltJobId' => $jobRecord->liltJobId,
199+
]
200+
)),
201+
FetchJobStatusFromConnector::PRIORITY,
202+
0
203+
);
204+
205+
continue;
206+
}
186207
}
187208

188209
//Sending job to lilt

src/services/handlers/CopySourceTextHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function __invoke(Job $job): void
5353
$job->title,
5454
$job->sourceSiteId,
5555
$targetSiteId,
56-
$job->translationWorkflow
56+
$job->translationWorkflow,
57+
$job->authorId
5758
)
5859
);
5960

src/services/handlers/CreateDraftHandler.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,9 @@ public function create(
5858
$element->getCanonicalId()
5959
) : $element;
6060

61-
$creatorId = Craft::$app->user->getId();
62-
if ($creatorId === null) {
63-
$creatorId = $element->authorId;
64-
}
65-
6661
$draft = Craft::$app->drafts->createDraft(
6762
$createFrom,
68-
$creatorId ?? 0,
63+
$command->getAuthorId(),
6964
sprintf(
7065
'%s [%s -> %s] ' . (new DateTime())->format('H:i:s'),
7166
$jobTitle,

src/services/handlers/PublishDraftHandler.php

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
use craft\base\ElementInterface;
1414
use craft\errors\InvalidElementException;
1515
use craft\services\Drafts as DraftRepository;
16-
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
1716
use lilthq\craftliltplugin\records\SettingRecord;
18-
use lilthq\craftliltplugin\records\TranslationRecord;
1917
use Throwable;
2018
use yii\base\Exception;
2119

@@ -41,90 +39,6 @@ public function __invoke(int $draftId, int $targetSiteId): void
4139
return;
4240
}
4341

44-
// Merge canonical changes for supertable fields in current draft before publishing
45-
if (
46-
class_exists('verbb\supertable\SuperTable')
47-
|| class_exists('benf\neo\Plugin')
48-
) {
49-
$translation = TranslationRecord::findOne(['translatedDraftId' => $draftId]);
50-
$translations = TranslationRecord::findAll(
51-
[
52-
'jobId' => $translation->jobId,
53-
'status' => TranslationRecord::STATUS_PUBLISHED
54-
]
55-
);
56-
57-
foreach ($translations as $translation) {
58-
$draftElementLanguageToUpdate = Craft::$app->elements->getElementById(
59-
$draftId,
60-
null,
61-
$translation->targetSiteId
62-
);
63-
$draftElementLanguageToUpdate->mergingCanonicalChanges = true;
64-
65-
$fieldLayout = $draftElementLanguageToUpdate->getFieldLayout();
66-
$fields = $fieldLayout ? $fieldLayout->getCustomFields() : [];
67-
foreach ($fields as $field) {
68-
// Check if the field is of Super Table type and the required classes and methods are available
69-
if (
70-
get_class($field) === CraftliltpluginParameters::CRAFT_FIELDS_SUPER_TABLE
71-
&& method_exists('verbb\supertable\SuperTable', 'getService')
72-
&& method_exists('verbb\supertable\SuperTable', 'getInstance')
73-
) {
74-
// Get the Super Table plugin instance
75-
$superTablePluginInstance = call_user_func(['verbb\supertable\SuperTable', 'getInstance']);
76-
77-
// Get the Super Table plugin service
78-
/** @var \verbb\supertable\services\Service $superTablePluginService */
79-
$superTablePluginService = $superTablePluginInstance->getService();
80-
81-
// Duplicate the blocks for the field
82-
$superTablePluginService->duplicateBlocks(
83-
$field,
84-
$draftElementLanguageToUpdate->getCanonical(),
85-
$draftElementLanguageToUpdate
86-
);
87-
88-
continue;
89-
}
90-
91-
if (
92-
get_class($field) === CraftliltpluginParameters::BENF_NEO_FIELD
93-
&& class_exists('benf\neo\Plugin')
94-
&& method_exists('benf\neo\Plugin', 'getInstance')
95-
) {
96-
// Get the Neo plugin instance
97-
/** @var \benf\neo\Plugin $neoPluginInstance */
98-
$neoPluginInstance = call_user_func(['benf\neo\Plugin', 'getInstance']);
99-
100-
// Get the Neo plugin Fields service
101-
/** @var \benf\neo\services\Fields $neoPluginFieldsService */
102-
$neoPluginFieldsService = $neoPluginInstance->get('fields');
103-
104-
// Clear current neo field value
105-
$neoField = $draftElementLanguageToUpdate->getFieldValue($field->handle);
106-
foreach ($neoField as $block) {
107-
if (!$block instanceof ElementInterface) {
108-
continue;
109-
}
110-
111-
Craft::$app->getElements()->deleteElement($block);
112-
}
113-
Craft::$app->getElements()->saveElement($draftElementLanguageToUpdate);
114-
115-
// Duplicate the blocks for the field
116-
$neoPluginFieldsService->duplicateBlocks(
117-
$field,
118-
$draftElementLanguageToUpdate->getCanonical(),
119-
$draftElementLanguageToUpdate
120-
);
121-
122-
continue;
123-
}
124-
}
125-
}
126-
}
127-
12842
$enableEntriesForTargetSitesRecord = SettingRecord::findOne(['name' => 'enable_entries_for_target_sites']);
12943
$enableEntriesForTargetSites = (bool)($enableEntriesForTargetSitesRecord->value
13044
?? false);

src/services/handlers/SendJobToLiltConnectorHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public function __invoke(Job $job): void
7676
$job->title,
7777
$job->sourceSiteId,
7878
$targetSiteId,
79-
$job->translationWorkflow
79+
$job->translationWorkflow,
80+
$job->authorId
8081
)
8182
);
8283

0 commit comments

Comments
 (0)