]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5457 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Sep 2022 16:19:24 +0000 (18:19 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 15 Sep 2022 16:19:24 +0000 (18:19 +0200)
app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php
app/Models/Base/ToolboxModel.php
app/Util/FluidbookLinks.php
public/packages/fluidbook/toolbox/js/linkeditor.js
resources/views/fluidbook_publication/link_editor.blade.php

index d725d89a7142fade88b8280e0f1cbc6f9ec3b07a..bfb19196192a2b06b741ade100489dbae6f416fb 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin\Operations\FluidbookPublication;
 
 use App\Models\FluidbookDocument;
 use App\Models\FluidbookPublication;
+use App\Util\FluidbookLinks;
 use Cubist\Backpack\Http\Controllers\Base\XSendFileController;
 use Illuminate\Support\Facades\Route;
 
@@ -13,13 +14,27 @@ trait EditOperation
     protected function setupEditRoutes($segment, $routeName, $controller)
     {
         Route::match(['get'], $segment . '/{id}/edit/links', $controller . '@links');
+        Route::put($segment . '/{id}/save/links', $controller . '@saveLinks');
         Route::get($segment . '/{id}/edit/{type}_{page}.{format}', $controller . '@getLinkPage')
-           // ->whereIn('type', ['raster', 'images', 'texts', 'vector'])
+            // ->whereIn('type', ['raster', 'images', 'texts', 'vector'])
             ->whereNumber('page');
-            //->whereIn('format', ['jpg', 'png', 'avif', 'webp', 'svg']);
+        //->whereIn('format', ['jpg', 'png', 'avif', 'webp', 'svg']);
     }
 
 
+    protected function saveLinks($fluidbook_id)
+    {
+        if (!FluidbookPublication::hasPermission($fluidbook_id)) {
+            abort(401);
+        }
+        FluidbookLinks::saveLinksInFile($fluidbook_id,
+            backpack_user()->id,
+            request('message'),
+            request('links', []),
+            request('rulers', [])
+        );
+    }
+
     protected function getLinkPage($fluidbook_id, $type, $page, $format = 'jpg')
     {
         /** @var FluidbookPublication $fluibdook */
index 61204fcc8e54e65dddf1538d1e8bdd1295b0e63e..84b7c2ec7cfb12361d42ba2db121fea101ac946b 100644 (file)
@@ -27,6 +27,33 @@ class ToolboxModel extends CubistMagicAbstractModel
         $builder->whereIn(static::$_ownerAttribute, backpack_user()->getManagedUsers());
     }
 
+    /**
+     * @param $id
+     * @param $permission
+     * @param $user
+     * @return bool
+     */
+    public static function hasPermission($id, $permission = 'write', $user = null)
+    {
+        if (null === $user) {
+            $user = Auth::user();
+        }
+        if (null === $user) {
+            return false;
+        }
+        if ($user->hasPermissionTo(static::$_permissionBase . ':admin')) {
+            return true;
+        }
+        if (!$user->hasPermissionTo(static::$_permissionBase . ':' . $permission)) {
+            return false;
+        }
+        $entry = static::find($id);
+        if (null === $entry) {
+            return false;
+        }
+        return true;
+    }
+
     public function addOwnerField($tab = null)
     {
         $fieldSettings = [
index 23569a361505d4f0e39724410f378b1e1a8ebb98..1994a34c13444aafd3c2c928c195a218803c91a9 100644 (file)
@@ -7,6 +7,11 @@ use Cubist\Util\Crypt;
 use Cubist\Util\Files\Files;
 use Cubist\Util\ObjectUtil;
 use Cubist\Util\Str;
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
+use PhpOffice\PhpSpreadsheet\Exception;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
+use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
 use SodiumException;
 
 class FluidbookLinks
@@ -14,6 +19,9 @@ class FluidbookLinks
     protected static $_testLinkCache = null;
     protected static $_linksKey = null;
 
+    /**
+     * @throws Exception
+     */
     public static function linksToExcel($links, $rulers, $pages = null)
     {
         set_time_limit(0);
@@ -38,7 +46,7 @@ class FluidbookLinks
 
         $comments = array();
 
-        $xls = new PHPExcel();
+        $xls = new Spreadsheet();
         $s = $xls->setActiveSheetIndex(0);
         $s->setTitle('Links');
 
@@ -47,7 +55,7 @@ class FluidbookLinks
         foreach ($cols as $id => $label) {
             $s->setCellValueByColumnAndRow($i, 1, $id);
             $s->getColumnDimensionByColumn($i)->setAutoSize(true);
-            $s->getStyleByColumnAndRow($i, 1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
+            $s->getStyleByColumnAndRow($i, 1)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
             $i++;
         }
 
@@ -76,7 +84,7 @@ class FluidbookLinks
                             }
                         }
                         if ($id === 'to') {
-                            $s->getCellByColumnAndRow($i, $j)->setDataType(PHPExcel_Cell_DataType::TYPE_STRING)->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
+                            $s->getCellByColumnAndRow($i, $j)->setDataType(DataType::TYPE_STRING)->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
                         }
                         $value = $l[$id];
                     } else {
@@ -84,7 +92,7 @@ class FluidbookLinks
                     }
                 }
 
-                $s->setCellValueExplicitByColumnAndRow($i, $j, $value);
+                $s->setCellValueExplicitByColumnAndRow($i, $j, $value, DataType::TYPE_STRING);
                 $i++;
             }
             $j++;
