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
* F oundation , 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 {
/**
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 ;
$element = $form -> addElement ( 'select_object' , $this -> name , $this -> config [ 'label' ], $this -> config , $this );
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 );
}
}
2009-01-28 09:13:36 +01:00
$element -> setSelectableObject ( $this -> config [ '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 ) {
$retValues = array ();
2009-01-28 09:13:36 +01:00
if ( isset ( $this -> config [ 'html_options' ][ 'selectable_object' ])) {
$conf = $this -> config [ 'html_options' ][ 'selectable_object' ];
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 ;
}
2009-01-24 18:45:14 +01:00
if ( ! LSsession :: loadLSobject ( $conf [ 'object_type' ])) {
2008-10-07 14:17:50 +02:00
return ;
}
2008-10-15 11:12:20 +02:00
if ( is_array ( $values )) {
2008-10-07 14:17:50 +02:00
$obj = new $conf [ 'object_type' ]();
foreach ( $values as $dn => $name ) {
if ( $obj -> loadData ( $dn )) {
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' )) {
$val = $dn ;
}
2010-04-26 16:33:10 +02:00
elseif ( ! isset ( $obj -> attrs [ $conf [ 'value_attribute' ]])) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
2008-10-07 14:17:50 +02:00
else {
$val = $obj -> getValue ( $conf [ 'value_attribute' ]);
$val = $val [ 0 ];
}
if ( empty ( $val )) {
continue ;
}
$retValues [] = $val ;
}
}
return $retValues ;
}
}
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é .
*/
function getFormValues ( $values = NULL , $fromDNs = false ) {
2008-02-26 18:40:05 +01:00
$retInfos = array ();
2008-10-07 14:17:50 +02:00
$DNs = array ();
2009-01-28 09:13:36 +01:00
if ( isset ( $this -> config [ 'html_options' ][ 'selectable_object' ])) {
$conf = $this -> config [ 'html_options' ][ 'selectable_object' ];
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 ;
}
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
}
2008-10-15 11:12:20 +02:00
if ( is_array ( $values )) {
2008-10-07 14:17:50 +02:00
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' ) || $fromDNs ) {
$DNs = $values ;
$obj = new $conf [ 'object_type' ]();
foreach ( $DNs as $dn ) {
if ( $obj -> loadData ( $dn )) {
2009-02-11 17:01:21 +01:00
$retInfos [ $dn ] = $obj -> getDisplayName ( $conf [ 'display_name_format' ]);
2008-02-26 18:40:05 +01:00
}
}
}
else {
2010-04-26 16:33:10 +02:00
if ( ! isset ( $conf [ 'value_attribute' ]) || ( ! is_array ( LSconfig :: get ( 'LSobjects.' . $conf [ 'object_type' ] . '.attrs.' . $conf [ 'value_attribute' ])))) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_02' , $this -> name );
return ;
}
2009-10-30 01:03:17 +01:00
$filters = array ();
2008-02-26 18:40:05 +01:00
foreach ( $values as $val ) {
2008-10-06 16:53:32 +02:00
if ( ! empty ( $val )) {
2009-10-30 01:03:17 +01:00
$filters [] = Net_LDAP2_Filter :: create ( $conf [ 'value_attribute' ], 'equals' , $val );
2008-10-06 16:53:32 +02:00
}
}
2009-10-30 01:03:17 +01:00
if ( ! empty ( $filters )) {
$filter = LSldap :: combineFilters ( 'or' , $filters );
if ( $filter ) {
$obj = new $conf [ 'object_type' ]();
$listobj = $obj -> listObjectsName ( $filter , NULL , array (), $conf [ 'display_name_format' ]);
foreach ( $listobj as $dn => $name ) {
$DNs [] = $dn ;
$retInfos [ $dn ] = $name ;
}
2008-10-07 14:17:50 +02:00
}
2008-02-26 18:40:05 +01:00
}
}
}
else {
return false ;
}
$_SESSION [ 'LSselect' ][ $conf [ 'object_type' ]] = $DNs ;
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 () {
2009-01-28 09:13:36 +01:00
if ( is_array ( $_SESSION [ 'LSselect' ][ $this -> config [ 'html_options' ][ 'selectable_object' ][ 'object_type' ]])) {
return $this -> getFormValues ( $_SESSION [ 'LSselect' ][ $this -> config [ 'html_options' ][ 'selectable_object' ][ 'object_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. " )
);
2009-01-02 17:00:25 +01:00
2008-02-26 18:40:05 +01:00
?>