]> _ Git - cubist_cms-back.git/commitdiff
wip #3158 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Oct 2019 14:30:58 +0000 (16:30 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Oct 2019 14:30:58 +0000 (16:30 +0200)
src/CubistBackpackServiceProvider.php
src/app/Console/Commands/LocaleCopy.php
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CMSPage.php
src/app/Magic/Models/CubistMagicTranslatableModel.php

index d9f768ef724d8f0d9b279d15f86e3af130d101ae..3d78b4e83581238c98076bcd9a80dd8c0a990f6d 100644 (file)
@@ -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
+        ]);
     }
 }
index 39ccb9b34980e7793ba736b5db101eaebdcb315b..832d4a04809bad1b8c29eb7a00e98cf31c1517b7 100644 (file)
@@ -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'));
     }
 }
index bd51659e7dae31fa1a8a48048719e8c7a9b48b7c..bf47273658ca5c73f8ddd9f3f4253a4910bfc43c 100644 (file)
@@ -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()
index d261c9356b53b10f4627ba1acf100ec1926e812a..1a816c84b0cc41cdbf205baa6c2684fbe035c4bd 100644 (file)
@@ -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;
 
index db96702cc37924dea343562e1cab5dd19eb56880..4d9740f83038699aab27feb626cf30368f8b2596 100644 (file)
@@ -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);