Source for file class.entities_import.php
Documentation is available at class.entities_import.php
* Logique des entités - import
* LODEL - Logiciel d'Edition ELectronique.
* Home page: http://www.lodel.org
* E-Mail: lodel@lodel.org
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* @author Ghislain Picard
* @copyright 2001-2002, Ghislain Picard, Marin Dacos
* @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
* @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
* @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* @licence http://www.gnu.org/copyleft/gpl.html
* @since Fichier ajouté depuis la version 0.8
* @version CVS:$Id: class.entities_import.php 4006 2007-10-05 11:53:41Z malafosse $
require_once 'logic/class.entities_edition.php';
* Classe de logique des entités (gestion de l'import)
* @author Ghislain Picard
* @copyright 2001-2002, Ghislain Picard, Marin Dacos
* @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
* @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
* @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* @licence http://www.gnu.org/copyleft/gpl.html
* @since Classe ajouté depuis la version 0.8
* Tableau des équivalents génériques
var $context; // save the current context
* Importation d'une entité
* @param array &$context le contexte passé par référence
* @param array &$error le tableau des erreurs éventuelles passé par référence
$idtask = intval ($context['idtask']);
require_once ("taskfunc.php");
if ($task['identity']) $context['id'] = $task['identity'];
require_once("xmlimport.php");
$parser->init ($context['class']);
if (!$this->id) die("ERROR: internal error in Entities_ImportLogic::importAction");
if ($this->nbdocuments> 1) { // save the file
$sourcefile= SITEROOT. "lodel/sources/entite-multidoc-". $task['idparent']. ".source";
$sourcefile= SITEROOT. "lodel/sources/entite-". $this->id. ".source";
copy ($task['source'], $sourcefile);
@chmod ($sourcefile, 0666 & octdec($GLOBALS['filemask']));
if ($idtask) { // close the task
$dao->deleteObject ($idtask);
if ($this->ret!= '_error' && $context['finish']) {
} elseif ($this->ret!= '_error') {
return "_location: index.php?do=view&id=". $this->id;
return "_location: index.php?do=view&id=". $this->id. "&check=oui";
* method to move img link when the new id is known
preg_match_all ('/<img\b[^>]+src=\\\?"([^"]+\.([^"\.]+?))\\\?"([^>]*>)/i', $text, $results, PREG_SET_ORDER);
foreach ($results as $result) {
$imgfile= $result[1]; $ext= $result[2];
if (substr ($imgfile, 0, 5)== "http:") continue; // external image
if ($imglist[$imgfile]) { // is it in the cache ?
$text= str_replace ($result[0], "<img src=\\\"$imglist[$imgfile]\\\"", $text);
// not in the cache let's move it
$dir= "docannexe/image/". $context['id'];
$imglist[$imgfile]= $newimgfile= "$dir". "/img-". $count. ".". $ext;
if ($newimgfile) { // ok, the image has been correctly copied
$text= str_replace ($result[0], '<img src="'. $newimgfile. '"'. $result[3], $text);
} else { // no, problem copying the image
$text= str_replace ($result[0], "<span class=\"image_error\">[Image non convertie]</span>", $text);
} else { // clear the directory the first time.
if (!$fd) die ("ERROR: cannot open the directory $dir");
if ($file{0}== ".") continue;
function openClass ($class, $obj= null, $multidoc= false)
switch ($class[1]) { // classtype
switch ($class[1]) { // classtype
case 'persons': // come back to the main context
case 'entities': // let's import now.
if ($this->task['idparent']) $localcontext['idparent']= $this->task['idparent'];
if ($this->task['idtype']) $localcontext['idtype']= $this->task['idtype'];
if ($multidoc) { // try to find the id
$result= $db->execute (lq ("SELECT id FROM #_TP_entities WHERE idparent='". $localcontext['idparent']. "' AND creationmethod='servoo;multidoc' ORDER BY id LIMIT ". intval($this->nbdocuments). ",1")) or dberror ();
if (!$result->EOF) $localcontext['id']= $result->fields['id'];
} else if ($this->task['identity']) $localcontext['id']= $this->task['identity'];
$localcontext['creationmethod']= $multidoc ? "servoo;multidoc" : "servoo";
$localcontext['creationinfo']= $this->task['sourceoriginale'];
if ($multidoc) $this->context['finish']= "oui";
if (!$this->context['finish']) $localcontext['status']=- 64;
$this->ret= $this->editAction ($localcontext, $error, FORCE);
#echo "ret1=".$this->ret."<br />";
if (!$this->id) $this->id= $localcontext['id']; // record the first one only
// move the source file and the files
if (!$styles_string) { // record all the internal into a string to use it in the following regexp
foreach ($this->commonstyles as $key => $val) {
if ($class == "internalstylesvo") //if internalstyle add it to the array $styles
if (count ($styles) > 0) $styles_string = implode ('|',$styles);
function myfunction ($arg0, $arg1, $arg2, $arg3, $styles,$style) {
//si on trouve pas $arg(2) dans les styles on remplace le style par le style de l'objet
if (strstr ($styles, $arg2)=== false) {
return '<p class="'. $style. '"';
return '<p class="'. $arg2. '"';
// replace all the paragraph containing classes added by Oo except paragraph with internal style
$data = preg_replace ('/(<p\b[^>]+class=")([^"]*)(")/e', "myfunction('\\0', '\\1','\\2','\\3','". $styles_string. "','". $obj->style. "')", $data);
if ($obj->type== "file" || $obj->type== "image") {
} elseif ($obj->type== "mltext") {
$lang= $obj->lang ? $obj->lang : $GLOBALS['lodeluser']['lang'];
//$this->_currentcontext['data'][$obj->name][$lang].=addslashes ($data);
$this->_currentcontext['data'][$obj->name][$lang].= $data;
} elseif ($obj->style[0]!= ".") {
//$this->_currentcontext['data'][$obj->name].=addslashes ($data);
$this->_currentcontext['data'][$obj->name].= $data;
foreach (preg_split ("/<\/p>/", $data) as $data2) {
//$this->_localcontext['entries'][$obj->id][]=array ("g_name"=>trim (addslashes ($entry)));
// le 2 ème argument de trim liste les caractères correspondant aux espaces dans le fichier source (utilisé pour supprimer TOUS les espaces avant et après l'entrée)
$this->_localcontext['entries'][$obj->id][]= array ("g_name"=> trim($entry,"\xC2\xA0\x00\x1F\x20"));
if (!$g_name_cache[$obj->class]) { // get the generic type
$vos= $dao->findMany ("class='". $obj->class. "' or class='entites_". $obj->class. "' and g_name IN ('familyname','firstname','prefix')", "", "name,g_name");
$g_name_cache[$obj->class][$vo->g_name]= $vo->name;
$g_name= $g_name_cache[$obj->class];
// ok, we have the generic type
// let's split the paragraph and the comma
foreach (preg_split ("/<\/p>/", $data) as $data2) {
if (!trim ($person)) continue;
$this->_localcontext['persons'][$obj->id][]= array(); // add a person
$this->_currentcontext[$g_name['prefix']]= $result[1];
// ok, we have the prefix
if (!$have_firstname && !$have_familyname) {
// ok, on cherche maintenant a separer le name et le firstname
} else { // sinon coupe apres le premiere espace
$firstname= $result[1]; $name= $result[2];
//$this->_currentcontext['data'][$g_name['firstname']]=addslashes(trim($firstname));
//$this->_currentcontext['data'][$g_name['familyname']]=addslashes(trim($name));
// le 2 ème argument de trim liste les caractères correspondant aux espaces dans le fichier source (utilisé pour supprimer TOUS les espaces avant et après l'entrée)
$this->_currentcontext['data'][$g_name['firstname']]= trim($firstname,"\xC2\xA0\x00\x1F\x20");
$this->_currentcontext['data'][$g_name['familyname']]= trim($name,"\xC2\xA0\x00\x1F\x20");
return $obj->conversion. $data. closetags ($obj->conversion);
if (strpos ($obj->conversion, '<li>') !== false) {
$conversion = str_replace ('<li>', '', $obj->conversion);
$data = preg_replace (array ("/(<p\b)/", "/(<\/p>)/"), array ("<li>\\1", "\\1</li>"), $data);
elseif (preg_match ("/<hr\s*\/?>/", $obj->conversion)) {
case '*' : return "<hr width=\"30%\" \ >";
case '**' : return "<hr width=\"50%\" \ >";
case '***' : return "<hr width=\"80%\" \ >";
case '****' : return "<hr \ >";
default: return "<hr width=\"10%\" \ >";
$conversion = $obj->conversion;
return $conversion. $data. closetags ($conversion);
// nothing... let's clean it.
return preg_replace(array("/^<span\b[^>]*>/","/<\/span>$/"),"",$data);
// begin{publicfields} automatic generation //
// end{publicfields} automatic generation //
// begin{uniquefields} automatic generation //
// end{uniquefields} automatic generation //
|