Skip to content

Commit

Permalink
Merge pull request #14 from appleple/unit-object
Browse files Browse the repository at this point in the history
Unit object
  • Loading branch information
atsu666 authored Nov 28, 2024
2 parents c4b8cee + b1f7ca2 commit 6d88774
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 25 additions & 1 deletion src/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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;
}
}
}
Expand Down
43 changes: 35 additions & 8 deletions src/Models/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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));
}
Expand Down
88 changes: 46 additions & 42 deletions src/POST/GoogleTranslate/CreateEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand All @@ -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;
}
}
}
}
Expand Down

0 comments on commit 6d88774

Please sign in to comment.