Skip to content

Commit

Permalink
Merge pull request #122 from lilt/3.x-fix-author-id
Browse files Browse the repository at this point in the history
Fix author id for draft creation
  • Loading branch information
hadomskyi authored Sep 21, 2023
2 parents e3c2b44 + 6f0b0bf commit c356a72
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.6.1 - 2023-09-19
### Fixed
- Empty user id on draft creation

## 3.6.0 - 2023-05-24
### Added
- Introduced new job and translation status "needs attention"
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.6.0",
"version": "3.6.1",
"keywords": [
"craft",
"cms",
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/job/AbstractJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use lilthq\craftliltplugin\Craftliltplugin;
use lilthq\craftliltplugin\elements\Job;
use lilthq\craftliltplugin\parameters\CraftliltpluginParameters;
use RuntimeException;
use yii\base\InvalidConfigException;
use yii\web\IdentityInterface;
use yii\web\Response;

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

if (empty($job->authorId)) {
$userIdentity = Craft::$app->getUser()->getIdentity();

if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) {
throw new RuntimeException("can't create job: author id is empty");
}

$job->authorId = (int) $userIdentity->getId();
}

//TODO: due date not using right now
//$job->dueDate = DateTimeHelper::toDateTime($this->request->getBodyParam('dueDate')) ?: null;

Expand Down
21 changes: 9 additions & 12 deletions src/controllers/job/GetJobCreateFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Craft;
use craft\helpers\UrlHelper;
use lilthq\craftliltplugin\elements\Job;
use RuntimeException;
use yii\base\InvalidConfigException;
use yii\web\IdentityInterface;
use yii\web\Response;

class GetJobCreateFormController extends AbstractJobController
Expand Down Expand Up @@ -46,20 +48,16 @@ public function actionInvoke(): Response
$job->sourceSiteId = (int) $sourceSiteId;
}

$userIdentity = Craft::$app->getUser()->getIdentity();
if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) {
throw new RuntimeException("can't create job: author id is empty");
}

$job->authorId = (int)$userIdentity->getId();

return $this->renderJobForm(
$job,
[
/*'formActions' => [
[
"label" => "Create and continue editing",
"redirect" => "{cpEditUrl}",
"shortcut" => true,
"retainScroll" => true,
"eventData" => [
"autosave" => false
]
]
],*/
'crumbs' => [
[
'label' => 'Lilt Plugin',
Expand All @@ -70,7 +68,6 @@ public function actionInvoke(): Response
'url' => UrlHelper::cpUrl('admin/craft-lilt-plugin/jobs')
],
],
'author' => Craft::$app->getUser()->getIdentity()
]
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/handlers/CopySourceTextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function __invoke(Job $job): void
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
$job->translationWorkflow,
$job->authorId
)
);

Expand Down
7 changes: 1 addition & 6 deletions src/services/handlers/CreateDraftHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ public function create(
$element->getCanonicalId()
) : $element;

$creatorId = Craft::$app->user->getId();
if ($creatorId === null) {
$creatorId = $element->authorId;
}

$draft = Craft::$app->drafts->createDraft(
$createFrom,
$creatorId ?? 0,
$command->getAuthorId(),
sprintf(
'%s [%s -> %s] ' . (new DateTime())->format('H:i:s'),
$jobTitle,
Expand Down
3 changes: 2 additions & 1 deletion src/services/handlers/SendJobToLiltConnectorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function __invoke(Job $job): void
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
$job->translationWorkflow,
$job->authorId
)
);

Expand Down
10 changes: 9 additions & 1 deletion src/services/handlers/commands/CreateDraftCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class CreateDraftCommand
private $sourceSiteId;
private $targetSiteId;
private $flow;
private $authorId;

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

public function getElement(): ElementInterface
Expand All @@ -57,4 +60,9 @@ public function getFlow(): string
{
return $this->flow;
}

public function getAuthorId(): int
{
return $this->authorId;
}
}
3 changes: 2 additions & 1 deletion tests/_support/Helper/CraftLiltPluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function createJobWithTranslations(array $data): array
$job->title,
$job->sourceSiteId,
$targetSiteId,
$job->translationWorkflow
$job->translationWorkflow,
$job->authorId
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function testCopySlugSettingEnabled(IntegrationTester $I): void
$job->title,
$job->siteId,
Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('ru-RU'),
'instant'
'instant',
$job->authorId
)
);

Expand Down Expand Up @@ -206,7 +207,8 @@ public function testCopySlugSettingDisabled(IntegrationTester $I): void
$job->title,
$job->siteId,
Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('ru-RU'),
'instant'
'instant',
$job->authorId
)
);

Expand Down

0 comments on commit c356a72

Please sign in to comment.