$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;
\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
\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
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
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
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
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