From dbeea660600b3f7085c5f82050d487dad477e897 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 12 Jun 2018 18:11:59 +0200
Subject: [PATCH] dev #72825 : add filters for loan page (wip)

---
 .../opac/controllers/AbonneController.php     |  7 ++
 .../opac/views/scripts/abonne/prets.phtml     |  2 +
 library/Class/User/LoanCriteria.php           | 93 +++++++++++++++++++
 library/Class/WebService/SIGB/Emprunt.php     | 12 +++
 .../Controller/Action/Helper/LoanSearch.php   | 60 ++++++++++++
 .../HandleBranchcode/HandleBranchcodeTest.php | 18 ++++
 6 files changed, 192 insertions(+)
 create mode 100644 library/Class/User/LoanCriteria.php
 create mode 100644 library/ZendAfi/Controller/Action/Helper/LoanSearch.php

diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index d229ed53881..b53c1d4843b 100644
--- a/application/modules/opac/controllers/AbonneController.php
+++ b/application/modules/opac/controllers/AbonneController.php
@@ -357,6 +357,7 @@ class AbonneController extends ZendAfi_Controller_Action {
 
 
   public function ficheAction() {
+
     $fiche_sigb = $this->_user->getFicheSigb();
 
     $this->view->error = isset($fiche_sigb['erreur']) ? $fiche_sigb["erreur"] : '';
@@ -366,6 +367,12 @@ class AbonneController extends ZendAfi_Controller_Action {
 
 
   public function pretsAction() {
+    $criteria = (new Class_User_LoanCriteria($this->_request->getParams()));
+    $this->_helper
+      ->loanSearch([],
+                   $criteria);
+    $this->view->criteria = $criteria;
+
     $fiche = $this->_user->getFicheSigb();
     if (isset($this->fiche['error']) && $this->fiche['error'])
       $this->view->error = $this->fiche['error'];
diff --git a/application/modules/opac/views/scripts/abonne/prets.phtml b/application/modules/opac/views/scripts/abonne/prets.phtml
index c43fb36bdf3..1ec151fc6d4 100644
--- a/application/modules/opac/views/scripts/abonne/prets.phtml
+++ b/application/modules/opac/views/scripts/abonne/prets.phtml
@@ -2,8 +2,10 @@
 <div class="abonneTitre"><?php echo $this->user->getNomAff();?></div>
 <?php
 
+echo $this->renderForm($this->form);
 $cards = new Class_User_Cards($this->user);
 $loans = $cards->getLoansWithOutPNB();
+$loans = $this->criteria->filter($loans);
 $renewable_loan = [];
 
 foreach ($loans as $loan) {
diff --git a/library/Class/User/LoanCriteria.php b/library/Class/User/LoanCriteria.php
new file mode 100644
index 00000000000..fc3d4c6bed0
--- /dev/null
+++ b/library/Class/User/LoanCriteria.php
@@ -0,0 +1,93 @@
+<?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 Class_User_LoanCriteria {
+  use Trait_Translator;
+  protected $_criteria = [];
+  protected $_params = ['start_date_retour' => '',
+                        'end_date_retour' => '',
+                        'onhold' => ''];
+  protected $_selected_filters = [];
+  public function __construct($filters) {
+    $this->_selected_filters = array_intersect_key($filters, $this->_params);
+    xdebug_break();
+
+    $this->_criteria = [ new ZendAfi_Form_Element_DateRangePicker('loan_dates',
+                                                                  ['label' => $this->_('Date de prêt'),
+                                                                   'start' => ['name' => 'start_loan_date'],
+                                         'end' => ['name' => 'end_loan_date']
+                                                                  ]),
+                        new ZendAfi_Form_Element_DateRangePicker('return_dates',
+                                                                  ['label' => $this->_('Date de retour'),
+                                                                   'start' => ['name' => 'start_date_retour'],
+                                         'end' => ['name' => 'end_date_retour']
+                                                                  ]),
+                        new  Zend_Form_Element_Select('onhold',
+                                        ['label' => $this->_('Réservé par d\'autres'),
+                                         'multiOptions' => ['yes' => $this->_('Oui'),
+                                                            'no' => $this->_('Non'),
+                                                            'all' => $this->_('Indifférent')],
+                                         'value' => 'all'])];
+
+  }
+
+
+  public function getForm() {
+    $form = (new ZendAfi_Form())
+      ->setAttrib('style', 'position: relative')
+      ->setMethod('get');
+    xdebug_break();
+    $names = (new Storm_Collection($this->_criteria))
+      ->select(function($c) { return $c; })
+      ->eachDo(function($c) use ($form) { $form->addElement($c); })
+      ->collect(function($c) { return $c->getName(); })
+      ->getArrayCopy();
+
+    if (!$names)
+      return $form;
+
+    $form->addDisplayGroup($names,
+                           'loan_search_group',
+                           ['legend' => $this->_('Filtrer par')]);
+
+    return $form;
+  }
+
+
+  public function filter($loans) {
+    foreach ($this->_selected_filters as $key => $value) {
+      $loans = $loans->select(function($loan) use ($key,$value)
+                              {
+                                if (($pos = strpos($key, 'start_')) !== false)
+                                  return $loan->callGetterByAttributeName(substr($key,6)) > $value;
+                                if (($pos = strpos($key, 'end_')) !== false)
+                                  return $loan->callGetterByAttributeName(substr($key,6)) < $value;
+
+
+                                return $loan->callGetterByAttributeName($key) == $value;
+                              });
+    }
+
+    return $loans;
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/Class/WebService/SIGB/Emprunt.php b/library/Class/WebService/SIGB/Emprunt.php
index b85125de296..75f5ed5d99d 100644
--- a/library/Class/WebService/SIGB/Emprunt.php
+++ b/library/Class/WebService/SIGB/Emprunt.php
@@ -63,6 +63,9 @@ class Class_WebService_SIGB_Emprunt extends Class_WebService_SIGB_ExemplaireOper
     $this->_exemplaire->setOnHold($onhold);
   }
 
+  public function getOnHold() {
+    return $this->_exemplaire->onHold();
+  }
 
   public function onParseAttributes() {
     if( !$this->type = $this->getAttribute('Type'))
@@ -141,6 +144,15 @@ class Class_WebService_SIGB_Emprunt extends Class_WebService_SIGB_ExemplaireOper
     $this->type = $type;
     return $this;
   }
+
+  public function callGetterByAttributeName($attribute) {
+    return call_user_func(array($this, 'get'.$this->attributeNameToAccessor($attribute)));
+  }
+  public function attributeNameToAccessor($name) {
+    return Storm_Inflector::camelize($name);
+
+  }
+
 }
 
 ?>
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Action/Helper/LoanSearch.php b/library/ZendAfi/Controller/Action/Helper/LoanSearch.php
new file mode 100644
index 00000000000..4dfeba9a191
--- /dev/null
+++ b/library/ZendAfi/Controller/Action/Helper/LoanSearch.php
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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_Controller_Action_Helper_LoanSearch extends Zend_Controller_Action_Helper_Abstract {
+  protected $view;
+
+  public function loanSearch($action_params=[], $criteria) {
+    $this->view = $this->getActionController()->view;
+
+    $this->view->page = $this->_getParam('page', 1);
+//    $this->view->users = $criteria->findPage($this->view->page);
+//    $this->view->total = $criteria->count();
+    $this->view->form = $this->_prepareForm($action_params, $criteria);
+    $this->view->params = array_merge($this->view->form->getValues(),
+                                      ['page' => $this->view->page,
+                                       'search_order' => $this->_getParam('search_order', 'nom asc')]);
+
+  }
+
+
+  protected function _prepareForm($action_params, $criteria) {
+    $form =  $criteria->getForm();
+
+    $url_params = array_merge(['module' => $this->getRequest()->getModuleName(),
+                               'controller' => $this->getRequest()->getControllerName(),
+                               'action' => $this->getRequest()->getActionName()],
+                              $action_params);
+
+    return $form->setAction(Class_Url::absolute($url_params, null, true));
+  }
+
+
+  protected function _getParam($name, $default=null) {
+    return $this->getRequest()->getParam($name, $default);
+  }
+
+
+  public function direct($action_params=[], $criteria) {
+    return $this->loanSearch($action_params, $criteria);
+  }
+}
\ No newline at end of file
diff --git a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
index 052ca462d68..5e4ec85fa8c 100644
--- a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
+++ b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
@@ -168,5 +168,23 @@ class HandleBranchcodeDisplayLoanByOthersTest extends HandleBranchcodeTestCase {
     $this->assertXPathContentContains('//div', 'réservé par d\'autres', $this->_response->getBody());
   }
 
+
+
+  /** @test */
+  public function postOnHoldShouldDisplayBookedByOther() {
+    $this->borrower = $this->service->getEmprunteur($this->user);
+    $this->postDispatch('/opac/abonne/prets', ['onhold' => 1],true);
+    $this->assertXPathContentContains('//div', 'réservé par d\'autres', $this->_response->getBody());
+  }
+
+
+
+  /** @test */
+  public function postReturnDateShouldDisplayBookedByOther() {
+    $this->borrower = $this->service->getEmprunteur($this->user);
+    $this->postDispatch('/opac/abonne/prets', ['start_date_retour' => '2018-01-01'],true);
+    $this->assertXPathContentContains('//div', '', $this->_response->getBody());
+  }
+
 }
 ?>
-- 
GitLab