]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4210 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Jul 2021 12:59:28 +0000 (14:59 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Jul 2021 12:59:28 +0000 (14:59 +0200)
app/Http/Controllers/API/FluidbookThemeAPIController.php
app/Jobs/GenerateThemePreview.php
app/Jobs/UpdateWS2ThemeTable.php [new file with mode: 0644]
app/Models/FluidbookTheme.php

index 42bc4618cd925cd1733c43cbd40977822e9f8fa9..5dfbd4ba4f1c2ec21949e89b00d8447f9f969a24 100644 (file)
@@ -50,8 +50,6 @@ class FluidbookThemeAPIController extends Controller
             }
             $theme->setAttribute($k, $v);
         }
-
-
         return $theme;
     }
 
index ff3ec14ef8bec67d5efd4ed7f4509c075c68fd7d..332b1df383c81019a2cabb9f898c810ee2b2da5a 100644 (file)
@@ -49,7 +49,7 @@ class GenerateThemePreview implements ShouldQueue
         $cl->setArg(null, resource_path('js/social_screenshot/social_screenshot.js'));
         $cl->setArg('width', 1024);
         $cl->setArg('height', 768);
-        $cl->setArg('delay', 2);
+        $cl->setArg('delay', 4);
         $cl->setArg('scale', 1);
         $cl->setArg('dest', $dest);
         $cl->setArg('url', $this->theme->getPreviewURL(['shortLoading'=>1]));
