From: Vincent Vanwaelscappel Date: Tue, 22 Oct 2019 14:30:58 +0000 (+0200) Subject: wip #3158 @3 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=1694d4c47e313dec150d85f4abe9689f60522abb;p=cubist_cms-back.git wip #3158 @3 --- diff --git a/src/CubistBackpackServiceProvider.php b/src/CubistBackpackServiceProvider.php index d9f768e..3d78b4e 100644 --- a/src/CubistBackpackServiceProvider.php +++ b/src/CubistBackpackServiceProvider.php @@ -5,6 +5,7 @@ namespace Cubist\Backpack; use Cubist\Backpack\app\Console\Command\MigrateCommand; use Cubist\Backpack\app\Console\Commands\GenerateCommand; use Cubist\Backpack\app\Console\Commands\InstallCommand; +use Cubist\Backpack\app\Console\Commands\LocaleCopy; use Cubist\Backpack\app\Console\Commands\SearchIndexCommand; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -54,6 +55,12 @@ class CubistBackpackServiceProvider extends ServiceProvider */ public function register() { - $this->commands([InstallCommand::class, GenerateCommand::class, MigrateCommand::class, SearchIndexCommand::class]); + $this->commands([ + InstallCommand::class, + GenerateCommand::class, + MigrateCommand::class, + SearchIndexCommand::class, + LocaleCopy::class + ]); } } diff --git a/src/app/Console/Commands/LocaleCopy.php b/src/app/Console/Commands/LocaleCopy.php index 39ccb9b..832d4a0 100644 --- a/src/app/Console/Commands/LocaleCopy.php +++ b/src/app/Console/Commands/LocaleCopy.php @@ -3,8 +3,8 @@ namespace Cubist\Backpack\app\Console\Commands; - use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; +use Cubist\Backpack\app\Magic\Models\CubistMagicTranslatableModel; class LocaleCopy extends CubistCommand { @@ -13,7 +13,7 @@ class LocaleCopy extends CubistCommand * * @var string */ - protected $signature = 'cubist:locale:copy {from} {to}'; + protected $signature = 'cubist:locale:copy {from} {to} {--overwrite}'; /** * The console command description. @@ -24,11 +24,9 @@ class LocaleCopy extends CubistCommand public function handle() { - - - $this->call('backup:run'); - + // $this->call('backup:run'); $this->_handleMagicFolder([$this, '_copyModel']); + $this->call('cache:clear'); } /** @@ -36,8 +34,10 @@ class LocaleCopy extends CubistCommand */ protected function _copyModel($model) { - $from = $this->argument('from'); - $to = $this->argument('to'); - echo 'Copy locale of model ' . $model . ' from ' . $from . ' to ' . $to; + if (!($model instanceof CubistMagicTranslatableModel)) { + return; + } + /** @var CubistMagicTranslatableModel $model */ + $model->copyTranslations($this->argument('from'), $this->argument('to'), $this->option('overwrite')); } } diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index bd51659..bf47273 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -271,12 +271,12 @@ class CubistMagicController extends CubistCrudController public function bulkPublish() { - return $this->_bulkChangeOnlineStatus(true); + return $this->_bulkChangeOnlineStatus(1); } public function bulkOffline() { - return $this->_bulkChangeOnlineStatus(false); + return $this->_bulkChangeOnlineStatus(0); } public function saveReorder() diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php index d261c93..1a816c8 100644 --- a/src/app/Magic/Models/CMSPage.php +++ b/src/app/Magic/Models/CMSPage.php @@ -114,19 +114,20 @@ class CMSPage extends CubistMagicNestedModel } /** - * @param $template TemplateAbstract + * @param $template TemplateAbstract|string|null * @param $controller CubistMagicController * @throws \Exception */ - public function useTemplate($template) + public function useTemplate($template = null) { + if (null === $template) { + $template = $this->getAttributeValue('template'); + } + if (is_string($template)) { $template = TemplateAbstract::getTemplateIntanceByName($template); } - if (null === $template) { - return; - } $this->_usedTemplate = $template; diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index db96702..4d9740f 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -50,13 +50,20 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel { if ($key !== null) { $this->guardAgainstNonTranslatableAttribute($key); + $v = $this->getAttributes()[$key]; + if (is_string($v)) { + $v = json_decode($v ?? '' ?: '{}', true); + } + if (!is_array($v)) { + $v = []; + } if ($key === 'slug') { - return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { + return array_filter($v, function ($value) { return $value !== null; }); } else { - return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { + return array_filter($v, function ($value) { return $value !== null && $value !== ''; }); } @@ -69,6 +76,36 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel }); } + public function copyTranslations($from, $to, $overwrite = false) + { + $i = 0; + foreach (static::all() as $item) { + //echo 'instance ' . $item->id . ' ' . implode(', ', $item->translatable) . "\n"; + if ($item instanceof CMSPage) { + $item->useTemplate(); + } + + /** @var $item self */ + + foreach ($item->translatable as $translatable) { + if (!$item->hasTranslation($translatable, $from)) { + //echo 'no translation for ' . $translatable . "\n"; + continue; + } + if ($overwrite || !$item->hasTranslation($translatable, $to)) { + // echo "copy translation for " . $translatable . "\n"; + $translated = $item->getTranslation($translatable, $from); + $item->setTranslation($translatable, $to, $translated); + } else { + // echo "no overwriting existing for " . $translatable . "\n"; + } + } + $item->save(); + $i++; + } + echo 'copy translation of ' . $i . ' instances of ' . get_class($this) . ' from ' . $from . ' to ' . $to . "\n"; + } + public function update(array $attributes = [], array $options = []) { return $this->updateTranslations($this->_prepareData($attributes), $options);