ILSSubscription.php 4.61 KB
Newer Older
Laurent's avatar
Laurent committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * 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).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Patrick Barroca's avatar
Patrick Barroca committed
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Laurent's avatar
Laurent committed
15 16 17 18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
Patrick Barroca's avatar
Patrick Barroca committed
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
Laurent's avatar
Laurent committed
20 21 22
 */

class Class_User_ILSSubscription {
Ghislain Loas's avatar
Ghislain Loas committed
23
  use Trait_Translator, Trait_TimeSource;
Laurent's avatar
Laurent committed
24

Patrick Barroca's avatar
Patrick Barroca committed
25
  protected $_user;
Laurent's avatar
Laurent committed
26

Patrick Barroca's avatar
Patrick Barroca committed
27 28 29
  public function __construct($user) {
    $this->_user = $user;
  }
Laurent's avatar
Laurent committed
30 31


32 33
  public function isExpirable() {
    return $this->_user->isAbonne() && $this->_user->hasDateFin();
Patrick Barroca's avatar
Patrick Barroca committed
34
  }
Laurent's avatar
Laurent committed
35 36


37
  public function isExpired() {
Patrick Barroca's avatar
Patrick Barroca committed
38
    return
39
      $this->isExpirable()
Ghislain Loas's avatar
Ghislain Loas committed
40
      && ($this->_user->getDateFin() < $this->getTimeSource()->dateYmd());
Patrick Barroca's avatar
Patrick Barroca committed
41
  }
Laurent's avatar
Laurent committed
42 43


44
  public function isAboutToExpire() {
Patrick Barroca's avatar
Patrick Barroca committed
45
    return
46
      $this->isExpirable()
Patrick Barroca's avatar
Patrick Barroca committed
47 48 49
      && ($this->ilsExpireIn() >= 0)
      && (Class_AdminVar::getValueOrDefault('NDAYS_EXPIRY_NOTICE') >= $this->ilsExpireIn());
  }
Laurent's avatar
Laurent committed
50 51


Patrick Barroca's avatar
Patrick Barroca committed
52 53
  public function ilsExpireIn() {
    $date_expiry = new DateTime($this->_user->getDateFin());
Ghislain Loas's avatar
Ghislain Loas committed
54
    $today = new DateTime($this->getTimeSource()->dateYmd());
Patrick Barroca's avatar
Patrick Barroca committed
55 56
    return $today->diff($date_expiry)->format("%r%a");
  }
Laurent's avatar
Laurent committed
57 58


Patrick Barroca's avatar
Patrick Barroca committed
59
  public function isValid() {
60
    if (!$this->_user->isAbonne())
61 62
      return false;

63 64 65
    return !$this->isExpired() && !$this->isBlocked();
  }

66

67 68
  public function isBlocked() {
    return $this->_user->isAbonne() && $this->_user->isBlocked();
Patrick Barroca's avatar
Patrick Barroca committed
69
  }
Laurent's avatar
Laurent committed
70 71


Laurent's avatar
Laurent committed
72 73 74 75 76 77
  public function beValidUntilNextYear() {
    $this->_user->beAbonneSIGB();

    $date = new DateTime($this->getTimeSource()->dateYmd());
    $date->modify('+1 year');

78 79 80
    if ($this->_isLibrarySendData())
      $this->_user->setIdIntBib($this->_user->getIdSite());

Laurent's avatar
Laurent committed
81 82 83 84
    return $this->_user->setDateFin($date->format('Y-m-d'));
  }


85 86 87 88 89
  protected function _isLibrarySendData() {
    return $this->_user->hasBib() && $this->_user->getBib()->isSIGBSendData();
  }


Patrick Barroca's avatar
Patrick Barroca committed
90
  public function registerNotificationsOn($notifiable) {
91 92 93 94 95
    $this->_user->updateFromPatron();

    if ($this->_user->hasPagedLoans())
      return;

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    if(($user = Class_Users::getIdentity()) && $this->_user->getId() == $user->getId())
      return $this->registerNotificationsForMeOn($notifiable);

    return $this->registerNotificationsWith($notifiable,
                                            $this->_('L\'abonnement de %s a expiré - Merci de contacter la médiathèque',
                                                     $this->_user->getNomComplet()),
                                            $this->_plural($this->ilsExpireIn(),
                                                           'L\'abonnement de %s expire aujourd\'hui.',
                                                           'L\'abonnement de %s arrivera à expiration demain.',
                                                           'L\'abonnement de %s arrivera à expiration dans %d jours.',
                                                           $this->_user->getNomComplet(),
                                                           $this->ilsExpireIn()));
  }


  public function registerNotificationsForMeOn($notifiable) {
    return $this->registerNotificationsWith($notifiable,
                                            $this->_('Votre abonnement a expiré - Merci de contacter la médiathèque',
                                                     $this->_user->getNomComplet()),
                                            $this->_plural($this->ilsExpireIn(),
                                                           'Votre abonnement expire aujourd\'hui.',
                                                           'Votre abonnement arrivera à expiration demain.',
                                                           'Votre abonnement arrivera à expiration dans %d jours.',
                                                           $this->ilsExpireIn()));
  }


  public function registerNotificationsWith($notifiable, $expired_message, $about_to_expire_message) {
124
    if ($this->isExpired())
125 126
      $notifiable->notify($expired_message);

127
    if ($this->isAboutToExpire())
128 129
      $notifiable->notify($about_to_expire_message);

Patrick Barroca's avatar
Patrick Barroca committed
130 131
    return $this;
  }
132
}