From 96d51a82bf4aa01d3753cd2c5fd6e1d91df29e60 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 14 Jun 2023 11:43:26 +0200 Subject: [PATCH] wip #6014 @0.5 --- app/Fluidbook/Compiler/Links.php | 19 +++++++++- app/Fluidbook/Farm.php | 12 +++++- app/Fluidbook/Link/LinksData.php | 15 ++++++-- app/Models/FluidbookPublication.php | 46 ++++++++++++++++++++++- app/Models/Traits/PublicationSettings.php | 41 ++++++++------------ 5 files changed, 98 insertions(+), 35 deletions(-) diff --git a/app/Fluidbook/Compiler/Links.php b/app/Fluidbook/Compiler/Links.php index ecc5b7215..c7085fca1 100644 --- a/app/Fluidbook/Compiler/Links.php +++ b/app/Fluidbook/Compiler/Links.php @@ -131,6 +131,20 @@ trait Links $anchorExists = []; $closedLinks = []; + $anchorsFromExcel = $this->_fluidbook->getAnchorsFromExcel(); + foreach ($anchorsFromExcel as $name => $page) { + $links[] = [ + 'page' => $page, + 'top' => 0, + 'left' => 0, + 'width' => 10, + 'height' => 10, + 'type' => AnchorLink::class, + 'to' => $name, + 'uid' => LinksData::generateUID() + ]; + } + $linksCopy = $links; @@ -219,7 +233,7 @@ trait Links $ids = explode(',', $linkData['to']); $close = ($linkData['close_button'] && $linkData['close_button'] !== 'none'); foreach ($ids as $id) { - $id = trim($id,'+- '); + $id = trim($id, '+- '); if ($id === 'tabs') { $this->config->tabsHiddenAtStartup = true; } else { @@ -234,6 +248,7 @@ trait Links } } + if ($this->fluidbookSettings->anchorsAliases && file_exists($this->fluidbookSettings->anchorsAliases)) { $aliases = []; for ($i = 0; $i <= 2; $i++) { @@ -250,7 +265,7 @@ trait Links 'left' => 0, 'width' => 100, 'height' => 100, - 'type' => 26, + 'type' => AnchorLink::class, 'to' => $from, 'uid' => LinksData::generateUID() ]; diff --git a/app/Fluidbook/Farm.php b/app/Fluidbook/Farm.php index ef9c820c8..e2f500294 100644 --- a/app/Fluidbook/Farm.php +++ b/app/Fluidbook/Farm.php @@ -137,12 +137,16 @@ class Farm Cache::forget($cachekey); } - return Cache::rememberForever($cachekey, function () use ($params) { + $res = Cache::rememberForever($cachekey, function () use ($params) { return self::_getFile($params); }); + if (!$res) { + Cache::forget($cachekey); + } + return $res; } - protected static function _getFile($params) + protected static function _getFile($params, $attempts = 3) { $start = microtime(true); $farmer = self::pickOneServer(); @@ -167,6 +171,10 @@ class Farm error_log($log); + if (!$res && $attempts > 0) { + return static::_getFile($params, $attempts - 1); + } + return $res; } diff --git a/app/Fluidbook/Link/LinksData.php b/app/Fluidbook/Link/LinksData.php index ea2d137a2..f529730ab 100644 --- a/app/Fluidbook/Link/LinksData.php +++ b/app/Fluidbook/Link/LinksData.php @@ -507,14 +507,21 @@ class LinksData protected static function getName($u) { - if (!isset(self::$_names[$u])) { + if (is_array($u)) { + if (isset($u['firstname'])) { + return $u['firstname'] . ' ' . $u['lastname']; + } else { + return '-'; + } + } + if (!isset(static::$_names[$u])) { try { - self::$_names[$u] = User::find($u)->name; + static::$_names[$u] = User::find($u)->name; } catch (\Exception $e) { - self::$_names[$u] = '-'; + static::$_names[$u] = '-'; } } - return self::$_names[$u]; + return static::$_names[$u]; } public static function getMeta($book_id, $update = 'latest') diff --git a/app/Models/FluidbookPublication.php b/app/Models/FluidbookPublication.php index 8b2003c55..af9129584 100644 --- a/app/Models/FluidbookPublication.php +++ b/app/Models/FluidbookPublication.php @@ -39,6 +39,8 @@ use Cubist\Backpack\Magic\Fields\Hidden; use Cubist\Backpack\Magic\Fields\Integer; use Cubist\Backpack\Magic\Fields\SelectFromArray; use Cubist\Backpack\Magic\Operations\CreateOperation; +use Cubist\Excel\Excel; +use Cubist\Excel\ExcelToArray; use Cubist\Util\ArrayUtil; use Cubist\Util\Files\Files; use Cubist\Util\Graphics\Image; @@ -789,6 +791,7 @@ class FluidbookPublication extends ToolboxSettingsModel for ($i = 1; $i <= $this->getPagesNumber(); $i++) { $res[$i] = []; } + // Get anchors from links $this->getLinksAndRulers($links, $rulers); foreach ($links as $link) { if ($link['type'] !== Link::ANCHOR && $link['type'] !== Link::PAGE_LABEL) { @@ -799,10 +802,49 @@ class FluidbookPublication extends ToolboxSettingsModel } $res[$link['page']][] = $link['to']; } + // Get anchors from Excel file + $anchors = $this->getAnchorsFromExcel(); + foreach ($anchors as $anchor => $page) { + if (!isset($res[$page])) { + continue; + } + $res[$page][] = $anchor; + } return $res; } - public function getPreviewURL(){ - return route('fluidbook_preview',['version'=>'online','id'=>$this->id,'hash'=>$this->hash]); + public function getAnchorsFromExcel() + { + $res = []; + if (!$this->anchors) { + return $res; + } + $file = $this->getAssetDir() . '/' . $this->anchors; + if (!file_exists($file)) { + return $res; + } + + ExcelToArray::setCache(protected_path('fluidbookpublication/cache/exceltoarray')); + $contents = ExcelToArray::excelToArrayFirstSheet($file); + + foreach ($contents as $row) { + $page = trim($row[0]); + if (!is_numeric($page)) { + continue; + } + $page = (int)$page; + $name = trim($row[1]); + if ($page >= 0 && $page <= $this->getPagesNumber()) { + $res[$name] = $page; + } + } + return $res; + + + } + + public function getPreviewURL() + { + return route('fluidbook_preview', ['version' => 'online', 'id' => $this->id, 'hash' => $this->hash]); } } diff --git a/app/Models/Traits/PublicationSettings.php b/app/Models/Traits/PublicationSettings.php index e9bffbb91..bf89bd082 100644 --- a/app/Models/Traits/PublicationSettings.php +++ b/app/Models/Traits/PublicationSettings.php @@ -114,6 +114,7 @@ trait PublicationSettings $this->_audioplayer(); $this->_downloadPortions(); $this->_articles(); + $this->_anchors(); $this->_assets(); } @@ -266,11 +267,7 @@ trait PublicationSettings ]); $this->addSettingField('facebook_image', FilesOrURL::class, $this->__('Miniature affichée'), [ 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Miniature affich\\u00e9e!\\u00a7!","fileFilter":{"name":"\\u00a7!\\u00a7Images!\\u00a7! (*.jpg, *.png)","extensions":"*.jpg;*.jpeg;*.png"}}', - 'accept' => [ - 0 => '.jpg', - 1 => '.jpeg', - 2 => '.png', - ], + 'accept' => self::$acceptImages, ]); $this->addSettingField('twitter_description', Textarea::class, $this->__('Contenu Partage court'), [ 'v2' => '{"type":"textarea","default":"%title% : %short%","editable":true,"label":"\\u00a7!\\u00a7Contenu Partage court!\\u00a7!","hint":"\\u00a7!\\u00a7Contenu du partag\\u00e9 sur les partages courts!\\u00a7!"}', @@ -1491,11 +1488,7 @@ trait PublicationSettings $this->addSettingField('section_splash', FormSection::class, $this->__('Ecran de chargement')); $this->addSettingField('splashImage', FilesOrURL::class, $this->__('Image'), [ 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Image!\\u00a7!","grade":3,"fileFilter":{"name":"\\u00a7!\\u00a7Images!\\u00a7! (*.jpg, *.png)","extensions":"*.jpg;*.jpeg;*.png"}}', - 'accept' => [ - 0 => '.jpg', - 1 => '.jpeg', - 2 => '.png', - ], + 'accept' => self::$acceptImages, ]); $this->addSettingField('splashURL', LongText::class, $this->__('URL'), [ 'v2' => '{"type":"text","default":"","editable":true,"label":"\\u00a7!\\u00a7URL!\\u00a7!","grade":3}', @@ -1568,16 +1561,22 @@ trait PublicationSettings } + protected function _anchors() + { + $this->addSettingField('section_anchors', FormSection::class, $this->__('Ancres')); + $this->addSettingField('anchors', FilesOrURL::class, $this->__('Définir les ancres'), [ + 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Ancres!\\u00a7!","fileFilter":{"name":"\\u00a7!\\u00a7Fichier Excel!\\u00a7! (.xlsx)","extensions":"*.xlsx"}}', + 'accept' => self::$acceptXLSX, + 'hint' => __('Colonne A : numéro de page physique, Colonne B : nom de l\'ancre') + ]); + } + protected function _archives() { $this->addSettingField('section_archives', FormSection::class, $this->__('Archives')); $this->addSettingField('externalArchives', FilesOrURL::class, $this->__('Archives'), [ 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Archives!\\u00a7!","grade":3,"fileFilter":{"name":"\\u00a7!\\u00a7Images!\\u00a7! (*.jpg, *.png)","extensions":"*.jpg;*.jpeg;*.png"}}', - 'accept' => [ - 0 => '.jpg', - 1 => '.jpeg', - 2 => '.png', - ], + 'accept' => self::$acceptImages, ]); $this->addSettingField('archivesLabel', LongText::class, $this->__('Label'), [ 'v2' => '{"type":"text","default":"","editable":true,"label":"\\u00a7!\\u00a7Label!\\u00a7!","grade":5}', @@ -1682,20 +1681,12 @@ trait PublicationSettings $this->addSettingField('', FormSeparator::class); $this->addSettingField('cartHeaderImage', FilesOrURL::class, $this->__('Header panier'), [ 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Header panier!\\u00a7!","grade":3,"fileFilter":{"name":"\\u00a7!\\u00a7Images!\\u00a7! (*.jpg, *.png)","extensions":"*.jpg;*.jpeg;*.png"},"dir":"commerce"}', - 'accept' => [ - 0 => '.jpg', - 1 => '.jpeg', - 2 => '.png', - ], + 'accept' => self::$acceptImages, 'destination' => 'commerce', ]); $this->addSettingField('cartHeaderMobileImage', FilesOrURL::class, $this->__('Header panier (mobile)'), [ 'v2' => '{"type":"freefile","default":"","editable":true,"label":"\\u00a7!\\u00a7Header panier (mobile)!\\u00a7!","grade":3,"fileFilter":{"name":"\\u00a7!\\u00a7Images!\\u00a7! (*.jpg, *.png)","extensions":"*.jpg;*.jpeg;*.png"},"dir":"commerce"}', - 'accept' => [ - 0 => '.jpg', - 1 => '.jpeg', - 2 => '.png', - ], + 'accept' => self::$acceptImages, 'destination' => 'commerce', ]); $this->addSettingField('cartExtraSettings', Textarea::class, $this->__('Paramètres panier'), [ -- 2.39.5