From ee45143f430e60a8c434fea8a2ce2d5871457594 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 15 Sep 2022 18:19:24 +0200 Subject: [PATCH] wip #5457 @1.5 --- .../FluidbookPublication/EditOperation.php | 19 ++++++++- app/Models/Base/ToolboxModel.php | 27 ++++++++++++ app/Util/FluidbookLinks.php | 41 +++++++++++-------- .../fluidbook/toolbox/js/linkeditor.js | 26 +++++++++++- .../link_editor.blade.php | 11 ++++- 5 files changed, 102 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php index d725d89a7..bfb191961 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php @@ -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 */ diff --git a/app/Models/Base/ToolboxModel.php b/app/Models/Base/ToolboxModel.php index 61204fcc8..84b7c2ec7 100644 --- a/app/Models/Base/ToolboxModel.php +++ b/app/Models/Base/ToolboxModel.php @@ -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 = [ diff --git a/app/Util/FluidbookLinks.php b/app/Util/FluidbookLinks.php index 23569a361..1994a34c1 100644 --- a/app/Util/FluidbookLinks.php +++ b/app/Util/FluidbookLinks.php @@ -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; diff --git a/public/packages/fluidbook/toolbox/js/linkeditor.js b/public/packages/fluidbook/toolbox/js/linkeditor.js index 42238ae5b..cf6b69bd9 100644 --- a/public/packages/fluidbook/toolbox/js/linkeditor.js +++ b/public/packages/fluidbook/toolbox/js/linkeditor.js @@ -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 = $('
'); if (pos === undefined) { @@ -232,6 +249,11 @@ function deleteRuler(ruler) { delete RULERS[uid]; $(ruler).remove(); movingRuler = null; + hasChanged(); +} + +function hasChanged() { + unsavedChanges = true; } function moveRuler() { diff --git a/resources/views/fluidbook_publication/link_editor.blade.php b/resources/views/fluidbook_publication/link_editor.blade.php index 23b0f0344..4586610a7 100644 --- a/resources/views/fluidbook_publication/link_editor.blade.php +++ b/resources/views/fluidbook_publication/link_editor.blade.php @@ -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')
@@ -70,6 +76,7 @@ @push('after_scripts')