From 68a1f3110bce8b05d91939c11d6ffe711a0d3199 Mon Sep 17 00:00:00 2001 From: Alexander Purr <purr@ub.uni-leipzig.de> Date: Tue, 24 Mar 2020 17:09:20 +0100 Subject: [PATCH] refs #17176 [fid] add advanced subito partial copy page selection validaton * add new validators ** page selection: correct page order ** page selection: highest selected page can not exceed number of pages ** page selection: can not not exceed a certain percentaged part of whole record * add first validator to form configuration * add other validators within controller trait to from * add hyphen filter to form configuration * add dynamic page selection validators ** add session to RecordController via DelegatorFactory ** adjust behavior to handle false page selection warning with session parameter * add fid translation * display number of record pages in summary of subito partial copy ** changes in RecordDataFormatterFactory *** add general spec builder function *** extend general specs for subito partial copy --- ...d-acquisition-subito-partial-copy-form.php | 20 ++++++ .../CustomTraits/FidAcquisitionTrait.php | 25 ++++++-- .../fid/src/Controller/RecordController.php | 2 + .../RecordControllerDelegatorFactory.php | 1 - module/fid/src/Hydrator/OrderHydrator.php | 4 +- .../Validator/SubitoPartialCopyPageBounds.php | 59 +++++++++++++++++ .../Validator/SubitoPartialCopyPageOrder.php | 49 +++++++++++++++ ...bitoPartialCopyPageRangeValidatorTrait.php | 37 +++++++++++ .../SubitoPartialCopyPageSelection.php | 63 +++++++++++++++++++ .../Root/RecordDataFormatterFactory.php | 34 ++++++++-- themes/fid/languages/fid/de.ini | 13 ++-- themes/fid/languages/fid/en.ini | 11 +++- .../acquisition-subito-partial-copy.phtml | 4 +- 13 files changed, 298 insertions(+), 24 deletions(-) create mode 100644 module/fid/src/Validator/SubitoPartialCopyPageBounds.php create mode 100644 module/fid/src/Validator/SubitoPartialCopyPageOrder.php create mode 100644 module/fid/src/Validator/SubitoPartialCopyPageRangeValidatorTrait.php create mode 100644 module/fid/src/Validator/SubitoPartialCopyPageSelection.php diff --git a/module/fid/config/fid-acquisition-subito-partial-copy-form.php b/module/fid/config/fid-acquisition-subito-partial-copy-form.php index 7bc4339be56..bcd36f3f715 100644 --- a/module/fid/config/fid-acquisition-subito-partial-copy-form.php +++ b/module/fid/config/fid-acquisition-subito-partial-copy-form.php @@ -19,7 +19,11 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 */ +use fid\Controller\RecordController; use fid\Hydrator\OrderHydrator; +use fid\Validator\SubitoPartialCopyPageLimit; +use fid\Validator\SubitoPartialCopyPageOrder; +use Zend\Filter\PregReplace; use Zend\Filter\StringTrim; use Zend\Form\Element\Submit; use Zend\Form\Element\Text; @@ -59,6 +63,13 @@ return [ StringTrim::class => [ 'name' => StringTrim::class, ], + PregReplace::class => [ + 'name' => PregReplace::class, + 'options' => [ + 'pattern' => '/(-|–|—|−)/', + 'replacement' => '-' + ] + ], ], 'validators' => [ StringLength::class => [ @@ -74,6 +85,7 @@ return [ ], Regex::class => [ 'name' => Regex::class, + 'break_chain_on_failure' => true, 'options' => [ 'pattern' => '/^(0|[1-9]\d*)-([1-9]\d*)$/', 'messages' => [ @@ -81,6 +93,14 @@ return [ ], ], ], + SubitoPartialCopyPageOrder::class => [ + 'name' => SubitoPartialCopyPageOrder::class, + 'options' => [ + 'messages' => [ + SubitoPartialCopyPageOrder::PAGES_ORDER => 'acquisition_error_page_order', + ], + ], + ], ], ], 'submit' => [ diff --git a/module/fid/src/Controller/CustomTraits/FidAcquisitionTrait.php b/module/fid/src/Controller/CustomTraits/FidAcquisitionTrait.php index 896170bae09..4999b59b5b5 100644 --- a/module/fid/src/Controller/CustomTraits/FidAcquisitionTrait.php +++ b/module/fid/src/Controller/CustomTraits/FidAcquisitionTrait.php @@ -29,6 +29,8 @@ namespace fid\Controller\CustomTraits; use fid\Service\Client; use fid\Service\DataTransferObject\Order; +use fid\Validator\SubitoPartialCopyPageBounds; +use fid\Validator\SubitoPartialCopyPageSelection; use finc\View\Helper\Root\Citation; use Zend\Form\Form; use Zend\Http\PhpEnvironment\Request; @@ -154,13 +156,28 @@ trait FidAcquisitionTrait $request = $this->getRequest(); $form = $this->serviceLocator->get('fid-acquisition-subito-partial-copy-form'); + // add number of pages specific validators + $recordPages = $driver->tryMethod('getNumberOfPages'); + if ($recordPages) + { + $form->getInputFilter()->get('pages')->getValidatorChain()->attach( + new SubitoPartialCopyPageBounds(['numberPages' => $recordPages]), + true + ); + $form->getInputFilter()->get('pages')->getValidatorChain()->attach( + new SubitoPartialCopyPageSelection(['numberPages' => $recordPages]) + ); + } + if ($submitted = $this->formWasSubmitted()) { $form->setData($request->getPost()); if ($form->isValid()) { $url = $this->getRecordUrl($recordId); $label = $this->getOrderLabel($driver); - $pages = $form->getData()['pages']; - $form->getHydrator()->hydrate(compact('driver','user', 'type', 'url', 'label', 'pages'), $order = new Order()); + $subitoPartialCopy = [ + 'selection' => $form->getData()['pages'] + ]; + $form->getHydrator()->hydrate(compact('driver','user', 'type', 'url', 'label', 'subitoPartialCopy'), $order = new Order()); $this->client->requestOrderCreation($order); $message = $this->translate('fid::acquisition_success'); $messenger->addSuccessMessage($message); @@ -168,10 +185,6 @@ trait FidAcquisitionTrait } } - $patron = $this->catalogLogin(); - $catalog = $this->getILS(); - $profile = $catalog->getMyProfile($patron); - $action = $this->url()->fromRoute('record-fidsubitopartialcopy',['id' => $recordId]); $form->setAttribute('action', $action); $form->prepare(); diff --git a/module/fid/src/Controller/RecordController.php b/module/fid/src/Controller/RecordController.php index 8b9bf816544..0bd3d654f76 100644 --- a/module/fid/src/Controller/RecordController.php +++ b/module/fid/src/Controller/RecordController.php @@ -29,4 +29,6 @@ namespace fid\Controller; class RecordController extends \finc\Controller\RecordController { use CustomTraits\FidAcquisitionTrait; + + const PAGE_RANGE_PATTERN = '/^(0|[1-9]\d*)-([1-9]\d*)$/'; } diff --git a/module/fid/src/Controller/RecordControllerDelegatorFactory.php b/module/fid/src/Controller/RecordControllerDelegatorFactory.php index 5a53868764c..84d8577d46b 100644 --- a/module/fid/src/Controller/RecordControllerDelegatorFactory.php +++ b/module/fid/src/Controller/RecordControllerDelegatorFactory.php @@ -12,7 +12,6 @@ class RecordControllerDelegatorFactory implements DelegatorFactoryInterface /** @var RecordController $instance */ $instance = call_user_func($callback); $instance->setClient($container->get(Client::class)); - return $instance; } } \ No newline at end of file diff --git a/module/fid/src/Hydrator/OrderHydrator.php b/module/fid/src/Hydrator/OrderHydrator.php index 244ffcfe7f8..9edb7e79d93 100644 --- a/module/fid/src/Hydrator/OrderHydrator.php +++ b/module/fid/src/Hydrator/OrderHydrator.php @@ -27,7 +27,7 @@ class OrderHydrator extends AbstractHydrator { $object->setType($data['type']); $object->setUser($data['user']); - $partialCopyPages = array_key_exists('pages',$data) ? $data['pages'] : null; + $partialCopy = array_key_exists('subitoPartialCopy', $data) ? $data['subitoPartialCopy'] : null; /* Collect needed record data */ $id = $driver->tryMethod('getUniqueID'); @@ -64,7 +64,7 @@ class OrderHydrator extends AbstractHydrator { 'url' ); - $object->setData(compact('record','partialCopyPages')); + $object->setData(compact('record','partialCopy')); $object->setLabel($data['label']); return; } diff --git a/module/fid/src/Validator/SubitoPartialCopyPageBounds.php b/module/fid/src/Validator/SubitoPartialCopyPageBounds.php new file mode 100644 index 00000000000..18a68db0487 --- /dev/null +++ b/module/fid/src/Validator/SubitoPartialCopyPageBounds.php @@ -0,0 +1,59 @@ +<?php +/** + * Copyright (C) 2020 Leipzig University Library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * @author Alexander Purr <purr@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid\Validator; + +use Zend\Validator\AbstractValidator; + +class SubitoPartialCopyPageBounds extends AbstractValidator +{ + use SubitoPartialCopyPageRangeValidatorTrait; + + public const PAGE_BOUNDS = 'subitoPartialCopyPageBounds'; + + // Available validator options. + protected $options = [ + 'numberPages' => null + ]; + + protected $messageTemplates = [ + self::PAGE_BOUNDS => "acquisition_error_page_bounds", + ]; + + public function isValid($value) + { + if ($this->options['numberPages'] == null) + { + return false; + } + $this->extractPages($value); + + if (($this->pageEnd > $this->options['numberPages']) || ($this->pageStart < 1)) + { + $this->error(self::PAGE_BOUNDS); + return false; + } + else + { + return true; + } + } +} diff --git a/module/fid/src/Validator/SubitoPartialCopyPageOrder.php b/module/fid/src/Validator/SubitoPartialCopyPageOrder.php new file mode 100644 index 00000000000..74961ec79e8 --- /dev/null +++ b/module/fid/src/Validator/SubitoPartialCopyPageOrder.php @@ -0,0 +1,49 @@ +<?php +/** + * Copyright (C) 2020 Leipzig University Library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * @author Alexander Purr <purr@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid\Validator; + +use Zend\Validator\AbstractValidator; + +class SubitoPartialCopyPageOrder extends AbstractValidator +{ + use SubitoPartialCopyPageRangeValidatorTrait; + + public const PAGES_ORDER = 'subitoPartialCopyPageOrder'; + + protected $messageTemplates = [ + self::PAGES_ORDER => "acquisition_error_page_order", + ]; + + public function isValid($value) + { + $this->extractPages($value); + + if($this->pageStart > $this->pageEnd) { + $this->error(self::PAGES_ORDER); + return false; + } + else + { + return true; + } + } +} diff --git a/module/fid/src/Validator/SubitoPartialCopyPageRangeValidatorTrait.php b/module/fid/src/Validator/SubitoPartialCopyPageRangeValidatorTrait.php new file mode 100644 index 00000000000..f828b069390 --- /dev/null +++ b/module/fid/src/Validator/SubitoPartialCopyPageRangeValidatorTrait.php @@ -0,0 +1,37 @@ +<?php +/** + * Copyright (C) 2020 Leipzig University Library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * @author Alexander Purr <purr@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid\Validator; + +use fid\Controller\RecordController; + +trait SubitoPartialCopyPageRangeValidatorTrait +{ + private $pageStart = null; + private $pageEnd = null; + + private function extractPages ($value) + { + preg_match(RecordController::PAGE_RANGE_PATTERN, $value, $matches); + $this->pageStart = (int) $matches[1]; + $this->pageEnd = (int) $matches[2]; + } +} \ No newline at end of file diff --git a/module/fid/src/Validator/SubitoPartialCopyPageSelection.php b/module/fid/src/Validator/SubitoPartialCopyPageSelection.php new file mode 100644 index 00000000000..2818ab17a82 --- /dev/null +++ b/module/fid/src/Validator/SubitoPartialCopyPageSelection.php @@ -0,0 +1,63 @@ +<?php +/** + * Copyright (C) 2020 Leipzig University Library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * @author Alexander Purr <purr@ub.uni-leipzig.de> + * @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2 + */ + +namespace fid\Validator; + +use Zend\Validator\AbstractValidator; + +class SubitoPartialCopyPageSelection extends AbstractValidator +{ + use SubitoPartialCopyPageRangeValidatorTrait; + + public const PAGE_SELECTION = 'subitoPartialCopyPageSelection'; + + protected $messageTemplates = [ + self::PAGE_SELECTION => "acquisition_error_page_selection", + ]; + + // Available validator options. + protected $options = [ + 'numberPages' => null, + 'tolerancePages' => 1, + 'percentagedLimit' => 10 + ]; + + public function isValid ($value) + { + if ($this->options['numberPages'] == null) + { + return false; + } + $this->extractPages($value); + + $selection = $this->pageEnd - $this->pageStart; + $permittedSelection = (($this->options['percentagedLimit'] / 100) * $this->options['numberPages']) + $this->options['tolerancePages']; + if ($selection > $permittedSelection) + { + $this->error(self::PAGE_SELECTION); + return false; + } + else + { + return true; + } + } +} diff --git a/module/fid/src/View/Helper/Root/RecordDataFormatterFactory.php b/module/fid/src/View/Helper/Root/RecordDataFormatterFactory.php index 6b1261732bf..185a746ac8c 100644 --- a/module/fid/src/View/Helper/Root/RecordDataFormatterFactory.php +++ b/module/fid/src/View/Helper/Root/RecordDataFormatterFactory.php @@ -27,6 +27,7 @@ namespace fid\View\Helper\Root; use VuFind\View\Helper\Root\RecordDataFormatter; +use VuFind\View\Helper\Root\RecordDataFormatter\SpecBuilder; class RecordDataFormatterFactory { @@ -40,21 +41,19 @@ class RecordDataFormatterFactory $helper = new RecordDataFormatter(); $helper->setDefaults('fid-acquisition-general', [$this, 'getFidAcquisitionSpecs']); - //$helper->setDefaults('fid-pda', [$this, 'getFidPdaSpecs']); - //$helper->setDefaults('fid-subito-articel', [$this, 'getFidSubitoArticelSpecs']); - //$helper->setDefaults('fid-subito-partial-copy', [$this, 'getFidSubitoPartialCopySpecs']); + $helper->setDefaults('fid-acquisition-subito-partial-copy', [$this, 'getFidAcquisitionSubitoPartialCopySpecs']); return $helper; } /** - * Get specifications for displaying data in fid pda form. + * Get general specifications for displaying data in fid pda form. * * @return array */ - public function getFidAcquisitionSpecs() + public function getFidAcquisitionSpecBuilder() { - $spec = new RecordDataFormatter\SpecBuilder(); + $spec = new SpecBuilder(); $spec->setLine('Title','getTitle'); $spec->setTemplateLine( 'Authors/Corporations', @@ -80,6 +79,29 @@ class RecordDataFormatterFactory 'data-price.phtml' ); $spec->setLine('Language','getLanguages'); + return $spec; + } + + /** + * Get specifications for displaying data in fid pda form. + * + * @return array + */ + public function getFidAcquisitionSpecs() + { + $spec = $this->getFidAcquisitionSpecBuilder(); + return $spec->getArray(); + } + + /** + * Get specifications for displaying data in fid subito partial copy form. + * + * @return array + */ + public function getFidAcquisitionSubitoPartialCopySpecs() + { + $spec = $this->getFidAcquisitionSpecBuilder(); + $spec->setLine('fid::number_of_pages','getNumberOfPages'); return $spec->getArray(); } } diff --git a/themes/fid/languages/fid/de.ini b/themes/fid/languages/fid/de.ini index 970c97e4919..df6b0f15663 100644 --- a/themes/fid/languages/fid/de.ini +++ b/themes/fid/languages/fid/de.ini @@ -144,11 +144,16 @@ acquisition_delivery_to = "Lieferung an" acquisition_info_requested_item = "Bestellung" acquisition_label_pages = "Seitenzahlen (von-bis)" acquisition_label_submit = "Bestellen" -acquisition_error_pages_too_short = "Zu wenig Zeichen" -acquisition_error_pages_too_long = "Zu viele Zeichen" -acquisition_error_pages_pattern = "Fehlerhaftes oder falsches Format" +acquisition_error_pages_too_short = "Ihre Eingabe hat zu wenige Zeichen. Bitte geben Sie mind. 3 Zeichen ein." +acquisition_error_pages_too_long = "Ihre Eingabe hat zu viele Zeichen. Bitte geben Sie max. 13 Zeichen ein." +acquisition_error_pages_pattern = "Falsches Format. Bitte geben Sie die gewünschten Seiten nur im Format VON-BIS ein, z. B. 23-30." +acquisition_error_page_order = "Die Seitenzahlen sind in einer falschen Reihenfolge. Bitte korrigieren Sie Ihre Eingabe." +acquisition_error_page_bounds = "Das Buch hat weniger Seiten als von Ihnen gewünscht. Bitte korrigieren Sie Ihre Eingabe." +acquisition_error_page_selection = "Der von Ihnen angegebene Seitenbereich überschreitet die urheberrechtlich erlaubten 10 Prozent des Gesamtumfangs. Ihre Bestellung kann daher nicht ausgeführt werden. Bitte überprüfen Sie Ihre Angaben." acquisition_subito_article_confirmation = "Möchten Sie den Artikel bestellen?" acquisition_subito_partial_copy_confirmation = "Möchten Sie die Teilkopie bestellen?" acquisition_pda_confirmation = "Möchten Sie das Buch bestellen?" acquisition_pda_ordered = "Dieser Titel wurde bereits auf Nutzerwunsch durch adlr.link erworben und wird in Kürze verfügbar sein." -acquisition_success = "Ihre Bestellung wurde entgegengenommen und wird nun von uns bearbeitet." \ No newline at end of file +acquisition_success = "Ihre Bestellung wurde entgegengenommen und wird nun von uns bearbeitet." + +number_of_pages = "Seiten" \ No newline at end of file diff --git a/themes/fid/languages/fid/en.ini b/themes/fid/languages/fid/en.ini index 3f1c0be6fef..ecb16166624 100644 --- a/themes/fid/languages/fid/en.ini +++ b/themes/fid/languages/fid/en.ini @@ -142,11 +142,16 @@ acquisition_delivery_to = "Delivery to" acquisition_info_requested_item = "Order item" acquisition_label_pages = "Pages (from-to)" acquisition_label_submit = "Order" -acquisition_error_pages_too_short = "Too few characters" -acquisition_error_pages_too_long = "Too many characters" -acquisition_error_pages_pattern = "Inaccurate or wrong format" +acquisition_error_pages_too_short = "Your request has too few characters. Please enter at least 3 characters." +acquisition_error_pages_too_long = "Your request has too many characters. Please enter a maximum of 13 characters." +acquisition_error_pages_pattern = "Wrong format. Please enter the required pages only in the FROM-TO format, e.g. 23-30." +acquisition_error_page_order = "The page numbers are in the wrong order. Please correct your order." +acquisition_error_page_bounds = "The book has fewer pages than you requested. Please correct your order." +acquisition_error_page_selection = "The page range you specify exceeds the 10 percent of the total volume permitted by copyright law. Therefore we will not be able to process your order. Please check your order details." acquisition_subito_article_confirmation = "Do you like to order the article?" acquisition_subito_partial_copy_confirmation = "Do you like to order the partial copy?" acquisition_pda_confirmation = "Do you like to order the book?" acquisition_pda_ordered = "This item has been acquired by adlr.link by request of a patron and will be available in a short time." acquisition_success = "Your order is received and will be processed now." + +number_of_pages = "Pages" \ No newline at end of file diff --git a/themes/fid/templates/fid/record/acquisition-subito-partial-copy.phtml b/themes/fid/templates/fid/record/acquisition-subito-partial-copy.phtml index d86c3dcc050..5405b306203 100644 --- a/themes/fid/templates/fid/record/acquisition-subito-partial-copy.phtml +++ b/themes/fid/templates/fid/record/acquisition-subito-partial-copy.phtml @@ -53,7 +53,7 @@ $form->setAttribute('class','fid-acquisition-form'); <div class="col col-md-6"> <?php $formatter = $this->recordDataFormatter(); - $this->coreFields = $formatter->getData($driver, $formatter->getDefaults('fid-acquisition-general')); + $this->coreFields = $formatter->getData($driver, $formatter->getDefaults('fid-acquisition-subito-partial-copy')); ?> <?= $this->render('fid/record/acquisition-record-details'); ?> </div> @@ -69,9 +69,9 @@ $form->setAttribute('class','fid-acquisition-form'); $elemPages->setAttributes(['class' => 'form-control']); ?> <div class="form-group"> + <?= $this->formElementErrors($elemPages) ?> <?= $this->formLabel($elemPages) ?> <?= $this->formElement($elemPages) ?> - <?= $this->formElementErrors($elemPages) ?> </div> </div> </div> -- GitLab