diff --git a/app/Jobs/UpdateWS2ThemeTable.php b/app/Jobs/UpdateWS2ThemeTable.php
new file mode 100644 (file)
index 0000000..9c94a73
--- /dev/null
@@ -0,0 +1,177 @@
+<?php
+
+
+namespace App\Jobs;
+
+
+use App\Models\FluidbookTheme;
+use Cubist\Backpack\Magic\Fields\Color;
+use Cubist\Backpack\Magic\Fields\Files;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\DB;
+use Spatie\MediaLibrary\MediaCollections\Models\Media;
+
+class UpdateWS2ThemeTable implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected static $_colorToWS2Cache = [];
+    protected static $_colorAlphaToWS2Cache = [];
+
+    public function __construct()
+    {
+    }
+
+    public function uniqueId()
+    {
+        return 'updatews2themetable';
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $theme = new FluidbookTheme();
+
+        $fileFields = [];
+        $colorFields = [];
+        $colorAlphaFields = [];
+        $allFields = [];
+        foreach ($theme->getFields() as $field) {
+            $name = $field->getAttribute('name');
+            $allFields[] = $name;
+            if ($field instanceof Files) {
+                $fileFields[] = $name;
+            } else if ($field instanceof Color) {
+                if ($field->getAttribute('allows_alpha')) {
+                    $colorAlphaFields[] = $name;
+                } else {
+                    $colorFields[] = $name;
+                }
+            }
+        }
+        $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug'];
+        $t3dir = '/data1/extranet/www/fluidbook/themes3/';
+
+        $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 = '';
+                        }
+                    }
+                }
+                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`;
+            }
+        }
+        $t = DB::table('extranet_clean.ws3_theme');
+        $t->truncate();
+        $t->insert($data);
+
+    }
+
+    public static function colorAlphaToWS2($data)
+    {
+        if ($data === '') {
+            return null;
+        }
+        if (!isset(self::$_colorAlphaToWS2Cache[$data])) {
+            self::$_colorAlphaToWS2Cache[$data] = self::_colorToWS2($data, true);
+        }
+        return self::$_colorAlphaToWS2Cache[$data];
+    }
+
+    public static function colorToWS2($data)
+    {
+        if ($data === '') {
+            return '';
+        }
+        if (!isset(self::$_colorToWS2Cache[$data])) {
+            self::$_colorToWS2Cache[$data] = self::_colorToWS2($data, false);
+        }
+        return self::$_colorToWS2Cache[$data];
+    }
+
+    protected static function _colorToWS2($data, $alpha): string
+    {
+        $data = trim($data, '# ');
+        if (strlen($data) === 6) {
+            $res = $data;
+        } else if ($data === 'transparent') {
+            $res = '00000000';
+        } else if (preg_match('/rgba?\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(\s*,\s*([0-9.]*))?\)/', $data, $matches)) {
+            $res = self::dechex($matches[1]) . self::dechex($matches[2]) . self::dechex($matches[3]);
+            if (isset($matches[5])) {
+                $res = self::dechex($matches[5] * 255) . $res;
+            }
+        } else {
+            $res = $data;
+        }
+        if ($alpha && strlen($res) === 6) {
+            $res = 'ff' . $res;
+        }
+        return $res;
+    }
+
+    public static function dechex($e)
+    {
+        if ($e == 0) {
+            $res = '00';
+        } else {
+            $res = dechex(round($e));
+            if (strlen($res) == 1) {
+                $res = '0' . $res;
+            }
+        }
+        return $res;
+    }
+
+}
index 7c505551e9cc358adad7d75cb8d3589e7a84d92b..2bba683eb9fceb0c39ec4729a3a10374e8acc409 100644 (file)
@@ -6,6 +6,7 @@ namespace App\Models;
 
 use App\Fields\User;
 use App\Jobs\GenerateThemePreview;
+use App\Jobs\UpdateWS2ThemeTable;
 use Cubist\Backpack\Magic\Fields\Color;
 use Cubist\Backpack\Magic\Fields\Files;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
@@ -19,9 +20,9 @@ class FluidbookTheme extends CubistMagicAbstractModel
         'singular' => 'theme',
         'plural' => 'themes'];
 
-    protected static $_colorToWS2Cache = [];
+
     protected static $_colorToWS3Cache = [];
-    protected static $_colorAlphaToWS2Cache = [];
+
     protected $_oldRoot = '/home/extranet/www/fluidbook/';
 
     protected $_enableTrackNonDefaultValues = true;
@@ -602,82 +603,6 @@ class FluidbookTheme extends CubistMagicAbstractModel
         $this->addMediaToField($fieldname, $path, true);
     }
 
-    public function updateWS2Table()
-    {
-        $fileFields = [];
-        $colorFields = [];
-        $colorAlphaFields = [];
-        $allFields = [];
-        foreach ($this->getFields() as $field) {
-            $name = $field->getAttribute('name');
-            $allFields[] = $name;
-            if ($field instanceof Files) {
-                $fileFields[] = $name;
-            } else if ($field instanceof Color) {
-                if ($field->getAttribute('allows_alpha')) {
-                    $colorAlphaFields[] = $name;
-                } else {
-                    $colorFields[] = $name;
-                }
-            }
-        }
-        $ignore = ['id', 'name', 'owner', 'created_at', 'deleted_at', 'updated_at', 'slug'];
-        $t3dir = '/data1/extranet/www/fluidbook/themes3/';
-
-        $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`;
-                            }
-                        }
-                    }
-                }
-                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 -s $preview $dest`;
-            }
-        }
-        $t = DB::table('extranet_clean.ws3_theme');
-        $t->truncate();
-        $t->insert($data);
-    }
 
     public static function _colorToWS3($data)
     {
@@ -701,77 +626,23 @@ class FluidbookTheme extends CubistMagicAbstractModel
         return self::$_colorToWS3Cache[$data];
     }
 
-    public static function _colorAlphaToWS2($data)
-    {
-        if ($data === '') {
-            return null;
-        }
-        if (!isset(self::$_colorAlphaToWS2Cache[$data])) {
-            self::$_colorAlphaToWS2Cache[$data] = self::__colorToWS2($data, true);
-        }
-        return self::$_colorAlphaToWS2Cache[$data];
-    }
-
-    public static function _colorToWS2($data)
-    {
-        if ($data === '') {
-            return '';
-        }
-        if (!isset(self::$_colorToWS2Cache[$data])) {
-            self::$_colorToWS2Cache[$data] = self::__colorToWS2($data, false);
-        }
-        return self::$_colorToWS2Cache[$data];
-    }
-
-    protected static function __colorToWS2($data, $alpha)
-    {
-        $data = trim($data, '# ');
-        if (strlen($data) === 6) {
-            $res = $data;
-        } else if ($data === 'transparent') {
-            $res = '00000000';
-        } else if (preg_match('/rgba?\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(\s*,\s*([0-9.]*))?\)/', $data, $matches)) {
-            $res = self::dechex($matches[1]) . self::dechex($matches[2]) . self::dechex($matches[3]);
-            if (isset($matches[5])) {
-                $res = self::dechex($matches[5] * 255) . $res;
-            }
-        } else {
-            $res = $data;
-        }
-        if ($alpha && strlen($res) === 6) {
-            $res = 'ff' . $res;
-        }
-        return $res;
-    }
-
-    public static function dechex($e)
-    {
-        if ($e == 0) {
-            $res = '00';
-        } else {
-            $res = dechex(round($e));
-            if (strlen($res) == 1) {
-                $res = '0' . $res;
-            }
-        }
-        return $res;
-    }
-
-
     public function postSave()
     {
         parent::postSave();
-        if (self::$updateWS2ViewOnChange) {
-            $this->updateWS2Table();
-        }
+        self::updateWS2Table();
         dispatch(new GenerateThemePreview($this))->onQueue('theme');
     }
 
     public function postDelete()
     {
         parent::postDelete();
+        self::updateWS2Table();
+    }
+
+    public static function updateWS2Table()
+    {
         if (self::$updateWS2ViewOnChange) {
-            $this->updateWS2Table();
+            dispatch(new UpdateWS2ThemeTable())->onConnection('sync');
         }
     }