Commit 77494966 authored by efalcy's avatar efalcy
Browse files

dev #72825 : refacto

parent 1f8c35f4
Pipeline #4419 failed with stage
in 33 minutes and 51 seconds
<?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
......@@ -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);
}
......
......@@ -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;
......
......@@ -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;
}
}
<?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
......@@ -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());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment