| 1 |
lars |
1 |
<?php
|
|
|
2 |
// $Id: EbatNs_PaginationHelper.php,v 1.2 2008-05-02 15:04:05 carsten Exp $
|
|
|
3 |
// $Log: EbatNs_PaginationHelper.php,v $
|
|
|
4 |
// Revision 1.2 2008-05-02 15:04:05 carsten
|
|
|
5 |
// Initial, PHP5
|
|
|
6 |
//
|
|
|
7 |
//
|
|
|
8 |
class EbatNs_PaginationHelper
|
|
|
9 |
{
|
|
|
10 |
protected $_proxy;
|
|
|
11 |
|
|
|
12 |
protected $_callname;
|
|
|
13 |
|
|
|
14 |
protected $_request;
|
|
|
15 |
|
|
|
16 |
protected $_responseElementToMerge;
|
|
|
17 |
|
|
|
18 |
protected $_maxEntries;
|
|
|
19 |
|
|
|
20 |
protected $_currentPage;
|
|
|
21 |
|
|
|
22 |
protected $_receivedElements;
|
|
|
23 |
|
|
|
24 |
protected $_accumulatedResponse;
|
|
|
25 |
|
|
|
26 |
protected $_receivedMaxPages;
|
|
|
27 |
|
|
|
28 |
protected $_bCountedByHandler;
|
|
|
29 |
|
|
|
30 |
protected $_debug = 0;
|
|
|
31 |
|
|
|
32 |
// if $responseElementToMerge is set to '__COUNT_BY_HANDLER' an attached handler HAS to count the elements
|
|
|
33 |
// and inform the $EbatNs_ServiceProxy by calling ->incrementPaginationCounter();
|
|
|
34 |
function __construct ($proxy, $callName, $request, $responseElementToMerge = '__COUNT_BY_HANDLER', $maxEntries = 200, $pageSize = 200, $initialPage = 1)
|
|
|
35 |
{
|
|
|
36 |
$this->_proxy = $proxy;
|
|
|
37 |
$this->_callname = $callName;
|
|
|
38 |
$this->_request = $request;
|
|
|
39 |
|
|
|
40 |
if ($responseElementToMerge == '__COUNT_BY_HANDLER')
|
|
|
41 |
{
|
|
|
42 |
$this->_bCountedByHandler = true;
|
|
|
43 |
$this->_responseElementToMerge = null;
|
|
|
44 |
} else
|
|
|
45 |
{
|
|
|
46 |
$this->_responseElementToMerge = $responseElementToMerge;
|
|
|
47 |
$this->_bCountedByHandler = false;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
$this->_maxEntries = $maxEntries;
|
|
|
51 |
|
|
|
52 |
$this->_currentPage = 0;
|
|
|
53 |
$this->_receivedElements = 0;
|
|
|
54 |
$this->_receivedMaxPages = - 1;
|
|
|
55 |
|
|
|
56 |
$this->_accumulatedResponse = null;
|
|
|
57 |
|
|
|
58 |
// add pagination infomation to the request
|
|
|
59 |
$this->_request->Pagination = new PaginationType();
|
|
|
60 |
|
|
|
61 |
$this->_request->Pagination->EntriesPerPage = $pageSize;
|
|
|
62 |
$this->_request->Pagination->PageNumber = ($initialPage - 1);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
function getNextPage ()
|
|
|
66 |
{
|
|
|
67 |
// important to have the data here !!!
|
|
|
68 |
global $Facet_AckCodeType;
|
|
|
69 |
|
|
|
70 |
$bFirst = false;
|
|
|
71 |
if ($this->_currentPage == 0)
|
|
|
72 |
{
|
|
|
73 |
// prepare first call
|
|
|
74 |
$bFirst = true;
|
|
|
75 |
}
|
|
|
76 |
else
|
|
|
77 |
{
|
|
|
78 |
// break the operation if the maximum count is reached
|
|
|
79 |
//
|
|
|
80 |
// $this->_maxEntries eq -1 will not stop till all data was downloaded !
|
|
|
81 |
//
|
|
|
82 |
if ($this->_maxEntries > 0)
|
|
|
83 |
{
|
|
|
84 |
if ($this->_bCountedByHandler)
|
|
|
85 |
{
|
|
|
86 |
// echo "<br>checking (handler) " . $this->_proxy->getPaginationCounter() . " to " . $this->_maxEntries . "<br>";
|
|
|
87 |
if ($this->_proxy->getPaginationCounter() >= $this->_maxEntries)
|
|
|
88 |
{
|
|
|
89 |
// Break out getPaginationCounter() >= _maxEntries;
|
|
|
90 |
return false;
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
else
|
|
|
94 |
{
|
|
|
95 |
// echo "<br>checking (attach-mode) " . $this->_proxy->getPaginationCounter() . " to " . $this->_maxEntries . "<br>";
|
|
|
96 |
if (count($this->_accumulatedResponse) >=
|
|
|
97 |
$this->_maxEntries)
|
|
|
98 |
{
|
|
|
99 |
// Break out _accumulatedResponse >= _maxEntries
|
|
|
100 |
return false;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$this->_request->Pagination->PageNumber ++;
|
|
|
107 |
|
|
|
108 |
// calling the proxy method for this api-call
|
|
|
109 |
$res = call_user_method($this->_callname, $this->_proxy, $this->_request);
|
|
|
110 |
if ($bFirst)
|
|
|
111 |
{
|
|
|
112 |
$this->_accumulatedResponse = $res;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if ($res->Ack != $Facet_AckCodeType->Success)
|
|
|
116 |
{
|
|
|
117 |
// overwrite the response in case of an error
|
|
|
118 |
$this->_accumulatedResponse->Ack = $res->Ack;
|
|
|
119 |
if (is_array($this->_accumulatedResponse->Errors))
|
|
|
120 |
{
|
|
|
121 |
$this->_accumulatedResponse->Errors = array_merge($this->_accumulatedResponse->Errors, $res->Errors);
|
|
|
122 |
} else
|
|
|
123 |
$this->_accumulatedResponse->Errors = $res->Errors;
|
|
|
124 |
|
|
|
125 |
// Break out, got an error
|
|
|
126 |
return false;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if ($this->_bCountedByHandler)
|
|
|
130 |
{
|
|
|
131 |
$this->_receivedElements = $this->_proxy->getPaginationCounter();
|
|
|
132 |
} else
|
|
|
133 |
{
|
|
|
134 |
if (is_array($res->{$this->_responseElementToMerge}))
|
|
|
135 |
$this->_receivedElements += count($res->{$this->_responseElementToMerge});
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$this->_receivedMaxPages = $res->PaginationResult->TotalNumberOfPages;
|
|
|
139 |
$this->_currentPage = $res->PageNumber;
|
|
|
140 |
|
|
|
141 |
if (! $bFirst && ! $this->_bCountedByHandler)
|
|
|
142 |
$this->_accumulatedResponse->{$this->_responseElementToMerge} = array_merge($this->_accumulatedResponse->{$this->_responseElementToMerge}, $res->{$this->_responseElementToMerge});
|
|
|
143 |
|
|
|
144 |
if ($this->_receivedMaxPages == 0 || ($this->_receivedMaxPages == $this->_currentPage))
|
|
|
145 |
{
|
|
|
146 |
// this was the final page returned from the API
|
|
|
147 |
return false;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// ok, we have more pages
|
|
|
151 |
return true;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
function QueryAll ()
|
|
|
155 |
{
|
|
|
156 |
while ($this->getNextPage())
|
|
|
157 |
;
|
|
|
158 |
|
|
|
159 |
return $this->_accumulatedResponse;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
?>
|