From: Vincent Vanwaelscappel Date: Thu, 8 Dec 2022 17:11:50 +0000 (+0100) Subject: wip #5634 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=fd789af5c6df4df481d084e112817de92f1677e9;p=fluidbook-toolbox.git wip #5634 @2 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php index 609de1634..a03aa2508 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/EditOperation.php @@ -18,6 +18,8 @@ trait EditOperation Route::match(['get'], $segment . '/{id}/edit/links', $controller . '@links'); Route::match(['get'], $segment . '/{id}/edit/links/versions', $controller . '@getLinkVersions'); Route::match(['get'], $segment . '/{id}/edit/links/versions/export/{version}', $controller . '@exportLinks'); + Route::match(['post'], $segment . '/{id}/edit/links/import/merge', $controller . '@importLinksMerge'); + Route::match(['post'], $segment . '/{id}/edit/links/import/replace', $controller . '@importLinks'); Route::match(['get'], $segment . '/{id}/edit/links/versions/restore/{version}', $controller . '@restoreLinks'); Route::put($segment . '/{id}/save/links', $controller . '@saveLinks'); Route::get($segment . '/{id}/edit/{type}_{page}.{format}', $controller . '@getLinkPage') @@ -100,6 +102,42 @@ trait EditOperation FluidbookLinks::saveLinksInFile($fluidbook_id, backpack_user()->id, $comments, $links, $rulers, [], []); } + + protected function importLinks($fluidbook_id) + { + if (!FluidbookPublication::hasPermission($fluidbook_id)) { + abort(401); + } + FluidbookLinks::getLinksAndRulersFromExcelFile(request()->file('file')->getPathname(), $links, $rulers); + FluidbookLinks::saveLinksInFile($fluidbook_id, backpack_user()->id, 'Replace links from excel', $links, $rulers, [], []); + } + + protected function importLinksMerge($fluidbook_id) + { + if (!FluidbookPublication::hasPermission($fluidbook_id)) { + abort(401); + } + + FluidbookLinks::getLinksAndRulers($fluidbook_id, $merged_links, $merged_rulers); + FluidbookLinks::getLinksAndRulersFromExcelFile(request()->file('file')->getPathname(), $links, $rulers); + $existing_uids = []; + foreach ($merged_links as $merged_link) { + $existing_uids[$merged_link['uid']] = true; + } + + foreach ($links as $link) { + if (isset($existing_uids[$link['uid']])) { + $link['uid'] = FluidbookLinks::generateUID(); + $existing_uids[$link['uid']] = true; + } + $merged_links[] = $link; + } + + $merged_rulers = array_merge($merged_rulers, $rulers); + + FluidbookLinks::saveLinksInFile($fluidbook_id, backpack_user()->id, 'Merge existing links with excel', $merged_links, $merged_rulers, [], []); + } + // protected function getThumb($doc_id, $doc_page) // { // diff --git a/app/Util/FluidbookLinks.php b/app/Util/FluidbookLinks.php index 01ad6a19a..430f99223 100644 --- a/app/Util/FluidbookLinks.php +++ b/app/Util/FluidbookLinks.php @@ -2,6 +2,7 @@ namespace App\Util; +use App\Models\FluidbookPublication; use App\Models\User; use Cubist\Util\ArrayUtil; use Cubist\Util\Crypt; @@ -536,4 +537,21 @@ class FluidbookLinks self::saveLinksInFile($book_id, $core->user->utilisateur_id, 'Links imported from PDF', $links, []); } + + public static function getLinksAndRulersFromExcelFile($path, &$links, &$rulers) + { + $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); + $xls = $reader->load($path); + FluidbookLinks::getLinksFromExcel($xls, $links, $rulers); + } + + public static function generateUID() + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; + $randstring = ''; + for ($i = 0; $i < 12; $i++) { + $randstring = $characters[rand(0, 35)]; + } + return $randstring; + } } diff --git a/resources/linkeditor/js/linkeditor.save.js b/resources/linkeditor/js/linkeditor.save.js index 0f0353b99..07e7d3b8e 100644 --- a/resources/linkeditor/js/linkeditor.save.js +++ b/resources/linkeditor/js/linkeditor.save.js @@ -30,6 +30,18 @@ LinkeditorSave.prototype = { } }, + saveIfUnsavedChanges: function (message, notify, callback) { + if (this.unsavedChanges) { + this.save(message, false, function () { + setTimeout(function () { + callback(); + }, 1000); + }); + } else { + callback(); + } + }, + save: function (message, notify, callback) { if (notify === undefined) { notify = true; diff --git a/resources/linkeditor/js/linkeditor.toolbar.js b/resources/linkeditor/js/linkeditor.toolbar.js index b85291a51..38deaf197 100644 --- a/resources/linkeditor/js/linkeditor.toolbar.js +++ b/resources/linkeditor/js/linkeditor.toolbar.js @@ -29,6 +29,22 @@ LinkeditorToolbar.prototype = { return false; }); + $(document).on('change', '.importExcel input', function () { + var e = $(this); + var callback = function () { + var form = $(e).closest('form'); + var f = $(form).ajaxSubmit({dataType: 'json'}); + var xhr = f.data('jqxhr'); + xhr.done(function (data) { + setTimeout(function () { + console.log('reload'); + window.location.reload(); + }, 1000); + }); + } + $this.linkeditor.save.saveIfUnsavedChanges('Sauvegarde before excel import', false, callback); + }); + this.linkeditor.initTooltips(); }, }; diff --git a/resources/linkeditor/js/linkeditor.versions.js b/resources/linkeditor/js/linkeditor.versions.js index 9804e060d..1050ddc23 100644 --- a/resources/linkeditor/js/linkeditor.versions.js +++ b/resources/linkeditor/js/linkeditor.versions.js @@ -52,16 +52,7 @@ LinkeditorVersions.prototype = { } //Restore links from 2022-12-07 13:37:15 - if (this.linkeditor.save.unsavedChanges) { - console.log('save save save'); - this.linkeditor.save.save('Save before restoring links', false, function () { - setTimeout(function () { - callback(); - }, 1000); - }); - } else { - callback(); - } + this.linkeditor.save.saveIfUnsavedChanges('Save before restoring links',false,callback); }, _restoreVersion(timestamp) { diff --git a/resources/linkeditor/style/inc/_toolbar.sass b/resources/linkeditor/style/inc/_toolbar.sass index c0a389937..e99453e9e 100644 --- a/resources/linkeditor/style/inc/_toolbar.sass +++ b/resources/linkeditor/style/inc/_toolbar.sass @@ -31,6 +31,21 @@ margin: 0 2px border-left: 1px solid $toolbar-color + div[data-icon] + position: relative + cursor: pointer + + form.importExcel + input + position: absolute + opacity: 0 + width: 100% + height: 100% + z-index: 1 + cursor: pointer + top: 0 + left: 0 + #linkeditor-page-field padding: 2px 10px background-color: #fff diff --git a/resources/views/fluidbook_publication/link_editor.blade.php b/resources/views/fluidbook_publication/link_editor.blade.php index 7fc382f33..0e62a435e 100644 --- a/resources/views/fluidbook_publication/link_editor.blade.php +++ b/resources/views/fluidbook_publication/link_editor.blade.php @@ -71,10 +71,26 @@
- - +
+
+ +
+
+
+
+ +
+