]> _ Git - odl.git/commitdiff
wip #4666 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Sep 2021 15:23:59 +0000 (17:23 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Sep 2021 15:23:59 +0000 (17:23 +0200)
app/Http/Controllers/Tools/FluidbookPreview.php
app/Jobs/ProcessFluidbook.php
app/Models/Publication.php [new file with mode: 0644]
resources/views/vendor/backpack/base/inc/sidebar_content.blade.php

index 319b59d5c20246e035830f26a467d235fec8b119..c699cec4c39fbc296da9d3e142475ec053a9c70a 100644 (file)
@@ -11,6 +11,9 @@ trait FluidbookPreview
     {
         if (count($args) > 0) {
             $path = implode('/', $args);
+        }else{
+            return redirect(backpack_url('tools/fluidbookpreview/index.html'));
+
         }
         $path = storage_path('fluidbook/out') . '/' . $path;
         if(file_exists($path)){
index 742b5147f134308294e51e9b77cfb4dd567a7b83..2eb32da0b5ebb03919629c7721da165faf51983f 100644 (file)
@@ -2,12 +2,13 @@
 
 namespace App\Jobs;
 
-use Cubist\Util\CommandLine;
+use App\Models\Publication;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Files\VirtualDirectory;
 use Cubist\Util\PHP;
-use Fluidbook\Tools\FluidbookTools;
+use Exception;
 use Fluidbook\Tools\Jobs\ProcessFile;
+use Fluidbook\Tools\Jobs\ProcessPage;
 use Fluidbook\Tools\PDF\Document;
 use Fluidbook\Tools\Search\SearchIndex;
 use Illuminate\Bus\Queueable;
@@ -18,6 +19,8 @@ use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\Jobs\SyncJob;
 use Illuminate\Queue\SerializesModels;
 use JsonException;
+use Spatie\MediaLibrary\MediaCollections\Models\Media;
+use stdClass;
 
 class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
 {
@@ -27,12 +30,31 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
      * @var Document
      */
     protected $in;
+
+    /**
+     * @var string
+     */
     protected $out;
+
+    /**
+     * @var string
+     */
     protected $stub;
 
+    /**
+     * @var Publication
+     */
+    protected $pub;
+
+
     /** @var VirtualDirectory */
     protected $vdir;
 
+    /**
+     * @var stdClass
+     */
+    protected $config;
+
     /**
      * Create a new job instance.
      *
@@ -40,7 +62,13 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
      */
     public function __construct()
     {
-        $this->in = new Document(storage_path('fluidbook/in/in.pdf'));
+
+        $this->pub = Publication::find(1);
+
+        /** @var Media $media */
+        $media = $this->pub->getMediaInField($this->pub->getAttributeValue('document'))->first();
+
+        $this->in = new Document($media->getPath());
         $this->out = storage_path('fluidbook/out/');
 
         $this->stub = resource_path('fluidbook/');
@@ -71,13 +99,41 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
         start_measure('Process Pages');
         $out = $this->getConvertPath();
         Files::mkdir($out);
-        $this->in->processPages($out, [new ProcessFile('jpg', 'thumb'),
-            new ProcessFile('jpg', 150, true, false),
-            new ProcessFile('svg', 300, false, true),
-        ], $sync);
+        $this->in->processPages($out, $this->getProcessFiles(), $sync);
         stop_measure('Process Pages');
     }
 
+    /**
+     * @return ProcessFile[]
+     */
+    public function getProcessFiles()
+    {
+        return [
+            'thumb' => new ProcessFile('jpg', 'thumb'),
+            'back150' => new ProcessFile('jpg', 150, true, false),
+            'text' => new ProcessFile('svg', 300, false, true),
+        ];
+    }
+
+    /**
+     * @param $name
+     * @param null|int $page
+     * @return ProcessFile
+     * @throws Exception
+     */
+    public function getProcessFile($name, $page = null)
+    {
+        $files = $this->getProcessFiles();
+        if (isset($files[$name])) {
+            $res = $files[$name];
+            if (null !== $page) {
+                $res->setJob(new ProcessPage($this->in, $page, $this->getConvertPath()));
+            }
+            return $res;
+        }
+        throw new Exception(sprintf('File %s does not exist', $name));
+    }
+
     public function getConvertPath()
     {
         return storage_path('fluidbook/convert/' . $this->in->getHash() . '/');
@@ -97,6 +153,9 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
         start_measure('Process links');
     }
 
+    /**
+     * @throws JsonException
+     */
     protected function compileFluidbook()
     {
         start_measure('Compile fluidbook');
@@ -115,11 +174,11 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
 
     public function compileContents()
     {
-        $cp = $this->getConvertPath();
         for ($i = 1; $i <= $this->in->getPages(); $i++) {
-            $this->vdir->copy($cp . 'html/t' . $i . '-150.jpg', 'data/background/150/p' . $i . '.jpg');
-            $this->vdir->copy($cp . 'html/to' . $i . '.svg', 'data/contents/p' . $i . '.svg');
-            $this->vdir->copy($cp . 'html/p' . $i . '.jpg', 'data/thumbnails/p' . $i . '.jpg');
+
+            $this->vdir->copy($this->getProcessFile('back150', $i)->getPath(), 'data/background/150/p' . $i . '.jpg');
+            $this->vdir->copy($this->getProcessFile('text', $i)->getPath(), 'data/contents/p' . $i . '.svg');
+            $this->vdir->copy($this->getProcessFile('thumb', $i)->getPath(), 'data/thumbnails/p' . $i . '.jpg');
         }
     }
 
@@ -145,20 +204,57 @@ class ProcessFluidbook implements ShouldQueue, ShouldBeUnique
     public function compileConfig()
     {
         $settings = mb_substr(file_get_contents($this->stub . '/data/datas.js'), 13, -2);
-        $config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR);
+        $this->config = json_decode($settings, false, 512, JSON_THROW_ON_ERROR);
+
+        $this->compileDimensions();
+        $this->compilePageNumbers();
+        $this->compileChapters();
+        $this->config->links = $this->compileLinks();
+
+        $c = 'var SETTINGS=' . json_encode($this->config, JSON_THROW_ON_ERROR) . ';';
+        $this->vdir->file_put_contents('data/datas.js', $c);
+    }
+
+    protected function compileDimensions()
+    {
+        $width = round($this->in->getWidth(), 8);
+        $height = round($this->in->getHeight(), 8);
 
-        $w = $this->in->getWidth();
-        $h = $this->in->getHeight();
+        $this->config->pageZoomFactor = 3;
+        $this->config->optimalWidth = 567;
+        $this->config->optimalHeight = 709;
+        $imagesize = getimagesize($this->getProcessFile('back150', 1)->getPath());
+        $this->config->pdfZoomFactor = round(($imagesize[0] * 0.48) / $width, 12);
+
+        $this->config->cssScale = $this->config->pageZoomFactor * min($this->config->optimalWidth / $width, $this->config->optimalHeight / $height);
+        $cssWidth = $width * $this->config->cssScale;
+        $cssHeight = $height * $this->config->cssScale;
+
+        $this->config->pages = $this->in->getPages();
+        $this->config->width = $cssWidth;
+        $this->config->height = $cssHeight;
+
+        $this->config->multiply = $this->config->pdfZoomFactor * $this->config->cssScale;
+
+        $this->config->pagesDimensions = [];
+
+        $thumbDimensions = getimagesize($this->getProcessFile('thumb', 1)->getPath());
+        $this->config->thumbWidth = $thumbDimensions[0];
+        $this->config->thumbHeight = $thumbDimensions[1];
 
-        $config->pages = $this->in->getPages();
-        $config->width = $w;
-        $config->height = $h;
-        $config->pagesDimensions = [];
         for ($i = 1; $i <= $this->in->getPages(); $i++) {
-            $config->pagesDimensions[$i] = [$w, $h];
+            $this->config->pagesDimensions[$i] = [$this->config->width, $this->config->height];
         }
-        $config->links = $this->compileLinks();
-        $this->vdir->file_put_contents('data/datas.js', 'var SETTINGS=' . json_encode($config, JSON_THROW_ON_ERROR) . ';');
+    }
+
+    protected function compilePageNumbers()
+    {
+        $this->config->numerotation = $this->in->getPageNumbers();
+    }
+
+    protected function compileChapters()
+    {
+        $this->config->chapters = $this->in->getChapters();
     }
 
 
diff --git a/app/Models/Publication.php b/app/Models/Publication.php
new file mode 100644 (file)
index 0000000..7722190
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Models;
+
+use App\Jobs\ProcessFluidbook;
+use Cubist\Backpack\Magic\Fields\Files;
+use Cubist\Backpack\Magic\Fields\FormSection;
+use Cubist\Backpack\Magic\Fields\Text;
+use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
+
+class Publication extends CubistMagicAbstractModel
+{
+    protected $table = 'publications';
+    protected $_options = ['name' => 'publications',
+        'singular' => 'publication',
+        'plural' => 'publications',
+        'oneinstance' => true];
+
+    public function setFields()
+    {
+        parent::setFields();
+
+        $this->addField('document', Files::class, 'Document');
+        $this->addField('s_download', FormSection::class, 'Téléchargement et impression');
+        $this->addField("download_name", Text::class, 'Nom du fichier', ['default' => 'document.pdf']);
+        $this->addField('pdf_download', Files::class, 'PDF pour le téléchargement et l\'impression');
+    }
+
+    public function postSave()
+    {
+        ProcessFluidbook::dispatch();
+    }
+}
index 71d7efcf7c363d04bc1bb639b68f8fa996e8c819..2a002b019468825230de645b3c2d012975a855b7 100644 (file)
@@ -4,8 +4,10 @@
 
 @can('cms')
     <li class='nav-item nav-dropdown open'><a class='nav-link nav-dropdown-toggle' href='#'><i
-                class='nav-icon la la-edit'></i>Mini-site</a>
+                class='nav-icon la la-edit'></i>Edition des contenus</a>
         <ul class='nav-dropdown-items'>
+            <li class='nav-item'><a class='nav-link' href='{{ backpack_url('publications') }}'><i class='la la-book-open'></i>
+                    <span>Publication</span></a></li>
             <li class='nav-item'><a class='nav-link' href='{{ backpack_url('page') }}'><i class='la la-file-o'></i>
                     <span>Pages</span></a></li>
             <li class='nav-item'><a class='nav-link' href='{{ backpack_url('settings') }}'><i class='la la-cog'></i>