From e6da13692be12e5b79738a346319b73e4a33b85c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 22 Jul 2021 16:36:02 +0200 Subject: [PATCH] wip #4210 @2 --- .../API/FluidbookThemeAPIController.php | 7 + app/Jobs/UpdateWS2ThemeTable.php | 139 ++++++++++-------- app/Models/FluidbookTheme.php | 16 +- 3 files changed, 98 insertions(+), 64 deletions(-) diff --git a/app/Http/Controllers/API/FluidbookThemeAPIController.php b/app/Http/Controllers/API/FluidbookThemeAPIController.php index 5dfbd4ba4..035e255ff 100644 --- a/app/Http/Controllers/API/FluidbookThemeAPIController.php +++ b/app/Http/Controllers/API/FluidbookThemeAPIController.php @@ -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 */ diff --git a/app/Jobs/UpdateWS2ThemeTable.php b/app/Jobs/UpdateWS2ThemeTable.php index 9c94a739d..8d951f696 100644 --- a/app/Jobs/UpdateWS2ThemeTable.php +++ b/app/Jobs/UpdateWS2ThemeTable.php @@ -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) diff --git a/app/Models/FluidbookTheme.php b/app/Models/FluidbookTheme.php index 2bba683eb..f31ef5b01 100644 --- a/app/Models/FluidbookTheme.php +++ b/app/Models/FluidbookTheme.php @@ -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'); } } -- 2.39.5