2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
23 LSsession :: loadLSclass('LSattr_ldap');
24 LSsession :: loadLSclass('LSattr_html');
29 * Cette classe modélise un attribut Ldap
31 * @author Benjamin Renard <brenard@easter-eggs.com>
41 var $updateData=false;
42 var $is_validate=false;
43 var $_finalUpdateData=false;
46 var $_objectEvents=array();
51 * Cette methode construit l'objet et définis la configuration.
52 * Elle lance la construction des objets LSattr_html et LSattr_ldap correspondant
53 * à ses types définis définis dans sa configuration
55 * @author Benjamin Renard <brenard@easter-eggs.com>
57 * @param[in] $name string Nom de l'attribut ldap
58 * @param[in] $config array Configuration de l'objet
59 * @param[in] &$ldapObject LSldapObject L'objet ldap parent
61 * @retval boolean Retourne true si la création a réussi, false sinon.
63 function LSattribute ($name,$config,&$ldapObject) {
64 $this -> name = $name;
65 $this -> config = $config;
66 $this -> ldapObject = $ldapObject;
67 $html_type = "LSattr_html_".$config['html_type'];
68 $ldap_type = "LSattr_ldap_".$config['ldap_type'];
69 LSsession :: loadLSclass($html_type);
70 LSsession :: loadLSclass($ldap_type);
71 if((class_exists($html_type))&&(class_exists($ldap_type))) {
72 $this -> html = new $html_type($name,$config,$this);
73 $this -> ldap = new $ldap_type($name,$config,$this);
76 LSerror :: addErrorCode('LSattribute_01',array('attr' => $name,'html'=>$config['html_type'],'ldap'=>$config['ldap_type']));
84 * Retourne la valeur du label de l'attribut
86 * @author Benjamin Renard <brenard@easter-eggs.com>
88 * @retval string Le label de l'attribut
90 * @see LSattr_html::getLabel()
95 LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name));
98 return $this -> html -> getLabel();
102 * Défini la valeur de l'attribut
104 * @author Benjamin Renard <brenard@easter-eggs.com>
106 * @retval boolean true
108 function loadData($attr_data) {
109 if ((!is_array($attr_data))&&(!empty($attr_data))) {
110 $attr_data = array($attr_data);
112 $this -> data = $attr_data;
117 * Redéfini la valeur de l'attribut
119 * @author Benjamin Renard <brenard@easter-eggs.com>
121 * @retval boolean true
123 function reloadData($attr_data) {
124 if ((!is_array($attr_data))&&(!empty($attr_data))) {
125 $attr_data = array($attr_data);
127 $this -> data = $attr_data;
128 $this -> updateData=false;
129 $this -> is_validate=false;
134 * Retourne la valeur de l'attribut
136 * Retourne la valeur nouvelle si elle existe, sinon la valeur passé.
138 * @author Benjamin Renard <brenard@easter-eggs.com>
140 * @retval mixed La valeur de l'attribut
142 function getValue() {
143 $updateData=$this -> getUpdateData();
144 if (empty($updateData)) {
145 return $this -> data;
153 * Retourne la valeur d'affichage de l'attribut
155 * @author Benjamin Renard <brenard@easter-eggs.com>
157 * @retval string La valeur d'affichage de l'attribut
159 function getDisplayValue() {
160 if (!$this -> ldap) {
161 LSerror :: addErrorCode('LSattribute_09',array('type' => 'ldap','name' => $this -> name));
164 $data = $this -> ldap -> getDisplayValue($this -> data);
165 if ($this -> config['onDisplay']) {
166 if (is_array($this -> config['onDisplay'])) {
168 foreach($this -> config['onDisplay'] as $func) {
169 if (function_exists($func)) {
170 $result=$func($result);
173 LSerror :: addErrorCode('LSattribute_02',array('attr' => $this->name,'func' => $func));
180 if (function_exists($this -> config['onDisplay'])) {
181 return $this -> config['onDisplay']($data);
184 LSerror :: addErrorCode('LSattribute_02',array('attr' => $this->name,'func' => $this -> config['onDisplay']));
193 * Ajoute l'attribut au formulaire
195 * Cette méthode ajoute l'attribut au formulaire $form si l'identifiant de celui-ci
196 * ($idForm) est connu dans la configuration de l'objet.
198 * @author Benjamin Renard <brenard@easter-eggs.com>
200 * @param[in] object $form Le formulaire dans lequel doit être ajouté l'attribut
201 * @param[in] string $idForm L'identifiant du formulaire
202 * @param[in] objet &$obj Objet utilisable pour la génération de la valeur de l'attribut
203 * @param[in] array $value valeur de l'élement
205 * @retval boolean true si l'ajout a fonctionner ou qu'il n'est pas nécessaire, false sinon
207 function addToForm(&$form,$idForm,&$obj=NULL,$value=NULL) {
208 if(isset($this -> config['form'][$idForm])) {
209 if (!$this -> html) {
210 LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name));
213 if($this -> myRights() == 'n') {
219 else if($this -> data !='') {
220 $data=$this -> getFormVal();
222 else if (isset($this -> config['default_value'])) {
223 $data=$obj -> getFData($this -> config['default_value']);
226 $element = $this -> html -> addToForm($form,$idForm,$data);
228 LSerror :: addErrorCode('LSform_06',$this -> name);
231 if($this -> config['required']==1) {
232 $form -> setRequired($this -> name);
235 if (($this -> config['form'][$idForm]==0) || ($this -> myRights() == 'r')) {
236 $element -> freeze();
239 if(isset($this -> config['check_data'])) {
240 if(is_array($this -> config['check_data'])) {
241 foreach ($this -> config['check_data'] as $rule => $rule_infos) {
242 if((!$form -> isRuleRegistered($rule))&&($rule!='')) {
243 LSerror :: addErrorCode('LSattribute_03',array('attr' => $this->name,'rule' => $rule));
246 if(!isset($rule_infos['msg'])) {
247 $rule_infos['msg']=getFData(_('The value of field %{label} is invalid.'),$this -> getLabel());
250 $rule_infos['msg']=__($rule_infos['msg']);
252 if(!isset($rule_infos['params']))
253 $rule_infos['params']=NULL;
254 $form -> addRule($this -> name,$rule,array('msg' => $rule_infos['msg'], 'params' => $rule_infos['params']));
258 LSerror :: addErrorCode('LSattribute_04',$this->name);
267 * Récupération des droits de l'utilisateur sur l'attribut
269 * @retval string 'r'/'w'/'n' pour 'read'/'write'/'none'
271 function myRights() {
273 if ($this -> _myRights != NULL) {
274 return $this -> _myRights;
277 $whoami = $this -> ldapObject -> whoami();
278 foreach($whoami as $who) {
281 if($this -> config['rights']['admin']=='w') {
290 if ($this -> config['rights'][$who] == 'w') {
294 else if($this -> config['rights'][$who] == 'r') {
303 $this -> _myRights = $return;
308 * Ajoute l'attribut au formualaire de vue
310 * Cette méthode ajoute l'attribut au formulaire $form de vue si il doit l'être
312 * @author Benjamin Renard <brenard@easter-eggs.com>
314 * @param[in] object $form Le formulaire dans lequel doit être ajouté l'attribut
316 * @retval boolean true si l'ajout a fonctionner ou qu'il n'est pas nécessaire, false sinon
318 function addToView(&$form) {
319 if((isset($this -> config['view'])) && ($this -> config['view']) && ($this -> myRights() != 'n') ) {
320 if (!$this -> html) {
321 LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name));
324 if($this -> data !='') {
325 $data=$this -> getFormVal();
330 $element = $this -> html -> addToForm($form,'view',$data);
331 if(!$element instanceof LSformElement) {
332 LSerror :: addErrorCode('LSform_06',$this -> name);
335 $element -> freeze();
342 * Rafraichis la valeur de l'attribut dans un formualaire
344 * @author Benjamin Renard <brenard@easter-eggs.com>
346 * @param[in] object &$form LSform Le formulaire dans lequel doit être ajouté l'attribut
347 * @param[in] string $idForm L'identifiant du formulaire
349 * @retval boolean true si la valeur a été rafraichie ou que ce n'est pas nécessaire, false sinon
351 function refreshForm(&$form,$idForm) {
352 if(isset($this -> config['form'][$idForm])&&($this -> myRights()!='n')) {
353 if (!$this -> html) {
354 LSerror :: addErrorCode('LSattribute_09',array('type' => 'html','name' => $this -> name));
357 $form_element = $form -> getElement($this -> name);
359 $values = $this -> html -> refreshForm($this -> getFormVal());
360 return $form_element -> setValue($values);
363 LSdebug('LSformElement "'.$this -> name.'" n\'existe pas');
370 * Retourne la valeur a afficher dans le formulaire
372 * @author Benjamin Renard <brenard@easter-eggs.com>
374 * @retval string La valeur a afficher dans le formulaire.
376 function getFormVal() {
377 $data=$this -> getDisplayValue();
381 if(!is_array($data)) {
388 * Définis les données de mises à jour si un changement a eut lieu
390 * @author Benjamin Renard <brenard@easter-eggs.com>
392 * @param[in] string $data Les données de mise à jour.
396 function setUpdateData($data) {
397 if($this -> ldap -> isUpdated($data)) {
398 $this -> updateData=$data;
403 * Vérifie si l'attribut a été validé
405 * @author Benjamin Renard <brenard@easter-eggs.com>
407 * @retval boolean true si l'attribut a été validé, false sinon
409 function isValidate() {
410 return $this -> is_validate;
416 * @author Benjamin Renard <brenard@easter-eggs.com>
420 function validate() {
421 $this -> is_validate=true;
425 * Vérifie si l'attribut a été mise à jour
427 * @author Benjamin Renard <brenard@easter-eggs.com>
429 * @retval boolean true si l'attribut a été mis à jour, false sinon
431 function isUpdate() {
432 return ($this -> updateData===false)?false:true;
436 * Vérifie si l'attribut est obligatoire
438 * @author Benjamin Renard <brenard@easter-eggs.com>
440 * @retval boolean true si l'attribut est obligatoire, false sinon
442 function isRequired() {
443 return $this -> config['required'];
447 * Vérifie si la valeur de l'attribut peut être générée
449 * @author Benjamin Renard <brenard@easter-eggs.com>
451 * @retval boolean true si la valeur de l'attribut peut être générée, false sinon
453 function canBeGenerated() {
455 (function_exists($this -> config['generate_function']))
457 (isset($this -> config['generate_value_format']))
460 (is_string($this -> config['default_value']))
462 (strlen($this -> config['default_value'])>0)
468 * Génere la valeur de l'attribut à partir de la fonction de génération
470 * @author Benjamin Renard <brenard@easter-eggs.com>
472 * @retval boolean true si la valeur à put être générée, false sinon
474 function generateValue() {
475 if (function_exists($this -> config['generate_function'])) {
476 $value=call_user_func($this -> config['generate_function'],$this -> ldapObject);
478 else if (isset($this -> config['generate_value_format'])) {
479 $value = $this -> ldapObject -> getFData($this -> config['generate_value_format']);
481 else if (is_string($this -> config['default_value']) && strlen($this -> config['default_value'])>0) {
482 $value = $this -> config['default_value'];
484 if (!empty($value)) {
485 if (!is_array($value)) {
486 $value=array($value);
488 $this -> updateData=$value;
495 * Retourne la valeur de l'attribut pour son enregistrement dans l'annuaire
496 * si l'attribut à été modifié.
498 * @author Benjamin Renard <brenard@easter-eggs.com>
500 * @retval mixed La valeur de l'attribut pour son enregistrement dans l'annuaire
502 function getUpdateData() {
503 if (!$this -> isUpdate()) {
506 if ( $this -> _finalUpdateData ) {
507 return $this -> _finalUpdateData;
509 $data=$this -> updateData;
510 if ($this -> config['onSave']) {
511 if (is_array($this -> config['onSave'])) {
513 foreach($this -> config['onSave'] as $func) {
514 if (function_exists($func)) {
515 $result=$func($result);
518 LSerror :: addErrorCode('LSattribute_05',array('attr' => $this->name,'func' => $func));
524 if (function_exists($this -> config['onSave'])) {
525 $result = $this -> config['onSave']($data);
528 LSerror :: addErrorCode('LSattribute_05',array('attr' => $this->name,'func' => $this -> config['onSave']));
534 if (!$this -> ldap) {
535 LSerror :: addErrorCode('LSattribute_09',array('type' => 'ldap','name' => $this -> name));
539 $result = $this -> ldap -> getUpdateData($data);
542 $this -> _finalUpdateData = $result;
547 * Retourne la configuration de validation de l'attribut
549 * @author Benjamin Renard <brenard@easter-eggs.com>
551 * @retval mixed La configuration de validation de l'attribut
553 function getValidateConfig() {
554 return $this -> config['validation'];
558 * Retourne les attributs dépendants de celui-ci
560 * @author Benjamin Renard <brenard@easter-eggs.com>
562 * @retval array les noms des attributs dépendants
564 function getDependsAttrs() {
565 return $this -> config['dependAttrs'];
569 * Ajouter une action lors d'un événement
571 * @param[in] $event string Le nom de l'événement
572 * @param[in] $fct string Le nom de la fonction à exectuer
573 * @param[in] $params mixed Paramètres pour le lancement de la fonction
574 * @param[in] $class Nom de la classe possèdant la méthode $fct à executer
578 function addEvent($event,$fct,$params,$class=NULL) {
579 $this -> _events[$event][] = array(
587 * Ajouter une action sur un objet lors d'un événement
589 * @param[in] $event string Le nom de l'événement
590 * @param[in] $obj object L'objet dont la méthode doit être executé
591 * @param[in] $meth string Le nom de la méthode
592 * @param[in] $params mixed Paramètres d'execution de la méthode
596 function addObjectEvent($event,&$obj,$meth,$params=NULL) {
597 $this -> _objectEvents[$event][] = array(
605 * Lance les actions à executer lors d'un événement
607 * @param[in] $event string Le nom de l'événement
609 * @retval boolean True si tout c'est bien passé, false sinon
611 function fireEvent($event) {
613 if(isset($this -> config[$event])) {
614 if (!is_array($this -> config[$event])) {
615 $funcs = array($this -> config[$event]);
618 $funcs = $this -> config[$event];
620 foreach($funcs as $func) {
621 if(function_exists($func)) {
622 if(!$func($this -> ldapObject)) {
632 if (is_array($this -> _events[$event])) {
633 foreach ($this -> _events[$event] as $e) {
635 if (class_exists($e['class'])) {
636 $obj = new $e['class']();
637 if (method_exists($obj,$e['fct'])) {
639 $obj -> $e['fct']($e['params']);
641 catch(Exception $er) {
643 LSdebug("Event ".$event." : Erreur durant l'execution de la méthode ".$e['fct']." de la classe ".$e['class']);
647 LSdebug("Event ".$event." : La méthode ".$e['fct']." de la classe ".$e['class']." n'existe pas.");
653 LSdebug("Event ".$event." : La classe ".$e['class']." n'existe pas");
657 if (function_exists($e['fct'])) {
659 $e['fct']($e['params']);
661 catch(Exception $er) {
662 LSdebug("Event ".$event." : Erreur durant l'execution de la function ".$e['fct']);
667 LSdebug("Event ".$event." : la function ".$e['fct']." n'existe pas");
674 if (is_array($this -> _objectEvents[$event])) {
675 foreach ($this -> _objectEvents[$event] as $e) {
676 if (method_exists($e['obj'],$e['meth'])) {
678 $e['obj'] -> $e['meth']($e['params']);
680 catch(Exception $er) {
682 LSdebug("Event ".$event." : Erreur durant l'execution de la méthode ".$e['meth']." sur l'objet.");
686 LSdebug("Event ".$event." : La méthode ".$e['meth']." de l'objet n'existe pas.");
700 LSerror :: defineError('LSattribute_01',
701 _("LSattribute : Attribute %{attr} : LDAP or HTML types unknow (LDAP = %{ldap} & HTML = %{html}).")
703 LSerror :: defineError('LSattribute_02',
704 _("LSattribute : The function %{func} to display the attribute %{attr} is unknow.")
706 LSerror :: defineError('LSattribute_03',
707 _("LSattribute : The rule %{rule} to validate the attribute %{attr} is unknow.")
709 LSerror :: defineError('LSattribute_04',
710 _("LSattribute : Configuration data to verify the attribute %{attr} are incorrect.")
712 LSerror :: defineError('LSattribute_05',
713 _("LSattribute : The function %{func} to save the attribute %{attr} is unknow.")
715 LSerror :: defineError('LSattribute_06',
716 _("LSattribute : The value of the attribute %{attr} can't be generated.")
718 LSerror :: defineError('LSattribute_07',
719 _("LSattribute : Generation of the attribute %{attr} failed.")
721 LSerror :: defineError('LSattribute_08',
722 _("LSattribute : Generation of the attribute %{attr} did not return a correct value.")
724 LSerror :: defineError('LSattribute_09',
725 _("LSattribute : The attr_%{type} of the attribute %{name} is not yet defined.")