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

Source for file class.entities_edition.php

Documentation is available at class.entities_edition.php

  1. <?php
  2. /**    
  3.  * Logique des entités - edition
  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_edition.php 4478 2008-07-23 10:17:06Z mignot $
  40.  */
  41.  
  42. require_once 'genericlogic.php';
  43. /**
  44.  * Classe de logique des entités (gestion de l'édition)
  45.  * 
  46.  * @package lodel/logic
  47.  * @author Ghislain Picard
  48.  * @author Jean Lamy
  49.  * @copyright 2001-2002, Ghislain Picard, Marin Dacos
  50.  * @copyright 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  51.  * @copyright 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  52.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  53.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  54.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  55.  * @licence http://www.gnu.org/copyleft/gpl.html
  56.  * @since Classe ajouté depuis la version 0.8
  57.  * @see logic.php
  58.  */
  59. {
  60.  
  61.     /**
  62.      * Tableau des équivalents génériques
  63.      *
  64.      * @var array 
  65.      */
  66.     var $g_name;
  67.  
  68.     /**
  69.      * Constructeur
  70.      */
  71.     function Entities_EditionLogic()
  72.     {
  73.         $this->GenericLogic('entities');
  74.     }
  75.  
  76.     /**
  77.      * Affichage d'un objet
  78.      *
  79.      * @param array &$context le contexte passé par référence
  80.      * @param array &$error le tableau des erreurs éventuelles passé par référence
  81.      */
  82.     function viewAction(&$context&$error)
  83.     {
  84.         if ($context['check'&& $error{
  85.             return '_error';
  86.         }
  87.         if (!rightonentity($context['id''edit' 'create'$context)) {
  88.             die('ERROR: you don\'t have the right to perform this operation');
  89.         }
  90.  
  91.         // define some loop functions
  92.         function loop_persons_in_entities($context$funcname)
  93.         {
  94.             $varname $context['varname'];
  95.             if (!$varnamereturn;
  96.  
  97.             $idtype $context['idtype'];
  98.             if (!$idtype{
  99.                 die("ERROR: internal error in Entities_EditionLogic::loop_persons_in_entities");
  100.             }
  101.             if (!is_numeric($idtype)) {
  102.                 return;
  103.             }
  104.         
  105.             if (!$context['persons'][$idtype]{
  106.                 call_user_func("code_alter_$funcname",$context);
  107.                 return;
  108.             }
  109.  
  110.             foreach($context['persons'][$idtypeas $degree => $arr{
  111.                 if (!is_numeric($degree)) {
  112.                     return;
  113.                 }
  114.                 $localcontext              array_merge($context,$arr);
  115.                 $localcontext['name']      $varname;
  116.                 $localcontext['classtype'"persons";
  117.                 $localcontext['degree']    $degree;
  118.                 if ($degree $maxdegree{
  119.                     $maxdegree $degree;
  120.                 }
  121.                 call_user_func("code_do_$funcname"$localcontext);
  122.             }
  123.  
  124.             if (function_exists("code_after_$funcname")) {
  125.                 $localcontext $context;
  126.                 $localcontext['maxdegree'$maxdegree;
  127.                 call_user_func("code_after_$funcname"$localcontext);
  128.             }
  129.         }      
  130.         /////
  131.         function loop_entries_in_entities($context$funcname
  132.         {
  133.             $varname $context['varname'];
  134.             if (!$varname{
  135.                 return;
  136.             }
  137.  
  138.             $idtype $context['idtype'];
  139.             if (!is_numeric($idtype)) {
  140.                 return;
  141.             }
  142.             $dao &getDAO("entrytypes");
  143.             $votype $dao->getById($idtype"id,sort,flat");
  144.             if (!$votype{
  145.                 die("ERROR: internal error in loop_entries_in_entities");
  146.             }
  147.             if ($context['entries'][$idtype]{
  148.                 foreach ($context['entries'][$idtypeas $entry{
  149.                     $checkarr[&$entry['g_name'];
  150.                 }
  151.             }
  152.             $context['id'0// start by the parents
  153.             loop_entries_in_entities_rec ($context$funcname$votype$checkarr);
  154.         }
  155.         
  156.         function loop_entries_in_entities_rec ($context$funcname&$votype&$checkarr
  157.         {
  158.             global $db;
  159.  
  160.             // get the entries
  161.             $result $db->execute(lq("SELECT * FROM #_TP_entries WHERE idtype='"$votype->id"' AND idparent='"$context['id']"' AND status>-64 ORDER BY "$votype->sort)) or dberror();
  162.             while (!$result->EOF{
  163.                 $localcontext array_merge($context$result->fields);
  164.                 $localcontext['selected'$checkarr && in_array($result->fields['g_name'],$checkarr"selected=\"selected\"" "";
  165.                 call_user_func("code_do_$funcname"$localcontext);
  166.                 if (!$votype->flat{
  167.                     $localcontext['root'].= $localcontext['g_name']"/";
  168.                 }
  169.                 loop_entries_in_entities_rec ($localcontext$funcname$votype$checkarr);
  170.                 $result->MoveNext();
  171.             }
  172.         }
  173.         /////
  174.         function loop_entities_select($context$funcname)
  175.         {
  176.             global $db;
  177.             $varname $context['varname'];
  178.             if (!$varname{
  179.                 if (function_exists("code_alter_$funcname")) {
  180.                     call_user_func("code_alter_$funcname",$context);
  181.                 }
  182.                 return;
  183.             }
  184.             $ids preg_split("/,/"$context['entities'][$varname]-1PREG_SPLIT_NO_EMPTY);
  185.             $result $db->execute(lq("SELECT #_TP_entities.*, #_TP_types.type, #_TP_types.tpledition FROM #_TP_entities JOIN #_TP_types ON (#_TP_entities.idtype=#_TP_types.id) WHERE #_TP_entities.status>-64 AND #_TP_entities.id "sql_in_array($ids))) or dberror();
  186.             while (!$result->EOF{
  187.                 $localcontext array_merge($context$result->fields);
  188.                 call_user_func("code_do_$funcname"$localcontext);
  189.                 $result->MoveNext();
  190.             }
  191.             if (function_exists("code_after_$funcname")) {
  192.                 $localcontext $context;
  193.                 $localcontext['all'$context['entities'][$varname];
  194.                 call_user_func("code_after_$funcname",$localcontext);
  195.             }
  196.         }
  197.         /////
  198.         $ret GenericLogic::viewAction($context$error);
  199.  
  200.         //if ((!$context['id'] || (preg_match ("/servoo.*/", $context['creationmethod']) &&  $context['status'] == -64)) && !$error) { // add
  201.         if (!$context['id'&& !$error// add : récupération des valeurs par défaut
  202.             $daotablefields &getDAO("tablefields");
  203.             $fields $daotablefields->findMany("class='"$context['type']['class']"' AND status>0 AND type!='passwd'""",    "name,defaultvalue");
  204.             foreach($fields as $field{
  205.                 if (empty($context['data'][$field->name]))
  206.                 $context['data'][$field->name$field->defaultvalue;
  207.             }
  208.         }
  209.     
  210.         if ($context['check'&& !$error{
  211.             $context['status'= -1;
  212.             //il semble nécessaire de nettoyer request pour eviter les requetes pétées.
  213.             require_once('func.php');
  214.             clean_request_variable($context);
  215.             return $this->editAction($context$error);
  216.         }
  217.         return $ret $ret "_ok";
  218.     }
  219.  
  220.  
  221.  
  222.     /**
  223.      * Ajout d'un nouvel objet ou Edition d'un objet existant
  224.      *
  225.      * Ajout d'une nouvelle entité. Dans un premier temps on vérifie si l'utilisateur
  226.      * possède les bons droits pour modifier ou ajouter cette entité. Ensuite les différents
  227.      * champs de l'entité sont validés. Si ceux-ci sont valide, alors l'objet est créé dans
  228.      * la base de données et les objets liés sont aussi créés : personnes, entrées d'index
  229.      *
  230.      * @param array &$context le contexte passé par référence
  231.      * @param array &$error le tableau des erreurs éventuelles passé par référence
  232.      */
  233.     function editAction (&$context&$error$opt false)
  234.     {
  235.         if (!rightonentity($context['id''edit' 'create'$context)) {
  236.             die ("ERROR: you don't have the right to perform this operation");
  237.         }
  238.         if ($context['cancel']{
  239.             return '_back';
  240.         }
  241.         
  242.         global $lodeluser$home;
  243.         $id $context['id'];
  244.         if ($id && (!isset($context['idparent']|| !isset($context['idtype']))) {
  245.             $dao $this->_getMainTableDAO();
  246.             $vo  $dao->getById($id"idparent, idtype");
  247.             $context['idparent'$vo->idparent;
  248.             $context['idtype']   $vo->idtype;
  249.         }
  250.         $idparent $context['idparent'];
  251.         $idtype   $context['idtype'];
  252.         $status   intval($context['status']);
  253.         $this->_isAuthorizedStatus($status);
  254.         // iduser
  255.         $context['iduser'!SINGLESITE && $lodeluser['adminlodel'$lodeluser['id'];
  256.  
  257.         require_once 'entitiesfunc.php';
  258.         if (!checkTypesCompatibility($id$idparent$idtype)) {
  259.             $error['idtype''types_compatibility';
  260.             return '_error';
  261.         }
  262.  
  263.         // get the class 
  264.         $daotype &getDAO("types");
  265.         $votype $daotype->getById($context['idtype']"class,creationstatus,search");
  266.         $class $context['class']=$votype->class;
  267.         if (!$class{
  268.             die ("ERROR: idtype is not valid in Entities_EditionLogic::editAction");
  269.         }
  270.         
  271.         // Récupération des valeurs par défaut pour les champs vides À L'IMPORT
  272.         if ($context['lo'== 'entities_import' && !empty($context['idtask']&& !$error{
  273.             $daotablefields &getDAO("tablefields");
  274.             $fields $daotablefields->findMany("class='"$context['class']"' AND status>0 AND type!='passwd'""",    "name,defaultvalue");
  275.             foreach($fields as $field{
  276.                 if (empty($context['data'][$field->name]))
  277.                 $context['data'][$field->name$field->defaultvalue;
  278.             }
  279.         }
  280.  
  281.         if (!$this->validateFields($context$error)) {
  282.             // error.
  283.             // if the entity is imported and will be checked
  284.             // that's fine, let's continue, if not return an error
  285.             if ($opt == FORCE
  286.                 $status=-64;
  287.                 $ret="_error";
  288.             }
  289.             if ($status>-64{
  290.                 return "_error";
  291.             }
  292.         }
  293.  
  294.         // get the dao for working with the object
  295.         $dao $this->_getMainTableDAO();
  296.         $now date("Y-m-d H:i:s");
  297.         if ($id//edit the entity
  298.             $new false;
  299.             $vo $dao->getById($id"id,identifier,status, g_title");
  300.             if ($vo->status >= 16{
  301.                 die("ERROR: entity is locked. No operation is allowed");
  302.             }
  303.             // possibly document reloading
  304.             if($vo->status>-64{
  305.                 // let's deal with document reloading problem : PDF file and entries disapeared :
  306.                 $daotablefields &getDAO("tablefields");
  307.                 $Filefields $daotablefields->findMany("class='"$context['class']"' AND status>0 AND (type='file' OR type='image')""",    "name");            
  308.                 foreach($Filefields as $ffield{
  309.                     $gdaoaf &getGenericDAO ($class"identity");
  310.                     $tmpfile $gdaoaf->getById($id$ffield->name);             
  311.                     $fieldname $ffield->name;
  312.                     if($context['data'][$ffield->name== 'deleted'{
  313.                         $context['data'][$ffield->name'';
  314.                     elseif(empty($context['data'][$ffield->name]&& !empty($tmpfile->$fieldname)) {
  315.                         $name $ffield->name;
  316.                         $context['data'][$ffield->name$tmpfile->$name;
  317.                     }
  318.                 }
  319.                 // entries
  320.                 $daorelations &getDAO("relations");
  321.                 $Entryfields $daorelations->findMany("id1='{$id}AND nature = 'E'""""id2");
  322.                 $daoentries &getDAO("entries");
  323.                 $daoentrytypes &getDAO("entrytypes");
  324.                 foreach($Entryfields as $ffield{
  325.                     $reloaded false;
  326.                     $entry $daoentries->getById($ffield->id2'idtype, g_name');
  327.                     $entryclass $daoentrytypes->getById($entry->idtype'class');
  328.                     if(is_array($context['entries'][$entry->idtype])) {
  329.                         foreach($context['entries'][$entry->idtypeas $entryfield{
  330.                             if($entryfield['g_name'== $entry->g_name{
  331.                                 $reloaded true;
  332.                                 break;
  333.                             }
  334.                         }
  335.                     }
  336.                     if($reloadedcontinue;
  337.                     $pos count($context['entries'][$entry->idtype]);
  338.                     $context['entries'][$entry->idtype][$pos]['g_name'$entry->g_name;
  339.                     $context['entries'][$entry->idtype][$pos]['class'$entryclass->class;
  340.                     $context['entries'][$entry->idtype][$pos]['idtype'$entry->idtype;
  341.                     $indexfields $daotablefields->find("class='{$entryclass->class}'"'name');
  342.                     foreach($indexfields as $indexfield)
  343.                         $context['entries'][$entry->idtype][$pos]['data'][$indexfield->namenull;
  344.                 }
  345.                 unset($entry$entryclass$daoentries$daoentrytypes$Entryfields$daorelations$daotablefields$Filefields)// save some memory
  346.             }
  347.             // change the usergroup of the entity ?
  348.             if ($lodeluser['admin'&& $context['usergroup']{
  349.                 $vo->usergroup intval ($context['usergroup']);
  350.             }
  351.             if ($vo->status<=-64{  // like a creation
  352.                 $vo->status $votype->creationstatus;
  353.                 $vo->creationdate $now;
  354.             }
  355.         else //create the entity
  356.             $new true;
  357.             $vo $dao->createObject ();
  358.             $vo->idparent       $idparent;
  359.             $vo->usergroup      $this->_getUserGroup ($context$idparent);
  360.             $vo->iduser         $context['iduser'];
  361.             $vo->status         $status $status $votype->creationstatus;
  362.             $vo->creationdate   $now;
  363.         }
  364.         $vo->modificationdate $now;
  365.         // populate the entity
  366.         if ($idtype{
  367.             $vo->idtype=$idtype;
  368.         }
  369.         // permanent identifier management
  370.         $dctitle $this->getGenericEquivalent ($class'dc.title');
  371.         if ($dctitle{
  372.             $vo->g_title strip_tags ($context['data'][$dctitle]"<em><strong><span><sup><sub>");
  373.         }
  374.         // If Identifier is not set, let's calcul it with the generic title
  375.         
  376.         if (!$vo->identifier && trim($context['identifier']=== ''{
  377.             $vo->identifier $this->_calculateIdentifier ($id$vo->g_title);
  378.         }    else // else simply clean bad chars
  379.             if(empty($context['identifier'])) {
  380.                 $context['identifier'$vo->identifier;
  381.             }
  382.             /*if (is_null ($context['identifier'])) {//identifier desactivated
  383.                 $vo->identifier = $this->_calculateIdentifier ($id, $vo->identifier);
  384.             } else {// else that means that we have modified it*/
  385.                 $vo->identifier$this->_calculateIdentifier ($id$context['identifier']);
  386.             /*}*/
  387.         }
  388.         //print_r($context);
  389.         if ($context['creationmethod']{
  390.             $vo->creationmethod $context['creationmethod'];
  391.         }
  392.         if ($context['creationinfo']{
  393.             $vo->creationinfo $context['creationinfo'];
  394.         }
  395.     
  396.         $id $context['id'$dao->save($vo);
  397.         // change the group recursively