]> _ Git - cubeextranet.git/commitdiff
wip #3655 @4
authorvincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Wed, 20 May 2020 15:34:22 +0000 (15:34 +0000)
committervincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Wed, 20 May 2020 15:34:22 +0000 (15:34 +0000)
inc/ws/Controlleur/class.ws.maintenance.php
inc/ws/DAO/class.ws.dao.book.php
inc/ws/DAO/class.ws.dao.document.php
inc/ws/Util/class.ws.links.php
inc/ws/Util/class.ws.util.php
inc/ws/Util/html5/master/class.ws.html5.links.php

index 6735fe32156ff0730454c4e1d1b16e42e2bbea29..9aa50bdb1362d2691822cc659e5bcc5d1fc8ad0f 100644 (file)
@@ -1736,6 +1736,52 @@ class wsMaintenance
             $exporter->export($id, $x, 'install_hosting', 'online', "/airtahitiexport/" . $id, '');
         }
     }
+
+    public static function moveLinksToFiles()
+    {
+        CubeIT_Util_PHP::neverStop();
+
+        global $core;
+        $r = $core->con->select('SELECT * FROM books');
+
+        $books = [];
+        while ($r->fetch()) {
+            $books[] = $r->book_id;
+        }
+
+
+        foreach ($books as $id) {
+            $dir = WS_BOOKS . '/links/' . $id;
+            if (!file_exists($dir)) {
+                mkdir($dir, 0777, true);
+            }
+            /** @var $book wsBook */
+            $dao = new wsDAODocument($core->con);
+            $updates = $dao->getLinksVersions($id);
+
+            foreach ($updates as $time => $update) {
+                $file = $dir . '/' . $time . '.json.gz';
+                $meta = $dir . '/' . $time . '.meta.json.gz';
+                if (file_exists($file)) {
+                    continue;
+                }
+
+                $links = [];
+                $rulers = [];
+
+                wsLinks::getLinksAndRulers($id, $links, $rulers, $time);
+
+                $res = ['links' => $links, 'rulers' => $rulers];
+                $gz = gzopen($file, 'ab');
+                gzwrite($gz, json_encode($res));
+                gzclose($gz);
+
+                $gz = gzopen($meta, 'ab');
+                gzwrite($gz, json_encode($update));
+                gzclose($gz);
+            }
+        }
+    }
 }
 
 
index 21763d4774d77080121fe713b1f50a52c4568104..6e049778c730ad661ec45f68ab5eaf5324d94c2e 100644 (file)
@@ -142,6 +142,13 @@ class wsDAOBook extends commonDAO
         return $res;
     }
 
