]> _ Git - cubeextranet.git/commitdiff
wip #3909 @6
authorvincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Mon, 28 Sep 2020 18:08:54 +0000 (18:08 +0000)
committervincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Mon, 28 Sep 2020 18:08:54 +0000 (18:08 +0000)
inc/ws/Controlleur/class.ws.ajax.php
inc/ws/Util/class.ws.util.php

index 2ece9e6eaee166d2ff299801dcaf525c1db3cf3a..3a5620074fd5e6a04e4d01c8c3b75769877570d7 100644 (file)
@@ -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;
index fcdd9c0408f0f246268cb1cf5d40251d97db1339..ecbc58340891500e65c3348e95666fbd6091dba2 100644 (file)
@@ -2,7 +2,7 @@
 \r
 class wsUtil\r
 {\r
-    public static function excelToArray($excelFile, $assoc = false)\r
+    public static function excelToArray($excelFile, $assoc = false, $reader = 'Excel2007', $force = false)\r
     {\r
         $worksheets = array();\r
 \r
@@ -33,12 +33,15 @@ class wsUtil
 \r
         $cacheFile = $cacheBase . sha1($excelFile . '/' . filemtime($excelFile));\r
 \r
-        if (file_exists($cacheFile) && filemtime($cacheFile) > filemtime($excelFile) && !$assoc) {\r
+        if (!$force && file_exists($cacheFile) && filemtime($cacheFile) > filemtime($excelFile) && !$assoc) {\r
             $worksheets = json_decode(file_get_contents($cacheFile), true);\r
         } else {\r
             set_time_limit(0);\r
             include_once ROOT . '/inc/ZendFramework/PHPExcel/PHPExcel.php';\r
-            $objReader = PHPExcel_IOFactory::createReader('Excel2007');\r
+            $objReader = PHPExcel_IOFactory::createReader($reader);\r
+            if ($reader === 'CSV') {\r
+                $objReader->setDelimiter(';');\r
+            }\r
             $objPHPExcel = $objReader->load($excelFile);\r
 \r
             foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {\r
@@ -62,9 +65,9 @@ class wsUtil
         return $worksheets;\r
     }\r
 \r
-    public static function excelToArrayKeyVal($excelFile)\r
+    public static function excelToArrayKeyVal($excelFile, $reader = 'Excel2007')\r
     {\r
-        $worksheets = self::excelToArray($excelFile);\r
+        $worksheets = self::excelToArray($excelFile, false, $reader);\r
         $res = [];\r
         foreach ($worksheets as $worksheet) {\r
             foreach ($worksheet as $line) {\r
@@ -75,9 +78,9 @@ class wsUtil
         return $res;\r
     }\r
 \r
-    public static function excelToArrayKeyVars($excelFile)\r
+    public static function excelToArrayKeyVars($excelFile, $reader = 'Excel2007')\r
     {\r
-        $worksheets = self::excelToArray($excelFile);\r
+        $worksheets = self::excelToArray($excelFile, false, $reader);\r
         $res = [];\r
         foreach ($worksheets as $worksheet) {\r
 \r
@@ -95,7 +98,11 @@ class wsUtil
                     foreach ($vars as $j => $varname) {\r
                         $r[$varname] = trim($line[$j]);\r
                     }\r
-                    $res[trim($line[0])] = $r;\r
+                    $id = trim($line[0]);\r
+                    if ($id === '') {\r
+                        $id = $i;\r
+                    }\r
+                    $res[$id] = $r;\r
                 }\r
             }\r
             break;\r
@@ -103,9 +110,9 @@ class wsUtil
         return $res;\r
     }\r
 \r
-    public static function excelToArrayKeyValMulti($excelFile)\r
+    public static function excelToArrayKeyValMulti($excelFile, $reader = 'Excel2007')\r
     {\r
-        $worksheets = self::excelToArray($excelFile);\r
+        $worksheets = self::excelToArray($excelFile, false, $reader);\r
         $res = [];\r
         foreach ($worksheets as $worksheet) {\r
             foreach ($worksheet as $line) {\r