Source for file genericlogic.php
Documentation is available at genericlogic.php
* Fichier de la classe Genericlogic
* LODEL - Logiciel d'Edition ELectronique.
* Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
* Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
* Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
* Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
* Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
* Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
* 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 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
* Classe des logiques métiers générique.
* <p>Cette classe définit la logique par défaut pour les objets dynamiques de l'interface :
* entrées, personnes par exemple</p>
* @author Ghislain Picard
* @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ée depuis la version 0.8
* Constructeur de la classe
* Définit le nom de la table type pour l'objet ainsi que le nom du champ identifiant unique.
* @param string $classtype le type d'objet generique, parmis : entities, entries et persons.
$this->_typetable = "types";
$this->_idfield = "identity";
$this->_typetable = "entrytypes";
$this->_idfield = "identry";
$this->_typetable = "persontypes";
$this->_idfield = "idperson";
$this->Logic($classtype);
* Implémentation pour les objets générique de l'action permettant d'appeler l'affichage d'un objet.
* Cette fonction récupère les données de l'objet <em>via</em> la DAO de l'objet. Ensuite elle
* met ces données dans le context (utilisation de la fonction privée _populateContext())
* @param array $context le tableau des données passé par référence.
* @param array $error le tableau des erreurs rencontrées passé par référence.
* @return string les différentes valeurs possibles de retour d'une action (_ok, _back, _error ou xxxx).
// define some loop functions
function loop_edition_fields($context, $funcname)
require_once 'validfunc.php';
validfield($context['class'], 'class', '', '','data');
$class = $context['class'];
} elseif ($context['type']['class']) {
validfield($context['type']['class'], 'class', '', '', 'data');
$class = $context['type']['class'];
die("ERROR: internal error in loop_edition_fields");
if ($context['classtype'] == "persons") {
$criteria = "class='". $class. "'";
// degree is defined only when the persons is related to a document. Is it a hack ? A little no more...
if ($context['identifier']) {
$criteria .= " OR class='entities_". $class. "'";
} elseif ($context['classtype'] == "entries") {
$criteria = "class='". $class. "'";
$criteria = "idgroup='". $context['id']. "'";
$context['idgroup'] = $context['id'];
$result = $db->execute(lq("SELECT * FROM #_TP_tablefields WHERE ". $criteria. " AND status>0 AND edition!='' AND edition!='none' AND edition!='importable' ORDER BY rank")) or dberror();
$haveresult = !empty ($result->fields);
$localcontext = array_merge($context, $result->fields);
$name = $result->fields['name'];
$localcontext['value'] = $result->fields['edition'] != "display" && is_string($context['data'][$name]) ? htmlspecialchars($context['data'][$name]) : $context['data'][$name];
$vo = $dao->getById($id);
die("ERROR: can't find object $id in the table ". $this->maintable);
$daotype = &getDAO($this->_typetable);
$votype = $daotype->getById($context['idtype']);
die("ERROR: idtype must me known in GenericLogic::viewAction");
$gvo = $gdao->getById($id);
die("ERROR: can't find object $id in the associated table. Please report this bug");
// nettoyage avant affichage
require_once('func.php');
return $ret ? $ret : "_ok";
* Validated the public fields and the unicity as usual and in addition the typescompatibility
* Validation des champs publics et de l'unicité comme dans la fonction de logic.php. Mais vérifie
* la compatibilité des types d'objet en plus.
* @param array $context le tableau des données passé par référence.
* @param array $error le tableau des erreurs rencontrées passé par référence.
// get the fields of class
require_once "validfunc.php";
validfield($context['class'], 'class', '', '', 'data');
$class = $context['class'];
} elseif ($context['type']['class']) {
validfield($context['type']['class'], "class", '', '', 'data');
$class = $context['type']['class'];
die("ERROR: internal error in loop_edition_fields");
$daotablefields = &getDAO("tablefields");
$fields = $daotablefields->findMany("(class='". $class. "' OR class='entities_". $class. "') AND status>0 ", "", "name,type,class,cond,defaultvalue,allowedtags,edition,g_name");
// file to move once the document id is know.
$this->files_to_move = array ();
$this->_publicfields = array ();
require_once "fieldfunc.php";
foreach ($fields as $field) {
// check if the field is required or not, and rise an error if any problem.
$value = &$context['data'][$name];
$empty = $type != "boolean" && (// boolean are always true or false
!isset ($context['data'][$name]) || // not set
$context['data'][$name] === ""); // or empty string
if (($context['do'] == "edit" && ($field->edition == "importable" ||
$field->edition == "none" || $field->edition == "display"))) {
// in edition interface and field is not editable in the interface
if ($field->cond != "+") { // the field is not required.
$value = lodel_strip_tags($field->default, $field->allowedtags); // default value
if ($context['id'] > 0 && (($field->cond == "permanent") || ($field->cond == "defaultnew" && $empty))) {
// or field is empty and the default value must not be used
if ($type != "persons" && $type != "entries" && $type != "entities") {
$this->_publicfields[$field->class][$name] = true; // this field is public
if ($field->edition == "none") {
$value = lodel_strip_tags($field->default, $field->allowedtags); // default value
if ($field->cond == "+" && $empty) {
$error[$name] = "+"; // required
if ($field->cond == 'unique' && !$this->_is_unique($class, $name, $value, $context['id'])) {
$error[$name] = "1"; // must be unique
// clean automatically the fields when required.
if (!is_array($value) && $GLOBALS['lodelfieldtypes'][$type]['autostriptags']) {
$valid = validfield($value, $type, $field->default, $name, 'data');
if ($type == "file" || $type == "image") {
if (preg_match("/\/tmpdir-\d+\/[^\/]+$/", $value)) {
// add this file to the file to move.
$this->files_to_move[$name] = array ('filename' => $value, 'type' => $type, 'name' => $name);
$error[$name] = $valid; // error
// not validated... let's try other type
if ($type == "persons") {
$dao = &getDAO("persontypes");
$vo = $dao->find("type='". $name. "'", "class,id");
$localcontext = &$context[$type][$idtype];
if ($type == "entries" && !is_array($localcontext)) {
$keys = explode(",", $localcontext);
$localcontext = array ();
foreach ($keys as $key) {
$localcontext[] = array ("g_name" => $key);
$logic = &getLogic($type); // the logic is used to validate
die("ERROR: internal error in GenericLogic::validateFields");
$localcontext[$k]['class'] = $vo->class;
$localcontext[$k]['idtype'] = $idtype;
$logic->validateFields($localcontext[$k], $err);
$error[$type][$idtype][$k] = $err;
$value = &$context['entities'][$name];
unset ($context['entities'][$name]);
foreach (explode(",", $value) as $id) {
$count = $GLOBALS['db']->getOne(lq("SELECT count(*) FROM #_TP_entities WHERE status>-64 AND id ". sql_in_array($value)));
if ($GLOBALS['db']->errorno()) {
if ($count != count($value)) {
die("ERROR: some entities in $name are invalid. Please report the bug");
// don't check they exists, the interface ensure it ! (... hum)
die("ERROR: unable to check the validity of the field ". $name. " of type ". $type);
* Déplacement des fichiers associés à l'objet dans le bon répertoire
* @param integer $id l'identifiant numérique de l'objet
* @param array $files_to_move un tableau contenant les informations de tous les fichiers (nom et type)
* @param object &$vo l'objet virtuel correspondant à l'objet passé par référence
foreach ($files_to_move as $file) {
$dest = basename($file['filename']); // basename
die("ERROR: error in move_files");
$dirdest = "docannexe/". $file['type']. "/". $id;
$dest = $dirdest. "/". $dest;
|