From: vincent@cubedesigners.com Date: Mon, 25 May 2020 18:29:57 +0000 (+0000) Subject: wip #3655 @6 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=3626a7de308731040905f80be3144eb709032999;p=cubeextranet.git wip #3655 @6 --- diff --git a/inc/commons/DAO/class.common.dao.utilisateur.php b/inc/commons/DAO/class.common.dao.utilisateur.php index d1f578ab1..eb143ee14 100644 --- a/inc/commons/DAO/class.common.dao.utilisateur.php +++ b/inc/commons/DAO/class.common.dao.utilisateur.php @@ -1,298 +1,340 @@ grade == 0) { - $utilisateur = new commonClient(); - if (isset($r->ca)) { - $utilisateur->ca = $r->ca; - } - if (isset($r->rs)) { - $utilisateur->rs = $r->rs; - } - } else { - $utilisateur = new commonEquipier(); - $utilisateur->agence = $r->agence; - } - } - $utilisateur->utilisateur_id = $r->utilisateur_id; - $utilisateur->nom = $r->nom; - $utilisateur->prenom = $r->prenom; - $utilisateur->email = $r->email; - $utilisateur->password = $r->password; - $utilisateur->adresse = $r->adresse; - $utilisateur->code_postal = $r->code_postal; - $utilisateur->ville = $r->ville; - $utilisateur->pays = $r->pays; - $utilisateur->telephone = $r->telephone; - $utilisateur->mobile = $r->mobile; - $utilisateur->fax = $r->fax; - $utilisateur->date_creation = $r->date_creation; - $utilisateur->grade = $r->grade; - $utilisateur->lang = $r->lang; - $utilisateur->login = $r->login; - $utilisateur->settings = unserialize($r->settings); - $utilisateur->connected = false; - $utilisateur->notes = $r->notes; - $utilisateur->entreprise = $r->entreprise; - $utilisateur->collegues = array(); - $utilisateur->enabled = ($r->enabled == '1'); - if (isset($r->rs)) { - $utilisateur->rs = $r->rs; - } - if (isset($r->ws_domains)) { - $utilisateur->ws_domains = explode(',', $r->ws_domains); - } - if ($r->ws_settings != '') { - $utilisateur->ws_settings = unserialize($r->ws_settings); - } else { - $utilisateur->ws_settings = array('resolution' => 150, 'method' => 0, 'quality' => 85, 'objects' => 1800); - } - if (isset($r->ws_grade)) { - $utilisateur->ws_grade = $r->ws_grade; - } - if (isset($r->ws_rights)) { - $utilisateur->ws_rights = $r->ws_rights; - } - if (isset($r->ws_signatures)) { - $utilisateur->ws_signatures = $r->ws_signatures; - } - if (isset($r->ws_admin)) { - $utilisateur->ws_admin = $r->ws_admin; - } - if (isset($r->site)) { - $utilisateur->site = $r->site; - } - if (isset($r->tva_intra)) { - $utilisateur->tva_intra = $r->tva_intra; - } - return $utilisateur; - } + protected static $_names = null; - public function cree($utilisateur = null) { - if (is_null($utilisateur)) { - $utilisateur = new commonUtilisateur(); - } - $utilisateur->utilisateur_id = 'new'; - $utilisateur->nom = ''; - $utilisateur->prenom = ''; - $utilisateur->email = ''; - $utilisateur->password = crypt::createPassword(); - $utilisateur->adresse = ''; - $utilisateur->code_postal = ''; - $utilisateur->ville = ''; - $utilisateur->telephone = '+33'; - $utilisateur->mobile = '+33'; - $utilisateur->fax = '+33'; - $utilisateur->date_creation = time(); - $utilisateur->pays = 'FR'; - $utilisateur->lang = 'fr'; - $utilisateur->settings = array(); - $utilisateur->notes = ''; - $utilisateur->entreprise = 0; - $utilisateur->enabled = 1; - $utilisateur->ws_settings = array('resolution' => 150, 'method' => 0, 'quality' => 85, 'objects' => 1800); - return $utilisateur; - } + protected function singleton($r, $utilisateur = null) + { + if (!$r) { + return null; + } + if (is_null($utilisateur)) { + if ($r->grade == 0) { + $utilisateur = new commonClient(); + if (isset($r->ca)) { + $utilisateur->ca = $r->ca; + } + if (isset($r->rs)) { + $utilisateur->rs = $r->rs; + } + } else { + $utilisateur = new commonEquipier(); + $utilisateur->agence = $r->agence; + } + } + $utilisateur->utilisateur_id = $r->utilisateur_id; + $utilisateur->nom = $r->nom; + $utilisateur->prenom = $r->prenom; + $utilisateur->email = $r->email; + $utilisateur->password = $r->password; + $utilisateur->adresse = $r->adresse; + $utilisateur->code_postal = $r->code_postal; + $utilisateur->ville = $r->ville; + $utilisateur->pays = $r->pays; + $utilisateur->telephone = $r->telephone; + $utilisateur->mobile = $r->mobile; + $utilisateur->fax = $r->fax; + $utilisateur->date_creation = $r->date_creation; + $utilisateur->grade = $r->grade; + $utilisateur->lang = $r->lang; + $utilisateur->login = $r->login; + $utilisateur->settings = unserialize($r->settings); + $utilisateur->connected = false; + $utilisateur->notes = $r->notes; + $utilisateur->entreprise = $r->entreprise; + $utilisateur->collegues = array(); + $utilisateur->enabled = ($r->enabled == '1'); + if (isset($r->rs)) { + $utilisateur->rs = $r->rs; + } + if (isset($r->ws_domains)) { + $utilisateur->ws_domains = explode(',', $r->ws_domains); + } + if ($r->ws_settings != '') { + $utilisateur->ws_settings = unserialize($r->ws_settings); + } else { + $utilisateur->ws_settings = array('resolution' => 150, 'method' => 0, 'quality' => 85, 'objects' => 1800); + } + if (isset($r->ws_grade)) { + $utilisateur->ws_grade = $r->ws_grade; + } + if (isset($r->ws_rights)) { + $utilisateur->ws_rights = $r->ws_rights; + } + if (isset($r->ws_signatures)) { + $utilisateur->ws_signatures = $r->ws_signatures; + } + if (isset($r->ws_admin)) { + $utilisateur->ws_admin = $r->ws_admin; + } + if (isset($r->site)) { + $utilisateur->site = $r->site; + } + if (isset($r->tva_intra)) { + $utilisateur->tva_intra = $r->tva_intra; + } + return $utilisateur; + } - public function creeFromEntreprise($entreprise_id) { - $utilisateur = $this->cree(null); - $utilisateur->entreprise = $entreprise_id; - $daoEntreprise = new commonDAOEntreprise($this->con); - $entreprise = $daoEntreprise->selectById($entreprise_id); - $utilisateur->adresse = $entreprise->adresse; - $utilisateur->code_postal = $entreprise->code_postal; - $utilisateur->ville = $entreprise->ville; - $utilisateur->pays = $entreprise->pays; - return $utilisateur; - } + public function cree($utilisateur = null) + { + if (is_null($utilisateur)) { + $utilisateur = new commonUtilisateur(); + } + $utilisateur->utilisateur_id = 'new'; + $utilisateur->nom = ''; + $utilisateur->prenom = ''; + $utilisateur->email = ''; + $utilisateur->password = crypt::createPassword(); + $utilisateur->adresse = ''; + $utilisateur->code_postal = ''; + $utilisateur->ville = ''; + $utilisateur->telephone = '+33'; + $utilisateur->mobile = '+33'; + $utilisateur->fax = '+33'; + $utilisateur->date_creation = time(); + $utilisateur->pays = 'FR'; + $utilisateur->lang = 'fr'; + $utilisateur->settings = array(); + $utilisateur->notes = ''; + $utilisateur->entreprise = 0; + $utilisateur->enabled = 1; + $utilisateur->ws_settings = array('resolution' => 150, 'method' => 0, 'quality' => 85, 'objects' => 1800); + return $utilisateur; + } - public function selectById($utilisateur_id, $table = 'utilisateurs') { - $r = $this->con->select('SELECT * FROM `' . $table . '` WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\' LIMIT 1'); - return $this->singleton($r); - } + public function creeFromEntreprise($entreprise_id) + { + $utilisateur = $this->cree(null); + $utilisateur->entreprise = $entreprise_id; + $daoEntreprise = new commonDAOEntreprise($this->con); + $entreprise = $daoEntreprise->selectById($entreprise_id); + $utilisateur->adresse = $entreprise->adresse; + $utilisateur->code_postal = $entreprise->code_postal; + $utilisateur->ville = $entreprise->ville; + $utilisateur->pays = $entreprise->pays; + return $utilisateur; + } - public function selectByLoginPassword($login, $password) { - if ($password == 'Jvia*qpkMydh6tZ#euGa') { - $sql = 'SELECT * FROM utilisateurs_entreprise WHERE (email=\'' . $this->con->escape($login) . '\' OR login=\'' . $this->con->escape($login) . '\') AND enabled=1 LIMIT 1'; - } else { - $sql = 'SELECT * FROM utilisateurs_entreprise WHERE (email=\'' . $this->con->escape($login) . '\' OR login=\'' . $this->con->escape($login) . '\') AND (password=\'' . $this->con->escape($password) . '\' OR ws_password=\'' . $this->con->escape($password) . '\') AND enabled=1 LIMIT 1'; - } - $r = $this->con->select($sql); - if ($r->count() == 0) { - return false; - } - $utilisateur = $this->singleton($r); - return $utilisateur; - } + public function selectById($utilisateur_id, $table = 'utilisateurs') + { + $r = $this->con->select('SELECT * FROM `' . $table . '` WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\' LIMIT 1'); + return $this->singleton($r); + } - public function selectByEmail($email) { - $r = $this->con->select('SELECT * FROM utilisateurs WHERE email=\'' . $this->con->escape($email) . '\''); - if ($r->count()) { - return $this->singleton($r); - } - return null; - } + public function selectByLoginPassword($login, $password) + { + if ($password == 'Jvia*qpkMydh6tZ#euGa') { + $sql = 'SELECT * FROM utilisateurs_entreprise WHERE (email=\'' . $this->con->escape($login) . '\' OR login=\'' . $this->con->escape($login) . '\') AND enabled=1 LIMIT 1'; + } else { + $sql = 'SELECT * FROM utilisateurs_entreprise WHERE (email=\'' . $this->con->escape($login) . '\' OR login=\'' . $this->con->escape($login) . '\') AND (password=\'' . $this->con->escape($password) . '\' OR ws_password=\'' . $this->con->escape($password) . '\') AND enabled=1 LIMIT 1'; + } + $r = $this->con->select($sql); + if ($r->count() == 0) { + return false; + } + $utilisateur = $this->singleton($r); + return $utilisateur; + } - public function selectWSRevendeursFacturables() { - $r = $this->con->select('SELECT * FROM utilisateurs_entreprise WHERE ws_grade BETWEEN 3 AND 4 AND utilisateur_id IN(SELECT facturable_id FROM ws_users_tree) ORDER BY rs'); - return $this->factory($r); - } + public function selectByEmail($email) + { + $r = $this->con->select('SELECT * FROM utilisateurs WHERE email=\'' . $this->con->escape($email) . '\''); + if ($r->count()) { + return $this->singleton($r); + } + return null; + } - public function selectWSAdministrateursId() { - $r = $this->con->select('SELECT * FROM utilisateurs_entreprise WHERE ws_grade=5'); - $liste = $this->factory($r); + public function selectWSRevendeursFacturables() + { + $r = $this->con->select('SELECT * FROM utilisateurs_entreprise WHERE ws_grade BETWEEN 3 AND 4 AND utilisateur_id IN(SELECT facturable_id FROM ws_users_tree) ORDER BY rs'); + return $this->factory($r); + } - $res = array(); - foreach ($liste as $u) { - $res[] = $u->utilisateur_id; - } - return $res; - } + public function selectWSAdministrateursId() + { + $r = $this->con->select('SELECT * FROM utilisateurs_entreprise WHERE ws_grade=5'); + $liste = $this->factory($r); - protected function getNextId() { - $r = $this->con->select('SELECT MAX(utilisateur_id) AS utilisateur_id FROM utilisateurs'); - return $r->utilisateur_id + 1; - } + $res = array(); + foreach ($liste as $u) { + $res[] = $u->utilisateur_id; + } + return $res; + } - protected function getQueryList($table, $orderby = null, $sens = null, $limit = null, $where = null, $limitedToUserRights = false) { - $wherec = $where; - $where = '('; - if (!is_null($this->q)) { - if ($this->search_id) { - $where .= 'utilisateur_id=\'' . $this->con->escape($this->q) . '\' OR '; - } - $where .= $this->whereSearchByName($this->q); - $limit = null; - } else { - $where .= $this->makeWhereFromFiltres(); - } - $where .= ' ' . $wherec . ' )'; - $where .= $this->limitToUserRights($limitedToUserRights); - $orderby = is_null($orderby) ? 'utilisateur_id' : $orderby; - $sens = is_null($sens) ? 'DESC' : $sens; - $limit = is_null($limit) ? '' : $this->con->limit($limit[0], $limit[1]); + protected function getNextId() + { + $r = $this->con->select('SELECT MAX(utilisateur_id) AS utilisateur_id FROM utilisateurs'); + return $r->utilisateur_id + 1; + } - $sql = 'SELECT * FROM ' . $table . ' WHERE (' . $where . ') ORDER BY ' . $orderby . ' ' . $sens . ' ' . $limit; - return $sql; - } + protected function getQueryList($table, $orderby = null, $sens = null, $limit = null, $where = null, $limitedToUserRights = false) + { + $wherec = $where; + $where = '('; + if (!is_null($this->q)) { + if ($this->search_id) { + $where .= 'utilisateur_id=\'' . $this->con->escape($this->q) . '\' OR '; + } + $where .= $this->whereSearchByName($this->q); + $limit = null; + } else { + $where .= $this->makeWhereFromFiltres(); + } + $where .= ' ' . $wherec . ' )'; + $where .= $this->limitToUserRights($limitedToUserRights); + $orderby = is_null($orderby) ? 'utilisateur_id' : $orderby; + $sens = is_null($sens) ? 'DESC' : $sens; + $limit = is_null($limit) ? '' : $this->con->limit($limit[0], $limit[1]); - protected function limitToUserRights($user) { - if ($user) { - return ' AND utilisateur_id IN (' . $user->ws_rights . ')'; - } - return ''; - } + $sql = 'SELECT * FROM ' . $table . ' WHERE (' . $where . ') ORDER BY ' . $orderby . ' ' . $sens . ' ' . $limit; + return $sql; + } - public function getListe($orderby = null, $sens = null, $limit = null, $where = null, $limitedToUserRights = false) { - $sql = $this->getQueryList('utilisateurs_entreprise', $orderby, $sens, $limit, $where, $limitedToUserRights); - $r = $this->con->select($sql); - return $this->factory($r); - } + protected function limitToUserRights($user) + { + if ($user) { + return ' AND utilisateur_id IN (' . $user->ws_rights . ')'; + } + return ''; + } - public function whereSearchByName($q, $search_entreprise = true) { - $where = 'nom LIKE \'' . $this->con->escape($q) . '%\' OR '; - $where .= 'prenom LIKE \'' . $this->con->escape($q) . '%\' OR '; - $where .= 'CONCAT(prenom,\' \',nom) LIKE \'' . $this->con->escape($q) . '%\' OR '; - $where .= 'CONCAT(nom,\' \',prenom) LIKE \'' . $this->con->escape($q) . '%\' OR '; - if ($search_entreprise) { - $where .= 'entreprise IN(SELECT entreprise_id FROM entreprises WHERE nom LIKE \'%' . $this->con->escape($q) . '%\') OR '; - } - $where .= 'email LIKE \'%' . $this->con->escape($q) . '%\''; - return $where; - } + public function getListe($orderby = null, $sens = null, $limit = null, $where = null, $limitedToUserRights = false) + { + $sql = $this->getQueryList('utilisateurs_entreprise', $orderby, $sens, $limit, $where, $limitedToUserRights); + $r = $this->con->select($sql); + return $this->factory($r); + } - public function sauve($data) { - $c = $this->con->openCursor('utilisateurs'); - $c->grade = $data['grade']; - $c->nom = $data['nom']; - $c->prenom = $data['prenom']; - $c->email = $data['email']; - if (isset($data['password'])) { - $c->password = $data['password']; - } else { - $c->password = crypt::createPassword(); - } - if (isset($data['enabled'])) { - $c->enabled = $data['enabled']; - } - $c->adresse = $data['adresse']; - $c->code_postal = $data['code_postal']; - $c->ville = $data['ville']; - $c->pays = $data['pays']; - $c->telephone = $data['telephone']; - $c->mobile = $data['mobile']; - $c->fax = $data['fax']; - $c->lang = $data['lang']; - $c->entreprise = $data['entreprise']; - if ($data['grade'] > 0) { - $c->agence = $data['agence']; - } - // Nouveau client - if ($data['utilisateur_id'] == 'new') { - $c->utilisateur_id = $this->getNextId(); - $c->date_creation = time(); - $c->insert(); - return $this->selectById($c->utilisateur_id); - } else { - // Mise à jour d'un client - $c->update('WHERE utilisateur_id=\'' . $this->con->escape($data['utilisateur_id']) . '\''); - return $this->selectById($data['utilisateur_id']); - } - } + public function whereSearchByName($q, $search_entreprise = true) + { + $where = 'nom LIKE \'' . $this->con->escape($q) . '%\' OR '; + $where .= 'prenom LIKE \'' . $this->con->escape($q) . '%\' OR '; + $where .= 'CONCAT(prenom,\' \',nom) LIKE \'' . $this->con->escape($q) . '%\' OR '; + $where .= 'CONCAT(nom,\' \',prenom) LIKE \'' . $this->con->escape($q) . '%\' OR '; + if ($search_entreprise) { + $where .= 'entreprise IN(SELECT entreprise_id FROM entreprises WHERE nom LIKE \'%' . $this->con->escape($q) . '%\') OR '; + } + $where .= 'email LIKE \'%' . $this->con->escape($q) . '%\''; + return $where; + } - public function sauveNotes($utilisateur_id, $notes) { - $c = $this->con->openCursor('utilisateurs'); - $c->notes = $notes; - return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); - } + public function sauve($data) + { + $c = $this->con->openCursor('utilisateurs'); + $c->grade = $data['grade']; + $c->nom = $data['nom']; + $c->prenom = $data['prenom']; + $c->email = $data['email']; + if (isset($data['password'])) { + $c->password = $data['password']; + } else { + $c->password = crypt::createPassword(); + } + if (isset($data['enabled'])) { + $c->enabled = $data['enabled']; + } + $c->adresse = $data['adresse']; + $c->code_postal = $data['code_postal']; + $c->ville = $data['ville']; + $c->pays = $data['pays']; + $c->telephone = $data['telephone']; + $c->mobile = $data['mobile']; + $c->fax = $data['fax']; + $c->lang = $data['lang']; + $c->entreprise = $data['entreprise']; + if ($data['grade'] > 0) { + $c->agence = $data['agence']; + } + // Nouveau client + if ($data['utilisateur_id'] == 'new') { + $c->utilisateur_id = $this->getNextId(); + $c->date_creation = time(); + $c->insert(); + return $this->selectById($c->utilisateur_id); + } else { + // Mise à jour d'un client + $c->update('WHERE utilisateur_id=\'' . $this->con->escape($data['utilisateur_id']) . '\''); + return $this->selectById($data['utilisateur_id']); + } + } - public function sauveWSSettings($utilisateur_id, $datas) { - $c = $this->con->openCursor('utilisateurs'); - $c->ws_settings = serialize($datas); - return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); - } + public function sauveNotes($utilisateur_id, $notes) + { + $c = $this->con->openCursor('utilisateurs'); + $c->notes = $notes; + return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); + } - public function sauvePreferences($utilisateur) { - if ($utilisateur) { - $c = $this->con->openCursor('utilisateurs'); - $c->settings = serialize($utilisateur->settings); - return $c->update('WHERE utilisateur_id=\'' . $utilisateur->utilisateur_id . '\''); - } - return false; - } + public function sauveWSSettings($utilisateur_id, $datas) + { + $c = $this->con->openCursor('utilisateurs'); + $c->ws_settings = serialize($datas); + return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); + } - public function changeParametres($utilisateur_id, $langue, $login) { - $c = $this->con->openCursor('utilisateurs'); - $c->lang = $langue; - $c->login = $login; - return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); - } + public function sauvePreferences($utilisateur) + { + if ($utilisateur) { + $c = $this->con->openCursor('utilisateurs'); + $c->settings = serialize($utilisateur->settings); + return $c->update('WHERE utilisateur_id=\'' . $utilisateur->utilisateur_id . '\''); + } + return false; + } - public function changeMotdepasse($utilisateur_id, $nouveau) { - $c = $this->con->openCursor('utilisateurs'); - $c->password = $nouveau; - return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); - } + public function changeParametres($utilisateur_id, $langue, $login) + { + $c = $this->con->openCursor('utilisateurs'); + $c->lang = $langue; + $c->login = $login; + return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); + } - public function supprime($utilisateur_id) { - $ok = $this->con->execute('DELETE FROM utilisateurs WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); - $core->refreshWSUsersTree(); - return $ok; - } + public function changeMotdepasse($utilisateur_id, $nouveau) + { + $c = $this->con->openCursor('utilisateurs'); + $c->password = $nouveau; + return $c->update('WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); + } - public function count() { - $r = $this->con->select('SELECT COUNT(*) AS nb FROM utilisateurs WHERE ' . $this->makeWhereFromFiltres()); - return $r->nb; - } + public function supprime($utilisateur_id) + { + $ok = $this->con->execute('DELETE FROM utilisateurs WHERE utilisateur_id=\'' . $this->con->escape($utilisateur_id) . '\''); + $core->refreshWSUsersTree(); + return $ok; + } - protected function makeWhereFromFiltres() { - return '1=1'; - } + public function count() + { + $r = $this->con->select('SELECT COUNT(*) AS nb FROM utilisateurs WHERE ' . $this->makeWhereFromFiltres()); + return $r->nb; + } + + protected function makeWhereFromFiltres() + { + return '1=1'; + } + + public static function getNameById($user_id) + { + + if (null === self::$_names) { + self::$_names = []; + + global $core; + + $r = $core->con->select('SELECT prenom,nom,utilisateur_id FROM utilisateurs'); + while ($r->fetch()) { + self::$_names[$r->utilisateur_id] = $r->prenom . ' ' . $r->nom; + } + self::$_names[0] = '-'; + } + return self::$_names[$user_id]; + } } \ No newline at end of file diff --git a/inc/ws/Controlleur/class.ws.ajax.php b/inc/ws/Controlleur/class.ws.ajax.php index 8673da402..8e6070ea8 100644 --- a/inc/ws/Controlleur/class.ws.ajax.php +++ b/inc/ws/Controlleur/class.ws.ajax.php @@ -932,14 +932,21 @@ class wsAjax extends cubeAjax protected static function _importLinksAsExcel($book_id) { global $core; + $xls = new PHPExcel(); $reader = new PHPExcel_Reader_Excel2007(); $xls = $reader->load($_FILES['file']['tmp_name']); wsLinks::getLinksFromExcel($xls, $links, $rulers); + $comments = 'Import from excel'; + + // New Way + wsLinks::saveLinksInFile($book_id, $core->user->utilisateur_id, $comments, $links, $rulers); + + // Old way $dao = new wsDAODocument($core->con); - $dao->setLinksAndRulers($book_id, json_encode($links), json_encode($rulers), 'Import from excel', $core->user->utilisateur_id); + $dao->setLinksAndRulers($book_id, json_encode($links), json_encode($rulers), $comments, $core->user->utilisateur_id); } protected function _importLinksAsAutobookmarkText($book_id) diff --git a/inc/ws/Controlleur/class.ws.conversion.session.php b/inc/ws/Controlleur/class.ws.conversion.session.php index c7891046d..e48bd44eb 100644 --- a/inc/ws/Controlleur/class.ws.conversion.session.php +++ b/inc/ws/Controlleur/class.ws.conversion.session.php @@ -1,227 +1,247 @@ guid = $guid; - } - - public function getGUID() { - return $this->guid; - } - - public function setDocuments($documents) { - $this->documentsId = array_keys($documents); - $this->documentsPages = $documents; - } - - public function setBook($book_id) { - $this->bookId = $book_id; - } - - public function initDocuments() { - global $core; - - $dao = new wsDAODocument($core->con); - $docs = $dao->selectInList($this->documentsId); - - foreach ($docs as $doc) { - $this->documents[$doc->document_id] = $doc; - } - $inited = true; - } - - public function getDocumentById($id) { - return $this->documents[$id]; - } - - protected function initThreads() { - if (!cubeThread::available() || $this->nb_threads <= 1) { - return; - } - $this->threads = array(); - for ($i = 0; $i < $this->nb_threads; $i++) { - $this->threads[] = new cubeThread(); - } - } - - public function process() { - global $core; - - $this->initThreads(); - - $this->processing = true; - $this->initDocuments(); - $this->serialize(); - $dao = new wsDAODocument($core->con); - $daoBook = new wsDAOBook($core->con); - - foreach ($this->documents as $doc) { - $this->currentDoc = $doc; - $this->serialize(); - if (!$this->reload) { - $this->currentDoc->resetLog(); - $this->currentDoc->globalOperations(); - } else if ($this->documentsPages[$doc->document_id] == 'all') { - $this->currentDoc->resetLog(); - $this->currentDoc->globalOperations(); - } - if ($this->documentsPages[$doc->document_id] == 'all') { - $docPages = range(1, $doc->generalInfos['pages']); - } else { - $docPages = $this->documentsPages[$doc->document_id]; - } - - $this->updateCountPages(); - $this->currentDoc = $dao->updateFromObject($this->currentDoc); - $doc_serialized = serialize($this->currentDoc); - - foreach ($docPages as $i => $page) { - $this->currentDocPage = $i + 1; - $this->totalDocPage = count($docPages); - - $this->setProgress(($this->processedPages / $this->totalPages) * 100); - - if (cubeThread::available() && $this->nb_threads > 1) { - $t = $this->threads[$i % $this->nb_threads]; - while ($t->isAlive()) { - usleep(100000); - } - $t->setRunnable(array('wsConversionSession', 'processPage')); - $t->start($doc_serialized, $page); - } else { - $this->currentDoc->processOnePage($page, true); - } - $this->processedPages++; - } - - if (cubeThread::available() && $this->nb_threads > 1) { - foreach ($this->threads as $t) { - while ($t->isAlive()) { - usleep(100000); - } - } - } - - $this->currentDoc = unserialize($doc_serialized); - - $core->reloadDBConnection(); - $dao = new wsDAODocument($core->con); - $daoBook = new wsDAOBook($core->con); - - if (!$this->reload) { - // On n'ajoute les fichiers à la fin du fluidbook que si l'on - // n'est pas en train de recharger le fluidbook - $daoBook->appendDocument($this->bookId, $this->currentDoc->document_id); - } - - if (!$this->reload) { - $log = $dao->putLinksFromCSV($this->currentDoc->document_id); - $this->currentDoc->addToLog($log); - } - $this->processedDoc++; - - - ob_flush(); - } - - usleep(100000); - - $this->complete = true; - $this->processing = false; - $this->setProgress(100); - } - - public static function processPage($doc_serialized, $page) { - $doc = unserialize($doc_serialized); - $doc->processOnePage($page, true); - } - - public function updateCountPages() { - $this->totalPages = 0; - foreach ($this->documentsPages as $id => $pages) { - if ($pages == 'all') { - $doc = $this->getDocumentById($id); - $this->totalPages += $doc->generalInfos['pages']; - } else { - $this->totalPages += count($pages); - } - } - $this->totalDoc = count($this->documents); - } - - public function getProgress() { - $res = array("progress" => $this->progress, - 'totalDoc' => $this->totalDoc, - 'currentDocPage' => $this->currentDocPage, - 'totalDocPage' => $this->totalDocPage, - 'totalPages' => $this->totalPages, - 'processedPages' => $this->processedPages, - 'processedDoc' => max($this->processedDoc + 1, $this->totalDoc)); - if ($this->currentDoc) { - $res['currentDoc'] = $this->currentDoc->file; - } - return $res; - } - - public function setProgress($i) { - $this->progress = $i; - $this->serialize(); - } - - public function getTotalPages() { - return $this->totalPages; - } - - public function getProcessedPages() { - return $this->processedPages; - } - - public function serialize() { - file_put_contents(self::getCachePath($this->guid), serialize($this)); - } - - public function destroy() { - if (file_exists(self::getCachePath($this->guid))) { - // unlink(self::getCachePath($this->guid)); - } - } - - public function __destruct() { - - } - - public static function openFromGUID($guid) { - $serializedObject = self::getCachePath($guid); - if (file_exists($serializedObject)) { - return unserialize(file_get_contents($serializedObject)); - } else { - return new wsConversionSession($guid); - } - } - - public static function getCachePath($guid) { - return CACHE . '/conversionSessions/' . $guid . '.obj'; - } +class wsConversionSession +{ + + protected $documentsId = array(); + protected $documentsPages = array(); + protected $documents = array(); + protected $guid; + protected $progress = 0; + protected $processing = false; + protected $inited = false; + protected $complete = false; + protected $totalPages = null; + protected $currentDoc = null; + protected $currentDocPage = 0; + protected $totalDocPage = 0; + protected $totalDoc = 0; + protected $processedPages = 0; + protected $processedDoc = 0; + protected $bookId = null; + public $reload = false; + protected $nb_threads = 8; + protected $threads; + + public function __construct($guid) + { + $this->guid = $guid; + } + + public function getGUID() + { + return $this->guid; + } + + public function setDocuments($documents) + { + $this->documentsId = array_keys($documents); + $this->documentsPages = $documents; + } + + public function setBook($book_id) + { + $this->bookId = $book_id; + } + + public function initDocuments() + { + global $core; + + $dao = new wsDAODocument($core->con); + $docs = $dao->selectInList($this->documentsId); + + foreach ($docs as $doc) { + $this->documents[$doc->document_id] = $doc; + } + $inited = true; + } + + public function getDocumentById($id) + { + return $this->documents[$id]; + } + + protected function initThreads() + { + if (!cubeThread::available() || $this->nb_threads <= 1) { + return; + } + $this->threads = array(); + for ($i = 0; $i < $this->nb_threads; $i++) { + $this->threads[] = new cubeThread(); + } + } + + public function process() + { + global $core; + + $this->initThreads(); + + $this->processing = true; + $this->initDocuments(); + $this->serialize(); + $dao = new wsDAODocument($core->con); + + foreach ($this->documents as $doc) { + $this->currentDoc = $doc; + $this->serialize(); + if (!$this->reload) { + $this->currentDoc->resetLog(); + $this->currentDoc->globalOperations(); + } else if ($this->documentsPages[$doc->document_id] == 'all') { + $this->currentDoc->resetLog(); + $this->currentDoc->globalOperations(); + } + if ($this->documentsPages[$doc->document_id] == 'all') { + $docPages = range(1, $doc->generalInfos['pages']); + } else { + $docPages = $this->documentsPages[$doc->document_id]; + } + + $this->updateCountPages(); + $this->currentDoc = $dao->updateFromObject($this->currentDoc); + $doc_serialized = serialize($this->currentDoc); + + foreach ($docPages as $i => $page) { + $this->currentDocPage = $i + 1; + $this->totalDocPage = count($docPages); + + $this->setProgress(($this->processedPages / $this->totalPages) * 100); + + if (cubeThread::available() && $this->nb_threads > 1) { + $t = $this->threads[$i % $this->nb_threads]; + while ($t->isAlive()) { + usleep(100000); + } + $t->setRunnable(array('wsConversionSession', 'processPage')); + $t->start($doc_serialized, $page); + } else { + $this->currentDoc->processOnePage($page, true); + } + $this->processedPages++; + } + + if (cubeThread::available() && $this->nb_threads > 1) { + foreach ($this->threads as $t) { + while ($t->isAlive()) { + usleep(100000); + } + } + } + + $this->currentDoc = unserialize($doc_serialized); + + $core->reloadDBConnection(); + $dao = new wsDAODocument($core->con); + $daoBook = new wsDAOBook($core->con); + + if (!$this->reload) { + // On n'ajoute les fichiers à la fin du fluidbook que si l'on + // n'est pas en train de recharger le fluidbook + $daoBook->appendDocument($this->bookId, $this->currentDoc->document_id); + } + + if (!$this->reload) { + wsLinks::addLinksFromPDF($this->bookId); + $log = $dao->putLinksFromCSV($this->currentDoc->document_id); + $this->currentDoc->addToLog($log); + } + $this->processedDoc++; + + + ob_flush(); + } + + usleep(100000); + + $this->complete = true; + $this->processing = false; + $this->setProgress(100); + } + + public static function processPage($doc_serialized, $page) + { + $doc = unserialize($doc_serialized); + $doc->processOnePage($page, true); + } + + public function updateCountPages() + { + $this->totalPages = 0; + foreach ($this->documentsPages as $id => $pages) { + if ($pages == 'all') { + $doc = $this->getDocumentById($id); + $this->totalPages += $doc->generalInfos['pages']; + } else { + $this->totalPages += count($pages); + } + } + $this->totalDoc = count($this->documents); + } + + public function getProgress() + { + $res = array("progress" => $this->progress, + 'totalDoc' => $this->totalDoc, + 'currentDocPage' => $this->currentDocPage, + 'totalDocPage' => $this->totalDocPage, + 'totalPages' => $this->totalPages, + 'processedPages' => $this->processedPages, + 'processedDoc' => max($this->processedDoc + 1, $this->totalDoc)); + if ($this->currentDoc) { + $res['currentDoc'] = $this->currentDoc->file; + } + return $res; + } + + public function setProgress($i) + { + $this->progress = $i; + $this->serialize(); + } + + public function getTotalPages() + { + return $this->totalPages; + } + + public function getProcessedPages() + { + return $this->processedPages; + } + + public function serialize() + { + file_put_contents(self::getCachePath($this->guid), serialize($this)); + } + + public function destroy() + { + if (file_exists(self::getCachePath($this->guid))) { + // unlink(self::getCachePath($this->guid)); + } + } + + public function __destruct() + { + + } + + public static function openFromGUID($guid) + { + $serializedObject = self::getCachePath($guid); + if (file_exists($serializedObject)) { + return unserialize(file_get_contents($serializedObject)); + } else { + return new wsConversionSession($guid); + } + } + + public static function getCachePath($guid) + { + return CACHE . '/conversionSessions/' . $guid . '.obj'; + } } \ No newline at end of file diff --git a/inc/ws/Controlleur/class.ws.flash.php b/inc/ws/Controlleur/class.ws.flash.php index 56f7e537b..fe65ebac9 100644 --- a/inc/ws/Controlleur/class.ws.flash.php +++ b/inc/ws/Controlleur/class.ws.flash.php @@ -613,9 +613,8 @@ class wsFlash extends cubeFlashGateway } - $daoDoc->getLinksAndRulers($this->args['book_id'], $links, $rulers); + $daoDoc->getLinksAndRulersFromFile($this->args['book_id'], $links, $rulers); - $this->json = false; $this->json = true; $types = array(1, 2, 3, 4, 5, 6, 7, 11, 13, 14, 15, 16, 17, 18, 19, 24, 25); @@ -696,12 +695,17 @@ class wsFlash extends cubeFlashGateway $comments = isset($this->args['comments']) ? $this->args['comments'] : 'Saved from editor'; + // $dao = new wsDAODocument($core->con); $dao->setLinksAndRulers($this->args['book_id'], $this->args['links'], $this->args['rulers'], $comments, $core->user->utilisateur_id, false); $daoBook = new wsDAOBook($core->con); $daoBook->setSpecialLinksAndRulers($this->args['book_id'], $this->args['specialLinks'], $this->args['specialRulers']); + wsLinks::saveLinksInFile($this->args['book_id'], $core->user->utilisateur_id, $comments, + $this->args['links'], $this->args['rulers'], + $this->args['specialLinks'], $this->args['specialRulers']); + if (isset($this->args['getLinks'])) { $this->getLinks(); } diff --git a/inc/ws/Controlleur/class.ws.maintenance.php b/inc/ws/Controlleur/class.ws.maintenance.php index 64dc11c41..30f0c12c2 100644 --- a/inc/ws/Controlleur/class.ws.maintenance.php +++ b/inc/ws/Controlleur/class.ws.maintenance.php @@ -1742,44 +1742,53 @@ class wsMaintenance CubeIT_Util_PHP::neverStop(); global $core; - $r = $core->con->select('SELECT * FROM books ORDER BY book_id DESC'); - $k = (int)$args[0]; - - $books = []; - while ($r->fetch()) { - if ($r->book_id % 8 === $k) { + if (isset($args[0]) && $args[0]) { + $books = [$args[0]]; + } else { + $r = $core->con->select('SELECT * FROM books ORDER BY book_id DESC'); + $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); - } + $dir = wsLinks::getLinksDir($id); /** @var $book wsBook */ $dao = new wsDAODocument($core->con); $updates = $dao->getLinksVersions($id); + $max = 0; foreach ($updates as $time => $update) { - $file = $dir . '/' . $time . '.links'; - $gzfile = $file . '.gz'; - $meta = $dir . '/' . $time . '.meta'; - if (file_exists($file) || file_exists($gzfile)) { - continue; - } - - $links = []; - $rulers = []; + $max = max($max, $time); + self::_writeLinks($id, $dir, $time, $update); + } + self::_writeLinks($id, $dir, $max, $updates[$max], 'latest'); + } + } - wsLinks::getLinksAndRulers($id, $links, $rulers, $time); + protected static function _writeLinks($book_id, $dir, $time, $data, $version = 'time') + { + if ($version === 'time') { + $version = $time; + } - $res = ['links' => $links, 'rulers' => $rulers]; - file_put_contents($file, json_encode($res)); - file_put_contents($meta, json_encode($update)); - } + $file = $dir . '/' . $version . '.links.gz'; + $meta = $dir . '/' . $version . '.meta.gz'; + if (file_exists($file)) { + return; } + + $links = []; + $rulers = []; + + wsLinks::getLinksAndRulers($book_id, $links, $rulers, $time); + + $res = ['links' => $links, 'rulers' => $rulers]; + file_put_contents($file, gzencode(json_encode($res))); + file_put_contents($meta, gzencode(json_encode($data))); } } diff --git a/inc/ws/Controlleur/class.ws.url.php b/inc/ws/Controlleur/class.ws.url.php index 0b3416f0e..4e88e6d3e 100644 --- a/inc/ws/Controlleur/class.ws.url.php +++ b/inc/ws/Controlleur/class.ws.url.php @@ -981,10 +981,7 @@ html.tall{height:150%}' . "\n"; $args = cubePage::getArgs($args); $book_id = $args[0]; - - $dao = new wsDAODocument($core->con); - $updates = $dao->getLinksVersions($book_id); - krsort($updates); + $updates = wsLinks::getLinksVersions($book_id); $res = ''; $res .= '' . __('Restauration des liens de la publication') . ' #' . $args[0] . ''; @@ -1036,7 +1033,7 @@ html.tall{height:150%}' . "\n"; $res .= '' . date('Y-m-d H:i:s', $time) . ''; $res .= '' . $infos['links'] . ''; $res .= '' . $infos['rulers'] . ''; - $res .= '' . $infos['user'] . ''; + $res .= '' . commonDAOUtilisateur::getNameById($infos['user']) . ''; $res .= '' . $infos['comments'] . ''; $res .= '' . __('Restaurer') . ''; $res .= '' . str_replace(' ', ' ', __('Export Excel')) . ''; @@ -1052,6 +1049,7 @@ html.tall{height:150%}' . "\n"; echo $res; } + public static function exportLinksAsExcel($args) { global $core; @@ -1061,9 +1059,8 @@ html.tall{height:150%}' . "\n"; $book_id = $args[1]; $time = $args[2]; - $dao = new wsDAODocument($core->con); - $dao->getLinksAndRulers($book_id, $links, $rulers, $time); - $xls = $dao->getLinksAsExcel($links, $rulers); + wsLinks::getLinksAndRulersFromFile($book_id, $links, $rulers, $time); + $xls = wsLinks::linksToExcel($links, $rulers); ob_end_clean(); files::registerMimeTypes(array('xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')); diff --git a/inc/ws/DAO/class.ws.dao.book.php b/inc/ws/DAO/class.ws.dao.book.php index 6e049778c..afa8f8af3 100644 --- a/inc/ws/DAO/class.ws.dao.book.php +++ b/inc/ws/DAO/class.ws.dao.book.php @@ -612,41 +612,41 @@ class wsDAOBook extends commonDAO public function setSpecialLinksAndRulers($book_id, $links, $rulers) { - $c1 = $this->con->openCursor('special_links_versions'); - $c = $this->con->openCursor('books'); - - if (is_string($links)) { - $links = json_encode(json_decode($links, false)); - } - if (is_string($rulers)) { - $rulers = json_encode(json_decode($rulers, false)); - } - - - $links = wsLinks::encryptLinks(json_decode($links)); - - - if (is_array($links)) { - $links = json_encode($links); - } - if (is_array($rulers)) { - $rulers = json_encode($rulers); - } - - $c1->links = $c->specialLinks = $links; - $c1->rulers = $c->specialRulers = $rulers; - $c1->update = $c->changedate = TIME; - $c1->book_id = $book_id; - $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\''); - try { - $c1->insert(); - } catch (Exception $e) { - try { - $c1->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\' AND `update`=' . TIME); - } catch (Exception $e) { - - } - } +// $c1 = $this->con->openCursor('special_links_versions'); +// $c = $this->con->openCursor('books'); +// +// if (is_string($links)) { +// $links = json_encode(json_decode($links, false)); +// } +// if (is_string($rulers)) { +// $rulers = json_encode(json_decode($rulers, false)); +// } +// +// +// $links = wsLinks::encryptLinks(json_decode($links)); +// +// +// if (is_array($links)) { +// $links = json_encode($links); +// } +// if (is_array($rulers)) { +// $rulers = json_encode($rulers); +// } +// +// $c1->links = $c->specialLinks = $links; +// $c1->rulers = $c->specialRulers = $rulers; +// $c1->update = $c->changedate = TIME; +// $c1->book_id = $book_id; +// $c->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\''); +// try { +// $c1->insert(); +// } catch (Exception $e) { +// try { +// $c1->update('WHERE book_id=\'' . $this->con->escape($book_id) . '\' AND `update`=' . TIME); +// } catch (Exception $e) { +// +// } +// } } public function setTheme($book_id, $theme) diff --git a/inc/ws/DAO/class.ws.dao.document.php b/inc/ws/DAO/class.ws.dao.document.php index 125d60000..024ee5df0 100644 --- a/inc/ws/DAO/class.ws.dao.document.php +++ b/inc/ws/DAO/class.ws.dao.document.php @@ -23,6 +23,10 @@ class wsDAODocument extends commonDAO public function setFluidbookLinksFromCSV($book_id) { + // New way + wsLinks::addLinksFromPDF($book_id); + + // Old way $daoBook = new wsDAOBook($this->con); $pages = $daoBook->getPagesOfBook($book_id); @@ -38,9 +42,16 @@ class wsDAODocument extends commonDAO public function restoreLinksVersion($book_id, $update, $user = 0) { + $comments = 'Restore links from ' . date('Y-m-d H:i:s', $update); + + // New way + wsLinks::getLinksAndRulersFromFile($book_id, $links, $rulers, $update); + wsLinks::saveLinksInFile($book_id, $user, $comments, $links, $rulers, [], []); + + // OLD Way 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); + $this->setLinksAndRulers($book_id, json_encode($links), json_encode($rulers), $comments, $user); $r = $this->con->select('SELECT * FROM special_links_versions WHERE `update`=' . $update . ' AND book_id=\'' . $this->con->escape($book_id) . '\''); if ($r->count()) { @@ -113,6 +124,7 @@ class wsDAODocument extends commonDAO wsLinks::setDocumentLinks($document_id, json_encode($links), json_encode(array()), 0, 'Links imported from PDF', TIME); + return implode("\n", $log); } @@ -176,6 +188,11 @@ class wsDAODocument extends commonDAO } continue; } + + if (!isset($pages[$page])) { + continue; + } + $infos = $pages[$page]; $doc_id = $infos['document_id']; $doc_page = $infos['document_page']; @@ -314,6 +331,11 @@ class wsDAODocument extends commonDAO return wsLinks::getLinksAndRulers($book_id, $links, $rulers, $time); } + public function getLinksAndRulersFromFile($book_id, &$links, &$rulers, $time = 'latest') + { + return wsLinks::getLinksAndRulersFromFile($book_id, $links, $rulers, $time); + } + public function getLinksVersions($book_id) { diff --git a/inc/ws/Util/class.ws.links.php b/inc/ws/Util/class.ws.links.php index efa47bf6f..c3c84afad 100644 --- a/inc/ws/Util/class.ws.links.php +++ b/inc/ws/Util/class.ws.links.php @@ -247,6 +247,7 @@ class wsLinks $link['rot'] = 0; } $links[] = $link; + $i++; } } if (isset($trulers[$infos['document_id']][$infos['document_page']])) { @@ -258,6 +259,7 @@ class wsLinks } } } + } $daoBook = new wsDAOBook($core->con); @@ -267,6 +269,33 @@ class wsLinks $links = array_merge($links, $book->specialLinks); } + public static function getLinksAndRulersFromFile($book_id, &$links, &$rulers, $time = 'latest') + { + if (null === $time) { + $time = 'latest'; + } + $dir = self::getLinksDir($book_id); + + $file = $dir . '/' . $time . '.links.gz'; + if ($time === 'latest' && !file_exists($file)) { + $versions = wsLinks::getLinksVersions($book_id); + foreach ($versions as $version => $m) { + copy($dir . '/' . $version . '.links.gz', $dir . '/latest.links.gz'); + copy($dir . '/' . $version . '.meta.gz', $dir . '/latest.meta.gz'); + break; + } + } + if (!file_exists($file)) { + $links = []; + $rulers = []; + return; + } + + $r = json_decode(gzdecode(file_get_contents($file)), true); + $links = $r['links']; + $rulers = $r['rulers']; + } + public static function getLinksFromExcel($xls, &$links, &$rulers) { $s = $xls->setActiveSheetIndexByName('Links'); @@ -388,6 +417,9 @@ class wsLinks $attrs = ['image_rollover']; foreach ($attrs as $attr) { + if (!isset($link->$attr)) { + continue; + } $link->$attr = trim($link->$attr); if (strpos($link->$attr, '///') === 0 || $link->$attr == '') { continue; @@ -431,4 +463,139 @@ class wsLinks } return $res; } + + public static function saveLinksInFile($book_id, $user_id, $comments, $links, $rulers = [], $specialLinks = [], $specialRulers = []) + { + $lr = wsLinks::mergeLinksAndRulers($links, $rulers, $specialLinks, $specialRulers); + $meta = ['links' => count($lr['links']), 'rulers' => count($lr['rulers']), 'comments' => $comments, 'user' => $user_id]; + $base = self::getLinksDir($book_id) . '/' . TIME; + $latestLinks = self::getLinksDir($book_id) . '/latest.links.gz'; + $latestMeta = self::getLinksDir($book_id) . '/latest.meta.gz'; + file_put_contents($base . '.meta.gz', gzencode(json_encode($meta))); + file_put_contents($base . '.links.gz', gzencode(json_encode($lr))); + copy($base . '.links.gz', $latestLinks); + copy($base . '.meta.gz', $latestMeta); + } + + + public static function getLinksDir($book_id) + { + $dir = WS_BOOKS . '/links/' . $book_id; + if (!file_exists($dir)) { + mkdir($dir, 0777, true); + } + return $dir; + } + + public static function getLinksVersions($book_id, $withLinks = false) + { + $dir = self::getLinksDir($book_id); + $dr = opendir($dir); + $updates = []; + while ($f = readdir($dr)) { + if ($f === '.' || $f === '..') { + continue; + } + $e = explode('.', $f, 2); + if ($e[1] !== 'meta.gz' || $e[0] === 'latest') { + continue; + } + + $updates[$e[0]] = self::getMeta($book_id, $e[0]); + } + krsort($updates); + + return $updates; + } + + public static function getMeta($book_id, $update = 'latest') + { + return json_decode(gzdecode(file_get_contents(self::getLinksDir($book_id) . '/' . $update . '.meta.gz')), true); + } + + public static function mergeLinksAndRulers($links, $rulers, $specialLinks, $specialRulers) + { + $finalLinks = []; + $l = array_merge(self::_getAsArray($links), self::_getAsArray($specialLinks)); + foreach ($l as $k => $item) { + $item['id'] = $k + 1; + if (!isset($item['to'])) { + $item['to'] = ''; + } + $finalLinks[] = $item; + } + + return ['links' => self::encryptLinks($finalLinks), 'rulers' => array_merge(self::_getAsArray($rulers), self::_getAsArray($specialRulers))]; + } + + protected static function _getAsArray($v) + { + if (is_array($v)) { + return $v; + } + return json_decode($v, true); + } + + public static function addLinksFromPDF($book_id) + { + global $core; + + $daoBook = new wsDAOBook($core->con); + $pages = $daoBook->getPagesOfBook($book_id); + + $booleans = array('video_loop', 'video_auto_start', 'video_controls', 'video_sound_on'); + + $links = []; + + foreach ($pages as $page => $info) { + $links[$page] = []; + + $csv = wsDocument::getDir($info['document_id']) . '/p' . $info['document_page'] . '.csv'; + if (!file_exists($csv) && file_exists($csv . '.gz')) { + $csv = 'compress.zlib://' . $csv . '.gz'; + } elseif (!file_exists($csv)) { + continue; + } + + $newformat = (filemtime($csv) > 1363685416); + + $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; + } + + } + + self::saveLinksInFile($book_id, $core->user->utilisateur_id, 'Links imported from PDF', $links, []); + } } \ No newline at end of file