2008-02-26 18:40:05 +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 .
******************************************************************************/
/**
* Type d ' attribut HTML select_object
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*/
class LSattr_html_select_object extends LSattr_html {
2012-10-03 12:23:49 +02:00
var $unrecognizedValues = false ;
2008-02-26 18:40:05 +01:00
/**
2008-04-25 16:09:27 +02:00
* Ajoute l ' attribut au formualaire passer en paramètre
2008-02-26 18:40:05 +01:00
*
* @ param [ in ] & $form LSform Le formulaire
* @ param [ in ] $idForm L ' identifiant du formulaire
* @ param [ in ] $data Valeur du champs du formulaire
*
2008-04-25 16:09:27 +02:00
* @ retval LSformElement L ' element du formulaire ajouté
2008-02-26 18:40:05 +01:00
*/
function addToForm ( & $form , $idForm , $data = NULL ) {
$this -> config [ 'attrObject' ] = $this ;
2019-03-11 22:21:25 +01:00
$element = $form -> addElement ( 'select_object' , $this -> name , $this -> getLabel (), $this -> config , $this );
2008-02-26 18:40:05 +01:00
if ( ! $element ) {
2009-01-24 18:45:14 +01:00
LSerror :: addErrorCode ( 'LSform_06' , $this -> name );
2008-02-26 18:40:05 +01:00
return ;
}
if ( $data ) {
2009-12-31 17:02:07 +01:00
if ( ! is_array ( $data )) {
$data = array ( $data );
}
2008-10-07 14:17:50 +02:00
$values = $this -> getFormValues ( $data );
2008-02-26 18:40:05 +01:00
if ( $values ) {
$element -> setValue ( $values );
}
}
2019-03-11 22:21:25 +01:00
$element -> setSelectableObject ( $this -> getConfig ( 'html_options.selectable_object.object_type' ));
2008-02-26 18:40:05 +01:00
return $element ;
}
2008-05-14 16:43:23 +02:00
/**
* Effectue les tâches nécéssaires au moment du rafraichissement du formulaire
*
2009-01-07 20:58:08 +01:00
* Récupère un array du type array ( 'DNs' => 'displayName' ) à partir d ' une
2008-05-14 16:43:23 +02:00
* liste de DNs .
*
* @ param [ in ] $data mixed La valeur de l ' attribut ( liste de DNs )
*
2009-01-07 20:58:08 +01:00
* @ retval mixed La valeur formatée de l 'attribut (array(' DNs ' => ' displayName ' ))
2008-05-14 16:43:23 +02:00
**/
2008-10-07 14:53:43 +02:00
function refreshForm ( $data , $fromDNs = false ) {
2008-10-07 14:44:49 +02:00
return $this -> getFormValues ( $data , $fromDNs );
2008-02-26 18:40:05 +01:00
}
/**
2008-10-07 14:17:50 +02:00
* Retourne un tableau des valeurs de l ' attribut à partir des valeurs du formulaire
2008-02-26 18:40:05 +01:00
*
2008-10-07 14:17:50 +02:00
* @ param [ in ] mixed Tableau des valeurs du formulaire
2008-02-26 18:40:05 +01:00
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2008-10-07 14:17:50 +02:00
* @ retval array Tableau des valeurs de l ' attribut
2008-02-26 18:40:05 +01:00
*/
2008-10-07 14:17:50 +02:00
function getValuesFromFormValues ( $values = NULL ) {
2019-03-11 22:21:25 +01:00
$conf = $this -> getConfig ( 'html_options.selectable_object' );
if ( is_array ( $conf ) && is_array ( $values )) {
$retValues = array ();
2008-10-07 14:17:50 +02:00
if ( ! isset ( $conf [ 'object_type' ])) {
2009-01-24 18:45:14 +01:00
LSerror :: addErrorCode ( 'LSattr_html_select_object_01' , $this -> name );
2008-10-07 14:17:50 +02:00
return ;
}
2019-03-11 22:21:25 +01:00
if ( ! isset ( $conf [ 'value_attribute' ])) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
2008-10-07 14:17:50 +02:00
2009-01-24 18:45:14 +01:00
if ( ! LSsession :: loadLSobject ( $conf [ 'object_type' ])) {
2008-10-07 14:17:50 +02:00
return ;
}
2019-03-11 22:21:25 +01:00
$obj = new $conf [ 'object_type' ]();
foreach ( $values as $dn => $name ) {
if ( $obj -> loadData ( $dn )) {
$val = '' ;
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' )) {
$val = $dn ;
}
else {
if ( ! isset ( $obj -> attrs [ $conf [ 'value_attribute' ]])) {
2010-04-26 16:33:10 +02:00
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
2019-03-11 22:21:25 +01:00
$val = $obj -> getValue ( $conf [ 'value_attribute' ]);
$val = $val [ 0 ];
}
if ( empty ( $val )) {
continue ;
2008-10-07 14:17:50 +02:00
}
2019-03-11 22:21:25 +01:00
$retValues [] = $val ;
2008-10-07 14:17:50 +02:00
}
}
2019-03-11 22:21:25 +01:00
return $retValues ;
2008-10-07 14:17:50 +02:00
}
return ;
}
/**
* Retourne un tableau des objects selectionnés
*
* @ param [ in ] mixed $values Tableau des valeurs de l ' attribut
* @ param [ in ] boolean $fromDNs True si les valeurs passées en paramètre sont des DNs
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
* @ retval array Tableau associatif des objects selectionés avec en clé
* le DN et en valeur ce qui sera affiché .
*/
2019-03-11 22:21:25 +01:00
function getFormValues ( $values = NULL , $fromDNs = false ) {
$conf = $this -> getConfig ( 'html_options.selectable_object' );
if ( is_array ( $conf ) && is_array ( $values )) {
2008-02-26 18:40:05 +01:00
if ( ! isset ( $conf [ 'object_type' ])) {
2009-01-24 18:45:14 +01:00
LSerror :: addErrorCode ( 'LSattr_html_select_object_01' , $this -> name );
2008-10-06 15:11:14 +02:00
return ;
}
2019-03-11 22:21:25 +01:00
if ( ! isset ( $conf [ 'value_attribute' ])) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
2008-10-06 15:11:14 +02:00
2009-01-24 18:45:14 +01:00
if ( ! LSsession :: loadLSobject ( $conf [ 'object_type' ])) {
2008-10-06 15:11:14 +02:00
return ;
2008-02-26 18:40:05 +01:00
}
2019-03-11 22:21:25 +01:00
$retInfos = array ();
$DNs = array ();
$obj = new $conf [ 'object_type' ]();
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' ) || $fromDNs ) {
$DNs = $values ;
foreach ( $DNs as $dn ) {
if ( $obj -> loadData ( $dn )) {
$retInfos [ $dn ] = $obj -> getDisplayName ( $conf [ 'display_name_format' ]);
2008-02-26 18:40:05 +01:00
}
}
2019-03-11 22:21:25 +01:00
}
else {
if ( ! is_array ( LSconfig :: get ( 'LSobjects.' . $conf [ 'object_type' ] . '.attrs.' . $conf [ 'value_attribute' ]))) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
$unrecognizedValues = array ();
foreach ( $values as $val ) {
if ( ! empty ( $val )) {
$filter = Net_LDAP2_Filter :: create ( $conf [ 'value_attribute' ], 'equals' , $val );
if ( isset ( $conf [ 'filter' ])) $filter = LSldap :: combineFilters ( 'and' , array ( $filter , $conf [ 'filter' ]));
$sparams = array ();
$sparams [ 'onlyAccessible' ] = ( isset ( $conf [ 'onlyAccessible' ]) ? $conf [ 'onlyAccessible' ] : False );
$listobj = $obj -> listObjectsName ( $filter , NULL , $sparams , ( isset ( $conf [ 'display_name_format' ]) ? $conf [ 'display_name_format' ] : false ));
if ( count ( $listobj ) == 1 ) {
foreach ( $listobj as $dn => $name ) {
$DNs [] = $dn ;
$retInfos [ $dn ] = $name ;
2012-10-03 12:23:49 +02:00
}
2019-03-11 22:21:25 +01:00
}
else {
$unrecognizedValues [] = $val ;
if ( count ( $listobj ) > 1 ) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_03' , array ( 'val' => $val , 'attribute' => $this -> name ));
2009-10-30 01:03:17 +01:00
}
2008-10-07 14:17:50 +02:00
}
2008-02-26 18:40:05 +01:00
}
}
2019-03-11 22:21:25 +01:00
$this -> unrecognizedValues = $unrecognizedValues ;
2008-02-26 18:40:05 +01:00
}
2019-03-11 22:21:25 +01:00
$_SESSION [ 'LSselect' ][ $conf [ 'object_type' ]] = $DNs ;
2008-02-26 18:40:05 +01:00
return $retInfos ;
}
return false ;
}
/**
2008-04-25 16:09:27 +02:00
* Retourne un tableau des valeurs de l ' attribut à partir de la variable session
2008-02-26 18:40:05 +01:00
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2008-10-07 14:17:50 +02:00
* @ retval array Tableau associatif des objects selectionnés avec en clé
* le DN et en valeur ce qui sera affiché .
2008-02-26 18:40:05 +01:00
*/
function getValuesFromSession () {
2019-03-11 22:21:25 +01:00
$obj_type = $this -> getConfig ( 'html_options.selectable_object.object_type' );
if ( $obj_type && is_array ( $_SESSION [ 'LSselect' ][ $obj_type ]) ) {
return $this -> getFormValues ( $_SESSION [ 'LSselect' ][ $obj_type ], true );
2008-02-26 18:40:05 +01:00
}
return false ;
}
2011-01-20 12:15:30 +01:00
/**
* Return the values to be displayed in the LSform
*
* @ param [ in ] $data The values of attribute
*
* @ retval array The values to be displayed in the LSform
**/
function getFormVal ( $data ) {
return $data ;
}
2008-02-26 18:40:05 +01:00
}
2009-01-02 17:00:25 +01:00
/*
* Error Codes
*/
2009-01-25 15:37:03 +01:00
LSerror :: defineError ( 'LSattr_html_select_object_01' ,
_ ( " LSattr_html_select_object : LSobject type is undefined (attribute : % { attr}). " )
2009-01-02 17:00:25 +01:00
);
2010-04-26 16:33:10 +02:00
LSerror :: defineError ( 'LSattr_html_select_object_02' ,
_ ( " LSattr_html_select_object : the value of the parameter value_attribute in the configuration of the attribute % { attrs} is incorrect. This attribute does not exists. " )
);
2012-10-03 12:23:49 +02:00
LSerror :: defineError ( 'LSattr_html_select_object_03' ,
_ ( " LSattr_html_select_object : more than one object returned corresponding to value % { val} of attribute % { attr}. " )
);
2009-01-02 17:00:25 +01:00
2008-02-26 18:40:05 +01:00
?>