| 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
  | 
        
        
            | 
            | 
           79 | 
           		$utilisateursInfos = json_decode($this->clientRest->consulter(sprintf(self::TPL_URL_WS_ANNUAIRE,
  | 
        
        
            | 
            | 
           80 | 
           																			  implode(',', $courriels))),
  | 
        
        
            | 
            | 
           81 | 
           										 true);
  | 
        
        
            | 
            | 
           82 | 
           		return self::extraireIdentites($utilisateursInfos, $this->courriels);
  | 
        
        
           | 139 | 
           jpm | 
           83 | 
           	}
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
           | 845 | 
           raphael | 
           85 | 
           	static function extraireIdentites($utilisateursInfos, $courriels) {
  | 
        
        
           | 139 | 
           jpm | 
           86 | 
           		$identites = array();
  | 
        
        
           | 845 | 
           raphael | 
           87 | 
           		foreach ($courriels as $courriel) {
  | 
        
        
           | 139 | 
           jpm | 
           88 | 
           			$info = array('id' => null, 'intitule' => '');
  | 
        
        
            | 
            | 
           89 | 
           			if (isset($utilisateursInfos[$courriel])) {
  | 
        
        
            | 
            | 
           90 | 
           				$info['intitule'] = $utilisateursInfos[$courriel]['intitule'];
  | 
        
        
            | 
            | 
           91 | 
           				$info['id'] = $utilisateursInfos[$courriel]['id'];
  | 
        
        
            | 
            | 
           92 | 
           			} else {
  | 
        
        
           | 845 | 
           raphael | 
           93 | 
           				$info['intitule'] = self::tronquerCourriel($courriel);
  | 
        
        
           | 139 | 
           jpm | 
           94 | 
           			}
  | 
        
        
            | 
            | 
           95 | 
           			$identites[$courriel] = $info;
  | 
        
        
            | 
            | 
           96 | 
           		}
  | 
        
        
            | 
            | 
           97 | 
           		return $identites;
  | 
        
        
            | 
            | 
           98 | 
           	}
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
           | 845 | 
           raphael | 
           100 | 
           	static function tronquerCourriel($courriel) {
  | 
        
        
            | 
            | 
           101 | 
           		return str_replace(substr($courriel, strpos($courriel, '@')), '@...', $courriel);
  | 
        
        
           | 139 | 
           jpm | 
           102 | 
           	}
  | 
        
        
            | 
            | 
           103 | 
           }
  | 
        
        
            | 
            | 
           104 | 
           ?>
  |