Skip to content

Commit 26a503a

Browse files
committed
Fixed pagination count total bug #213
1 parent 1bc95e2 commit 26a503a

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/IMAP/Query/Query.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ public function markAsRead() {
134134
return $this;
135135
}
136136

137+
/**
138+
* Perform an imap search request
139+
*
140+
* @return \Illuminate\Support\Collection
141+
* @throws \Webklex\IMAP\Exceptions\ConnectionFailedException
142+
*/
143+
protected function search(){
144+
$this->generate_query();
145+
146+
/**
147+
* Don't set the charset if it isn't used - prevent strange outlook mail server errors
148+
* @see https://github.com/Webklex/laravel-imap/issues/100
149+
*/
150+
if($this->getCharset() === null){
151+
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID);
152+
}else{
153+
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID, $this->getCharset());
154+
}
155+
156+
if ($available_messages !== false) {
157+
return collect($available_messages);
158+
}
159+
160+
return collect();
161+
}
162+
137163
/**
138164
* Fetch the current query and return all found messages
139165
*
@@ -144,21 +170,13 @@ public function get() {
144170
$messages = MessageCollection::make([]);
145171

146172
try {
147-
$this->generate_query();
148-
149-
/**
150-
* Don't set the charset if it isn't used - prevent strange outlook mail server errors
151-
* @see https://github.com/Webklex/laravel-imap/issues/100
152-
*/
153-
if($this->getCharset() === null){
154-
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID);
155-
}else{
156-
$available_messages = imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID, $this->getCharset());
157-
}
173+
$available_messages = $this->search();
174+
$available_messages_count = $available_messages->count();
175+
176+
if ($available_messages_count > 0) {
158177

159-
if ($available_messages !== false) {
178+
$messages->total($available_messages_count);
160179

161-
$available_messages = collect($available_messages);
162180
$options = config('imap.options');
163181

164182
if(strtolower($options['fetch_order']) === 'desc'){

src/IMAP/Support/PaginatedCollection.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
*/
2424
class PaginatedCollection extends Collection {
2525

26+
/** @var int $total */
27+
protected $total;
28+
2629
/**
2730
* Paginate the current collection.
2831
*
@@ -35,7 +38,9 @@ class PaginatedCollection extends Collection {
3538
public function paginate($per_page = 15, $page = null, $page_name = 'page') {
3639
$page = $page ?: Paginator::resolveCurrentPage($page_name);
3740

38-
$results = ($total = $this->count()) ? $this->forPage($page, $per_page) : $this->all();
41+
$total = $this->total ? $this->total : $this->count();
42+
43+
$results = $total ? $this->forPage($page, $per_page) : $this->all();
3944

4045
return $this->paginator($results, $total, $per_page, $page, [
4146
'path' => Paginator::resolveCurrentPath(),
@@ -57,4 +62,17 @@ public function paginate($per_page = 15, $page = null, $page_name = 'page') {
5762
protected function paginator($items, $total, $per_page, $current_page, array $options) {
5863
return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options);
5964
}
65+
66+
/**
67+
* @param null $total
68+
*
69+
* @return int|null
70+
*/
71+
public function total($total = null) {
72+
if($total === null) {
73+
return $this->total;
74+
}
75+
76+
return $this->total = $total;
77+
}
6078
}

0 commit comments

Comments
 (0)