diff --git a/src/Engine.php b/src/Engine.php index 1700ed9d..253a7735 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -144,7 +144,7 @@ public function buildEntryData($entry, $targetField, $detail = true) ]; if ($detail) { $item['fields'] = $this->buildFieldData($eid, $targetField); - $item['units'] = $this->buildUnitDate($eid); + $item['units'] = $this->buildUnitData($eid); } return $item; } @@ -347,7 +347,7 @@ protected function buildFieldData($eid, $targetField) * @param int $eid * @return array */ - protected function buildUnitDate($eid) + protected function buildUnitData($eid) { $item = []; $units = loadColumn($eid); diff --git a/src/Import.php b/src/Import.php index 19c90c7c..524a72e5 100644 --- a/src/Import.php +++ b/src/Import.php @@ -98,7 +98,7 @@ protected function buildEntry(&$model, $entry) foreach ($entry->units as $new) { $clid = $new->clid; foreach ($model->units as $i => $current) { - if ($current['clid'] === $clid) { + if (is_array($current) && $current['clid'] === $clid) { $type = detectUnitTypeSpecifier($new->type); switch ($type) { case 'text': @@ -119,6 +119,30 @@ protected function buildEntry(&$model, $entry) $current['id'] = uniqueString(); $model->units[$i] = $current; break; + } elseif (is_object($current) && $current->getId() === $clid) { + $type = detectUnitTypeSpecifier($new->type); + switch ($type) { + case 'text': + $current->setField1($new->text); + break; + case 'table': + $current->setField1($new->table); + break; + case 'media': + $current->setField2($new->caption); + $current->setField3($new->alt); + break; + case 'image': + $current->setField1($new->caption); + $current->setField4($new->alt); + break; + case 'file': + $current->setField1($new->caption); + break; + } + $current->setTempId(uniqueString()); + $model->units[$i] = $current; + break; } } } diff --git a/src/Models/Entry.php b/src/Models/Entry.php index 47ae99df..a325664c 100644 --- a/src/Models/Entry.php +++ b/src/Models/Entry.php @@ -3,11 +3,12 @@ namespace Acms\Plugins\GoogleTranslate\Models; use Acms\Plugins\GoogleTranslate\Contracts\Model; +use Acms\Services\Facades\Application; use DB; use SQL; use Field; use Common; -use Entry as EntryHelper; +use Acms\Services\Entry\Helper as EntryHelper; class Entry extends Model { @@ -132,14 +133,31 @@ public function load($item) $key = preg_replace('/entry_/', '', $key); $this->{$key} = $value; } + $this->fields = loadEntryField($this->id); - $this->units = loadColumn($this->id); - foreach ($this->units as & $unit) { - $type = detectUnitTypeSpecifier($unit['type']); - if ($type === 'custom') { - $unit['field'] = acmsUnserialize($unit['field']); + + // ablogcms v3.1.23 からリファクタリングによりメソッドがなくなっている問題の解決 + if (method_exists(EntryHelper::class, 'saveColumn')) { + $this->units = loadColumn($this->id); + foreach ($this->units as & $unit) { + $type = detectUnitTypeSpecifier($unit['type']); + if ($type === 'custom') { + $unit['field'] = acmsUnserialize($unit['field']); + } + $unit['id'] = uniqueString(); + } + } else { + /** @var \Acms\Services\Unit\Repository $unitService */ + $unitService = Application::make('unit-repository'); + $this->units = $unitService->loadUnits($this->id); + + foreach ($this->units as & $unit) { + $type = detectUnitTypeSpecifier($unit->getType()); + if ($type === 'custom') { + $unit->setField6(acmsUnserialize($unit->getField6())); + } + $unit->setTempId(uniqueString()); } - $unit['id'] = uniqueString(); } } @@ -168,7 +186,16 @@ public function save() $DB->query($SQL->get(dsn()), 'exec'); } - EntryHelper::saveColumn($this->units, $this->id, $this->blog_id); + // ablogcms v3.1.23 からリファクタリングによりメソッドがなくなっている問題の解決 + if (method_exists(EntryHelper::class, 'saveColumn')) { + $entryHelper = new EntryHelper(); + $entryHelper->saveColumn($this->units, $this->id, $this->blog_id); + } else { + $unitRepository = Application::make('unit-repository'); + assert($unitRepository instanceof \Acms\Services\Unit\Repository); + $unitRepository->saveUnits($this->units, $this->id, $this->blog_id); + } + Common::saveField('eid', $this->id, $this->fields); Common::saveFulltext('eid', $this->id, Common::loadEntryFulltext($this->id)); } diff --git a/src/POST/GoogleTranslate/CreateEntry.php b/src/POST/GoogleTranslate/CreateEntry.php index 79fa7022..9f9eb53b 100644 --- a/src/POST/GoogleTranslate/CreateEntry.php +++ b/src/POST/GoogleTranslate/CreateEntry.php @@ -109,27 +109,29 @@ protected function googleTranslate($targetBid, $newEid) protected function addToTranslationUnits($units, $googleTranslate) { foreach ($units as $i => $unit) { - $type = detectUnitTypeSpecifier($unit['type']); - switch ($type) { - case 'text': - $tagType = $this->getTextUnitFormat($unit['tag']); - if ($tagType === 'html') { - $googleTranslate->addHtml('unit_text_' . $i, $this->newLineEscape($unit['text'])); - } elseif ($tagType === 'text') { - $googleTranslate->addText('unit_text_' . $i, $unit['text']); - } - break; - case 'table': - $googleTranslate->addHtml('unit_table_' . $i, $unit['table']); - break; - case 'media': - case 'image': - $googleTranslate->addText('unit_caption_' . $i, $unit['caption']); - $googleTranslate->addText('unit_alt_' . $i, $unit['alt']); - break; - case 'file': - $googleTranslate->addText('unit_caption_' . $i, $unit['caption']); - break; + if (is_array($unit)) { + $type = detectUnitTypeSpecifier($unit['type']); + switch ($type) { + case 'text': + $tagType = $this->getTextUnitFormat($unit['tag']); + if ($tagType === 'html') { + $googleTranslate->addHtml('unit_text_' . $i, $this->newLineEscape($unit['text'])); + } elseif ($tagType === 'text') { + $googleTranslate->addText('unit_text_' . $i, $unit['text']); + } + break; + case 'table': + $googleTranslate->addHtml('unit_table_' . $i, $unit['table']); + break; + case 'media': + case 'image': + $googleTranslate->addText('unit_caption_' . $i, $unit['caption']); + $googleTranslate->addText('unit_alt_' . $i, $unit['alt']); + break; + case 'file': + $googleTranslate->addText('unit_caption_' . $i, $unit['caption']); + break; + } } } } @@ -141,27 +143,29 @@ protected function addToTranslationUnits($units, $googleTranslate) protected function getTranslationUnits(&$units, $googleTranslate) { foreach ($units as $i => & $unit) { - $type = detectUnitTypeSpecifier($unit['type']); - switch ($type) { - case 'text': - $tagType = $this->getTextUnitFormat($unit['tag']); - if ($tagType === 'html') { - $unit['text'] = $this->newLineUnEscape($googleTranslate->getHtml('unit_text_' . $i)); - } elseif ($tagType === 'text') { - $unit['text'] = $googleTranslate->getText('unit_text_' . $i); - } - break; - case 'table': - $unit['table'] = $googleTranslate->getHtml('unit_table_' . $i); - break; - case 'media': - case 'image': - $unit['caption'] = $googleTranslate->getText('unit_caption_' . $i); - $unit['alt'] = $googleTranslate->getText('unit_alt_' . $i); - break; - case 'file': - $unit['caption'] = $googleTranslate->getText('unit_caption_' . $i); - break; + if (is_array($unit)) { + $type = detectUnitTypeSpecifier($unit['type']); + switch ($type) { + case 'text': + $tagType = $this->getTextUnitFormat($unit['tag']); + if ($tagType === 'html') { + $unit['text'] = $this->newLineUnEscape($googleTranslate->getHtml('unit_text_' . $i)); + } elseif ($tagType === 'text') { + $unit['text'] = $googleTranslate->getText('unit_text_' . $i); + } + break; + case 'table': + $unit['table'] = $googleTranslate->getHtml('unit_table_' . $i); + break; + case 'media': + case 'image': + $unit['caption'] = $googleTranslate->getText('unit_caption_' . $i); + $unit['alt'] = $googleTranslate->getText('unit_alt_' . $i); + break; + case 'file': + $unit['caption'] = $googleTranslate->getText('unit_caption_' . $i); + break; + } } } }