Source for file class.entities_edition.php
Documentation is available at class.entities_edition.php
* Logique des entités - edition
* 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_edition.php 4478 2008-07-23 10:17:06Z mignot $
require_once 'genericlogic.php';
* Classe de logique des entités (gestion de l'édition)
* @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
* @param array &$context le contexte passé par référence
* @param array &$error le tableau des erreurs éventuelles passé par référence
if ($context['check'] && $error) {
if (!rightonentity($context['id'] ? 'edit' : 'create', $context)) {
die('ERROR: you don\'t have the right to perform this operation');
// define some loop functions
function loop_persons_in_entities($context, $funcname)
$varname = $context['varname'];
$idtype = $context['idtype'];
die("ERROR: internal error in Entities_EditionLogic::loop_persons_in_entities");
if (!$context['persons'][$idtype]) {
foreach($context['persons'][$idtype] as $degree => $arr) {
$localcontext['name'] = $varname;
$localcontext['classtype'] = "persons";
$localcontext['degree'] = $degree;
if ($degree > $maxdegree) {
$localcontext = $context;
$localcontext['maxdegree'] = $maxdegree;
function loop_entries_in_entities($context, $funcname)
$varname = $context['varname'];
$idtype = $context['idtype'];
$votype = $dao->getById($idtype, "id,sort,flat");
die("ERROR: internal error in loop_entries_in_entities");
if ($context['entries'][$idtype]) {
foreach ($context['entries'][$idtype] as $entry) {
$checkarr[] = &$entry['g_name'];
$context['id'] = 0; // start by the parents
loop_entries_in_entities_rec ($context, $funcname, $votype, $checkarr);
function loop_entries_in_entities_rec ($context, $funcname, &$votype, &$checkarr)
$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();
$localcontext = array_merge($context, $result->fields);
$localcontext['selected'] = $checkarr && in_array($result->fields['g_name'],$checkarr) ? "selected=\"selected\"" : "";
$localcontext['root'].= $localcontext['g_name']. "/";
loop_entries_in_entities_rec ($localcontext, $funcname, $votype, $checkarr);
function loop_entities_select($context, $funcname)
$varname = $context['varname'];
$ids = preg_split("/,/", $context['entities'][$varname], - 1, PREG_SPLIT_NO_EMPTY);
$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();
$localcontext = array_merge($context, $result->fields);
$localcontext = $context;
$localcontext['all'] = $context['entities'][$varname];
//if ((!$context['id'] || (preg_match ("/servoo.*/", $context['creationmethod']) && $context['status'] == -64)) && !$error) { // add
if (!$context['id'] && !$error) { // add : récupération des valeurs par défaut
$daotablefields = &getDAO("tablefields");
$fields = $daotablefields->findMany("class='". $context['type']['class']. "' AND status>0 AND type!='passwd'", "", "name,defaultvalue");
foreach($fields as $field) {
if (empty($context['data'][$field->name]))
$context['data'][$field->name] = $field->defaultvalue;
if ($context['check'] && !$error) {
//il semble nécessaire de nettoyer request pour eviter les requetes pétées.
require_once('func.php');
return $ret ? $ret : "_ok";
* Ajout d'un nouvel objet ou Edition d'un objet existant
* Ajout d'une nouvelle entité. Dans un premier temps on vérifie si l'utilisateur
* possède les bons droits pour modifier ou ajouter cette entité. Ensuite les différents
* champs de l'entité sont validés. Si ceux-ci sont valide, alors l'objet est créé dans
* la base de données et les objets liés sont aussi créés : personnes, entrées d'index
* @param array &$context le contexte passé par référence
* @param array &$error le tableau des erreurs éventuelles passé par référence
function editAction (&$context, &$error, $opt = false)
if (!rightonentity($context['id'] ? 'edit' : 'create', $context)) {
die ("ERROR: you don't have the right to perform this operation");
if ($context['cancel']) {
global $lodeluser, $home;
if ($id && (!isset ($context['idparent']) || !isset ($context['idtype']))) {
$vo = $dao->getById($id, "idparent, idtype");
$context['idparent'] = $vo->idparent;
$context['idtype'] = $vo->idtype;
$idparent = $context['idparent'];
$idtype = $context['idtype'];
$status = intval($context['status']);
$context['iduser'] = !SINGLESITE && $lodeluser['adminlodel'] ? 0 : $lodeluser['id'];
require_once 'entitiesfunc.php';
$error['idtype'] = 'types_compatibility';
$votype = $daotype->getById($context['idtype'], "class,creationstatus,search");
$class = $context['class']= $votype->class;
die ("ERROR: idtype is not valid in Entities_EditionLogic::editAction");
// Récupération des valeurs par défaut pour les champs vides À L'IMPORT
if ($context['lo'] == 'entities_import' && !empty($context['idtask']) && !$error) {
$daotablefields = &getDAO("tablefields");
$fields = $daotablefields->findMany("class='". $context['class']. "' AND status>0 AND type!='passwd'", "", "name,defaultvalue");
foreach($fields as $field) {
if (empty($context['data'][$field->name]))
$context['data'][$field->name] = $field->defaultvalue;
// if the entity is imported and will be checked
// that's fine, let's continue, if not return an error
// get the dao for working with the object
$now = date("Y-m-d H:i:s");
if ($id) { //edit the entity
$vo = $dao->getById($id, "id,identifier,status, g_title");
die("ERROR: entity is locked. No operation is allowed");
// possibly document reloading
// let's deal with document reloading problem : PDF file and entries disapeared :
$daotablefields = &getDAO("tablefields");
$Filefields = $daotablefields->findMany("class='". $context['class']. "' AND status>0 AND (type='file' OR type='image')", "", "name");
foreach($Filefields as $ffield) {
$tmpfile = $gdaoaf->getById($id, $ffield->name);
$fieldname = $ffield->name;
if($context['data'][$ffield->name] == 'deleted') {
$context['data'][$ffield->name] = '';
} elseif(empty($context['data'][$ffield->name]) && !empty($tmpfile->$fieldname)) {
$context['data'][$ffield->name] = $tmpfile->$name;
$daorelations = &getDAO("relations");
$Entryfields = $daorelations->findMany("id1='{$id}' AND nature = 'E'", "", "id2");
$daoentries = &getDAO("entries");
$daoentrytypes = &getDAO("entrytypes");
foreach($Entryfields as $ffield) {
$entry = $daoentries->getById($ffield->id2, 'idtype, g_name');
$entryclass = $daoentrytypes->getById($entry->idtype, 'class');
if(is_array($context['entries'][$entry->idtype])) {
foreach($context['entries'][$entry->idtype] as $entryfield) {
if($entryfield['g_name'] == $entry->g_name) {
$pos = count($context['entries'][$entry->idtype]);
$context['entries'][$entry->idtype][$pos]['g_name'] = $entry->g_name;
$context['entries'][$entry->idtype][$pos]['class'] = $entryclass->class;
$context['entries'][$entry->idtype][$pos]['idtype'] = $entry->idtype;
$indexfields = $daotablefields->find("class='{$entryclass->class}' ", 'name');
foreach($indexfields as $indexfield)
$context['entries'][$entry->idtype][$pos]['data'][$indexfield->name] = null;
unset ($entry, $entryclass, $daoentries, $daoentrytypes, $Entryfields, $daorelations, $daotablefields, $Filefields); // save some memory
// change the usergroup of the entity ?
if ($lodeluser['admin'] && $context['usergroup']) {
$vo->usergroup = intval ($context['usergroup']);
if ($vo->status<=- 64) { // like a creation
$vo->status = $votype->creationstatus;
$vo->creationdate = $now;
} else { //create the entity
$vo = $dao->createObject ();
$vo->idparent = $idparent;
$vo->iduser = $context['iduser'];
$vo->status = $status ? $status : $votype->creationstatus;
$vo->creationdate = $now;
$vo->modificationdate = $now;
// permanent identifier management
$vo->g_title = strip_tags ($context['data'][$dctitle], "<em><strong><span><sup><sub>");
// If Identifier is not set, let's calcul it with the generic title
if (!$vo->identifier && trim($context['identifier']) === '') {
} else { // else simply clean bad chars
if(empty($context['identifier'])) {
$context['identifier'] = $vo->identifier;
/*if (is_null ($context['identifier'])) {//identifier desactivated
$vo->identifier = $this->_calculateIdentifier ($id, $vo->identifier);
} else {// else that means that we have modified it*/
if ($context['creationmethod']) {
$vo->creationmethod = $context['creationmethod'];
if ($context['creationinfo']) {
$vo->creationinfo = $context['creationinfo'];
$id = $context['id'] = $dao->save($vo);
// change the group recursively |