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

Source for file class.translations.php

Documentation is available at class.translations.php

  1. <?php
  2. /**    
  3.  * Logique des traductions
  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.  * @author Sophie Malafosse
  32.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  33.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  34.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  35.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  36.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  37.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  38.  * @licence http://www.gnu.org/copyleft/gpl.html
  39.  * @since Fichier ajouté depuis la version 0.8
  40.  * @version CVS:$Id: class.translations.php 4006 2007-10-05 11:53:41Z malafosse $
  41.  */
  42.  
  43.  
  44.  
  45. $GLOBALS['translations_textgroups']=array("interface"=>array("common","edition","admin","lodeladmin","install","lodelloader"),
  46.                  "site"=>array("site"),
  47.                  );
  48.  
  49.  
  50. /**
  51.  * Classe de logique des traductions
  52.  * 
  53.  * @package lodel/logic
  54.  * @author Ghislain Picard
  55.  * @author Jean Lamy
  56.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  57.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  58.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  59.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  60.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  61.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  62.  * @licence http://www.gnu.org/copyleft/gpl.html
  63.  * @since Classe ajouté depuis la version 0.8
  64.  * @see logic.php
  65.  */
  66. class TranslationsLogic extends Logic {
  67.  
  68.     /** Constructor
  69.     */
  70.     function TranslationsLogic({
  71.         $this->Logic("translations");
  72.     }
  73.  
  74.     /**
  75.         * list Action
  76.         */
  77.  
  78.     function listAction(&$context,&$errro
  79.     {
  80.         $this->_setTextGroups($context);
  81.  
  82.         function loop_textgroups(&$context,$funcname)
  83.  
  84.         {
  85.             foreach($GLOBALS['translations_textgroups'][$context['textgroups']] as $textgroup{
  86.     $localcontext=$context;
  87.     $localcontext['textgroup']=$textgroup;
  88.     call_user_func("code_do_".$funcname,$localcontext);
  89.             }
  90.         }
  91.  
  92.         function loop_alltexts(&$context,$funcname)
  93.  
  94.         {
  95.             global $db,$distincttexts,$alltexts_cache;
  96.  
  97.             $result=$db->execute(lq("SELECT status,contents,name,id,lang FROM #_TP_texts WHERE status>=-1 AND textgroup='".$context['textgroup']."'")) or dberror();
  98.  
  99.             $distincttexts=array();
  100.             while(!$result->EOF{
  101.     $lang=$result->fields['lang'];
  102.     $name=$result->fields['name'];    
  103.     if ($name && $lang{
  104.         $alltexts_cache[$lang][$name]=$result->fields;
  105.         if ($lang==$GLOBALS['lang']{
  106.             $distincttexts[$name]=$result->fields['contents'];
  107.         elseif (!isset($distincttexts[$name])) {
  108.             $distincttexts[$name]=true;
  109.         }
  110.     // valid name
  111.     $result->MoveNext();
  112.             }
  113.             foreach($distincttexts as $name=>$contents{
  114.     $localcontext=$context;
  115.     $localcontext['name']=$name;
  116.     $localcontext['contents']=$contents;
  117.     call_user_func("code_do_".$funcname,$localcontext);
  118.             }
  119.         }
  120.  
  121.         function loop_lang_and_text(&$context,$funcname)
  122.             
  123.         {
  124.             foreach(array_keys($GLOBALS['alltexts_cache']as $lang{
  125.     $localcontext=$context;
  126.     $row=$GLOBALS['alltexts_cache'][$lang][$context['name']];
  127.     $localcontext=$row array_merge($context,$row$context;
  128.     call_user_func("code_do_".$funcname,$localcontext);       
  129.             }
  130.         }
  131.  
  132.         return "_ok";
  133.     }
  134.  
  135.     /**
  136.         * add/edit Action
  137.         */
  138.  
  139.     function editAction(&$context,&$error,$clean=false)
  140.  
  141.     {
  142.         $this->_setTextGroups($context);
  143.         if (!$context['id']$context['modificationdate']=date("Y-m-d");
  144.  
  145.         return Logic::editAction($context,$error);
  146.     }
  147.  
  148.     /**
  149.         * export Action
  150.         */
  151.     function exportAction(&$context,&$error)
  152.  
  153.     {
  154.         global $home;
  155.         require_once("validfunc.php");
  156.  
  157.         $lang=$context['lang'];
  158.         if ($lang!="all" && !validfield($lang,"lang")) die("ERROR: invalid lang");
  159.         
  160.         // lock the database
  161.         //lock_write("translations","textes");
  162.  
  163.         $tmpfile=tempnam(tmpdir(),"lodeltranslation");
  164.         require_once("translationfunc.php");
  165.  
  166.         $this->_setTextGroups($context);
  167.         $xmldb=new XMLDB_Translations($context['textgroups'],$lang);
  168.  
  169.         #$ret=$xmldb->saveToString();
  170.         #die($ret);
  171.  
  172.         $xmldb->saveToFile($tmpfile);
  173.  
  174.         $filename="translation-$lang-".date("dmy").".xml";
  175.  
  176.         download($tmpfile,$filename);
  177.         @unlink ($tmpfile);
  178.         exit();
  179.  
  180.         return "back";
  181.     }
  182.  
  183.  
  184.     function importAction(&$context,&$error)
  185.  
  186.     {
  187.         global $home;
  188.  
  189.         $this->_setTextGroups($context);
  190.         $lang="";
  191.  
  192.         require_once("importfunc.php");
  193.         $file=extract_import("translation",$context,"xml");
  194.  
  195.         if ($file{
  196.             require_once("validfunc.php");
  197.             require_once("translationfunc.php");
  198.             $xmldb=new XMLDB_Translations($context['textgroups']);
  199.             
  200.             $xmldb->readFromFile($file);
  201.             update();
  202.  
  203.             return "_back";
  204.         }
  205.  
  206.  
  207.         function loop_files(&$context,$funcname)
  208.  
  209.         {
  210.             global $fileregexp,$importdirs,$home;
  211.         
  212.             foreach ($importdirs as $dir{
  213.     if $dh@opendir($dir)) {
  214.         while (($file=readdir($dh))!==FALSE{
  215.             if (!preg_match("/^$fileregexp$/i",$file)) continue;
  216.             $localcontext=$context;
  217.             $localcontext['filename']=$file;
  218.             $localcontext['fullfilename']="$dir/$file";
  219.             if ($dir=="CACHE"$localcontext['maybedeleted']=1;
  220.             call_user_func("code_do_$funcname",$localcontext);       
  221.         }
  222.         closedir ($dh);
  223.     }
  224.             }
  225.         }
  226.  
  227.         function loop_translation(&$context,$funcname)
  228.         
  229.         {
  230.             $arr=preg_split("/<\/?row>/",file_get_contents($context['fullfilename']));
  231.  
  232.             $langs=array();
  233.             for($i=1$i<count($arr)$i+=2{
  234.     $localcontext=$context;
  235.     foreach (array("lang","title","creationdate","modificationdate"as $tag{
  236.         if (preg_match("/<$tag>(.*)<\/$tag>/",$arr[$i],$result)) 
  237.             $localcontext[$tag]=trim(strip_tags($result[1]));
  238.     }
  239.     if (!$localcontext['lang']continue;
  240.     call_user_func("code_do_$funcname",$localcontext);
  241.             }
  242.         }
  243.         return "import_translations";
  244.  
  245.     }
  246.     
  247.  
  248.     /*---------------------------------------------------------------*/
  249.     //! Private or protected from this point
  250.     /**
  251.         * @private
  252.         */
  253.  
  254.     /**
  255.         * Set the textgroups
  256.         */
  257.  
  258.     function _setTextGroups(&$context
  259.  
  260.     {
  261.         $context['textgroups']=$GLOBALS['site'"site" "interface";
  262.     }
  263.  
  264.     /**
  265.     * Sauve des données dans des tables liées éventuellement
  266.     *
  267.     * Appelé par editAction pour effectuer des opérations supplémentaires de sauvegarde.
  268.     *
  269.     * @param object $vo l'objet qui a été créé
  270.     * @param array $context le contexte
  271.     */
  272.     function _saveRelatedTables($vo,$context
  273.  
  274.     {
  275.         global $db,$lodeluser;
  276.         //
  277.         // create all the texts if needed
  278.         // 
  279.         // can't use insert select... so it not really funny to do
  280.         //
  281.  
  282.         
  283.         if (!$vo->lang// get the lang if we don't have it
  284.             $dao=$this->_getMainTableDAO();
  285.             $vo=$dao->getById($vo->id);
  286.         }
  287.         if ($vo->lang==$lodeluser['lang']{
  288.             // get any lang... this should not happen anyway
  289.             $dao=$this->_getMainTableDAO();
  290.             $vo2=$dao->find("status>0","lang");
  291.             $fromlang=$vo2->lang;
  292.         else {
  293.             // normal case... should be different !
  294.             $fromlang=$lodeluser['lang'];
  295.         }
  296.         $textscriteria=textgroupswheredefined("SITEROOT""site" "interface" );
  297.  
  298.         // get all the text name, group, text in current lang for which the translation does not exists in the new lang
  299.         $result=$db->execute(lq("SELECT t1.name,t1.textgroup,t1.contents FROM #_TP_texts as t1 LEFT OUTER JOIN #_TP_texts as t2 ON t1.name=t2.name AND t1.textgroup=t2.textgroup AND t2.lang='".$vo->lang."' WHERE t1.status>-64 AND t1.lang='".$fromlang."' AND t2.id IS NULL AND t1.".$textscriteria." GROUP BY t1.name,t1.textgroup")) or dberror();
  300.         do // use multiple insert but not to much... to minimize the size of the query
  301.             $inserts=array()$count=0;
  302.             while (!$result->EOF && $count<20{
  303.     $row=$result->fields;
  304.     #$langs=explode(",",$row['langs']); // get the lang
  305.                 #if (in_array($lang,$langs)) continue; // the text already exists in the correct lang
  306.                 #echo $row['name']," ";
  307.     
  308.     $inserts[]="('".$row['name']."','".$row['textgroup']."','".mysql_escape_string($row['contents'])."','-1','".$context['lang']."')";
  309.     $count++;
  310.     $result->MoveNext();
  311.             }
  312.             if ($inserts
  313.     $db->execute(lq("INSERT INTO #_TP_texts (name,textgroup,contents,status,lang) VALUES ".join(",",$inserts))) or dberror();
  314.         while (!$result->EOF);
  315.     }
  316.  
  317.     function _deleteRelatedTables($id{
  318.         //il faut supprimer les texts associés à la traduction
  319.         // reinitialise le cache surement.
  320.     }
  321.  
  322.  
  323.  
  324.     // begin{publicfields} automatic generation  //
  325.  
  326.     /**
  327.      * Retourne la liste des champs publics
  328.      * @access private
  329.      */
  330.     function _publicfields(
  331.     {
  332.         return array('lang' => array('text''+'),
  333.                                     'title' => array('text'''),
  334.                                     'textgroups' => array('text'''),
  335.                                     'translators' => array('text'''),
  336.                                     'creationdate' => array('date'''));
  337.     }
  338.     // end{publicfields} automatic generation  //
  339.  
  340.     // begin{uniquefields} automatic generation  //
  341.  
  342.     /**
  343.      * Retourne la liste des champs uniques
  344.      * @access private
  345.      */
  346.     function _uniqueFields(
  347.     
  348.         return array(array('lang''textgroups'));
  349.     }
  350.     // end{uniquefields} automatic generation  //
  351.  
  352.  
  353. // class 
  354.  
  355.  
  356. /*-----------------------------------*/
  357. /* function pipe                     */
  358.  
  359. function textgroupswhere($textgroups)
  360.  
  361. {
  362.     if (!$textgroupsdie("ERROR: which textgroups ?");
  363.     if ($GLOBALS['translations_textgroups'][$textgroups]{
  364.         return "textgroup IN ('".join("','",$GLOBALS['translations_textgroups'][$textgroups])."')";
  365.     else {
  366.         die("ERROR: unkown textgroup");
  367.     }
  368. }
  369.  
  370.  
  371. ?>

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