Skip to content

Commit 04a957d

Browse files
committed
#859 Configurable results per page options
1 parent 730c108 commit 04a957d

3 files changed

Lines changed: 104 additions & 3 deletions

File tree

application/configs/application.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ searchengine.solr.facets = author_facet,year,doctype,language,has_fulltext,belon
203203
; number of default search results
204204
searchengine.solr.parameterDefaults.rows = 10
205205

206+
search.resultsPerPageOptions = 10, 20, 50, 100
207+
206208
; Configuration for search facets
207209
search.facet.default.translated = 0
208210
search.facet.default.limit = 10

library/Application/View/Helper/ResultsPerPageOptions.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@
2929
* @license http://www.gnu.org/licenses/gpl.html General Public License
3030
*/
3131

32+
use Opus\Common\ConfigTrait;
33+
3234
/**
3335
* View helper for returning value of configuration option.
34-
*
35-
* TODO make class configurable (use different base class from Zend?)
3636
*/
3737
class Application_View_Helper_ResultsPerPageOptions extends Application_View_Helper_Abstract
3838
{
39+
use ConfigTrait;
40+
3941
/**
4042
* @param int $steps
4143
* @return string
4244
*/
4345
public function resultsPerPageOptions($steps = 0)
4446
{
45-
$options = [10, 20, 50, 100];
47+
$options = $this->getOptions();
4648

4749
$output = '<ul>';
4850

@@ -56,4 +58,24 @@ public function resultsPerPageOptions($steps = 0)
5658

5759
return $output;
5860
}
61+
62+
public function getOptions(): array
63+
{
64+
$config = $this->getConfig();
65+
66+
$options = [];
67+
68+
if (isset($config->search->resultsPerPageOptions)) {
69+
$list = $config->search->resultsPerPageOptions;
70+
if (strlen(trim($list)) > 0) {
71+
$options = array_filter(array_map('trim', explode(',', $list)));
72+
}
73+
}
74+
75+
if (count($options) === 0) {
76+
$options = [10, 20, 50, 100]; // TODO do we need defaults in the code?
77+
}
78+
79+
return $options;
80+
}
5981
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* This file is part of OPUS. The software OPUS has been originally developed
5+
* at the University of Stuttgart with funding from the German Research Net,
6+
* the Federal Department of Higher Education and Research and the Ministry
7+
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
8+
*
9+
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
10+
* by the Stuttgart University Library, the Library Service Center
11+
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
12+
* the Saarland University and State Library, the Saxon State Library -
13+
* Dresden State and University Library, the Bielefeld University Library and
14+
* the University Library of Hamburg University of Technology with funding from
15+
* the German Research Foundation and the European Regional Development Fund.
16+
*
17+
* LICENCE
18+
* OPUS is free software; you can redistribute it and/or modify it under the
19+
* terms of the GNU General Public License as published by the Free Software
20+
* Foundation; either version 2 of the Licence, or any later version.
21+
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
22+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24+
* details. You should have received a copy of the GNU General Public License
25+
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
26+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
*
28+
* @copyright Copyright (c) 2026, OPUS 4 development team
29+
* @license http://www.gnu.org/licenses/gpl.html General Public License
30+
*/
31+
32+
class Application_View_Helper_ResultsPerPageOptionsTest extends ControllerTestCase
33+
{
34+
/** @var string[] */
35+
protected $additionalResources = ['view'];
36+
37+
/** @var Application_View_Helper_ResultsPerPageOptions */
38+
protected $helper;
39+
40+
public function setUp(): void
41+
{
42+
parent::setUp();
43+
44+
$this->helper = new Application_View_Helper_ResultsPerPageOptions();
45+
$this->helper->setView($this->getView());
46+
}
47+
48+
public function testGetOptions()
49+
{
50+
$this->assertEqualsCanonicalizing(
51+
['10', '20', '50', '100'],
52+
$this->helper->getOptions()
53+
);
54+
}
55+
56+
public function testGetOptionsMalformedList()
57+
{
58+
$this->adjustConfiguration([
59+
'search' => ['resultsPerPageOptions' => '10, , 20, 50,100, 200, , '],
60+
]);
61+
$this->assertEqualsCanonicalizing(
62+
['10', '20', '50', '100', '200'],
63+
$this->helper->getOptions()
64+
);
65+
}
66+
67+
public function testGetOptionsEmptyList()
68+
{
69+
$this->adjustConfiguration([
70+
'search' => ['resultsPerPageOptions' => ''],
71+
]);
72+
$this->assertEqualsCanonicalizing(
73+
['10', '20', '50', '100'],
74+
$this->helper->getOptions()
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)