+    public function selectAll(){
+        $table = 'books_vue';
+        $sql = 'SELECT * FROM ' . $table;
+        $r = $this->con->select($sql);
+        return $this->factory($r);
+    }
+
     public function selectById($book_id = null, $simple = false)
     {
         if (is_null($book_id)) {
index d65454a4e65eb71ea149f820a764fcbcd7d895cf..125d600008bc0f96a4036d61e226b6c967abaee3 100644 (file)
 class wsDAODocument extends commonDAO
 {
 
-       public static $normal = array('document_id', 'file', 'proprietaire', 'pages', 'date', 'numberSections', 'version');
-       public static $complex = array('localInfos', 'bookmarks', 'generalInfos', 'conversionInfos', 'trim');
-
-       public function singleton($r)
-       {
-               $document = new wsDocument();
-               foreach (self::$normal as $k) {
-                       $document->$k = $r->$k;
-               }
-               foreach (self::$complex as $k) {
-                       if ($r->$k != '') {
-                               $document->$k = unserialize($r->$k);
-                       }
-               }
-               $document->init();
-               return $document;
-       }
-
-       public function setFluidbookLinksFromCSV($book_id)
-       {
-               $daoBook = new wsDAOBook($this->con);
-               $pages = $daoBook->getPagesOfBook($book_id);
-
-               foreach ($pages as $page => $infos) {
-                       $docs[] = $infos['document_id'];
-               }
-
-               $docs = array_unique($docs);
-               foreach ($docs as $document_id) {
-                       $this->putLinksFromCSV($document_id);
-               }
-       }
-
-       public function restoreLinksVersion($book_id, $update, $user = 0)
-       {
-               wsLinks::getLinksAndRulers($book_id, $links, $rulers, $update);
-
-               $this->setLinksAndRulers($book_id, json_encode($links), json_encode($rulers), 'Restore links from ' . date('Y-m-d H:i:s', $update), $user);
-
-               $r = $this->con->select('SELECT * FROM special_links_versions WHERE `update`=' . $update . ' AND book_id=\'' . $this->con->escape($book_id) . '\'');
-               if ($r->count()) {
-                       $daoBook = new wsDAOBook($this->con);
-                       $daoBook->setSpecialLinksAndRulers($book_id, $r->links, $r->rulers);
-               }
-       }
-
-       public function putLinksFromCSV($document_id)
-       {
-               $log = array('Copie des liens du PDF vers la base de donnĂ©es');
-               $document = $this->selectById($document_id);
-
-               $booleans = array('video_loop', 'video_auto_start', 'video_controls', 'video_sound_on');
-
-               $links = array();
-
-               for ($page = 1; $page <= $document->generalInfos['pages']; $page++) {
-                       $links[$page] = array();
-
-                       $csv = wsDocument::getDir($document->document_id) . '/p' . $page . '.csv';
-                       if (!file_exists($csv) && file_exists($csv . '.gz')) {
-                               $csv = 'compress.zlib://' . $csv . '.gz';
-                       } elseif (!file_exists($csv)) {
-                               $log[] = 'Links file of page ' . $page . ' not found';
-                               continue;
-                       }
-
-                       $newformat = (filemtime($csv) > 1363685416);
-
-                       $log[] = 'Links file of page ' . $page . ' found';
-
-                       $fp = fopen($csv, 'rb');
-
-                       while (true) {
-                               $line = fgetcsv($fp, 512, ';', '"');
-                               // End of file
-                               if (!$line) {
-                                       break;
-                               }
-                               // Commentaire || ligne vide
-                               if (substr($line[0], 0, 1) == '#' || is_null($line[0])) {
-                                       continue;
-                               }
-                               $link = array();
-                               if ($newformat) {
-                                       $cols = array('page' => '', 'left' => '', 'top' => '', 'width' => '', 'height' => '', 'type' => '', 'to' => '', 'target' => '_blank', 'video_loop' => true, 'video_auto_start' => true, 'video_controls' => true, 'video_sound_on' => true, 'infobulle' => '', 'numerotation' => 'physical');
-                               } else {
-                                       $cols = array('page' => '', 'type' => '', 'to' => '', 'left' => '', 'top' => '', 'width' => '', 'height' => '', 'target' => '_blank', 'video_loop' => true, 'video_auto_start' => true, 'video_controls' => true, 'video_sound_on' => true, 'infobulle' => '', 'numerotation' => 'physical');
-                               }
-                               $k = 0;
-                               foreach ($cols as $col => $default) {
-                                       if (isset($line[$k])) {
-                                               if (in_array($k, $booleans)) {
-                                                       $link[$col] = ($line[$k] == '1');
-                                               } else {
-                                                       $link[$col] = utf8_encode($line[$k]);
-                                               }
-                                       } else {
-                                               $link[$col] = $default;
-                                       }
-                                       $k++;
-                               }
-                               $link['page'] = $page;
-                               $links[$page][] = $link;
-                       }
-
-                       $log[] = 'Added ' . count($links) . ' links';
-               }
-
-               wsLinks::setDocumentLinks($document_id, json_encode($links), json_encode(array()), 0, 'Links imported from PDF', TIME);
-
-               return implode("\n", $log);
-       }
-
-       public function setLinksAndRulers($book_id, $links, $rulers, $comments = '', $user = 0, $handleSpecials = true)
-       {
-               $daoBook = new wsDAOBook($this->con);
-               $pages = $daoBook->getPagesOfBook($book_id, false);
-
-               if (is_array($links)) {
-                       $links = json_encode($links);
-               }
-               if (is_array($rulers)) {
-                       $rulers = json_encode($rulers);
-               }
-               $links = json_decode($links, false);
-               $rulers = json_decode($rulers, false);
-
-               $links = wsLinks::encryptLinks($links);
-
-               $t = array();
-
-               foreach ($links as $link) {
-                       if (!isset($t[$link->page])) {
-                               $t[$link->page] = array('links' => array(), 'rulers' => array());
-                       }
-                       $link->to = trim($link->to);
-                       $t[$link->page]['links'][] = $link;
-               }
-               foreach ($rulers as $ruler) {
-                       if (!isset($t[$ruler->page])) {
-                               $t[$ruler->page] = array('links' => array(), 'rulers' => array());
-                       }
-                       $t[$ruler->page]['rulers'][] = $ruler;
-               }
-
-               $links = array();
-               $rulers = array();
-
-               foreach ($pages as $p) {
-                       if (!isset($links[$p['document_id']])) {
-                               $links[$p['document_id']] = array();
-                               $rulers[$p['document_id']] = array();
-                       }
-
-                       if (!isset($links[$p['document_id']][$p['document_page']])) {
-                               $links[$p['document_id']][$p['document_page']] = array();
-                               $rulers[$p['document_id']][$p['document_page']] = array();
-                       }
-               }
-
-               $specialLinks = array();
-               $specialRulers = array();
-
-               foreach ($t as $page => $tt) {
-                       if (!is_int($page) && $handleSpecials) {
-                               if (isset($tt['links'])) {
-                                       $specialLinks = array_merge($specialLinks, $tt['links']);
-                               }
-                               if (isset($tt['rulers'])) {
-                                       $specialRulers = array_merge($specialRulers, $tt['rulers']);
-                               }
-                               continue;
-                       }
-                       $infos = $pages[$page];
-                       $doc_id = $infos['document_id'];
-                       $doc_page = $infos['document_page'];
-
-                       if (!isset($links[$doc_id])) {
-                               $links[$doc_id] = array();
-                               $rulers[$doc_id] = array();
-                       }
-                       if (!isset($links[$doc_id][$doc_page])) {
-                               $links[$doc_id][$doc_page] = array();
-                               $rulers[$doc_id][$doc_page] = array();
-                       }
-
-                       foreach ($tt['links'] as $link) {
-                               $link->to = trim($link->to);
-                               $links[$doc_id][$doc_page][] = $link;
-                       }
-
-                       foreach ($tt['rulers'] as $ruler) {
-                               $rulers[$doc_id][$doc_page][] = $ruler;
-                       }
-               }
-
-               foreach ($links as $document_id => $dummy) {
-                       wsLinks::setDocumentLinks($document_id, json_encode($links[$document_id]), json_encode($rulers[$document_id]), $user, $comments, TIME);
-               }
-               if ($handleSpecials) {
-                       $daoBook->setSpecialLinksAndRulers($book_id, $specialLinks, $specialRulers);
-               }
-
-               $c = $this->con->openCursor('books');
-               $c->changedate = TIME;
-               $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
-       }
-
-       public function copyLinks($from, $to)
-       {
-               $sql = 'REPLACE INTO document_links_versions (`document_id`,`links`,`rulers`,`update`) '
-                       . 'SELECT \'' . $to . '\',`links`,`rulers`,`update` FROM document_links_versions WHERE document_id=\'' . $from . '\'';
-
-               $this->con->execute($sql);
-       }
-
-       public function setLinksFromOldFluidbook($book_id)
-       {
-               $daoBook = new wsDAOBook($this->con);
-               $pages = $daoBook->getPagesOfBook($book_id);
-
-               $book = $daoBook->selectById($book_id);
-               $width = $book->parametres->width;
-
-               $xml = simplexml_load_file('https://ws.fluidbook.com/books/' . $book_id . '/data/links.xml');
-               echo $xml;
-               $links = $xml->xpath('//oneLink');
-
-               $res = array();
-               $lpages = array();
-               foreach ($links as $l) {
-                       if ((string)$l->page == '') {
-                               continue;
-                       }
-
-                       $left = floatval((string)$l->startX);
-                       $right = floatval((string)$l->endX);
-                       $top = floatval((string)$l->startY);
-                       $bottom = floatval((string)$l->endY);
-
-                       $lwidth = abs($right - $left);
-                       $lheight = abs($bottom - $top);
-
-                       $left = min($left, $right);
-                       $top = min($top, $bottom);
-
-                       $link = array();
-                       $link['page'] = (string)$l->page;
-                       $link['numerotation'] = false;
-                       $link['left'] = $left;
-                       $link['width'] = $lwidth;
-                       if ($link['left'] > $width) {
-                               $link['page']++;
-                               $link['left'] -= $width;
-                       }
-                       $page = $link['page'];
-                       $link['top'] = $top;
-                       $link['height'] = $lheight;
-                       $link['infobulle'] = '';
-                       $link['target'] = '_blank';
-                       $link['to'] = (string)$l->linkTo;
-                       $link['type'] = intval((string)$l->linkType);
-                       $link['video_loop'] = ((string)$l->loopFlag == '1');
-                       $link['video_auto_start'] = true;
-                       $link['video_controls'] = ((string)$l->playerActive == '1');
-                       $link['video_sound_on'] = ((string)$l->soundOn == '1');
-
-                       $dir = WS_BOOKS . '/working/' . $book_id . '/';
-
-                       if ($link['type'] == '4' || $link['type'] == '6') {
-                               if (!stristr($link['to'], ':/')) {
-                                       if (!file_exists($dir)) {
-                                               mkdir($dir, 0755, true);
-                                       }
-                                       copy('https://ws.fluidbook.com/books/' . $book_id . '/data/' . $link['to'], $dir . '/' . $link['to']);
-                               }
-                       }
-
-                       if (!isset($lpages[$page])) {
-                               $lpages[$page] = array();
-                       }
-                       $lpages[$page][] = $link;
-               }
-
-
-               $links = array();
-               foreach ($pages as $p => $infos) {
-                       if (!isset($links[$infos['document_id']])) {
-                               $links[$infos['document_id']] = array();
-                       }
-                       if (isset($lpages[$p])) {
-                               $links[$infos['document_id']][$infos['document_page']] = $lpages[$p];
-                       }
-               }
-
-               krsort($links);
-
-               foreach ($links as $document_id => $l) {
-                       wsLinks::setDocumentLinks($document_id, json_encode($l), json_encode(array()), 0, 'Import from Fluidbook V1', TIME);
-               }
-
-               $c = $this->con->openCursor('books');
-               $c->changedate = TIME;
-               $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
-       }
-
-       public function getLinksAndRulers($book_id, &$links, &$rulers, $time = null)
-       {
-               return wsLinks::getLinksAndRulers($book_id, $links, $rulers, $time);
-       }
-
-       public function getLinksVersions($book_id)
-       {
-
-               $r = $this->con->select('SELECT * FROM special_links_versions WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
-               $specials = array();
-               while ($r->fetch()) {
-                       $ll = json_decode($r->links);
-                       $lr = json_decode($r->rulers);
-                       $specials[$r->update] = array('links' => count($ll), 'rulers' => count($lr));
-               }
-
-               // Get composition versions
-               $r = $this->con->select('SELECT * FROM book_pages_versions WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
-               $docs = array();
-               while ($r->fetch()) {
-                       $c = unserialize($r->composition);
-                       foreach ($c as $infos) {
-                               $docs[] = $infos['document_id'];
-                       }
-               }
-
-               $res = array();
-               $sql = 'SELECT l.*,u.prenom,u.nom FROM document_links_versions l LEFT JOIN utilisateurs u ON l.user=u.utilisateur_id WHERE document_id IN(' . implode(',', $docs) . ')';
-               $r = $this->con->select($sql);
-
-
-               while ($r->fetch()) {
-                       if (!isset($res[$r->update])) {
-                               if (isset($specials[$r->update])) {
-                                       $res[$r->update] = $specials[$r->update];
-                               } else {
-                                       $res[$r->update] = array('links' => 0, 'rulers' => 0);
-                               }
-                       }
-
-
-                       $ll = json_decode($r->links);
-                       foreach ($ll as $lll) {
-                               $res[$r->update]['links'] += count($lll);
-                       }
-                       $rr = json_decode($r->rulers);
-                       foreach ($rr as $rrr) {
-                               $res[$r->update]['rulers'] += count($rrr);
-                       }
-                       $res[$r->update]['comments'] = $r->comments;
-                       if ($r->user > 0) {
-                               $user = $r->prenom . ' ' . $r->nom;
-                       } else {
-                               $user = '';
-                       }
-                       $res[$r->update]['user'] = $user;
-               }
-
-               return $res;
-       }
-
-       public function getNextId()
-       {
-               $r = $this->con->select('SELECT MAX(document_id) AS document_id FROM documents');
-               if ($r->document_id < 100000) {
-                       return 100000;
-               }
-               return $r->document_id + 1;
-       }
-
-       public function selectById($document_id)
-       {
-               $r = $this->con->select('SELECT * FROM documents WHERE document_id=\'' . $this->con->escape($document_id) . '\'');
-               return $this->singleton($r);
-       }
-
-       public function selectByBookId($book_id)
-       {
-               $r = $this->con->select('SELECT * FROM documents WHERE document_id IN(SELECT document_id FROM book_pages WHERE book_id=\'' . $this->con->escape($book_id) . '\')');
-               return $this->factory($r);
-       }
-
-       public function selectInList($documents_id)
-       {
-               $r = $this->con->select('SELECT * FROM documents WHERE document_id IN(' . implode(',', $documents_id) . ')');
-               return $this->factory($r);
-       }
-
-       public function cree()
-       {
-               $document = new wsDocument();
-               $document->document_id = 'new';
-               $document->file = '';
-               $document->proprietaire = 0;
-               $document->pages = 0;
-               $document->trim = array();
-               $document->date = TIME;
-               $document->localInfos = new wsDocumentLocalInfos();
-               return $document;
-       }
-
-       public function sauve($data)
-       {
-               if (!isset($data['document_id'])) {
-                       $data['document_id'] = 'new';
-               }
-               $c = $this->con->openCursor('documents');
-
-               foreach (self::$normal as $k) {
-                       if (isset($data[$k])) {
-                               $c->$k = $data[$k];
-                       }
-               }
-               foreach (self::$complex as $k) {
-                       if (isset($data[$k])) {
-                               $c->$k = serialize($data[$k]);
-                       }
-               }
-               if (isset($data['localInfos'])) {
-                       $c->localHash = md5($c->localInfos);
-               }
-
-               if ($data['document_id'] == 'new') {
-                       $document_id = $c->document_id = $this->getNextId();
-                       $c->date = TIME;
-                       $c->insert();
-               } else {
-                       $document_id = $data['document_id'];
-                       $c->update('WHERE document_id=\'' . $this->con->escape($data['document_id']) . '\'');
-               }
-               $doc = $this->selectById($document_id);
-               return $doc;
-       }
-
-       public function updateField($document_id, $fieldName, $value)
-       {
-               $c = $this->con->openCursor('documents');
-               $c->$fieldName = $value;
-               $c->update('WHERE document_id=\'' . $this->con->escape($document_id) . '\'');
-       }
-
-       public function updateFromObject($document)
-       {
-               $data = array();
-               foreach (self::$normal as $k) {
-                       $data[$k] = $document->$k;
-               }
-               foreach (self::$complex as $k) {
-                       $data[$k] = $document->$k;
-               }
-               return $this->sauve($data);
-       }
-
-       public function getLinksAsExcel($links, $rulers)
-       {
-               return wsLinks::linksToExcel($links, $rulers);
-       }
+    public static $normal = array('document_id', 'file', 'proprietaire', 'pages', 'date', 'numberSections', 'version');
+    public static $complex = array('localInfos', 'bookmarks', 'generalInfos', 'conversionInfos', 'trim');
+
+    public function singleton($r)
+    {
+        $document = new wsDocument();
+        foreach (self::$normal as $k) {
+            $document->$k = $r->$k;
+        }
+        foreach (self::$complex as $k) {
+            if ($r->$k != '') {
+                $document->$k = unserialize($r->$k);
+            }
+        }
+        $document->init();
+        return $document;
+    }
+
+    public function setFluidbookLinksFromCSV($book_id)
+    {
+        $daoBook = new wsDAOBook($this->con);
+        $pages = $daoBook->getPagesOfBook($book_id);
+
+        foreach ($pages as $page => $infos) {
+            $docs[] = $infos['document_id'];
+        }
+
+        $docs = array_unique($docs);
+        foreach ($docs as $document_id) {
+            $this->putLinksFromCSV($document_id);
+        }
+    }
+
+    public function restoreLinksVersion($book_id, $update, $user = 0)
+    {
+        wsLinks::getLinksAndRulers($book_id, $links, $rulers, $update);
+
+        $this->setLinksAndRulers($book_id, json_encode($links), json_encode($rulers), 'Restore links from ' . date('Y-m-d H:i:s', $update), $user);
+
+        $r = $this->con->select('SELECT * FROM special_links_versions WHERE `update`=' . $update . ' AND book_id=\'' . $this->con->escape($book_id) . '\'');
+        if ($r->count()) {
+            $daoBook = new wsDAOBook($this->con);
+            $daoBook->setSpecialLinksAndRulers($book_id, $r->links, $r->rulers);
+        }
+    }
+
+    public function putLinksFromCSV($document_id)
+    {
+        $log = array('Copie des liens du PDF vers la base de donnĂ©es');
+        $document = $this->selectById($document_id);
+
+        $booleans = array('video_loop', 'video_auto_start', 'video_controls', 'video_sound_on');
+
+        $links = array();
+
+        for ($page = 1; $page <= $document->generalInfos['pages']; $page++) {
+            $links[$page] = array();
+
+            $csv = wsDocument::getDir($document->document_id) . '/p' . $page . '.csv';
+            if (!file_exists($csv) && file_exists($csv . '.gz')) {
+                $csv = 'compress.zlib://' . $csv . '.gz';
+            } elseif (!file_exists($csv)) {
+                $log[] = 'Links file of page ' . $page . ' not found';
+                continue;
+            }
+
+            $newformat = (filemtime($csv) > 1363685416);
+
+            $log[] = 'Links file of page ' . $page . ' found';
+
+            $fp = fopen($csv, 'rb');
+
+            while (true) {
+                $line = fgetcsv($fp, 512, ';', '"');
+                // End of file
+                if (!$line) {
+                    break;
+                }
+                // Commentaire || ligne vide
+                if (substr($line[0], 0, 1) == '#' || is_null($line[0])) {
+                    continue;
+                }
+                $link = array();
+                if ($newformat) {
+                    $cols = array('page' => '', 'left' => '', 'top' => '', 'width' => '', 'height' => '', 'type' => '', 'to' => '', 'target' => '_blank', 'video_loop' => true, 'video_auto_start' => true, 'video_controls' => true, 'video_sound_on' => true, 'infobulle' => '', 'numerotation' => 'physical');
+                } else {
+                    $cols = array('page' => '', 'type' => '', 'to' => '', 'left' => '', 'top' => '', 'width' => '', 'height' => '', 'target' => '_blank', 'video_loop' => true, 'video_auto_start' => true, 'video_controls' => true, 'video_sound_on' => true, 'infobulle' => '', 'numerotation' => 'physical');
+                }
+                $k = 0;
+                foreach ($cols as $col => $default) {
+                    if (isset($line[$k])) {
+                        if (in_array($k, $booleans)) {
+                            $link[$col] = ($line[$k] == '1');
+                        } else {
+                            $link[$col] = utf8_encode($line[$k]);
+                        }
+                    } else {
+                        $link[$col] = $default;
+                    }
+                    $k++;
+                }
+                $link['page'] = $page;
+                $links[$page][] = $link;
+            }
+
+            $log[] = 'Added ' . count($links) . ' links';
+        }
+
+        wsLinks::setDocumentLinks($document_id, json_encode($links), json_encode(array()), 0, 'Links imported from PDF', TIME);
+
+        return implode("\n", $log);
+    }
+
+    public function setLinksAndRulers($book_id, $links, $rulers, $comments = '', $user = 0, $handleSpecials = true)
+    {
+        $daoBook = new wsDAOBook($this->con);
+        $pages = $daoBook->getPagesOfBook($book_id, false);
+
+        if (is_array($links)) {
+            $links = json_encode($links);
+        }
+        if (is_array($rulers)) {
+            $rulers = json_encode($rulers);
+        }
+        $links = json_decode($links, false);
+        $rulers = json_decode($rulers, false);
+
+        $links = wsLinks::encryptLinks($links);
+
+        $t = array();
+
+        foreach ($links as $link) {
+            if (!isset($t[$link->page])) {
+                $t[$link->page] = array('links' => array(), 'rulers' => array());
+            }
+            $link->to = trim($link->to);
+            $t[$link->page]['links'][] = $link;
+        }
+        foreach ($rulers as $ruler) {
+            if (!isset($t[$ruler->page])) {
+                $t[$ruler->page] = array('links' => array(), 'rulers' => array());
+            }
+            $t[$ruler->page]['rulers'][] = $ruler;
+        }
+
+        $links = array();
+        $rulers = array();
+
+        foreach ($pages as $p) {
+            if (!isset($links[$p['document_id']])) {
+                $links[$p['document_id']] = array();
+                $rulers[$p['document_id']] = array();
+            }
+
+            if (!isset($links[$p['document_id']][$p['document_page']])) {
+                $links[$p['document_id']][$p['document_page']] = array();
+                $rulers[$p['document_id']][$p['document_page']] = array();
+            }
+        }
+
+        $specialLinks = array();
+        $specialRulers = array();
+
+        foreach ($t as $page => $tt) {
+            if (!is_int($page) && $handleSpecials) {
+                if (isset($tt['links'])) {
+                    $specialLinks = array_merge($specialLinks, $tt['links']);
+                }
+                if (isset($tt['rulers'])) {
+                    $specialRulers = array_merge($specialRulers, $tt['rulers']);
+                }
+                continue;
+            }
+            $infos = $pages[$page];
+            $doc_id = $infos['document_id'];
+            $doc_page = $infos['document_page'];
+
+            if (!isset($links[$doc_id])) {
+                $links[$doc_id] = array();
+                $rulers[$doc_id] = array();
+            }
+            if (!isset($links[$doc_id][$doc_page])) {
+                $links[$doc_id][$doc_page] = array();
+                $rulers[$doc_id][$doc_page] = array();
+            }
+
+            foreach ($tt['links'] as $link) {
+                $link->to = trim($link->to);
+                $links[$doc_id][$doc_page][] = $link;
+            }
+
+            foreach ($tt['rulers'] as $ruler) {
+                $rulers[$doc_id][$doc_page][] = $ruler;
+            }
+        }
+
+        foreach ($links as $document_id => $dummy) {
+            wsLinks::setDocumentLinks($document_id, json_encode($links[$document_id]), json_encode($rulers[$document_id]), $user, $comments, TIME);
+        }
+        if ($handleSpecials) {
+            $daoBook->setSpecialLinksAndRulers($book_id, $specialLinks, $specialRulers);
+        }
+
+        $c = $this->con->openCursor('books');
+        $c->changedate = TIME;
+        $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
+    }
+
+    public function copyLinks($from, $to)
+    {
+        $sql = 'REPLACE INTO document_links_versions (`document_id`,`links`,`rulers`,`update`) '
+            . 'SELECT \'' . $to . '\',`links`,`rulers`,`update` FROM document_links_versions WHERE document_id=\'' . $from . '\'';
+
+        $this->con->execute($sql);
+    }
+
+    public function setLinksFromOldFluidbook($book_id)
+    {
+        $daoBook = new wsDAOBook($this->con);
+        $pages = $daoBook->getPagesOfBook($book_id);
+
+        $book = $daoBook->selectById($book_id);
+        $width = $book->parametres->width;
+
+        $xml = simplexml_load_file('https://ws.fluidbook.com/books/' . $book_id . '/data/links.xml');
+        echo $xml;
+        $links = $xml->xpath('//oneLink');
+
+        $res = array();
+        $lpages = array();
+        foreach ($links as $l) {
+            if ((string)$l->page == '') {
+                continue;
+            }
+
+            $left = floatval((string)$l->startX);
+            $right = floatval((string)$l->endX);
+            $top = floatval((string)$l->startY);
+            $bottom = floatval((string)$l->endY);
+
+            $lwidth = abs($right - $left);
+            $lheight = abs($bottom - $top);
+
+            $left = min($left, $right);
+            $top = min($top, $bottom);
+
+            $link = array();
+            $link['page'] = (string)$l->page;
+            $link['numerotation'] = false;
+            $link['left'] = $left;
+            $link['width'] = $lwidth;
+            if ($link['left'] > $width) {
+                $link['page']++;
+                $link['left'] -= $width;
+            }
+            $page = $link['page'];
+            $link['top'] = $top;
+            $link['height'] = $lheight;
+            $link['infobulle'] = '';
+            $link['target'] = '_blank';
+            $link['to'] = (string)$l->linkTo;
+            $link['type'] = intval((string)$l->linkType);
+            $link['video_loop'] = ((string)$l->loopFlag == '1');
+            $link['video_auto_start'] = true;
+            $link['video_controls'] = ((string)$l->playerActive == '1');
+            $link['video_sound_on'] = ((string)$l->soundOn == '1');
+
+            $dir = WS_BOOKS . '/working/' . $book_id . '/';
+
+            if ($link['type'] == '4' || $link['type'] == '6') {
+                if (!stristr($link['to'], ':/')) {
+                    if (!file_exists($dir)) {
+                        mkdir($dir, 0755, true);
+                    }
+                    copy('https://ws.fluidbook.com/books/' . $book_id . '/data/' . $link['to'], $dir . '/' . $link['to']);
+                }
+            }
+
+            if (!isset($lpages[$page])) {
+                $lpages[$page] = array();
+            }
+            $lpages[$page][] = $link;
+        }
+
+
+        $links = array();
+        foreach ($pages as $p => $infos) {
+            if (!isset($links[$infos['document_id']])) {
+                $links[$infos['document_id']] = array();
+            }
+            if (isset($lpages[$p])) {
+                $links[$infos['document_id']][$infos['document_page']] = $lpages[$p];
+            }
+        }
+
+        krsort($links);
+
+        foreach ($links as $document_id => $l) {
+            wsLinks::setDocumentLinks($document_id, json_encode($l), json_encode(array()), 0, 'Import from Fluidbook V1', TIME);
+        }
+
+        $c = $this->con->openCursor('books');
+        $c->changedate = TIME;
+        $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
+    }
+
+    public function getLinksAndRulers($book_id, &$links, &$rulers, $time = null)
+    {
+        return wsLinks::getLinksAndRulers($book_id, $links, $rulers, $time);
+    }
+
+    public function getLinksVersions($book_id)
+    {
+
+        $r = $this->con->select('SELECT * FROM special_links_versions WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
+        $specials = ['links' => 0, 'rulers' => 0];
+        while ($r->fetch()) {
+            $ll = json_decode($r->links);
+            $lr = json_decode($r->rulers);
+            $specials[$r->update] = ['links' => count($ll), 'rulers' => count($lr)];
+        }
+
+        // Get composition versions
+        $r = $this->con->select('SELECT * FROM book_pages_versions WHERE book_id=\'' . $this->con->escape($book_id) . '\'');
+        $docs = [];
+        while ($r->fetch()) {
+            $c = unserialize($r->composition);
+            foreach ($c as $infos) {
+                $docs[] = $infos['document_id'];
+            }
+        }
+
+        if (!$docs) {
+            return [];
+        }
+
+        $res = array();
+        $sql = 'SELECT * FROM document_links_versions  WHERE document_id IN(' . implode(',', $docs) . ')';
+        $r = $this->con->select($sql);
+
+        while ($r->fetch()) {
+            if (!isset($res[$r->update])) {
+                if (isset($specials[$r->update])) {
+                    $res[$r->update] = $specials[$r->update];
+                } else {
+                    $res[$r->update] = array('links' => 0, 'rulers' => 0);
+                }
+            }
+
+            $ll = json_decode($r->links);
+            foreach ($ll as $lll) {
+                $res[$r->update]['links'] += count($lll);
+            }
+            $rr = json_decode($r->rulers);
+            foreach ($rr as $rrr) {
+                $res[$r->update]['rulers'] += count($rrr);
+            }
+            $res[$r->update]['comments'] = $r->comments;
+            $res[$r->update]['user'] = $r->user;
+        }
+
+        return $res;
+    }
+
+    public function getNextId()
+    {
+        $r = $this->con->select('SELECT MAX(document_id) AS document_id FROM documents');
+        if ($r->document_id < 100000) {
+            return 100000;
+        }
+        return $r->document_id + 1;
+    }
+
+    public function selectById($document_id)
+    {
+        $r = $this->con->select('SELECT * FROM documents WHERE document_id=\'' . $this->con->escape($document_id) . '\'');
+        return $this->singleton($r);
+    }
+
+    public function selectByBookId($book_id)
+    {
+        $r = $this->con->select('SELECT * FROM documents WHERE document_id IN(SELECT document_id FROM book_pages WHERE book_id=\'' . $this->con->escape($book_id) . '\')');
+        return $this->factory($r);
+    }
+
+    public function selectInList($documents_id)
+    {
+        $r = $this->con->select('SELECT * FROM documents WHERE document_id IN(' . implode(',', $documents_id) . ')');
+        return $this->factory($r);
+    }
+
+    public function cree()
+    {
+        $document = new wsDocument();
+        $document->document_id = 'new';
+        $document->file = '';
+        $document->proprietaire = 0;
+        $document->pages = 0;
+        $document->trim = array();
+        $document->date = TIME;
+        $document->localInfos = new wsDocumentLocalInfos();
+        return $document;
+    }
+
+    public function sauve($data)
+    {
+        if (!isset($data['document_id'])) {
+            $data['document_id'] = 'new';
+        }
+        $c = $this->con->openCursor('documents');
+
+        foreach (self::$normal as $k) {
+            if (isset($data[$k])) {
+                $c->$k = $data[$k];
+            }
+        }
+        foreach (self::$complex as $k) {
+            if (isset($data[$k])) {
+                $c->$k = serialize($data[$k]);
+            }
+        }
+        if (isset($data['localInfos'])) {
+            $c->localHash = md5($c->localInfos);
+        }
+
+        if ($data['document_id'] == 'new') {
+            $document_id = $c->document_id = $this->getNextId();
+            $c->date = TIME;
+            $c->insert();
+        } else {
+            $document_id = $data['document_id'];
+            $c->update('WHERE document_id=\'' . $this->con->escape($data['document_id']) . '\'');
+        }
+        $doc = $this->selectById($document_id);
+        return $doc;
+    }
+
+    public function updateField($document_id, $fieldName, $value)
+    {
+        $c = $this->con->openCursor('documents');
+        $c->$fieldName = $value;
+        $c->update('WHERE document_id=\'' . $this->con->escape($document_id) . '\'');
+    }
+
+    public function updateFromObject($document)
+    {
+        $data = array();
+        foreach (self::$normal as $k) {
+            $data[$k] = $document->$k;
+        }
+        foreach (self::$complex as $k) {
+            $data[$k] = $document->$k;
+        }
+        return $this->sauve($data);
+    }
+
+    public function getLinksAsExcel($links, $rulers)
+    {
+        return wsLinks::linksToExcel($links, $rulers);
+    }
 
 }
index 38b6ead433bdd40cc6a2556706d8b71909fb0f22..e2d2650c2a068117e75d8ebb3dad9071b3fe3381 100644 (file)
@@ -252,9 +252,11 @@ class wsLinks
             }
             if (isset($trulers[$infos['document_id']][$infos['document_page']])) {
                 $ru = $trulers[$infos['document_id']][$infos['document_page']];
-                foreach ($ru as $ruler) {
-                    $ruler['page'] = $p;
-                    $rulers[] = $ruler;
+                if (is_array($ru)) {
+                    foreach ($ru as $ruler) {
+                        $ruler['page'] = $p;
+                        $rulers[] = $ruler;
+                    }
                 }
             }
         }
index 073fbf14501b287bf4dda7cab8a6b9f72b94255d..63873f17d4da90ac782d4daaa3bb1b3cd6958a55 100644 (file)
@@ -344,7 +344,9 @@ class wsUtil
 \r
     public static function copy($source, $dest)\r
     {\r
-        copy($source, $dest);\r
-        touch($dest, filemtime($source));\r
+        if (file_exists($source)) {\r
+            copy($source, $dest);\r
+            touch($dest, filemtime($source));\r
+        }\r
     }\r
 }
\ No newline at end of file
index 44f1fa476c2647ea52643d6345a1c1ea74918e22..87916f45d3d6385bee09d3f5532bbf5fe0d42b29 100644 (file)
@@ -1368,7 +1368,7 @@ class htmlMultimediaLink extends wsHTML5Link
             foreach ($this->_config['inject'] as $i) {
                 $infos = ['path' => 'data/links/' . str_replace('.', '_', $this->alternative)];
                 $i = str_replace('$id', '"#l_' . $this->id . '"', $i);
-                $i = str_replace('$path', '"'.$infos['path'].'"', $i);
+                $i = str_replace('$path', '"' . $infos['path'] . '"', $i);
                 $i = str_replace('$init', CubeIT_Util_Json::encode($infos), $i);
                 $this->compiler->htmlmultimedia[] = $i;
             }
@@ -1872,21 +1872,21 @@ class zoomLink extends normalLink
         //error_log("--- Book Width: $bookwidth ---");
 
         $p = wsDAOBook::getDocumentPage($this->compiler->book_id, $this->page);
-        $pdfpath = wsDocument::getDir($p['document_id']) . 'original.pdf';
+        $pdfpath = wsDocument::getDir($p['document_id']) . '/pdf/p' . $p['document_page'] . '.pdf';
 
         $left = CubeIT_Files::tempnam();
         $leftfile = CubeIT_CommandLine_Poppler::extractArea($pdfpath,
-            $p['document_page'],
+            1,
             array('x' => $x, 'y' => $y, 'width' => $w, 'height' => $h),
             $left, $extractOptions, WS_CACHE . '/zoomarea/' . $this->compiler->book_id . '/');
 
         if (($x + $w) > $bookwidth) {
             $p = wsDAOBook::getDocumentPage($this->compiler->book_id, $this->page + 1);
-            $pdfpath = wsDocument::getDir($p['document_id']) . 'original.pdf';
+            $pdfpath = wsDocument::getDir($p['document_id']) . '/pdf/p' . $p['document_page'] . '.pdf';
             $diff = ($w + $x) - $bookwidth;
             $right = CubeIT_Files::tempnam();
             $rightfile = CubeIT_CommandLine_Poppler::extractArea($pdfpath,
-                $p['document_page'],
+                1,
                 array('x' => 0, 'y' => $y, 'width' => $diff, 'height' => $h),
                 $right, $extractOptions, WS_CACHE . '/zoomarea/' . $this->compiler->book_id . '/');