@@ -114,7 +122,7 @@ class FluidbookLinks
                     $value = $r[$id];
                 }
                 $s->setCellValueByColumnAndRow($i, $j, $value);
-                $s->getStyleByColumnAndRow($i, $j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
+                $s->getStyleByColumnAndRow($i, $j)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
                 $i++;
             }
             $j++;
@@ -132,12 +140,12 @@ class FluidbookLinks
         }
         $dir = self::getLinksDir($book_id);
 
-        $file = $dir . '/' . $time . '.links.gz';
+        $file = $dir . '/' . $time . '.links3.gz';
         if ($time === 'latest' && !file_exists($file)) {
             $versions = self::getLinksVersions($book_id);
             foreach ($versions as $version => $m) {
-                copy($dir . '/' . $version . '.links.gz', $dir . '/latest.links.gz');
-                copy($dir . '/' . $version . '.meta.gz', $dir . '/latest.meta.gz');
+                copy(Files::firstThatExists($dir . '/' . $version . '.links3.gz', $dir . '/' . $version . '.links.gz'), $dir . '/latest.links3.gz');
+                copy(Files::firstThatExists($dir . '/' . $version . '.meta3.gz', $dir . '/' . $version . '.meta.gz'), $dir . '/latest.meta3.gz');
                 break;
             }
         }
@@ -373,12 +381,12 @@ class FluidbookLinks
         $lr = self::mergeLinksAndRulers($links, $rulers, $specialLinks, $specialRulers);
         $meta = ['links' => count($lr['links']), 'rulers' => count($lr['rulers']), 'comments' => $comments, 'user' => $user_id];
         $base = self::getLinksDir($book_id) . '/' . time();
-        $latestLinks = self::getLinksDir($book_id) . '/latest.links.gz';
-        $latestMeta = self::getLinksDir($book_id) . '/latest.meta.gz';
-        file_put_contents($base . '.meta.gz', gzencode(json_encode($meta)));
-        file_put_contents($base . '.links.gz', gzencode(json_encode($lr)));
-        copy($base . '.links.gz', $latestLinks);
-        copy($base . '.meta.gz', $latestMeta);
+        $latestLinks = self::getLinksDir($book_id) . '/latest.links3.gz';
+        $latestMeta = self::getLinksDir($book_id) . '/latest.meta3.gz';
+        file_put_contents($base . '.meta3.gz', gzencode(json_encode($meta)));
+        file_put_contents($base . '.links3.gz', gzencode(json_encode($lr)));
+        copy($base . '.links3.gz', $latestLinks);
+        copy($base . '.meta3.gz', $latestMeta);
     }
 
 
@@ -397,7 +405,7 @@ class FluidbookLinks
                 continue;
             }
             $e = explode('.', $f, 2);
