]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4210 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Jul 2021 14:36:02 +0000 (16:36 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Jul 2021 14:36:02 +0000 (16:36 +0200)
app/Http/Controllers/API/FluidbookThemeAPIController.php
app/Jobs/UpdateWS2ThemeTable.php
app/Models/FluidbookTheme.php

index 5dfbd4ba4f1c2ec21949e89b00d8447f9f969a24..035e255ff540e8c7f55142b101fcb6941697c00e 100644 (file)
@@ -125,6 +125,13 @@ class FluidbookThemeAPIController extends Controller
         self::_handleFiles($theme, $request->get('theme'));
     }
 
+    public function clonefromws2(Request $request, $id)
+    {
+        $theme = FluidbookTheme::find($id);
+        $newtheme = $theme->replicate();
+        $newtheme->push();
+    }
+
     public function deletefromws2(Request $request, $id)
     {
         /** @var FluidbookTheme $theme */
index 9c94a739d0f5ba7a3124e50bec2309eff2612eb8..8d951f696b6487409115be016dfc0b7e75f5402d 100644 (file)
@@ -19,16 +19,26 @@ class UpdateWS2ThemeTable implements ShouldQueue
 {
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
+    protected $id = 'all';
+
+    protected $_fileFields = [];
+    protected $_colorFields = [];
+    protected $_colorAlphaFields = [];
+    protected $_allFields = [];
+    protected $_ignore = [];
+    protected $_t3dir = '/data1/extranet/www/fluidbook/themes3/';
+
     protected static $_colorToWS2Cache = [];
     protected static $_colorAlphaToWS2Cache = [];
 
-    public function __construct()
+    public function __construct($id = 'all')
     {
+        $this->id = $id;
     }
 
     public function uniqueId()
     {
-        return 'updatews2themetable';
+        return 'updatews2themetable_' . $this->id;
     }
 
     /**
@@ -40,82 +50,89 @@ class UpdateWS2ThemeTable implements ShouldQueue
     {
         $theme = new FluidbookTheme();
 
-        $fileFields = [];
-        $colorFields = [];
-        $colorAlphaFields = [];
-        $allFields = [];
         foreach ($theme->getFields() as $field) {
             $name = $field->getAttribute('name');
-            $allFields[] = $name;
+            $this->_allFields[] = $name;
             if ($field instanceof Files) {
-                $fileFields[] = $name;
+                $this->_fileFields[] = $name;
             } else if ($field instanceof Color) {
                 if ($field->getAttribute('allows_alpha')) {
-                    $colorAlphaFields[] = $name;
+                    $this->_colorAlphaFields[] = $name;
                 } else {
-                    $colorFields[] = $name;
+                    $this->_colorFields[] = $name;
                 }
             }
         }
-        $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug'];
-        $t3dir = '/data1/extranet/www/fluidbook/themes3/';
+        $this->_ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug'];
 
         $data = [];
-        foreach (FluidbookTheme::all() as $theme) {
-            $settings = [];
-            foreach ($allFields as $k) {
-                if (in_array($k, $ignore)) {
-                    continue;
-                }
-                $v = $theme->$k;
-
-                if (in_array($k, $colorAlphaFields)) {
-                    $v = self::colorAlphaToWS2($v);
-                } else if (in_array($k, $colorFields)) {
-                    $v = self::colorToWS2($v);
-                } else if (in_array($k, $fileFields)) {
-                    if (null === $v) {
-                        $v = '';
-                    } else {
-                        /** @var Media $media */
-                        $media = $theme->getMedia($v)->get(0);
-                        if (null !== $media) {
-                            $v = $media->getAttribute('file_name');
-                            $dir = $t3dir . $theme->id;
-                            if (!file_exists($dir)) {
-                                mkdir($dir, 0777, true);
-                            }
-                            $dest = $dir . '/' . $v;
-                            $path = $media->getPath();
-                            $exists = file_exists($dest);
-                            if (!$exists || realpath($dest) !== realpath($path)) {
-                                if ($exists) {
-                                    unlink($dest);
-                                }
-                                `ln -s $path $dest`;
-                            }
-                        } else {
-                            $v = '';
+        $t = DB::table('extranet_clean.ws3_theme');
+        if ($this->id === 'all') {
+            foreach (FluidbookTheme::all() as $theme) {
+                $data[] = $this->_handleTheme($theme);
+            }
+            $t->truncate();
+        } else {
+            $theme = FluidbookTheme::find($this->id);
+            $data[] = $this->_handleTheme($theme);
+            $t->where('theme_id', '=', $this->id)->delete();
+        }
+        $t->insert($data);
+    }
+
+    protected function _handleTheme($theme)
+    {
+        $res = $this->_getThemeData($theme);
+        $dest = $this->_t3dir . '/' . $theme->id . '.jpg';
+        if (!file_exists($dest)) {
+            $preview = storage_path('themes/' . $theme->id . '.jpg');
+            `ln -sf $preview $dest`;
+        }
+        return $res;
+    }
+
+    protected function _getThemeData($theme)
+    {
+        $settings = [];
+        foreach ($this->_allFields as $k) {
+            if (in_array($k, $this->_ignore)) {
+                continue;
+            }
+            $v = $theme->$k;
+
+            if (in_array($k, $this->_colorAlphaFields)) {
+                $v = self::colorAlphaToWS2($v);
+            } else if (in_array($k, $this->_colorFields)) {
+                $v = self::colorToWS2($v);
+            } else if (in_array($k, $this->_fileFields)) {
+                if (null === $v) {
+                    $v = '';
+                } else {
+                    /** @var Media $media */
+                    $media = $theme->getMedia($v)->get(0);
+                    if (null !== $media) {
+                        $v = $media->getAttribute('file_name');
+                        $dir = $this->_t3dir . $theme->id;
+                        if (!file_exists($dir)) {
+                            mkdir($dir, 0777, true);
                         }
+                        $dest = $dir . '/' . $v;
+                        $path = $media->getPath();
+                        $exists = file_exists($dest);
+                        if (!$exists || realpath($dest) !== realpath($path)) {
+                            `rm -f $dest;ln -sf $path $dest`;
+                        }
+                    } else {
+                        $v = '';
                     }
                 }
-                if (null === $v) {
-                    continue;
-                }
-                $settings[$k] = $v;
             }
-            $data[] = ['theme_id' => $theme->id, 'signature' => 0, 'nom' => $theme->name, 'proprietaire' => $theme->owner, 'icones' => $theme->iconSet, 'date' => strtotime($theme->creation_date), 'parametres' => json_encode($settings)];
-
-            $dest = $t3dir . '/' . $theme->id . '.jpg';
-            if (!file_exists($dest)) {
-                $preview = storage_path('themes/' . $theme->id . '.jpg');
-                `ln -sf $preview $dest`;
+            if (null === $v) {
+                continue;
             }
+            $settings[$k] = $v;
         }
-        $t = DB::table('extranet_clean.ws3_theme');
-        $t->truncate();
-        $t->insert($data);
-
+        return ['theme_id' => $theme->id, 'signature' => 0, 'nom' => $theme->name, 'proprietaire' => $theme->owner, 'icones' => $theme->iconSet, 'date' => strtotime($theme->creation_date), 'parametres' => json_encode($settings)];
     }
 
     public static function colorAlphaToWS2($data)
index 2bba683eb9fceb0c39ec4729a3a10374e8acc409..f31ef5b01be3f6ac954e4711d9cddb48f0ef10b3 100644 (file)
@@ -629,7 +629,14 @@ class FluidbookTheme extends CubistMagicAbstractModel
     public function postSave()
     {
         parent::postSave();
-        self::updateWS2Table();
+        self::updateWS2Table($this->id);
+        dispatch(new GenerateThemePreview($this))->onQueue('theme');
+    }
+
+    public function postCreate()
+    {
+        parent::postCreate();
+        self::updateWS2Table($this->id);
         dispatch(new GenerateThemePreview($this))->onQueue('theme');
     }
 
@@ -639,10 +646,13 @@ class FluidbookTheme extends CubistMagicAbstractModel
         self::updateWS2Table();
     }
 
-    public static function updateWS2Table()
+    public static function updateWS2Table($id = null)
     {
         if (self::$updateWS2ViewOnChange) {
-            dispatch(new UpdateWS2ThemeTable())->onConnection('sync');
+            if (null !== $id) {
+                dispatch(new UpdateWS2ThemeTable($id))->onConnection('sync');
+            }
+            dispatch(new UpdateWS2ThemeTable('all'))->onQueue('theme');
         }
     }