Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: code issues #499

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/elements/db/TranslatorQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class TranslatorQuery extends ElementQuery
/**
* @inheritdoc
*/
public $id;
public mixed $id;
public $label;
public $service;
public $status;
public array|string|null $status;
public $settings;

/**
Expand Down
41 changes: 0 additions & 41 deletions src/models/AssetDraftModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

namespace acclaro\translations\models;

use Craft;
use craft\elements\Asset;
use craft\behaviors\DraftBehavior;
use craft\validators\SiteIdValidator;
use acclaro\translations\Translations;
use craft\behaviors\FieldLayoutBehavior;
use craft\behaviors\CustomFieldBehavior;
use acclaro\translations\records\AssetDraftRecord;


/**
Expand Down Expand Up @@ -65,40 +62,6 @@ public function getHandle()
return $this->getAsset()->handle;
}

public static function populateModel($attributes)
{
if ($attributes instanceof AssetDraftRecord) {
$attributes = $attributes->getAttributes();
}

$assetData = json_decode($attributes['data'], true);
$fieldContent = isset($assetData['fields']) ? $assetData['fields'] : null;
// $attributes['draftId'] = $attributes['id'];
$attributes['id'] = $attributes['assetId'];

$attributes = array_diff_key($attributes, array_flip(array('data', 'fields', 'assetId')));

$attributes = array_merge($attributes, $assetData);

$draft = parent::setAttributes($attributes);

if ($fieldContent) {
$post = array();

foreach ($fieldContent as $fieldId => $fieldValue) {
$field = Craft::$app->fields->getFieldById($fieldId);

if ($field) {
$post[$field->handle] = $fieldValue;
}
}

$draft->setFieldValues($post);
}

return $draft;
}

Comment on lines -68 to -101
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed due to not being in use.

public function getAsset()
{
if (is_null($this->assetId)) {
Expand Down Expand Up @@ -133,10 +96,6 @@ public function getCpEditUrl(): ?string
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['customFields'] = [
'class' => CustomFieldBehavior::class,
'hasMethods' => false,
];
$behaviors['fieldLayout'] = [
'class' => FieldLayoutBehavior::class,
'elementType' => Asset::class,
Expand Down
7 changes: 2 additions & 5 deletions src/models/CommerceDraftModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
use Craft;
use craft\commerce\elements\Product;
use acclaro\translations\Translations;
use craft\behaviors\CustomFieldBehavior;
use craft\behaviors\DraftBehavior;
use craft\behaviors\FieldLayoutBehavior;
use craft\validators\DateTimeValidator;
use craft\validators\SiteIdValidator;

/**
* @author Acclaro
Expand Down Expand Up @@ -109,10 +110,6 @@ public function getCpEditUrl(): ?string
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['customFields'] = [
'class' => CustomFieldBehavior::class,
'hasMethods' => false,
];
$behaviors['fieldLayout'] = [
'class' => FieldLayoutBehavior::class,
'elementType' => Product::class,
Expand Down
40 changes: 0 additions & 40 deletions src/models/GlobalSetDraftModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@

namespace acclaro\translations\models;

use Craft;
use craft\elements\GlobalSet;
use craft\behaviors\DraftBehavior;
use craft\validators\SiteIdValidator;
use craft\validators\DateTimeValidator;
use craft\behaviors\CustomFieldBehavior;
use craft\behaviors\FieldLayoutBehavior;

use acclaro\translations\Translations;
use acclaro\translations\records\GlobalSetDraftRecord;

/**
* @author Acclaro
Expand Down Expand Up @@ -73,39 +70,6 @@ public function getHandle()
return $this->getGlobalSet()->handle;
}

public static function populateModel($attributes)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in use

{
if ($attributes instanceof GlobalSetDraftRecord) {
$attributes = $attributes->getAttributes();
}

$globalSetData = json_decode($attributes['data'], true);
$fieldContent = isset($globalSetData['fields']) ? $globalSetData['fields'] : null;
$attributes['id'] = $attributes['globalSetId'];

$attributes = array_diff_key($attributes, array_flip(array('data', 'fields', 'globalSetId')));

$attributes = array_merge($attributes, $globalSetData);

$draft = parent::setAttributes($attributes);

if ($fieldContent) {
$post = array();

foreach ($fieldContent as $fieldId => $fieldValue) {
$field = Craft::$app->fields->getFieldById($fieldId);

if ($field) {
$post[$field->handle] = $fieldValue;
}
}

$draft->setFieldValues($post);
}

return $draft;
}

public function getGlobalSet()
{
if (is_null($this->globalSetId)) {
Expand Down Expand Up @@ -140,10 +104,6 @@ public function getCpEditUrl(): ?string
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['customFields'] = [
'class' => CustomFieldBehavior::class,
'hasMethods' => false,
];
$behaviors['fieldLayout'] = [
'class' => FieldLayoutBehavior::class,
'elementType' => GlobalSet::class,
Expand Down
6 changes: 0 additions & 6 deletions src/services/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ class App extends Component
*/
public $translator;