-            if ($e[1] !== 'meta.gz' || $e[0] === 'latest') {
+            if (($e[1] !== 'meta.gz' && $e[1] !== 'meta3.gz') || $e[0] === 'latest') {
                 continue;
             }
 
@@ -410,7 +418,7 @@ class FluidbookLinks
 
     public static function getMeta($book_id, $update = 'latest')
     {
-        return json_decode(gzdecode(file_get_contents(self::getLinksDir($book_id) . '/' . $update . '.meta.gz')), true);
+        return json_decode(gzdecode(file_get_contents(Files::firstThatExists(self::getLinksDir($book_id) . '/' . $update . '.meta3.gz', self::getLinksDir($book_id) . '/' . $update . '.meta.gz'))), true);
     }
 
     public static function mergeLinksAndRulers($links, $rulers, $specialLinks, $specialRulers)
@@ -468,6 +476,7 @@ class FluidbookLinks
                 if (!$line) {
                     break;
                 }
+
                 // Commentaire || ligne vide
                 if (substr($line[0], 0, 1) == '#' || is_null($line[0])) {
                     continue;
index 42238ae5b1de9a3aadd2450f88681ca0734fd67e..cf6b69bd90b2484d2d028f54b27b6fafae927acf 100644 (file)
@@ -166,8 +166,24 @@ function runAction(act) {
     window[act]();
 }
 
-function saveLinks() {
-
+function saveLinks(message) {
+    if (message === undefined) {
+        message = TRANSLATIONS.manual_save_message;
+    }
+    $.ajax({
+        url: '/fluidbook-publication/' + FLUIDBOOK_DATA.id + '/save/links', method: 'post', data: {
+            _method: 'put', 'message': message, rulers: window.RULERS, links: window.LINKS,
+        }, success: function (data) {
+            new Noty({
+                type: "success", text: TRANSLATIONS.success_save,
+            }).show();
+            unsavedChanges = false;
+        }, error: function (jqXHR, status, error) {
+            new Noty({
+                type: "error", text: TRANSLATIONS.error_save + ' : ' + error,
+            }).show();
+        },
+    });
 }
 
 function firstPage() {
@@ -212,6 +228,7 @@ function addRuler(axis, pos, uid) {
     if (undefined === uid) {
         uid = generateUID();
         RULERS[uid] = {page: currentPage, type: axis, uid: uid};
+        hasChanged();
     }
     var ruler = $('<div class="ruler" data-uid="' + uid + '" fb-ref="editor" data-axis="' + axis + '"></div>');
     if (pos === undefined) {
@@ -232,6 +249,11 @@ function deleteRuler(ruler) {
     delete RULERS[uid];
     $(ruler).remove();
     movingRuler = null;
+    hasChanged();
+}
+
+function hasChanged() {
+    unsavedChanges = true;
 }
 
 function moveRuler() {
index 23b0f03448af428af8072b0274bccbdf4bc49534..4586610a76020ec4d1601a252e8d6ed07247fb70 100644 (file)
@@ -6,12 +6,18 @@
     $fbdata['settings']['width']=$fbdata['width']=$fluidbook->getPageWidth();
     $fbdata['settings']['height']=$fbdata['height']=$fluidbook->getPageHeight();
     $fbdata['settings']['pages']=$fbdata['pages']=$fluidbook->getPagesNumber();
+    $translations=[
+        'success_save'=>__('Liens sauvegardés'),
+        'error_save'=>__('Une erreur s\'est produite lors de la sauvegarde des liens'),
+        'manual_save_message'=>__('Sauvegarde manuelle'),
+        'automatic_save_message'=>__('Sauvegarde automatique'),
+        'warning_unsaved_changes'=>__('Des données n\'ont pas été sauvegardées'),
+    ];
 @endphp
 @extends('layouts.empty')
 
-@include('fluidbook_publication.link_editor_icons')
-
 @section('content')
+    @include('fluidbook_publication.link_editor_icons')
     <div draggable="false" id="linkeditor">
         <aside id="linkeditor-left"></aside>
         <div id="linkeditor-main">
@@ -70,6 +76,7 @@
 
 @push('after_scripts')
     <script>
+        var TRANSLATIONS =@json($translations);
         var FLUIDBOOK_DATA = @json($fbdata);
         var LINKS =@json($links);
         var RULERS =@json($rulers);