phpDocumentor lodel-logic
[ class tree: lodel-logic ] [ index: lodel-logic ] [ all elements ]

Source for file class.entities_import.php

Documentation is available at class.entities_import.php

  1. <?php
  2. /**    
  3.  * Logique des entités - import
  4.  *
  5.  * PHP versions 4 et 5
  6.  *
  7.  * LODEL - Logiciel d'Edition ELectronique.
  8.  *
  9.  * Home page: http://www.lodel.org
  10.  * E-Mail: lodel@lodel.org
  11.  *
  12.  * All Rights Reserved
  13.  *
  14.  * This program is free software; you can redistribute it and/or modify
  15.  * it under the terms of the GNU General Public License as published by
  16.  * the Free Software Foundation; either version 2 of the License, or
  17.  * (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU General Public License
  25.  * along with this program; if not, write to the Free Software
  26.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  *
  28.  * @package lodel/logic
  29.  * @author Ghislain Picard
  30.  * @author Jean Lamy
  31.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  32.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  33.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  34.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  35.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  36.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  37.  * @licence http://www.gnu.org/copyleft/gpl.html
  38.  * @since Fichier ajouté depuis la version 0.8
  39.  * @version CVS:$Id: class.entities_import.php 4006 2007-10-05 11:53:41Z malafosse $
  40.  */
  41.  
  42.  
  43. require_once 'logic/class.entities_edition.php';
  44.  
  45. /**
  46.  * Classe de logique des entités (gestion de l'import)
  47.  * 
  48.  * @package lodel/logic
  49.  * @author Ghislain Picard
  50.  * @author Jean Lamy
  51.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  52.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  53.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  54.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  55.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  56.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  57.  * @licence http://www.gnu.org/copyleft/gpl.html
  58.  * @since Classe ajouté depuis la version 0.8
  59.  * @see logic.php
  60.  */
  61. {
  62.     /**
  63.      * Tableau des équivalents génériques
  64.      *
  65.      * @var array 
  66.      */
  67.     var $g_name;
  68.     
  69.     var $prefixregexp="Pr\.|Dr\.|Mr\.|Ms\.";
  70.     
  71.   var $context// save the current context
  72.  
  73.   var $task;
  74.  
  75.   /**
  76.      * Constructeur
  77.      */
  78.     function Entities_ImportLogic()
  79.     {
  80.     }
  81.  
  82.     /**
  83.      * Importation d'une entité
  84.      * @param array &$context le contexte passé par référence
  85.      * @param array &$error le tableau des erreurs éventuelles passé par référence
  86.      */
  87.     function importAction (&$context&$error
  88.     {
  89.         global $db;
  90.         $this->context=&$context;
  91.         $idtask intval ($context['idtask']);
  92.         require_once ("taskfunc.php");
  93.         $this->task = $task gettask ($idtask);
  94.         gettypeandclassfromtask ($task$context);
  95.         if ($task['identity']$context['id'$task['identity'];
  96.         require_once("xmlimport.php");
  97.         $parser=new XMLImportParser();
  98.         $parser->init ($context['class']);
  99.         $parser->parse (file_get_contents ($task['fichier'])$this);
  100.         if (!$this->iddie("ERROR: internal error in Entities_ImportLogic::importAction");        
  101.         if ($this->nbdocuments>1// save the file
  102.             $sourcefile=SITEROOT."lodel/sources/entite-multidoc-".$task['idparent'].".source";
  103.         else {
  104.             $sourcefile=SITEROOT."lodel/sources/entite-".$this->id.".source";
  105.         }
  106.         @unlink ($sourcefile);
  107.         copy ($task['source']$sourcefile);
  108.         @chmod ($sourcefile0666 octdec($GLOBALS['filemask']));
  109.         if ($idtask// close the task
  110.             $dao=&getDAO ("tasks");
  111.             $dao->deleteObject ($idtask);
  112.         }
  113.         if ($this->ret!='_error' && $context['finish']{
  114.             return $this->ret;
  115.         elseif ($this->ret!='_error'{
  116.             return "_location: index.php?do=view&id=".$this->id;
  117.         else //ret=error
  118.             return "_location: index.php?do=view&id=".$this->id."&check=oui";
  119.         }
  120.     }
  121.  
  122.  
  123.     /**
  124.      * method to move img link when the new id is known
  125.      * @access private
  126.      */
  127.     function _moveImages (&$context)
  128.     
  129.         $count 1;
  130.         $dir '';
  131.         $this->_moveImages_rec ($context$dir$count)
  132.     }
  133.  
  134.     function _moveImages_rec (&$context&$dir&$count
  135.     {
  136.         foreach (array_keys ($contextas $k{
  137.             if (is_array ($context[$k])) {
  138.                 $this->_moveImages_rec ($context[$k]$dir$count);
  139.             continue;
  140.             }
  141.             $text=&$context[$k];
  142.        preg_match_all ('/<img\b[^>]+src=\\\?"([^"]+\.([^"\.]+?))\\\?"([^>]*>)/i'$text$resultsPREG_SET_ORDER);
  143.             foreach ($results as $result{
  144.                 $imgfile=$result[1];       $ext=$result[2];
  145.                 if (substr ($imgfile05)=="http:"continue// external image
  146.                 // local.image so
  147.                 if ($imglist[$imgfile]// is it in the cache ?
  148.                     $text=str_replace ($result[0]"<img src=\\\"$imglist[$imgfile]\\\""$text);
  149.                 else {
  150.                     // not in the cache let's move it
  151.                     if (!$dir{
  152.                         $dir="docannexe/image/".$context['id'];
  153.                         $this->_checkdir ($dir);
  154.                     }
  155.                     $imglist[$imgfile]=$newimgfile="$dir"."/img-".$count.".".$ext;
  156.                     copy ($imgfileSITEROOT.$newimgfile);
  157.                     @unlink ($imgfile);
  158.                     if ($newimgfile// ok, the image has been correctly copied
  159.                         $text=str_replace ($result[0]'<img src="'.$newimgfile.'"'.$result[3]$text);
  160.                         @chmod (SITEROOT.$newimgfile0666  octdec($GLOBALS['filemask']));
  161.                         $count++;
  162.                     else // no, problem copying the image
  163.                         $text=str_replace ($result[0]"<span class=\"image_error\">[Image non convertie]</span>"$text);
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     function _checkdir ($dir
  171.     {
  172.         if (!is_dir (SITEROOT.$dir)) {
  173.             mkdir (SITEROOT.$dir0777 octdec($GLOBALS['filemask']));
  174.             @chmod(SITEROOT.$dir,0777 octdec($GLOBALS['filemask']));
  175.         else // clear the directory the first time.
  176.             $fd=opendir(SITEROOT.$dir);
  177.             if (!$fddie ("ERRORcannot open the directory $dir");
  178.             while ($file=readdir($fd)) {
  179.                 if ($file{0}=="."continue;
  180.                 $file=SITEROOT.$dir."/".$file;
  181.                 if (is_file($file)) @unlink($file);
  182.             }
  183.         }
  184.     }
  185.  
  186.     var $_localcontext;
  187.  
  188.     function openClass ($class$obj=null$multidoc=false
  189.     {
  190.         switch ($class[1]// classtype
  191.             case 'entries':
  192.                 break;
  193.             case 'persons':
  194.                 break;
  195.             case 'entities':
  196.                 $this->_localcontext=array();
  197.                 $this->_currentcontext=&$this->_localcontext;
  198.                 break;
  199.         }
  200.     }
  201.  
  202.     function closeClass ($class$multidoc=false
  203.     {
  204.         global $db;
  205.         switch ($class[1]// classtype
  206.         case 'entries':
  207.             break;
  208.         case 'persons'// come back to the main context
  209.             $this->_currentcontext=&$this->_localcontext;
  210.             break;
  211.         case 'entities':    // let's import now.
  212.             $localcontext=array_merge ($this->context$this->_localcontext);
  213.             if ($this->task['idparent']$localcontext['idparent']=$this->task['idparent'];
  214.             if ($this->task['idtype']$localcontext['idtype']=$this->task['idtype'];
  215.             if ($multidoc// try to find the id
  216.                 $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 ();
  217.                 if (!$result->EOF$localcontext['id']=$result->fields['id'];
  218.                 $this->nbdocuments++;
  219.             else if ($this->task['identity']$localcontext['id']=$this->task['identity'];
  220.             $localcontext['creationmethod']=$multidoc "servoo;multidoc" "servoo";
  221.             $localcontext['creationinfo']=$this->task['sourceoriginale'];
  222.  
  223.             if ($multidoc$this->context['finish']="oui";
  224.             if (!$this->context['finish']$localcontext['status']=-64;
  225.  
  226.             $error=array ();
  227.             $this->ret=$this->editAction ($localcontext$errorFORCE);
  228.             #echo "ret1=".$this->ret."<br />";
  229.             #print_r($error);
  230.             if (!$this->id$this->id=$localcontext['id']// record the first one only
  231.             // move the source file and the files
  232.         }
  233.     }
  234.  
  235.     function processData($data
  236.     {
  237.         return $data;
  238.     }
  239.  
  240.     function processTableFields ($obj$data
  241.     {
  242.         global $db;
  243.         static $styles_string;
  244.         if (!$styles_string// record all the internal into a string to use it in the following regexp
  245.             $styles array();
  246.             if (is_array ($this->commonstyles)) {
  247.                 foreach ($this->commonstyles as $key => $val{
  248.                     $class strtolower (get_class ($val));
  249.                     if ($class == "internalstylesvo"//if internalstyle add it to the array $styles
  250.                         $styles[$key;
  251.                 }
  252.                 if (count ($styles0)    $styles_string implode ('|',$styles);
  253.                 unset($styles);
  254.             }
  255.         }
  256.         if(!function_exists('myfunction')) {
  257.             function myfunction ($arg0$arg1$arg2$arg3$styles,$style{
  258.                 //si on trouve pas $arg(2) dans les styles on remplace le style par le style de l'objet
  259.                 if (strstr ($styles$arg2)===false{
  260.                     return '<p class="'.$style.'"';
  261.                 }
  262.                 else {
  263.                     return '<p class="'.$arg2.'"';
  264.                 }
  265.             }
  266.         }
  267.         
  268.         // replace all the paragraph containing classes added by Oo except paragraph with internal style
  269.         $data preg_replace ('/(<p\b[^>]+class=")([^"]*)(")/e'"myfunction('\\0', '\\1','\\2','\\3','"$styles_string."','".$obj->style."')"$data);
  270.         if ($obj->type=="file" || $obj->type=="image"{
  271.             // nothing...
  272.         elseif ($obj->type=="mltext"{
  273.             $lang=$obj->lang $obj->lang $GLOBALS['lodeluser']['lang'];
  274.             //$this->_currentcontext['data'][$obj->name][$lang].=addslashes ($data);
  275.             $this->_currentcontext['data'][$obj->name][$lang].=$data;
  276.         elseif ($obj->style[0]!="."{
  277.             //$this->_currentcontext['data'][$obj->name].=addslashes ($data);
  278.             $this->_currentcontext['data'][$obj->name].=$data;
  279.         }
  280.         return $data;
  281.     }
  282.  
  283.     function processEntryTypes ($obj$data
  284.     {
  285.         foreach (preg_split ("/<\/p>/"$dataas $data2{
  286.             foreach (preg_split ("/,/"strip_tags ($data2)) as $entry{
  287.                 //$this->_localcontext['entries'][$obj->id][]=array ("g_name"=>trim (addslashes ($entry)));
  288.             
  289.             // 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)    
  290.             $this->_localcontext['entries'][$obj->id][]=array ("g_name"=>trim($entry,"\xC2\xA0\x00\x1F\x20"));
  291.              }
  292.         }
  293.     }
  294.  
  295.     function processPersonTypes ($obj$data
  296.     {
  297.         static $g_name_cache;
  298.         if (!$g_name_cache[$obj->class]{  // get the generic type     
  299.             $dao=&getDAO("tablefields");
  300.             $vos=$dao->findMany ("class='".$obj->class."' or class='entites_".$obj->class."' and g_name IN ('familyname','firstname','prefix')""""name,g_name");
  301.             foreach ($vos as $vo{
  302.                 $g_name_cache[$obj->class][$vo->g_name]=$vo->name;
  303.             }
  304.         }
  305.         $g_name=$g_name_cache[$obj->class];
  306.         // ok, we have the generic type
  307.         // let's split the paragraph and the comma
  308.         foreach (preg_split ("/<\/p>/"$dataas $data2
  309.             foreach (preg_split ("/,/"strip_tags ($data2)) as $person{
  310.                 if (!trim ($person)) continue;
  311.                 $this->_localcontext['persons'][$obj->id][]=array()// add a person
  312.                 $this->_currentcontext=&$this->_localcontext['persons'][$obj->id][count ($this->_localcontext['persons'][$obj->id])-1];
  313.                 if (preg_match("/^\s*(".$this->prefixregexp.")\s/",$person,$result)) {
  314.                     $this->_currentcontext[$g_name['prefix']]=$result[1];
  315.                     $person=str_replace($result[0],"",$person);
  316.                 }
  317.                 // ok, we have the prefix
  318.                 // try to guess
  319.                 if (!$have_firstname && !$have_familyname{
  320.                     // ok, on cherche maintenant a separer le name et le firstname
  321.                     $name=$person;
  322.                     while ($name && strtoupper($name)!=$name$name=substr(strstr($name," "),1);}
  323.                     if ($name{
  324.                         $firstname=str_replace ($name""$person);
  325.                     else // sinon coupe apres le premiere espace
  326.                         if (preg_match ("/^(.*?)\s+([^\s]+)$/i"trim ($person)$result)) {
  327.                             $firstname=$result[1]$name=$result[2];
  328.                         else $name=$person;
  329.                     }
  330.                 }
  331.                 //$this->_currentcontext['data'][$g_name['firstname']]=addslashes(trim($firstname));
  332.                 //$this->_currentcontext['data'][$g_name['familyname']]=addslashes(trim($name));
  333.                 
  334.                 // 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)
  335.                 $this->_currentcontext['data'][$g_name['firstname']]=trim($firstname,"\xC2\xA0\x00\x1F\x20");
  336.                 $this->_currentcontext['data'][$g_name['familyname']]=trim($name,"\xC2\xA0\x00\x1F\x20");
  337.             // for each person
  338.         }
  339.   }
  340.  
  341.     function processCharacterStyles ($obj$data
  342.     {
  343.         return $obj->conversion.$data.closetags ($obj->conversion);
  344.     }
  345.  
  346.     function processInternalStyles ($obj$data
  347.     {
  348.         if (strpos ($obj->conversion'<li>'!== false{
  349.             $conversion str_replace ('<li>'''$obj->conversion);
  350.             $data preg_replace (array ("/(<p\b)/""/(<\/p>)/")array ("<li>\\1""\\1</li>")$data);
  351.         }
  352.         elseif (preg_match ("/<hr\s*\/?>/"$obj->conversion)) {
  353.             switch (trim (strip_tags ($data))) {
  354.                 case '*' return "<hr width=\"30%\" \ >";
  355.                 case '**' return "<hr width=\"50%\" \ >";
  356.                 case '***' return "<hr width=\"80%\" \ >";
  357.                 case '****' return "<hr \ >";
  358.                 defaultreturn "<hr width=\"10%\" \ >";
  359.             }
  360.         
  361.         else  {
  362.             $conversion $obj->conversion;
  363.         }
  364.         return $conversion.$data.closetags ($conversion);
  365.     }
  366.  
  367.     function unknownParagraphStyle ($style$data
  368.     {
  369.         // nothing to do ?
  370.     }
  371.  
  372.     function unknownCharacterStyle ($style$data
  373.     {
  374.         // nothing... let's clean it.
  375.         return preg_replace(array("/^<span\b[^>]*>/","/<\/span>$/"),"",$data);
  376.     }
  377.  
  378.    // begin{publicfields} automatic generation  //
  379.    // end{publicfields} automatic generation  //
  380.  
  381.    // begin{uniquefields} automatic generation  //
  382.    // end{uniquefields} automatic generation  //
  383. // class 
  384. ?>

Documentation generated on Thu, 24 Jul 2008 05:07:37 +0200 by phpDocumentor 1.4.0a2