use Cubist\Excel\ExcelToArray;
use Cubist\Util\Files\Files;
use Cubist\Util\Str;
+use Cubist\Util\WebVideo;
+use Fluidbook\Tools\Links\Link;
use Illuminate\Support\Facades\Route;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
{
Route::match(['get', 'post'], $segment . '/{id}/export_excel', $controller . '@exportExcel')->name("download_audit_links");
Route::match(['get', 'post'], $segment . '/{id}/import_excel', $controller . '@importExcel')->name("import_audit_links");
- Route::match(['get', 'post'], $segment . '/{cid}/import_excel_confirm', $controller . '@importExcel')->name("import_audit_links");
+ Route::match(['get', 'post'], $segment . '/{cid}/import_excel_confirm', $controller . '@importExcelConfirm')->name("import_audit_links_confirm");
+ }
+
+
+ protected function importExcelConfirm($cid)
+ {
+ $updates = cache()->get('auditlinks_confirmation_data_' . $cid);
+ $grouped = [];
+ foreach ($updates as $update) {
+ if (!isset($grouped[$update['fluidbook_id']])) {
+ $grouped[$update['fluidbook_id']] = [];
+ }
+ $grouped[$update['fluidbook_id']][$update['link_id']] = $update['new_url'];
+ }
+
+ foreach ($grouped as $fluidbook_id => $updates) {
+ LinksData::getLinksAndRulers($fluidbook_id, $links, $rulers);
+ foreach ($updates as $id => $url) {
+ $link = $links[$id];
+ if ($link['type'] == Link::WEBVIDEO) {
+ $link['to'] = WebVideo::_findVideoId($url, 'youtube');
+ } else {
+ $link['to'] = $url;
+ }
+ $links[$id] = $link;
+ }
+ LinksData::saveLinksInFile($fluidbook_id, backpack_user()->id, __('Mise à jour par un fichier d\'audit de liens'), $links, $rulers);
+ }
+
+ return ['alert' => ['icon' => 'success', 'title' => __('Mise à jour de liens'), 'message' => __(':nb liens mis à jour dans :fb publication', ['nb' => count($updates), 'fb' => count($grouped)])]];
}
protected function importExcel($id)
$updates = [];
$changedFluidbooks = [];
+ $notInFluidbooks = 0;
+ $noNewURL = 0;
+ $total = 0;
+
foreach ($data as $sheet) {
foreach ($sheet as $link) {
+ $total++;
$new_url = $link[$columns['new_url']];
if (!$new_url) {
+ $noNewURL++;
continue;
}
$fluidbook_id = $link[$columns['fluidbook_id']];
$link_id = $link[$columns['link_id']];
- if (in_array($fluidbook_id, $fluidbooks)) {
+ if (!in_array($fluidbook_id, $fluidbooks)) {
// not in the collection
+ $notInFluidbooks++;
continue;
}
$changedFluidbooks[] = $fluidbook_id;
}
$actualUpdates = [];
+ $notExistingAnymore = 0;
+ $sameValue = 0;
foreach ($updates as $u) {
if (!isset($allLinks[$u['fluidbook_id']][$u['link_id']])) {
// The links doesnt exists anymore
+ $notExistingAnymore++;
continue;
}
- if ($allLinks[$u['fluidbook_id']][$u['link_id']]['to'] == $u['new_url']) {
+
+ $l = $allLinks[$u['fluidbook_id']][$u['link_id']];
+ if ($l['to'] == $u['new_url']) {
+ // The links already has the "new url"
+ $sameValue++;
+ continue;
+ } else if ($l['type'] == Link::WEBVIDEO && $l['to'] == WebVideo::_findVideoId($u['new_url'], 'youtube')) {
// The links already has the "new url"
+ $sameValue++;
continue;
}
$actualUpdates[] = $u;
}
+ $details = [__('Parmi les :nb liens présents dans le fichier', ['nb' => '<b>' . $total . '</b>'])];
+ if ($noNewURL > 0) {
+ $details[] = __(':nb n\'ont pas de nouvelle URL à mettre à jour', ['nb' => '<b>' . $noNewURL . '</b>']);
+ }
+ if ($notInFluidbooks > 0) {
+ $details[] = __(':nb concernent des liens de fluidbooks qui n\'appartiennent pas à la collection', ['nb' => '<b>' . $notInFluidbooks . '</b>']);
+ }
+ if ($notExistingAnymore > 0) {
+ $details[] = __(':nb n\'existent plus', ['nb' => '<b>' . $notExistingAnymore . '</b>']);
+ }
+ if ($sameValue > 0) {
+ $details[] = __(':nb ont déjà la valeur indiquée dans le fichier', ['nb' => '<b>' . $sameValue . '</b>']);
+ }
+ if (count($details) === 1) {
+ $details = '';
+ } else if (count($details) === 2) {
+ $details = implode(', ', $details) . '.';
+ } else {
+ $last = array_pop($details);
+ $details = implode(', ', $details) . ' ' . __('et') . ' ' . $last . '.';
+ }
+ if ($details) {
+ $details = "<br><br><em style='font-size: 80%'>" . $details . '</em>';
+ }
+
+
$count = count($actualUpdates);
if ($count == 0) {
- return ['alert' => __('Ce fichier ne contient aucun lien à modifier')];
+ return ['alert' => ['message' => __('Ce fichier ne contient aucun lien à modifier.') . $details, 'title' => __('Mise à jour de liens')]];
} else {
$cid = rand(1000, 1000000000);
cache()->set('auditlinks_confirmation_data_' . $cid, $actualUpdates, 360);
- return ['confirm' => ['message' => __('Vous vous apprêtez à modifier :nb liens. Veuillez confirmer cette opération.', ['nb' => $count]), 'confirm_action' => url('/fluidbookcollection/' . $cid . '/import_excel_confirm')]];
+ return ['confirm' => ['message' => __('Vous vous apprêtez à modifier :nb liens. Veuillez confirmer cette opération.', ['nb' => $count]) . $details, 'title' => __('Mise à jour de liens'), 'confirm_action' => url('/fluidbook-collection/' . $cid . '/import_excel_confirm')]];
}
}