From d87bdb75135856e5e0c553b0b97d1b49a1ba2121 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Fri, 17 Feb 2017 08:26:46 +0100
Subject: [PATCH] hotline #55292 refacto

---
 library/ZendAfi/View/Helper/Abonne/Loans.php  | 38 +++-------
 .../ZendAfi/View/Helper/Abonne/Operation.php  | 70 +++++++++++++++++++
 .../View/Helper/Abonne/ReservationsTable.php  | 37 +++-------
 3 files changed, 89 insertions(+), 56 deletions(-)
 create mode 100644 library/ZendAfi/View/Helper/Abonne/Operation.php

diff --git a/library/ZendAfi/View/Helper/Abonne/Loans.php b/library/ZendAfi/View/Helper/Abonne/Loans.php
index 465768034c6..39680d8de82 100644
--- a/library/ZendAfi/View/Helper/Abonne/Loans.php
+++ b/library/ZendAfi/View/Helper/Abonne/Loans.php
@@ -20,23 +20,19 @@
  */
 
 
-class ZendAfi_View_Helper_Abonne_Loans extends ZendAfi_View_Helper_BaseHelper {
+class ZendAfi_View_Helper_Abonne_Loans extends ZendAfi_View_Helper_Abonne_Operation {
   protected
     $_line_no,
-    $_lite = false,
     $_in_progress;
 
   public function abonne_Loans($loans, $in_progress = true) {
     $this->_in_progress = $in_progress;
-
-    if(100 < count($loans))
-      $this->_lite = true;
-
+    $this->_operations = $loans;
     Class_ScriptLoader::getInstance()->loadTableSorter();
     return
       $this->_tag('table',
                   $this->renderHeader()
-                  . $this->renderLoans($loans),
+                  . $this->renderLoans(),
                   ['class' => $this->_in_progress ? 'tablesorter loans' : 'tablesorter loans-history']);
   }
 
@@ -66,13 +62,13 @@ class ZendAfi_View_Helper_Abonne_Loans extends ZendAfi_View_Helper_BaseHelper {
   }
 
 
-  protected function renderLoans($loans) {
+  protected function renderLoans() {
     $html = '';
 
     if (!$this->_in_progress)
-      $loans = array_reverse($loans->getArrayCopy());
+      $this->_operations = array_reverse($this->_operations->getArrayCopy());
 
-    foreach($loans as $loan)
+    foreach($this->_operations as $loan)
       $html .= $this->renderLoan($loan);
 
     return $this->_tag('tbody', $html);
@@ -80,32 +76,16 @@ class ZendAfi_View_Helper_Abonne_Loans extends ZendAfi_View_Helper_BaseHelper {
 
 
   protected function renderLoan($loan) {
-    $record = new Class_Entity();
-
-    if(!$this->_lite && ($item = $loan->getExemplaireOPAC()))
-      $record = $item->getNotice();
-
-    $record_title = ($record->getId())
-      ? $this->view->recordAnchor($record, ['retour_abonne' => 'prets'])
-      : $loan->getTitre();
-
-    $author = $this->_lite
-      ? $loan->getAuteur()
-      : $this->view->authorAnchor($loan->getAuteur()
-                                  ? $loan->getAuteur()
-                                  : $record->getAuteurPrincipal(),
-                                  ['retour_abonne' => 'prets']);
-
     return
       $this->_tag('tr',
                   $this->_tag('td',
                               $loan->getUserFullName())
                   . $this->_tag('td',
-                                $record->getTypeDocLabel())
+                                $this->_renderDocTypeLabel($loan))
                   . $this->_tag('td',
-                                $record_title)
+                                $this->_renderTitle($loan, ['retour_abonne' => 'prets']))
                   . $this->_tag('td',
-                                $author)
+                                $this->_renderAuthor($loan, ['retour_abonne' => 'prets']))
                   . $this->_tag('td',
                                 $this->_in_progress ? $loan->getBibliotheque() : $loan->getIssueDate())
                   . $this->_tag('td',
diff --git a/library/ZendAfi/View/Helper/Abonne/Operation.php b/library/ZendAfi/View/Helper/Abonne/Operation.php
new file mode 100644
index 00000000000..14c172c9744
--- /dev/null
+++ b/library/ZendAfi/View/Helper/Abonne/Operation.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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
+ * 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
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class ZendAfi_View_Helper_Abonne_Operation extends ZendAfi_View_Helper_BaseHelper {
+  const STORM_LIMIT = 100;
+
+  protected $_operations;
+
+  protected function _renderAuthor($operation, $attribs = []) {
+    $record = $this->_getRecord($operation);
+
+    if($this->_isStormLimited())
+      return $operation->getAuteur();
+
+    return $this->view->authorAnchor($operation->getAuteur()
+                                     ? $operation->getAuteur()
+                                     : $record->getAuteurPrincipal(),
+                                     $attribs);
+  }
+
+
+  protected function _renderTitle($operation, $attribs = []) {
+    $record = $this->_getRecord($operation);
+    return ($record->getId())
+      ? $this->view->recordAnchor($record,
+                                  $attribs)
+      : $operation->getTitre();
+  }
+
+
+  protected function _renderDocTypeLabel($operation) {
+    return  $this->_getRecord($operation)->getTypeDocLabel();
+  }
+
+
+  protected function _getRecord($operation) {
+    $record = new Class_Entity();
+
+    if((!$this->_isStormLimited())
+       && ($item = $operation->getExemplaireOPAC()))
+      $record = $item->getNotice();
+
+    return $record;
+  }
+
+
+  protected function _isStormLimited() {
+    return count($this->_operations) > static::STORM_LIMIT;
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Abonne/ReservationsTable.php b/library/ZendAfi/View/Helper/Abonne/ReservationsTable.php
index fc61176b897..4573b9ef7d9 100644
--- a/library/ZendAfi/View/Helper/Abonne/ReservationsTable.php
+++ b/library/ZendAfi/View/Helper/Abonne/ReservationsTable.php
@@ -20,15 +20,13 @@
  */
 
 
-class ZendAfi_View_Helper_Abonne_ReservationsTable extends ZendAfi_View_Helper_BaseHelper {
-  protected $_line_no,
-    $_lite = false;
+class ZendAfi_View_Helper_Abonne_ReservationsTable extends ZendAfi_View_Helper_Abonne_Operation {
+  protected $_line_no;
 
   public function abonne_ReservationsTable($reservations, $fiche) {
     Class_ScriptLoader::getInstance()->loadTableSorter();
 
-    if(100 < count($reservations))
-      $this->_lite = true;
+    $this->_operations = $reservations;
 
     return $this->_tag('table',
                        $this->_renderHeader()
@@ -70,27 +68,16 @@ class ZendAfi_View_Helper_Abonne_ReservationsTable extends ZendAfi_View_Helper_B
 
 
   protected function _renderReservation($reservation) {
-    $record = new Class_Entity();
-    if(!$this->_lite && ($item = $reservation->getExemplaireOPAC()))
-      $record = $item->getNotice();
-
-    $record_title = ($record->getId())
-      ? $this->view->recordAnchor($record, ['retour_abonne' => 'reservations'])
-      : $reservation->getTitre();
-
-    $author = $this->_lite
-      ? ''
-      : $this->view->authorAnchor($record->getAuteurPrincipal(),
-                                  ['retour_abonne' => 'reservations']);
+    $attribs = ['retour_abonne' => 'reservations'];
 
     return
       $this->_tag('tr',
                   $this->_tag('td', $reservation->getUserFullName())
-                  . $this->_tag('td', $record->getTypeDocLabel())
+                  . $this->_tag('td', $this->_renderDocTypeLabel($reservation))
                   . $this->_tag('td',
-                                $record_title)
+                                $this->_renderTitle($reservation, $attribs))
                   . $this->_tag('td',
-                                $author)
+                                $this->_renderAuthor($reservation, $attribs))
                   . $this->_tag('td',
                                 $reservation->getBibliotheque())
                   . $this->_tag('td',
@@ -158,20 +145,16 @@ class ZendAfi_View_Helper_Abonne_OnPlaceConsultationReservationsTable extends Ze
 
 
   protected function _renderReservation($reservation) {
-    $record = $reservation->getRecord();
-    $record_title = ($record->getId())
-      ? $this->view->recordAnchor($record, ['retour_abonne' => 'reservations'])
-      : $reservation->getTitre();
+    $attribs = ['retour_abonne' => 'reservations'];
 
     return
       $this->_tag('tr',
                   $this->_tag('td',
                               $this->_line_no++)
                   . $this->_tag('td',
-                                $record_title)
+                                $this->_renderTitle($reservation, $attribs))
                   . $this->_tag('td',
-                                $this->view->authorAnchor($record->getAuteurPrincipal(),
-                                                          ['retour_abonne' => 'reservations']))
+                                $this->_renderAuthor($reservation, $attribs))
                   . $this->_tag('td',
                                 $reservation->getLocationLabel())
                   . $this->_tag('td',
-- 
GitLab