1
+ <?php
2
+ namespace Yacy \Yacy \Controller ;
3
+ use TYPO3 \CMS \Extbase \Utility \DebuggerUtility ;
4
+ /***************************************************************
5
+ *
6
+ * Copyright notice
7
+ *
8
+ * (c) 2014 Eike Starkmann <[email protected] >
9
+ *
10
+ * All rights reserved
11
+ *
12
+ * This script is part of the TYPO3 project. The TYPO3 project is
13
+ * free software; you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation; either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * The GNU General Public License can be found at
19
+ * http://www.gnu.org/copyleft/gpl.html.
20
+ *
21
+ * This script is distributed in the hope that it will be useful,
22
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ * GNU General Public License for more details.
25
+ *
26
+ * This copyright notice MUST APPEAR in all copies of the script!
27
+ ***************************************************************/
28
+
29
+ /**
30
+ * SearchController
31
+ */
32
+ class SearchController extends \TYPO3 \CMS \Extbase \Mvc \Controller \ActionController {
33
+
34
+
35
+ /**
36
+ * searchResultRepository
37
+ *
38
+ * @var \Yacy\Yacy\Domain\Repository\SearchRepository
39
+ * @inject
40
+ */
41
+ protected $ searchRepository = NULL ;
42
+
43
+ /**
44
+ * action index
45
+ *
46
+ * @return void
47
+ */
48
+ public function indexAction () {
49
+ /* @var $demand Yacy\Yacy\Domain\Model\Demand */
50
+ $ demand = $ this ->objectManager ->get ('Yacy\Yacy\Domain\Model\Demand ' );
51
+ $ demand ->setHost ('localhost ' );
52
+ $ demand ->setPort (8090 );
53
+
54
+ $ this ->view ->assign ('demand ' , $ demand );
55
+ }
56
+
57
+ /**
58
+ * Search Action
59
+ * @param \Yacy\Yacy\Domain\Model\Demand $demand
60
+ */
61
+ public function searchAction (\Yacy \Yacy \Domain \Model \Demand $ demand , $ page = 1 ) {
62
+
63
+ $ results = $ this ->searchRepository ->findDemandedViaYacyRss ($ demand );
64
+ $ pagination = $ this ->buildPagination (10 ,$ page ,$ demand );
65
+
66
+ $ this ->view ->assign ('pagination ' , $ pagination );
67
+ $ this ->view ->assign ('demand ' , $ demand );
68
+ $ this ->view ->assign ('results ' , $ results );
69
+ }
70
+
71
+ protected function buildPagination ($ itemsPerPage = 10 , $ page = 1 , \Yacy \Yacy \Domain \Model \Demand $ demand ){
72
+
73
+ $ resultsCount = $ this ->searchRepository ->countAllRequested ($ demand );
74
+ DebuggerUtility::var_dump ($ resultsCount );
75
+ if (!$ resultsCount <= $ itemsPerPage ) {
76
+ //build the pagination
77
+ //build query for page
78
+ $ queryOffset = $ itemsPerPage * ($ page - 1 );
79
+ $ demand ->setStartRecord ($ queryOffset );
80
+ //build paginator
81
+ $ pages = ceil ($ resultsCount / $ itemsPerPage );
82
+ //We limit the pagination menu to 11 pages
83
+ //Case I: the calculatet pages are below 10
84
+ if ($ pages <= 10 ) {
85
+ $ minPagination = 1 ;
86
+ $ maxPagination = $ pages ;
87
+ }
88
+ //Case II: the calculated pages are more than 10
89
+ if ($ pages > 10 ) {
90
+ $ maxPagination = $ page + 5 ;
91
+ if ($ maxPagination > $ pages ) {
92
+ $ maxPagination = $ pages ;
93
+ }
94
+ $ minPagination = $ maxPagination -11 ;
95
+ if ($ minPagination <1 ) {
96
+ $ minPagination = 1 ;
97
+ $ maxPagination = 11 ;
98
+ }
99
+ }
100
+ //Now we bild the page navigation
101
+ for ($ i = $ minPagination ; $ i <= $ maxPagination ; $ i ++) {
102
+ $ pagination ['pages ' ][$ i ]['text ' ] = $ i ;
103
+ }
104
+ //Build next / prev links
105
+ if ($ page >1 ) {
106
+ $ pagination ['prev ' ] = $ page -1 ;
107
+ }
108
+ if ($ page < $ pages ) {
109
+ $ pagination ['next ' ] = $ page +1 ;
110
+ }
111
+ //build the firstpage / lastPage links
112
+ if ($ minPagination > 1 ){
113
+ $ pagination ['first ' ] = 1 ;
114
+ }
115
+ if ($ maxPagination < $ pages ){
116
+ $ pagination ['last ' ] = $ pages ;
117
+ }
118
+ }
119
+ //write the actual page for css
120
+ $ pagination ['current ' ] = $ page ;
121
+
122
+ return $ pagination ;
123
+ }
124
+
125
+
126
+ }
0 commit comments