2007-11-15 19:07:24 +01:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
|
|
|
* http://ldapsaisie.labs.libre-entreprise.org
|
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Element d'un formulaire pour LdapSaisie
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette classe gère les éléments des formulaires.
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LSformElement {
|
|
|
|
|
|
|
|
var $name;
|
|
|
|
var $label;
|
|
|
|
var $params;
|
|
|
|
var $values = array();
|
|
|
|
var $_required = false;
|
|
|
|
var $_freeze = false;
|
2008-02-26 18:40:05 +01:00
|
|
|
var $attr_html;
|
2008-10-15 19:40:04 +02:00
|
|
|
var $fieldTemplate = 'LSformElement_field.tpl';
|
|
|
|
var $template = 'LSformElement.tpl';
|
2008-10-15 20:29:14 +02:00
|
|
|
var $fetchVariables = array();
|
2007-11-15 19:07:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructeur
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette methode construit l'objet et définis sa configuration de base.
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @param[in] &$form [<b>required</b>] LSform L'objet LSform parent
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] $name [<b>required</b>] string Le nom de référence de l'élément
|
|
|
|
* @param[in] $label [<b>required</b>] string Le label de l'élément
|
|
|
|
* @param[in] $params mixed Paramètres supplémentaires
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @retval true
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
2008-02-26 18:40:05 +01:00
|
|
|
function LSformElement (&$form, $name, $label, $params,&$attr_html){
|
2007-11-15 19:07:24 +01:00
|
|
|
$this -> name = $name;
|
2008-02-12 18:59:44 +01:00
|
|
|
$this -> label = $label;
|
|
|
|
$this -> params = $params;
|
2015-04-14 15:10:40 +02:00
|
|
|
$this -> form =& $form;
|
|
|
|
$this -> attr_html =& $attr_html;
|
2008-02-12 18:59:44 +01:00
|
|
|
return true;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Définis la valeur de l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette méthode définis la valeur de l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] [<b>required</b>] string or array La futur valeur de l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @retval boolean Retourne True
|
|
|
|
*/
|
|
|
|
function setValue($data) {
|
2008-02-12 18:59:44 +01:00
|
|
|
if (!is_array($data)) {
|
|
|
|
$data=array($data);
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
$this -> values = $data;
|
|
|
|
return true;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
2008-05-14 16:43:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Définis la valeur de l'élément à partir des données
|
|
|
|
* envoyées en POST du formulaire
|
|
|
|
*
|
|
|
|
* Cette méthode définis la valeur de l'élément à partir des données
|
|
|
|
* envoyées en POST du formulaire.
|
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @param[in] [<b>required</b>] string or array La futur valeur de l'élément
|
|
|
|
*
|
|
|
|
* @retval boolean Retourne True
|
|
|
|
*/
|
|
|
|
function setValueFromPostData($data) {
|
|
|
|
if (!is_array($data)) {
|
|
|
|
$data=array($data);
|
|
|
|
}
|
|
|
|
$this -> values = $data;
|
|
|
|
return true;
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
|
2008-07-18 16:02:46 +02:00
|
|
|
/**
|
|
|
|
* Exporte les valeurs de l'élément
|
|
|
|
*
|
|
|
|
* @retval Array Les valeurs de l'élement
|
|
|
|
*/
|
|
|
|
function exportValues(){
|
|
|
|
return $this -> values;
|
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Ajoute une valeur à l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette méthode ajoute une valeur à l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[in] [<b>required</b>] string or array La futur valeur de l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
function addValue($data) {
|
2008-02-12 18:59:44 +01:00
|
|
|
if (is_array($data)) {
|
|
|
|
$this -> values = array_merge($this -> values, $data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this -> values[] = $data;
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Test si l'élément est éditable
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette méthode test si l'élément est éditable
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
|
|
|
* @retval boolean
|
|
|
|
*/
|
|
|
|
function isFreeze(){
|
|
|
|
return $this -> _freeze;
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
|
2008-05-15 12:56:55 +02:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Freeze l'élément
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Rend l'élément non-editable
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
function freeze() {
|
2008-02-12 18:59:44 +01:00
|
|
|
$this -> _freeze = true;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-05-15 12:56:55 +02:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Défini la propriété required de l'élément.
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* param[in] $isRequired boolean true si l'élément est requis, false sinon
|
2007-11-15 19:07:24 +01:00
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
function setRequired($isRequired=true) {
|
2008-02-12 18:59:44 +01:00
|
|
|
$this -> _required = $isRequired;
|
|
|
|
}
|
|
|
|
|
2008-05-15 12:56:55 +02:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Test si l'élément est requis
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette méthode test si l'élément est requis
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
|
|
|
* @retval boolean
|
|
|
|
*/
|
|
|
|
function isRequired(){
|
|
|
|
return $this -> _required;
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Retourne le label de l'élement
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
|
|
|
* @retval void
|
|
|
|
*/
|
|
|
|
function getLabelInfos() {
|
|
|
|
if ($this -> isRequired()) {
|
|
|
|
$return['required']=true;
|
|
|
|
}
|
|
|
|
$return['label'] = $this -> getLabel();
|
2019-03-11 22:21:25 +01:00
|
|
|
$help_infos = array();
|
|
|
|
if ( $this -> getParam('displayAttrName', $this -> attr_html -> attribute -> ldapObject -> getConfig('displayAttrName', false, 'bool'), 'bool') ) {
|
|
|
|
$help_infos[] = _("Attribute")." : ".$this -> name."\n";
|
2010-11-19 18:41:10 +01:00
|
|
|
}
|
2019-03-11 22:21:25 +01:00
|
|
|
if ($this -> getParam('help_info')) {
|
|
|
|
$help_infos[] = __($this -> getParam('help_info'));
|
2008-09-26 20:03:56 +02:00
|
|
|
}
|
2019-03-11 22:21:25 +01:00
|
|
|
if (!empty($help_infos))
|
|
|
|
$return['help_info'] = implode(' - ', $help_infos);
|
2010-11-19 18:41:10 +01:00
|
|
|
|
2008-02-12 18:59:44 +01:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Recupère la valeur de l'élement passée en POST
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* Cette méthode vérifie la présence en POST de la valeur de l'élément et la récupère
|
|
|
|
* pour la mettre dans le tableau passer en paramètre avec en clef le nom de l'élément
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @param[] array Pointeur sur le tableau qui recupèrera la valeur.
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @retval boolean true si la valeur est présente en POST, false sinon
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
|
|
|
function getPostData(&$return) {
|
|
|
|
if($this -> isFreeze()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isset($_POST[$this -> name])) {
|
2008-10-15 12:32:51 +02:00
|
|
|
$return[$this -> name]=array();
|
2008-02-12 18:59:44 +01:00
|
|
|
if(!is_array($_POST[$this -> name])) {
|
|
|
|
$_POST[$this -> name] = array($_POST[$this -> name]);
|
|
|
|
}
|
|
|
|
foreach($_POST[$this -> name] as $key => $val) {
|
2013-09-05 11:40:06 +02:00
|
|
|
if (!empty($val)||(is_string($val)&&($val=="0"))) {
|
2008-02-12 18:59:44 +01:00
|
|
|
$return[$this -> name][$key] = $val;
|
2008-10-15 12:32:51 +02:00
|
|
|
}
|
2008-02-12 18:59:44 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return[$this -> name] = array();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-04-25 16:09:27 +02:00
|
|
|
* Retourne le label de l'élement
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
|
|
|
* Retourne $this -> label, ou $this -> params['label'], ou $this -> name
|
|
|
|
*
|
2008-04-25 16:09:27 +02:00
|
|
|
* @retval string Le label de l'élément
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
|
|
|
function getLabel() {
|
|
|
|
if ($this -> label != "") {
|
2009-03-19 18:42:51 +01:00
|
|
|
return __($this -> label);
|
2008-02-12 18:59:44 +01:00
|
|
|
}
|
2019-03-11 22:21:25 +01:00
|
|
|
return __($this -> getParam('label', $this -> name));
|
2008-02-12 18:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-10-16 17:35:20 +02:00
|
|
|
* Le champ est-il a valeur multiple
|
2008-02-12 18:59:44 +01:00
|
|
|
*
|
2008-10-16 17:35:20 +02:00
|
|
|
* @retval boolean True si le champ est à valeur multiple, False sinon
|
2008-02-12 18:59:44 +01:00
|
|
|
*/
|
2008-10-15 19:40:04 +02:00
|
|
|
function isMultiple() {
|
2019-03-11 22:21:25 +01:00
|
|
|
return $this -> getParam('multiple', false, 'bool');
|
2008-10-15 19:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retournne un template Smarty compilé dans le contexte d'un LSformElement
|
|
|
|
*
|
|
|
|
* @param[in] string $template Le template à retourner
|
|
|
|
* @param[in] array $variables Variables Smarty à assigner avant l'affichage
|
|
|
|
*
|
|
|
|
* @retval string Le HTML compilé du template
|
|
|
|
*/
|
|
|
|
function fetchTemplate($template=NULL,$variables=array()) {
|
|
|
|
if (!$template) {
|
|
|
|
$template = $this -> template;
|
|
|
|
}
|
2009-01-24 18:45:14 +01:00
|
|
|
return LSsession :: fetchTemplate(
|
2008-10-15 19:40:04 +02:00
|
|
|
$template,
|
|
|
|
array_merge_recursive(
|
|
|
|
$variables,
|
2008-10-15 20:29:14 +02:00
|
|
|
$this -> fetchVariables,
|
2008-10-15 19:40:04 +02:00
|
|
|
array(
|
|
|
|
'freeze' => $this -> isFreeze(),
|
|
|
|
'multiple'=> $this -> isMultiple(),
|
|
|
|
'value' => '',
|
|
|
|
'values' => $this -> values,
|
|
|
|
'attr_name' => $this -> name,
|
2019-03-11 22:21:25 +01:00
|
|
|
'noValueTxt' => __($this -> getParam('no_value_label', 'No set value', 'string')),
|
2014-11-20 00:31:08 +01:00
|
|
|
'fieldTemplate' => $this -> fieldTemplate,
|
|
|
|
'fieldType' => get_class($this)
|
2008-10-15 19:40:04 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retourne le code HTML d'un champ vide
|
|
|
|
*
|
|
|
|
* @retval string Code HTML d'un champ vide.
|
|
|
|
*/
|
|
|
|
function getEmptyField() {
|
|
|
|
return $this -> fetchTemplate($this -> fieldTemplate);
|
|
|
|
}
|
2019-03-11 22:21:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a parameter (or default value)
|
|
|
|
*
|
|
|
|
* @param[] $param The parameter
|
|
|
|
* @param[] $default The default value (default : null)
|
|
|
|
* @param[] $cast Cast resulting value in specific type (default : disabled)
|
|
|
|
*
|
|
|
|
* @retval mixed The parameter value or default value if not set
|
|
|
|
**/
|
|
|
|
public function getParam($param, $default=null, $cast=null) {
|
|
|
|
return LSconfig :: get($param, $default, $cast, $this -> params);
|
|
|
|
}
|
2007-11-15 19:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|