From: vincent@cubedesigners.com Date: Mon, 28 Sep 2020 18:08:54 +0000 (+0000) Subject: wip #3909 @6 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e61c6daa55bec52de891ba5721d5286efbe385ae;p=cubeextranet.git wip #3909 @6 --- diff --git a/inc/ws/Controlleur/class.ws.ajax.php b/inc/ws/Controlleur/class.ws.ajax.php index 2ece9e6ea..3a5620074 100644 --- a/inc/ws/Controlleur/class.ws.ajax.php +++ b/inc/ws/Controlleur/class.ws.ajax.php @@ -931,13 +931,81 @@ class wsAjax extends cubeAjax $fname = $_FILES['file']['name']; $ext = files::getExtension($fname); - if ($ext == 'xlsx') { + if ($ext === 'csv') { + self::_importLinksAsCSV($book_id, $fname, $args); + } else if ($ext == 'xlsx') { self::_importLinksAsExcel($book_id, $args); } else if ($ext == 'txt') { self::_importLinksAsAutobookmarkText($book_id, $args); } } + protected static function _importLinksAsCSV($book_id, $fname, $args) + { + global $core; + $d = wsUtil::excelToArrayKeyVars($_FILES['file']['tmp_name'], 'CSV'); + $daoBook = new wsDAOBook($core->con); + $book = $daoBook->selectById($book_id); + + + $images = [2 => 'btn-magnifier.gif', 4 => 'btn-video.gif', 10 => 'btn-video.gif', 12 => 'btn-wishlist.gif']; + + $links = []; + foreach ($d as $item) { + if ($item['x'] === '' || $item['y'] === '' || $item['asset_type'] === '' || $item['width'] === '' || $item['height'] === '' || $item['page_name'] === '') { + continue; + } + + $inline = false; + $video_service = ''; + + // Find dimensions ratio + $ratio = $book->parametres->width / $item['page_width']; + // Find page number + $e = explode('-', $item['page_name']); + foreach ($e as $portion) { + if (preg_match('/^\d+$/', $portion, $m)) { + $page = (int)ltrim($m[0], '0'); + break; + } + } + // Define link type and target + if ($item['asset_type'] === '3') { + // link + $type = 2; + $to = $item['asset_content']; + $webvideo = new CubeIT_Filter_WebVideo(); + $f = $webvideo->filter($to); + if ($f !== $to) { + $e = explode(':', $f); + $type = 10; + $video_service = $e[0]; + $to = $e[1]; + } + } else if ($item['asset_type'] === '5') { + // video + $type = 4; + $e = explode('-', $item['asset_name'], 2); + $to = trim($e[1]) . '.mp4'; + } else if ($item['asset_type'] === '7') { + // Wishlist + $type = 12; + $e = explode('#', $item['asset_name']); + $to = $e[1]; + } + + $link = array( + 'page' => $page, 'image' => $images[$type], + 'left' => $item['x'] * $ratio, 'top' => $item['y'] * $ratio, 'width' => $item['width'] * $ratio, 'height' => $item['height'] * $ratio, 'rot' => '', + 'type' => $type, 'to' => $to, 'target' => '_blank', + 'infobulle' => '', 'numerotation' => 'virtual', 'display_area' => '1', 'inline' => $inline, + 'video_service' => $video_service); + $links[] = $link; + } + $rulers = []; + wsLinks::saveLinksInFile($book_id, $core->user->utilisateur_id, 'Import links from CSV file : ' . $fname, $links, $rulers); + } + protected static function _importLinksAsExcel($book_id) { global $core; diff --git a/inc/ws/Util/class.ws.util.php b/inc/ws/Util/class.ws.util.php index fcdd9c040..ecbc58340 100644 --- a/inc/ws/Util/class.ws.util.php +++ b/inc/ws/Util/class.ws.util.php @@ -2,7 +2,7 @@ class wsUtil { - public static function excelToArray($excelFile, $assoc = false) + public static function excelToArray($excelFile, $assoc = false, $reader = 'Excel2007', $force = false) { $worksheets = array(); @@ -33,12 +33,15 @@ class wsUtil $cacheFile = $cacheBase . sha1($excelFile . '/' . filemtime($excelFile)); - if (file_exists($cacheFile) && filemtime($cacheFile) > filemtime($excelFile) && !$assoc) { + if (!$force && file_exists($cacheFile) && filemtime($cacheFile) > filemtime($excelFile) && !$assoc) { $worksheets = json_decode(file_get_contents($cacheFile), true); } else { set_time_limit(0); include_once ROOT . '/inc/ZendFramework/PHPExcel/PHPExcel.php'; - $objReader = PHPExcel_IOFactory::createReader('Excel2007'); + $objReader = PHPExcel_IOFactory::createReader($reader); + if ($reader === 'CSV') { + $objReader->setDelimiter(';'); + } $objPHPExcel = $objReader->load($excelFile); foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { @@ -62,9 +65,9 @@ class wsUtil return $worksheets; } - public static function excelToArrayKeyVal($excelFile) + public static function excelToArrayKeyVal($excelFile, $reader = 'Excel2007') { - $worksheets = self::excelToArray($excelFile); + $worksheets = self::excelToArray($excelFile, false, $reader); $res = []; foreach ($worksheets as $worksheet) { foreach ($worksheet as $line) { @@ -75,9 +78,9 @@ class wsUtil return $res; } - public static function excelToArrayKeyVars($excelFile) + public static function excelToArrayKeyVars($excelFile, $reader = 'Excel2007') { - $worksheets = self::excelToArray($excelFile); + $worksheets = self::excelToArray($excelFile, false, $reader); $res = []; foreach ($worksheets as $worksheet) { @@ -95,7 +98,11 @@ class wsUtil foreach ($vars as $j => $varname) { $r[$varname] = trim($line[$j]); } - $res[trim($line[0])] = $r; + $id = trim($line[0]); + if ($id === '') { + $id = $i; + } + $res[$id] = $r; } } break; @@ -103,9 +110,9 @@ class wsUtil return $res; } - public static function excelToArrayKeyValMulti($excelFile) + public static function excelToArrayKeyValMulti($excelFile, $reader = 'Excel2007') { - $worksheets = self::excelToArray($excelFile); + $worksheets = self::excelToArray($excelFile, false, $reader); $res = []; foreach ($worksheets as $worksheet) { foreach ($worksheet as $line) {