1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Grouping\GroupItem; |
18
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ResultPaginateController |
23
|
|
|
* |
24
|
|
|
* @author Frans Saris <[email protected]> |
25
|
|
|
* @author Timo Hund <[email protected]> |
26
|
|
|
* @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller |
27
|
|
|
*/ |
28
|
|
|
class GroupItemPaginateController extends AbstractPaginateWidgetController |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var SearchResultSet |
33
|
|
|
*/ |
34
|
|
|
protected $resultSet; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var GroupItem |
38
|
|
|
*/ |
39
|
|
|
protected $groupItem; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function initializeAction() |
45
|
|
|
{ |
46
|
|
|
parent::initializeAction(); |
47
|
|
|
|
48
|
|
|
$this->resultSet = $this->widgetConfiguration['resultSet']; |
49
|
|
|
$this->groupItem = $this->widgetConfiguration['groupItem']; |
50
|
|
|
$this->configuration['itemsPerPage'] = $this->getItemsPerPage(); |
51
|
|
|
|
52
|
|
|
$this->numberOfPages = (int)ceil($this->groupItem->getNumFound() / $this->configuration['itemsPerPage']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determines the number of results per page. When nothing is configured 10 will be returned. |
57
|
|
|
* |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
|
|
protected function getItemsPerPage() |
61
|
|
|
{ |
62
|
|
|
$perPage = (int)$this->groupItem->getGroup()->getResultsPerPage(); |
63
|
|
|
return $perPage > 0 ? $perPage : 10; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext $controllerContext |
68
|
|
|
* @return \ApacheSolrForTypo3\Solr\Mvc\Controller\SolrControllerContext |
69
|
|
|
*/ |
70
|
|
|
protected function setActiveSearchResultSet($controllerContext) { |
71
|
|
|
$controllerContext->setSearchResultSet($this->resultSet); |
72
|
|
|
return $controllerContext; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function indexAction() |
79
|
|
|
{ |
80
|
|
|
// set current page |
81
|
|
|
$groupName = $this->groupItem->getGroup()->getGroupName(); |
82
|
|
|
$groupItemValue = $this->groupItem->getGroupValue(); |
83
|
|
|
$this->currentPage = $this->resultSet->getUsedSearchRequest()->getGroupItemPage($groupName, $groupItemValue); |
84
|
|
|
if ($this->currentPage < 1) { |
85
|
|
|
$this->currentPage = 1; |
86
|
|
|
} |
87
|
|
|
$this->view->assign('contentArguments', [$this->widgetConfiguration['as'] => $this->groupItem->getSearchResults(), 'pagination' => $this->buildPagination()]); |
88
|
|
|
$this->view->assign('configuration', $this->configuration); |
89
|
|
|
$this->view->assign('resultSet', $this->resultSet); |
90
|
|
|
$this->view->assign('groupItem', $this->groupItem); |
91
|
|
|
|
92
|
|
|
if (!empty($this->templatePath)) { |
93
|
|
|
$this->view->setTemplatePathAndFilename($this->templatePath); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: