Service.php 8.18 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 21
 */
class Class_Webservice_SIGB_Dynix_Service extends Class_WebService_SIGB_AbstractRESTService {
Patrick Barroca's avatar
Patrick Barroca committed
22 23 24 25 26 27 28 29 30 31
  protected $_client_id;
  protected $_mail_bib = [
      'ALFAX1'=>'bibliotheque.alfortville.annexe@agglo-plainecentrale94.fr',
      'ALFMEDA'=>'malfortville@agglo-plainecentrale94.fr',
      'CRETBLE'=>'biblibleuets@agglo-plainecentrale94.fr',
      'CRETMES'=>'bibliotheque.creteil.biblimesly@agglo-plainecentrale94.fr',
      'CRETBUS'=>'bibliotheque.creteil.bibliobus@agglo-plainecentrale94.fr',
      'CRETMEC'=>'bibliotheque.creteil.meches@agglo-plainecentrale94.fr',
      'CRETDOY'=>'mediatheque.creteil.abbaye@agglo-plainecentrale94.fr',
      'ENMDAD'=>'',
32
      'CRETHAB'=>'medialudo.creteil@gpsea.fr',
33
      'CRETMAC'=>'mediabus@gpsea.fr',
Patrick Barroca's avatar
Patrick Barroca committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      'CRETMUL'=>'',
      'CRETRES'=>'bibliotheque.creteil.servicesinternes@agglo-plainecentrale94.fr',
      'CRETVIL'=>'mediatheque.creteil.village@agglo-plainecentrale94.fr',
      'LB'=>'bibliotheque.limeilbrevannes@agglo-plainecentrale94.fr'];

  /**
   * @param string $server_root
   * @return Class_Webservice_SIGB_Dynix_Service
   */
  public function setServerRoot($server_root) {
    if ('/' == substr($server_root, -1))
      $server_root = substr($server_root, 0, -1);
    return parent::setServerRoot($server_root);
  }


  public function setClientId($client_id) {
    $this->_client_id = $client_id;
    return $this;
  }


  public function getNotice($id){
    return $this->httpGetNotice(['namespace' => 'standard',
                                 'service' => 'lookupTitleInfo',
                                 'clientID' => $this->_client_id,
                                 'titleID' => $id,
                                 'includeItemInfo' => 'true',
                                 'includeAvailabilityInfo' => 'true'],

                                Class_WebService_SIGB_Dynix_TitleInfoResponseReader::newInstance());
  }


  public function openSessionForUser($user) {
    $xml = $this->httpGet(['namespace' => 'security',
                           'service' => 'loginUser',
                           'clientID' => $this->_client_id,
                           'login' => $user->getLogin(),
                           'password' => $user->getPassword()]);
    return $this->_getTagData($xml, 'sessionToken');
  }


  public function closeSession($token) {
    $this->httpGet(['namespace' => 'security',
                    'service' => 'logoutUser',
                    'clientID' => $this->_client_id,
                    'sessionToken' => $token]);
    return $this;
  }


87 88 89 90 91
  public function providesAuthentication() {
    return true;
  }


Patrick Barroca's avatar
Patrick Barroca committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  /**
   * @param Class_Users $user
   * @return Class_WebService_SIGB_Emprunteur
   */
  public function getEmprunteur($user) {
    $session_token = $this->openSessionForUser($user);
    $xml = $this->httpGet(['namespace' => 'patron',
                           'service' => 'lookupMyAccountInfo',
                           'includePatronHoldInfo' => 'ACTIVE',
                           'includePatronInfo' => 'true',
                           'includePatronCheckoutInfo' => 'ALL',
                           'clientID' => $this->_client_id,
                           'sessionToken' => $session_token]);

    $this->closeSession($session_token);

108
    $loaner = Class_WebService_SIGB_Dynix_LookupMyAccountInfoResponseReader::newInstance()
Patrick Barroca's avatar
Patrick Barroca committed
109 110
      ->getEmprunteurFromXML($xml)
      ->setService($this);
111 112 113 114 115

    if ($loaner->getId())
      $loaner->beValid();

    return $loaner;
Patrick Barroca's avatar
Patrick Barroca committed
116 117 118 119 120 121 122 123 124 125
  }


  public function getUserAnnexe($user) {
    return null;
  }


  public function reserverExemplaire($user, $exemplaire, $code_annexe){
    if (!$sigb_exemplaire = $this->getExemplaire($exemplaire->getIdOrigine(),
126
                                                 $exemplaire->getCodeBarres())) {
Patrick Barroca's avatar
Patrick Barroca committed
127 128 129
      return ['statut' => false, 'Erreur de communication: exemplaire non trouvé'];
    }

Ghislain Loas's avatar
Ghislain Loas committed
130
    if ($sigb_exemplaire->getDisponibilite() !== Class_WebService_SIGB_Exemplaire::newInstance()->message('DISPO_LIBRE'))
Patrick Barroca's avatar
Patrick Barroca committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      return $this->_userHttpAction($user,
                                    'createMyHold',
                                    ['titleKey' => $exemplaire->getIdOrigine(),
                                     'pickupLibraryID' => $code_annexe]);

    return $this->_reserverParMail($user, $exemplaire, $code_annexe);
  }


  protected function _reserverParMail($user, $exemplaire, $code_annexe) {
    $mail = new ZendAfi_Mail();
    try {
      $mail->setFrom(Class_CosmoVar::get('mail_admin'))
           ->addTo($this->_mail_bib[$exemplaire->getAnnexe()])
           ->setSubject('Demande de Reservation')
146
           ->setBodyText($this->_buildMailBody($user, $exemplaire, $code_annexe))
Patrick Barroca's avatar
Patrick Barroca committed
147 148 149 150 151 152 153 154
           ->send();
    } catch (Exception $e) {
      return ['statut' => false, 'erreur' => $e->getMessage()];
    }
    return ['statut' => true, 'erreur' => ''];
  }


155
  protected function _buildMailBody($user, $exemplaire, $code_annexe) {
Patrick Barroca's avatar
Patrick Barroca committed
156 157 158 159 160 161 162 163 164 165
    $content = "Bonjour,\n\n";
    $content .= sprintf("%s, %s (N° : %s) désire réserver le document suivant : \n",
                        $user->getNom(), $user->getPrenom(), $user->getIdabon());

    $content .= "URL de la notice : ".$this->_getUrlNotice($exemplaire->getNotice())."\n";

    $content .= "Titre : ".$exemplaire->getTitreEtSousTitre()."\n";
    $content .= "Auteur : ".$exemplaire->getAuteurPrincipal()."\n";
    $content .= "Editeur : ".$exemplaire->getEditeur()."\n";
    $content .= "Collection : ".$exemplaire->getCollectionPrincipale()."\n\n";
166
    $content .= "Lieu de retrait : ".Class_CodifAnnexe::findLibelleByCode($code_annexe)."\n";
Patrick Barroca's avatar
Patrick Barroca committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    $content .= "Adresse e-mail : ".$user->getMail();
    return $content;
  }


  protected function _getUrlNotice($notice){
    return (new ZendAfi_Controller_Action_Helper_View())
      ->absoluteUrl(['controller'=>'recherche',
                     'action'=>'viewnotice',
                     'id'=>$notice->getId(),
                     'clef'=>$notice->getClefAlpha()],null,true);
  }


  public function supprimerReservation($user, $reservation_id){
    return $this->_userHttpAction($user,
                                  'cancelMyHold',
                                  ['holdKey' => $reservation_id]);
  }


  public function prolongerPret($user, $pret_id){
    return $this->_userHttpAction($user,
                                  'renewMyCheckout',
                                  ['itemID' => $pret_id]);
  }


  protected function _userHttpAction($user, $service, $params) {
    $session_token = $this->openSessionForUser($user);
    $xml = $this->httpGet(array_merge(['namespace' => 'patron',
                                       'service' => $service,
                                       'clientID' => $this->_client_id,
                                       'sessionToken' => $session_token],

                                      $params));

    if ($error = $this->_getTagData($xml, 'string'))
      return ['statut' => false, 'erreur' => $error];

    return ['statut' => true, 'erreur' => ''];
  }
209

Patrick Barroca's avatar
Patrick Barroca committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

  /**
   * @param array $options
   * @return string
   */
  public function buildQueryURL($options) {
    $namespace = $options['namespace'];
    unset($options['namespace']);

    $service = $options['service'];
    unset($options['service']);

    return sprintf('%s/rest/%s/%s?%s',
                   $this->getServerRoot(),
                   $namespace,
                   $service,
                   http_build_query($options));
  }
228 229 230 231

}

?>