Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 979 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
139 jpm 1
<?php
2
class Utilisateurs {
3
	const TPL_URL_WS_ANNUAIRE = 'http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/%s';
4
 
5
	private $courriels = array();
387 jpm 6
	private $identites = array();
139 jpm 7
	private $clientRest = null;
8
 
9
	/**
10
	* Prend en paramêtre un tableau de courriels.
11
	*
12
	* @param array $courriels un tableau de courriels pour lesquels il faut rechercher les infos d'identité
13
	*/
14
	public function __construct(Array $courriels = array(), RestClient $clientRest = null) {
15
		$this->courriels = $courriels;
16
		$this->clientRest = is_null($clientRest) ? new RestClient() : $clientRest;
17
	}
18
 
19
	public function setCourriels($courriels) {
20
		$this->courriels = $courriels;
21
	}
845 raphael 22
	/**
23
	 * Lance l'interrogation du service de l'annuaire.
24
	 *
25
	 * @return null.
26
	 */
387 jpm 27
	public function chargerIdentites() {
845 raphael 28
		$this->identites = $this->getIdentites($this->courriels);
387 jpm 29
	}
139 jpm 30
 
31
	/**
387 jpm 32
	* Retourne après avoir interrogé un service de l'annuaire, les intitulés correspondant aux
139 jpm 33
	* courriels des utilisateurs.
34
	*
35
	* @return mixed tableau avec en clé le courriel et en valeur l'intitulé de la personne à
36
	* afficher, false en cas d'erreur ou de résultat vide.
37
	*/
38
	public function getIntitules() {
387 jpm 39
		$this->chargerIdentites();
845 raphael 40
		if (! $this->identites) return false;
41
 
42
		$intitules = array();
43
		foreach ($this->identites as $courriel => $infos) {
44
			$intitules[$courriel] = $infos['intitule'];
139 jpm 45
		}
46
		return $intitules;
47
	}
48
 
845 raphael 49
	/**
50
	 * Retourne un intitulé en fonction d'un courriel.
51
	 *
52
	 * @return String l'intitulé de l'utilisateur ou une chaine vide en cas de problème.
53
	 */
54
	public function getIntitule($courriel) {
55
		if ($this->contenirCourriel($courriel)) {
56
			return $this->identites[$courriel]['intitule'];
57
		}
58
		return '';
387 jpm 59
	}
60
 
845 raphael 61
	/**
62
	 * Retourne l'identifiant de l'utilisateur en fonction d'un courriel.
63
	 *
64
	 * @return String l'id de l'utilisateur ou une chaine vide en cas de problème.
65
	 */
66
	public function getId($courriel) {
67
		if ($this->contenirCourriel($courriel)) {
68
			return $this->identites[$courriel]['id'];
69
		}
70
		return '';
387 jpm 71
	}
72
 
73
	private function contenirCourriel($courriel) {
845 raphael 74
		return ($this->identites && isset($this->identites[$courriel])) ? true : false;
387 jpm 75
	}
76
 
845 raphael 77
	private function getIdentites($courriels) {
78
		// consulterServiceAnnuaire
979 mathias 79
		$courriels = array_unique($courriels);
845 raphael 80
		$utilisateursInfos = json_decode($this->clientRest->consulter(sprintf(self::TPL_URL_WS_ANNUAIRE,
81
																			  implode(',', $courriels))),
82
										 true);
83
		return self::extraireIdentites($utilisateursInfos, $this->courriels);
139 jpm 84
	}
85
 
845 raphael 86
	static function extraireIdentites($utilisateursInfos, $courriels) {
139 jpm 87
		$identites = array();
845 raphael 88
		foreach ($courriels as $courriel) {
139 jpm 89
			$info = array('id' => null, 'intitule' => '');
90
			if (isset($utilisateursInfos[$courriel])) {
91
				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
92
				$info['id'] = $utilisateursInfos[$courriel]['id'];
93
			} else {
845 raphael 94
				$info['intitule'] = self::tronquerCourriel($courriel);
139 jpm 95
			}
96
			$identites[$courriel] = $info;
97
		}
98
		return $identites;
99
	}
100
 
845 raphael 101
	static function tronquerCourriel($courriel) {
102
		return str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
139 jpm 103
	}
104
}
105
?>