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);
+ }
}