/**
* @var ElementCloner
*/
public $elementCloner;

/**
* @var repository\TranslationRepository
*/
Expand Down Expand Up @@ -181,7 +176,6 @@ public function init(): void
$this->urlHelper = new UrlHelper();
$this->urlGenerator = new UrlGenerator();
$this->translator = new Translator();
$this->elementCloner = new ElementCloner();
$this->translationRepository = new repository\TranslationRepository();
$this->categoryRepository = new repository\CategoryRepository();
$this->assetDraftRepository = new repository\AssetDraftRepository();
Expand Down
46 changes: 0 additions & 46 deletions src/services/ElementCloner.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/services/OrderSearchParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class OrderSearchParams
{
public function getParams()
{
// TODO: check usage of this class
$sites = Craft::$app->sites->getAllSiteIds();
$category = 'app';
$statuses = array_map(function($status) use ($category) {
return Translations::$plugin->translator->translate($category, $status);
}, Translations::$plugin->orderRepository->getOrderStatuses());

$query = parse_str(Craft::$app->request->getQueryStringWithoutPath(), $params);
parse_str(Craft::$app->request->getQueryStringWithoutPath(), $params);

$results = [];

Expand Down
3 changes: 1 addition & 2 deletions src/services/fieldtranslator/CategoryFieldTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace acclaro\translations\services\fieldtranslator;

use craft\base\Element;
use craft\elements\Category;
use acclaro\translations\Translations;
use acclaro\translations\services\ElementTranslator;

class CategoryFieldTranslator extends TaxonomyFieldTranslator
{
public function translateRelated(ElementTranslator $elementTranslator, Element $element, Category $category, $sourceSite, $targetSite, $fieldData)
public function translateRelated(ElementTranslator $elementTranslator, Element $element, $category, $sourceSite, $targetSite, $fieldData)
{
// search for existing translated category in the same group
$translatedCategory = Translations::$plugin->categoryRepository->find(array(
Expand Down
1 change: 1 addition & 0 deletions src/services/fieldtranslator/EntriesFieldTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ public function toPostArrayFromTranslationTarget(ElementTranslator $elementTrans
*/
public function getWordCount(ElementTranslator $elementTranslator, Element $element, Field $field)
{
return 0;
}
}
13 changes: 1 addition & 12 deletions src/services/fieldtranslator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Factory
Entries::class => EntriesFieldTranslator::class,
Matrix::class => MatrixFieldTranslator::class,
MultiSelect::class => MultiSelectFieldTranslator::class,
LinkField::class => LinkFieldTranslator::class,
TypedLinkField::class => TypedLinkFieldTranslator::class,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As link field plugin is one renamed to typed link field so removed old one.

LinkitField::class => LinkitFieldTranslator::class,
NeoField::class => NeoFieldTranslator::class,
Expand Down Expand Up @@ -91,17 +90,7 @@ public function makeTranslator(Field $field)
if (array_key_exists($class, $this->nativeFieldTypes)) {
$translatorClass = $this->nativeFieldTypes[$class];

switch ($translatorClass) {
case MultiSelectFieldTranslator::class:
return new MultiSelectFieldTranslator(Craft::$app, Translations::$plugin->wordCounter, Translations::$plugin->translationRepository);
case SingleOptionFieldTranslator::class:
return new SingleOptionFieldTranslator(Craft::$app, Translations::$plugin->wordCounter, Translations::$plugin->translationRepository);
case TagFieldTranslator::class:
return new TagFieldTranslator(Craft::$app, Translations::$plugin->wordCounter, Translations::$plugin->tagRepository, Translations::$plugin->elementCloner);
case CategoryFieldTranslator::class:
return new CategoryFieldTranslator(Craft::$app, Translations::$plugin->wordCounter, Translations::$plugin->categoryRepository);
}
return new $translatorClass(Craft::$app, Translations::$plugin->wordCounter);
return new $translatorClass();
}

return null;
Expand Down
13 changes: 2 additions & 11 deletions src/services/fieldtranslator/TagFieldTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,15 @@

namespace acclaro\translations\services\fieldtranslator;

use craft\base\Field;
use craft\base\Element;
use craft\elements\Tag;
use acclaro\translations\Translations;
use acclaro\translations\services\ElementTranslator;

class TagFieldTranslator extends TaxonomyFieldTranslator
{
public function translateRelated(ElementTranslator $elementTranslator, Element $element, Tag $existingTag, $sourceSite, $targetSite, $fieldData)
public function translateRelated(ElementTranslator $elementTranslator, Element $element, $existingTag, $sourceSite, $targetSite, $fieldData)
{
$translatedTag = Translations::$plugin->tagRepository->getTagById($existingTag->id, $targetSite);

if ($translatedTag) {
$tag = $translatedTag;
} else {
// Doesn't feels this part runs as tags are propagated to all target locales if localised
$tag = Translations::$plugin->elementCloner->cloneElement($existingTag);
}
$tag = Translations::$plugin->tagRepository->getTagById($existingTag->id, $targetSite);

$tag->siteId = $targetSite;

Expand Down
9 changes: 9 additions & 0 deletions src/services/fieldtranslator/TaxonomyFieldTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use craft\base\Field;
use craft\base\Element;
use acclaro\translations\services\ElementTranslator;
use craft\elements\Category;
use craft\elements\Tag;

class TaxonomyFieldTranslator extends GenericFieldTranslator
{
Expand Down Expand Up @@ -105,4 +107,11 @@ public function getWordCount(ElementTranslator $elementTranslator, Element $elem

return $wordCount;
}

/**
* To be overridden by child class
*/
protected function translateRelated(ElementTranslator $elementTranslator, Element $element, Tag|Category $category, $sourceSite, $targetSite, $fieldData) {
//
}
}
2 changes: 1 addition & 1 deletion src/services/fieldtranslator/VizyFieldTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private function parseSourceValues($value, $key, $sourceSite)
$source[$k] = $option->label;
}
break;
case $value instanceof \fruitstudios\linkit\fields\LinkitField:
case $value instanceof \presseddigital\linkit\fields\LinkitField:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ownership of plugin was changed.

$source[$key] = $value->serializeValue($value)['customText'];

break;
Expand Down
2 changes: 1 addition & 1 deletion src/services/job/ImportFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private function matchXmlKeys($dom, $file)
if (!$dom->loadXML( $xmlSource )) {
$errors = $this->reportXmlErrors();
if ($errors) {
$this->orderLog(sprintf("We found errors on source xml : ", $errors));
$this->orderLog("We found errors on source xml : " . sprintf($errors));
return true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/services/repository/ActivityLogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public function createActivityLog($message, $target)
$activityLog->created = date('Y-m-d H:i:s');
$activityLog->targetClass = get_class($target);

$this->saveActivityLog($activityLog);
return $this->saveActivityLog($activityLog);
} catch (Exception $e) {
// Handle the exception (log or rethrow)
Craft::error('Error creating ActivityLog: ' . $e->getMessage(), __METHOD__);
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function saveTranslation(TranslationModel $translation)
$isNew = !$translation->id;

if (!$isNew) {
$record = TranslationRecord::model()->findById($translation->id);
$record = TranslationRecord::findOne($translation->id);

if (!$record) {
throw new Exception('No translation exists with that ID.');
Expand Down
Loading