2008-02-26 18:40:05 +01:00
< ? php
/*******************************************************************************
* Copyright ( C ) 2007 Easter - eggs
2021-04-13 18:04:19 +02:00
* https :// ldapsaisie . org
2008-02-26 18:40:05 +01:00
*
* 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
*/
2019-03-12 11:42:53 +01:00
public function addToForm ( & $form , $idForm , $data = NULL ) {
2008-02-26 18:40:05 +01:00
$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 ) {
2020-11-30 19:43:52 +01:00
$values = $this -> getFormValues ( ensureIsArray ( $data ));
2008-02-26 18:40:05 +01:00
if ( $values ) {
$element -> setValue ( $values );
}
}
return $element ;
}
2008-05-14 16:43:23 +02:00
/**
* Effectue les tâches nécéssaires au moment du rafraichissement du formulaire
2020-04-29 15:54:21 +02:00
*
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 .
2020-04-29 15:54:21 +02:00
*
2008-05-14 16:43:23 +02:00
* @ param [ in ] $data mixed La valeur de l ' attribut ( liste de DNs )
2020-04-29 15:54:21 +02:00
*
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
**/
2019-03-12 11:42:53 +01:00
public 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
}
/**
2020-05-19 17:55:55 +02:00
* Check and return selectable objects configuration and eventually instanciate
* it if & instanciate
2008-02-26 18:40:05 +01:00
*
2020-05-19 17:55:55 +02:00
* @ param [ in ] boolean & $instanciated_objects reference If this reference point to an array , each valid
* selectable object type will be instanciated in this
* array with object type name as key .
2020-04-29 15:54:21 +02:00
*
2008-02-26 18:40:05 +01:00
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2020-05-19 17:55:55 +02:00
* @ retval array | false Selectable objects configuration or False on error .
2020-04-29 15:54:21 +02:00
*/
2020-05-19 17:55:55 +02:00
public function getSelectableObjectsConfig ( & $instanciated_objects ) {
$confs = $this -> getConfig ( 'html_options.selectable_object' );
if ( ! is_array ( $confs )) {
self :: log_debug ( 'getSelectableObjectsConfig(): html_options.selectable_object not properly configured' );
return false ;
}
// Make sure we have an array or configured selectable objects
if ( isset ( $confs [ 'object_type' ])) {
$confs = array ( $confs );
}
// Return confs
$ret_confs = array ();
// For each configured object types:
// - check we have the minimal configuration (object_type & value_attribute)
// - check we to not have multiple conf for the same object type
// - load object type
// - implement an object of this type (if is_array($instanciated_objects))
// - if value are from attributes, check this attribute exists
foreach ( $confs as $conf ) {
2008-10-07 14:17:50 +02:00
if ( ! isset ( $conf [ 'object_type' ])) {
2020-05-19 17:55:55 +02:00
LSerror :: addErrorCode (
'LSattr_html_select_object_01' ,
array (
'attr' => $this -> name ,
'parameter' => 'object_type' ,
)
);
return false ;
2008-10-07 14:17:50 +02:00
}
2019-03-11 22:21:25 +01:00
if ( ! isset ( $conf [ 'value_attribute' ])) {
2020-05-19 17:55:55 +02:00
LSerror :: addErrorCode (
'LSattr_html_select_object_01' ,
array (
'attr' => $this -> name ,
'parameter' => 'value_attribute' ,
)
);
return false ;
2019-03-11 22:21:25 +01:00
}
2020-04-29 15:54:21 +02:00
2020-05-19 17:55:55 +02:00
if ( array_key_exists ( $conf [ 'object_type' ], $ret_confs )) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_04' , array ( 'type' => $conf [ 'object_type' ], 'attr' => $this -> name ));
return false ;
2008-10-07 14:17:50 +02:00
}
2020-05-19 17:55:55 +02:00
$object_type = $conf [ 'object_type' ];
2019-03-11 22:21:25 +01:00
2020-05-19 17:55:55 +02:00
if ( ! LSsession :: loadLSobject ( $object_type ))
return false ;
if ( is_array ( $instanciated_objects ))
$instanciated_objects [ $object_type ] = new $object_type ();
if ( ! ( $conf [ 'value_attribute' ] == 'dn' ) && ! ( $conf [ 'value_attribute' ] == '%{dn}' )) {
if ( ! $object_type :: hasAttr ( $conf [ 'value_attribute' ])) {
LSerror :: addErrorCode (
'LSattr_html_select_object_02' ,
array (
'attr' => $this -> name ,
'object_type' => $conf [ 'object_type' ],
'value_attribute' => $conf [ 'value_attribute' ],
)
);
return false ;
2008-10-07 14:17:50 +02:00
}
}
2020-05-19 17:55:55 +02:00
// Handle other parameters
$conf [ 'display_name_format' ] = LSconfig :: get ( 'display_name_format' , null , null , $conf );
$conf [ 'onlyAccessible' ] = LSconfig :: get ( 'onlyAccessible' , false , 'bool' , $conf );
$conf [ 'filter' ] = LSconfig :: get ( 'filter' , null , null , $conf );
$ret_confs [ $object_type ] = $conf ;
2008-10-07 14:17:50 +02:00
}
2020-05-19 17:55:55 +02:00
return $ret_confs ;
2008-10-07 14:17:50 +02:00
}
2020-05-19 17:55:55 +02:00
2008-10-07 14:17:50 +02:00
/**
2020-05-19 17:55:55 +02:00
* Return an array of selected objects with DN as key and as value , an array with object name ( key name )
* and type ( key object_type ) .
2008-10-07 14:17:50 +02:00
*
2020-05-19 17:55:55 +02:00
* @ param [ in ] mixed $values array | null Array of the input values ()
* @ param [ in ] boolean $fromDNs boolean If true , considered provided values as DNs ( default : false )
* @ param [ in ] boolean $retreiveAttrValues boolean If true , attribute values will be returned instead
* of selected objects info ( default : false )
2020-04-29 15:54:21 +02:00
*
2008-10-07 14:17:50 +02:00
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
2020-05-19 17:55:55 +02:00
* @ retval array | false Array of selected objects with DN as key and object info as value or array
* of attribute values if $retreiveAttrValues == true or False on error .
2020-04-29 15:54:21 +02:00
*/
2020-05-19 17:55:55 +02:00
public function getFormValues ( $values = NULL , $fromDNs = false , $retreiveAttrValues = false ) {
self :: log_debug ( " getFormValues(): input values= " . varDump ( $values ));
// Check parameters consistency
if ( $retreiveAttrValues && ! $fromDNs ) {
self :: log_fatal ( 'getFormValues(): $fromDNs must be true if $retreiveAttrValues==true' );
return false ;
}
if ( ! is_array ( $values )) {
self :: log_warning ( 'getFormValues(): $values is not array' );
return false ;
}
2019-03-11 22:21:25 +01:00
2020-05-19 17:55:55 +02:00
// Retreive/check selectable objects config
$objs = array ();
$confs = $this -> getSelectableObjectsConfig ( $objs );
if ( ! is_array ( $confs ) || empty ( $confs )) {
self :: log_warning ( 'getFormValues(): invalid selectable objects config' );
return false ;
}
2020-04-29 15:54:21 +02:00
2020-05-19 17:55:55 +02:00
$selected_objects = array ();
$unrecognizedValues = array ();
$found_values = array ();
foreach ( $confs as $conf ) {
foreach ( $values as $value ) {
// If we already mark its value as unrecognized, pass
if ( in_array ( $value , $unrecognizedValues ))
continue ;
// Ignore empty value
if ( empty ( $value ))
continue ;
2020-04-29 15:54:21 +02:00
2020-05-19 17:55:55 +02:00
// Determine value attribute: DN/attribute valued (or force form DNs)
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' ) || $fromDNs ) {
// Construct resulting list from DN values
if ( $conf [ 'onlyAccessible' ]) {
if ( ! LSsession :: canAccess ( $conf [ 'object_type' ], $value )) {
self :: log_debug ( " getFormValues(): " . $conf [ 'object_type' ] . " ( $value ): not accessible, pass " );
continue ;
}
}
2019-03-11 22:21:25 +01:00
2020-05-19 17:55:55 +02:00
// Load object data (with custom filter if defined)
if ( ! $objs [ $conf [ 'object_type' ]] -> loadData ( $value , $conf [ 'filter' ])) {
self :: log_debug ( " getFormValues(): " . $conf [ 'object_type' ] . " ( $value ): not found, pass " );
continue ;
2008-02-26 18:40:05 +01:00
}
2020-05-19 17:55:55 +02:00
self :: log_debug ( " getFormValues(): " . $conf [ 'object_type' ] . " ( $value ): found " );
// Check if it's the first this value match with an object
if ( isset ( $found_values [ $value ])) {
// DN match with multiple object type
LSerror :: addErrorCode ( 'LSattr_html_select_object_03' , array ( 'val' => $value , 'attribute' => $this -> name ));
$unrecognizedValues [] = $value ;
unset ( $selected_objects [ $found_values [ $value ]]);
break ;
}
$found_values [ $value ] = $value ;
if ( $retreiveAttrValues ) {
// Retreive attribute value case: $selected_objects[dn] = attribute value
if (( $conf [ 'value_attribute' ] == 'dn' ) || ( $conf [ 'value_attribute' ] == '%{dn}' )) {
$selected_objects [ $value ] = $value ;
2019-03-11 22:21:25 +01:00
}
else {
2020-05-19 17:55:55 +02:00
$val = $objs [ $conf [ 'object_type' ]] -> getValue ( $conf [ 'value_attribute' ]);
if ( ! empty ( $val )) {
$selected_objects [ $value ] = $val [ 0 ];
}
else {
LSerror :: addErrorCode (
'LSattr_html_select_object_06' ,
array (
'name' => $objs [ $conf [ 'object_type' ]] -> getDisplayName ( $conf [ 'display_name_format' ]),
'attr' => $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
}
2020-05-19 17:55:55 +02:00
else {
// General case: $selected_objects[dn] = array(name + object_type)
$selected_objects [ $value ] = array (
'name' => $objs [ $conf [ 'object_type' ]] -> getDisplayName ( $conf [ 'display_name_format' ]),
'object_type' => $conf [ 'object_type' ],
);
self :: log_debug ( " getFormValues(): " . $conf [ 'object_type' ] . " ( $value ): " . varDump ( $selected_objects [ $value ]));
}
}
else {
// Construct resulting list from attributes values
$filter = Net_LDAP2_Filter :: create ( $conf [ 'value_attribute' ], 'equals' , $value );
if ( isset ( $conf [ 'filter' ]))
$filter = LSldap :: combineFilters ( 'and' , array ( $filter , $conf [ 'filter' ]));
$sparams = array ();
$sparams [ 'onlyAccessible' ] = $conf [ 'onlyAccessible' ];
$listobj = $objs [ $conf [ 'object_type' ]] -> listObjectsName (
$filter ,
NULL ,
$sparams ,
$conf [ 'display_name_format' ]
);
if ( count ( $listobj ) == 1 ) {
if ( isset ( $found_values [ $value ])) {
// Value match with multiple object type
LSerror :: addErrorCode ( 'LSattr_html_select_object_03' , array ( 'val' => $value , 'attribute' => $this -> name ));
$unrecognizedValues [] = $value ;
unset ( $selected_objects [ $found_values [ $value ]]);
break ;
}
$dn = key ( $listobj );
$selected_objects [ $dn ] = array (
'name' => $listobj [ $dn ],
'object_type' => $conf [ 'object_type' ],
);
$found_values [ $value ] = $dn ;
}
else if ( count ( $listobj ) > 1 ) {
LSerror :: addErrorCode ( 'LSattr_html_select_object_03' , array ( 'val' => $value , 'attribute' => $this -> name ));
if ( ! in_array ( $value , $unrecognizedValues ))
$unrecognizedValues [] = $value ;
break ;
}
2008-02-26 18:40:05 +01:00
}
}
}
2020-05-19 17:55:55 +02:00
// Check if all values have been found (or already considered as unrecognized)
foreach ( $values as $value ) {
if ( ! isset ( $found_values [ $value ]) && ! in_array ( $value , $unrecognizedValues )) {
self :: log_debug ( " getFormValues(): value ' $value ' not recognized " );
$unrecognizedValues [] = $value ;
}
}
// Retreive attribute values case: return forged array values (list of attribute values)
if ( $retreiveAttrValues )
return array_values ( $selected_objects );
// General case
self :: log_debug ( " getFormValues(): unrecognizedValues= " . varDump ( $unrecognizedValues ));
$this -> unrecognizedValues = $unrecognizedValues ;
self :: log_debug ( " getFormValues(): final values= " . varDump ( $selected_objects ));
return $selected_objects ;
}
/**
* Get LSselect ID for this attribute
*
* @ return string The LSselect ID for this attribute
*/
public function getLSselectId () {
$id = " " ;
if ( $this -> attribute -> ldapObject -> isNew ())
$id .= $this -> attribute -> ldapObject -> getType ();
else
$id .= $this -> attribute -> ldapObject -> getDn ();
$id .= " | " . $this -> name ;
return $id ;
2008-02-26 18:40:05 +01:00
}
/**
2020-05-19 17:55:55 +02:00
* Return array of atttribute values form array of form values
*
* @ param [ in ] mixed Array of form values
*
* @ author Benjamin Renard < brenard @ easter - eggs . com >
*
* @ retval array | false Array of attribute values or False on error .
*/
public function getValuesFromFormValues ( $values = NULL ) {
if ( is_array ( $values )) {
return $this -> getFormValues ( array_keys ( $values ), true , true );
}
return false ;
}
/**
* Return an array of selected objects with DN as key and display name as value
* from LSselect
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
*/
2020-05-19 17:55:55 +02:00
public function getValuesFromLSselect () {
$LSselect_id = $this -> getLSselectId ();
if ( LSsession :: loadLSclass ( 'LSselect' , null , true ) && LSselect :: exists ( $LSselect_id )) {
$selected_objects = LSselect :: getSelectedObjects ( $LSselect_id );
self :: log_debug ( " getValuesFromLSselect(): selected objects retreived from LSselect ' $LSselect_id ' = " . varDump ( $selected_objects ));
if ( is_array ( $selected_objects )) {
return $this -> getFormValues (
array_keys ( $selected_objects ),
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
**/
2019-03-12 11:42:53 +01:00
public function getFormVal ( $data ) {
2011-01-20 12:15:30 +01:00
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' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : parameter '% { parameter}' is missing (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' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : the value of the parameter value_attribute in the configuration of the attribute % { attr} is incorrect. Object % { object_type} have no attribute % { value_attribute}. " )
2010-04-26 16:33:10 +02:00
);
2012-10-03 12:23:49 +02:00
LSerror :: defineError ( 'LSattr_html_select_object_03' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : more than one object returned corresponding to value % { val} of attribute % { attr}. " )
2012-10-03 12:23:49 +02:00
);
2020-05-19 17:55:55 +02:00
LSerror :: defineError ( 'LSattr_html_select_object_04' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : selection of object type % { type} is configured multiple time for attribute % { attr}. " )
2020-05-19 17:55:55 +02:00
);
LSerror :: defineError ( 'LSattr_html_select_object_05' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : the value '% { value}' seem to be duplicated in values of the attribute % { attr}. " )
2020-05-19 17:55:55 +02:00
);
LSerror :: defineError ( 'LSattr_html_select_object_06' ,
2020-08-25 17:31:50 +02:00
___ ( " LSattr_html_select_object : selected object % { name} has no attribute % { attr} value, you can't select it. " )
2020-05-19 17:55:55 +02:00
);