From 7749496662843aba435268c25454cb3d9b7138a1 Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 19 Jun 2018 16:16:40 +0200
Subject: [PATCH] dev #72825 : refacto

---
 library/Class/User/LoanCriteria.php           | 131 ------------------
 library/Class/WebService/SIGB/Emprunt.php     |   8 +-
 library/ZendAfi/Form.php                      |   9 ++
 .../ZendAfi/Form/Element/DateRangePicker.php  |   9 ++
 library/ZendAfi/Form/User/LoanSearch.php      |  52 +++++++
 .../HandleBranchcode/HandleBranchcodeTest.php |  20 +++
 6 files changed, 97 insertions(+), 132 deletions(-)
 delete mode 100644 library/Class/User/LoanCriteria.php
 create mode 100644 library/ZendAfi/Form/User/LoanSearch.php

diff --git a/library/Class/User/LoanCriteria.php b/library/Class/User/LoanCriteria.php
deleted file mode 100644
index e85dc8ebb59..00000000000
--- a/library/Class/User/LoanCriteria.php
+++ /dev/null
@@ -1,131 +0,0 @@
-<?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' => '',
-                        'start_issue_date' => '',
-                        'end_issue_date' => '',
-                        'onhold' => ''];
-
-  protected $_onhold_values = ['yes' => 1,
-                               'no' => 0,
-                               'all' => ''];
-  protected $_selected_filters = [];
-
-
-  public function __construct($filters) {
-    $this->_selected_filters = array_filter(array_intersect_key($filters, $this->_params));
-
-    $this->_criteria = [
-                        (new ZendAfi_Form_Element_DateRangePicker('loan_dates',
-                                                                  ['label' => $this->_('Date de prêt'),
-                                                                   'start' => ['name' => 'start_issue_date'],
-                                                                   'end' => ['name' => 'end_issue_date']
-                                                                  ]))
-                        ->setStartValue( isset($this->_selected_filters['start_issue_date']) ? $this->_selected_filters['start_issue_date']  : '')
-                        ->setEndValue( isset($this->_selected_filters['end_issue_date']) ? $this->_selected_filters['end_issue_date']  : ''),
-                        (new ZendAfi_Form_Element_DateRangePicker('return_dates',
-                                                                  ['label' => $this->_('Date de retour'),
-                                                                   'start' => ['name' => 'start_date_retour'],
-                                                                   'end' => ['name' => 'end_date_retour']
-                                                                  ]))
-                        ->setStartValue( isset($this->_selected_filters['start_date_retour']) ? $this->_selected_filters['start_date_retour']  : '')
-                        ->setEndValue( isset($this->_selected_filters['end_date_retour']) ? $this->_selected_filters['end_date_retour']  : ''),
-                        new  Zend_Form_Element_Select('onhold',
-                                                      ['label' => $this->_('Réservé par d\'autres'),
-                                                       'multiOptions' => [
-                                                                          'no' => $this->_('Non'),
-                                                                          'yes' => $this->_('Oui'),
-                                                                          'all' => $this->_('Indifférent')],
-                                                       'value' => isset($this->_selected_filters['onhold']) ?  $this->_selected_filters['onhold'] : 'all' ])];
-
-  }
-
-
-  public function getForm() {
-    $form = (new ZendAfi_Form())
-      ->setAttrib('style', 'position: relative')
-      ->setMethod('get');
-
-    $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 isAfterDate($date1,$date2) {
-    if (!$date1 || !$date2)
-      return true;
-
-    return (DateTime::createFromFormat('d/m/Y',$date1)) >= (DateTime::createFromFormat('d/m/Y',$date2));
-  }
-
-
-  public function isBeforeDate($date1,$date2) {
-    if (!$date1 || !$date2)
-      return true;
-    return (DateTime::createFromFormat('d/m/Y',$date1)) <= (DateTime::createFromFormat('d/m/Y',$date2));
-  }
-
-
-  public function mapOnHoldFilter() {
-    if (!isset($this->_selected_filters['onhold']))
-      return $this->_selected_filters;
-    $this->_selected_filters['onhold'] = $this->_onhold_values[$this->_selected_filters['onhold']];
-
-    return $this->_selected_filters=array_filter($this->_selected_filters);
-  }
-
-
-  public function filter($loans) {
-
-    foreach ($this->mapOnHoldFilter($this->_selected_filters) as $key => $value) {
-      $loans = $loans->select(function($loan) use ($key,$value)
-                              {
-                                if (($pos = strpos($key, 'start_')) !== false)
-                                  return ($this->isAfterDate($loan->callGetterByAttributeName(substr($key,6)),$value));
-
-                                if (($pos = strpos($key, 'end_')) !== false)
-                                  return $this->isBeforeDate($loan->callGetterByAttributeName(substr($key,4)),$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 acf7a5402b5..370a495c7cb 100644
--- a/library/Class/WebService/SIGB/Emprunt.php
+++ b/library/Class/WebService/SIGB/Emprunt.php
@@ -159,7 +159,13 @@ class Class_WebService_SIGB_Emprunt extends Class_WebService_SIGB_ExemplaireOper
     if ('end_issue_date' == $key)
       return $this->_isBeforeDate($this->getIssueDate(), $value);
 
-    return $this->getOnhold() == $value;
+    if (!'onhold' == $key)
+      return true;
+
+    if ('all' == $value)
+      return true;
+
+    return $this->getOnhold() == (('yes' == $value) ? 1 : 0);
   }
 
 
diff --git a/library/ZendAfi/Form.php b/library/ZendAfi/Form.php
index 8bb6a90490f..7a4088dd670 100644
--- a/library/ZendAfi/Form.php
+++ b/library/ZendAfi/Form.php
@@ -52,6 +52,15 @@ class ZendAfi_Form extends Zend_Form {
   }
 
 
+  public function setDefaults(array $defaults) {
+    foreach ($this->getElements() as $name => $element)
+      if(method_exists($element, 'setDefaults'))
+        $element->setDefaults($this, $defaults);
+
+    return parent::setDefaults($defaults);
+  }
+
+
   public function setCustomForm($custom_form) {
     if (!$custom_form)
       return $this;
diff --git a/library/ZendAfi/Form/Element/DateRangePicker.php b/library/ZendAfi/Form/Element/DateRangePicker.php
index de523cdd8e7..440b02e8146 100644
--- a/library/ZendAfi/Form/Element/DateRangePicker.php
+++ b/library/ZendAfi/Form/Element/DateRangePicker.php
@@ -96,4 +96,13 @@ class ZendAfi_Form_Element_DateRangePicker extends Zend_Form_Element_Xhtml {
     $this->_end->setDateOnly(true);
     return $this;
   }
+
+
+  public function setDefaults($form, $params) {
+    foreach ([$this->_start, $this->_end] as $element)
+      if (array_key_exists($element->getName(), $params))
+        $element->setValue($params[$element->getName()]);
+
+    return $this;
+  }
 }
diff --git a/library/ZendAfi/Form/User/LoanSearch.php b/library/ZendAfi/Form/User/LoanSearch.php
new file mode 100644
index 00000000000..05b6b14da99
--- /dev/null
+++ b/library/ZendAfi/Form/User/LoanSearch.php
@@ -0,0 +1,52 @@
+<?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_Form_User_LoanSearch extends ZendAfi_Form {
+
+  public function init() {
+    parent::init();
+
+    $this
+      ->addElement('dateRangePicker',
+                   'issue_date',
+                   ['label' => $this->_('Date de prêt'),
+                    'start' => ['name' => 'start_issue_date'],
+                    'end' => ['name' => 'end_issue_date']])
+
+      ->addElement('dateRangePicker',
+                   'date_retour',
+                   ['label' => $this->_('Date de retour'),
+                    'start' => ['name' => 'start_date_retour'],
+                    'end' => ['name' => 'end_date_retour']])
+
+      ->addElement('select',
+                   'onhold',
+                   ['label' => $this->_('Réservé par d\'autres'),
+                    'multiOptions' => [
+                                       'no' => $this->_('Non'),
+                                       'yes' => $this->_('Oui'),
+                                       'all' => $this->_('Indifférent')]])
+
+      ->addUniqDisplayGroup('loan_search_group')
+      ->setAction(Class_Url::absolute('/opac/abonne/prets'));
+  }
+}
\ No newline at end of file
diff --git a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
index 541eb108725..ae5295445b3 100644
--- a/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
+++ b/tests/scenarios/HandleBranchcode/HandleBranchcodeTest.php
@@ -214,6 +214,13 @@ class HandleBranchcodeDisplayLoanByOthersTest extends HandleBranchcodeTestCase {
       }
 
 
+    /** @test */
+  public function dispatchOnHoldOnShouldDisplayLeonard() {
+    $this->dispatch('/opac/abonne/prets/onhold/yes',true);
+    $this->assertXPathContentContains('//td', 'Quel bazar, Léonard', $this->_response->getBody());
+      }
+
+
   /** @test */
   public function startIssueDateInFuturShouldNotDisplayBooks() {
     $this->borrower = $this->service->getEmprunteur($this->user);
@@ -236,4 +243,17 @@ class HandleBranchcodeDisplayLoanByOthersTest extends HandleBranchcodeTestCase {
     $this->dispatch('/opac/abonne/prets/start_issue_date//12%2F06%2F2015/end_issue_date/12%2F06%2F2019/start_date_retour//end_date_retour//onhold//',true);
     $this->assertXPathContentContains('//td', 'Quel bazar, Léonard', $this->_response->getBody());
   }
+
+
+  /** @test */
+  public function formShouldBeDisplayedSelectedValues() {
+    $this->borrower = $this->service->getEmprunteur($this->user);
+    $this->dispatch('/opac/abonne/prets/start_issue_date/12%2F06%2F2015/end_issue_date/12%2F06%2F2019/start_date_retour/20%2F06%2F2015/end_date_retour/20%2F06%2F2019/onhold//',true);
+    $this->assertXPath('//input[@name="start_issue_date"][@value="12/06/2015"]', $this->_response->getBody());
+    $this->assertXPath('//input[@name="end_issue_date"][@value="12/06/2019"]', $this->_response->getBody());
+    $this->assertXPath('//input[@name="start_date_retour"][@value="20/06/2015"]', $this->_response->getBody());
+    $this->assertXPath('//input[@name="end_date_retour"][@value="20/06/2019"]', $this->_response->getBody());
+
+  }
+
 }
-- 
GitLab