Skip to content

Commit b243399

Browse files
committed
Enhacement on getPaginatedChildren method for ContentDto
1 parent a2839f5 commit b243399

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

src/Dtos/ContentDto.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,52 @@ public function getChildren()
128128
return $this->children = $result;
129129
}
130130

131-
public function getPaginatedChildren($perPage = 10, $pageName = 'page', $page = null)
131+
public function getPaginatedChildren($perPage = 10, $pageName = 'page', $page = null, $isWebPage = null, $isPublished = null, $withRelations = [], $sorting = [])
132132
{
133-
// todo: implement pagination for children
134-
return $this->getChildren()->paginate($perPage, $pageName, $page);
133+
$query = $this->getModel()->children();
134+
135+
$query->with(static::getNecessaryRelationships());
136+
$query->with($withRelations);
137+
138+
if ($isWebPage != null) {
139+
$query->whereIsWebPage($isWebPage);
140+
}
141+
if ($isPublished != null) {
142+
$query->whereIsPublished($isPublished);
143+
}
144+
if (!empty($sorting)) {
145+
foreach ($sorting as $column => $direction) {
146+
if (! is_string($column) || ! is_string($direction)) {
147+
continue;
148+
}
149+
$direction = strtolower(trim($direction));
150+
if (! in_array($direction, ['asc', 'desc'])) {
151+
$direction = 'asc';
152+
}
153+
$query->orderBy($column, $direction);
154+
}
155+
}
156+
157+
$currLocale = $this->getLocale();
158+
159+
return $query->paginate($perPage, ['*'], $pageName, $page)
160+
->tap(function ($paginator) {
161+
if ($paginator instanceof \Illuminate\Contracts\Pagination\Paginator) {
162+
163+
$items = $paginator->getCollection();
164+
165+
// for "toDto" method
166+
if ($items instanceof ContentCollection) {
167+
$items = $items->setPaginator($paginator);
168+
}
169+
170+
$paginator->setCollection($items);
171+
172+
}
173+
174+
return $paginator;
175+
})
176+
->toDto($currLocale);
135177
}
136178

137179
public function getParent()

0 commit comments

Comments
 (0)