forked from jaeksoft/opensearchserver-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_paging.class.php
More file actions
132 lines (113 loc) · 3.99 KB
/
oss_paging.class.php
File metadata and controls
132 lines (113 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of OpenSearchServer PHP Client.
*
* Copyright (C) 2008-2013 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* OpenSearchServer PHP Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer PHP Client 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenSearchServer PHP Client. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Class to access OpenSearchServer API
*/
if (!extension_loaded('SimpleXML')) {
trigger_error("OssApi won't work whitout SimpleXML extension", E_USER_ERROR); die();
}
/**
* Class to access OpenSearchServer API
* @author philcube <egosse@open-search-server.com>
* @package OpenSearchServer
*/
class OssPaging {
protected $oss_result;
protected $resultTotal;
protected $resultLow;
protected $resultHigh;
protected $resultPrev;
protected $resultNext;
protected $pageBaseURI;
protected $rowsParameter;
protected $pageParameter;
protected $paramSeparator;
const MAX_PAGE_TO_LINK = 10;
/**
* @param $result The data
* @param $model The list of fields
* @return OssApi
*/
public function __construct(SimpleXMLElement $result, $rowsParam = 'rows', $pageParam = 'p', $paramSeparator = '&') {
$this->oss_result = $result;
$this->rowsParameter = $rowsParam;
$this->pageParameter = $pageParam;
$this->paramSeparator = $paramSeparator;
self::compute();
if (!function_exists('OssApi_Dummy_Function')) {
function OssApi_Dummy_Function() {
}
}
}
/**
* GETTER
*/
public function getResultCurrentPage() {
return $this->resultCurrentPage;
}
public function getResultTotal() {
return $this->resultTotal;
}
public function getResultLow() {
return $this->resultLow;
}
public function getResultHigh() {
return $this->resultHigh;
}
public function getResultPrev() {
return $this->resultPrev;
}
public function getResultNext() {
return $this->resultNext;
}
public function getPageBaseURI() {
return $this->pageBaseURI;
}
public function compute() {
$this->resultFound = ((int) $this->oss_result->result['numFound'] - (int) $this->oss_result->result['collapsedDocCount']);
$this->resultTime = (float) $this->oss_result->result['time'] / 1000;
$this->resultRows = (int) $this->oss_result->result['rows'];
$this->resultStart = (int) $this->oss_result->result['start'];
$this->resultCurrentPage = ($this->resultRows > 0) ? floor($this->resultStart / $this->resultRows) : 0;
$this->resultTotal = ($this->resultRows > 0) ? ceil($this->resultFound / $this->resultRows) : 0;
if ($this->resultTotal > 1) {
$low = $this->resultCurrentPage - (OssPaging::MAX_PAGE_TO_LINK / 2);
$high = $this->resultCurrentPage + (OssPaging::MAX_PAGE_TO_LINK / 2 - 1);
if ($low < 0) {
$high += $low * -1;
}
if ($high > $this->resultTotal) {
$low -= $high - $this->resultTotal;
}
$this->resultLow = max($low, 0);
$this->resultHigh = min($this->resultTotal, $high);
$this->resultPrev = max($this->resultCurrentPage - 1, 0);
if($this->resultCurrentPage + 1 < $this->resultHigh) {
$this->resultNext = min($this->resultCurrentPage + 1, $this->resultTotal);
}
$this->pageBaseURI = preg_replace('/&(?:' . $this->pageParameter . '|' . $this->rowsParameter . ')=[\d]+/', '', $_SERVER['REQUEST_URI'])
. $this->paramSeparator . $this->rowsParameter . '=' . $this->resultRows . $this->paramSeparator . $this->pageParameter . '=';
}
}
}
?>