1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
6
|
|
|
use Jidaikobo\Kontiki\Utils\Pagination; |
7
|
|
|
|
8
|
|
|
trait IndexTrait |
9
|
|
|
{ |
10
|
|
|
protected Pagination $pagination; |
11
|
|
|
|
12
|
|
|
public function getIndexData( |
13
|
|
|
string $context = '', |
14
|
|
|
array $queryParams = [] |
15
|
|
|
): array { |
16
|
|
|
$query = $this->applyFiltersToQuery($context, $queryParams); |
17
|
|
|
|
18
|
|
|
// Set up pagination |
19
|
|
|
$totalItems = $query->count(); |
20
|
|
|
$paged = (int)($queryParams['paged'] ?? 1); |
21
|
|
|
$perPage = (int)($queryParams['perPage'] ?? 10); |
22
|
|
|
$this->pagination = new Pagination($paged, $perPage); |
23
|
|
|
$this->pagination->setTotalItems($totalItems); |
24
|
|
|
|
25
|
|
|
// Fetch and process data |
26
|
|
|
$data = $query->limit($this->pagination->getLimit()) |
27
|
|
|
->offset($this->pagination->getOffset()) |
28
|
|
|
->get() |
29
|
|
|
->map(fn($item) => (array) $item) |
30
|
|
|
->toArray(); |
31
|
|
|
|
32
|
|
|
// process data (ex: UTC to JST) |
33
|
|
|
$processedData = array_map(fn($item) => $this->processDataBeforeGet($item), $data); |
|
|
|
|
34
|
|
|
|
35
|
|
|
return $processedData; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Applies all filters and conditions to the query builder. |
40
|
|
|
* |
41
|
|
|
* @param string $context context text. |
42
|
|
|
* @param array $queryParams The query parameters. |
43
|
|
|
* @return Builder The modified query. |
44
|
|
|
*/ |
45
|
|
|
private function applyFiltersToQuery( |
46
|
|
|
string $context = '', |
47
|
|
|
array $queryParams = [] |
48
|
|
|
): Builder { |
49
|
|
|
$query = $this->getQuery(); |
|
|
|
|
50
|
|
|
$query = $this->buildSearchConditions($query, $queryParams['s'] ?? ''); |
|
|
|
|
51
|
|
|
$query = $this->getAdditionalConditions($query, $context); |
|
|
|
|
52
|
|
|
$query = $this->applySorting($query, $queryParams); |
53
|
|
|
$query = $this->applyPostTypeFilter($query); |
54
|
|
|
return $query; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Applies sorting conditions to the query. |
59
|
|
|
* |
60
|
|
|
* @param Builder $query The query builder instance. |
61
|
|
|
* @param array $queryParams The query parameters containing sorting details. |
62
|
|
|
* @return Builder The modified query. |
63
|
|
|
*/ |
64
|
|
|
private function applySorting(Builder $query, array $queryParams): Builder |
65
|
|
|
{ |
66
|
|
|
if (!empty($queryParams['orderby']) && !empty($queryParams['order'])) { |
67
|
|
|
$validColumns = array_keys($this->getFields()); |
|
|
|
|
68
|
|
|
$column = in_array($queryParams['orderby'], $validColumns, true) ? |
69
|
|
|
$queryParams['orderby'] : |
70
|
|
|
'id'; |
71
|
|
|
$direction = strtoupper($queryParams['order']) === 'DESC' ? 'DESC' : 'ASC'; |
72
|
|
|
return $query->orderBy($column, $direction); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $query->orderBy('id', 'DESC'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Applies the post_type filter to the query. |
80
|
|
|
* |
81
|
|
|
* @param Builder $query The query builder instance. |
82
|
|
|
* @return Builder The modified query. |
83
|
|
|
*/ |
84
|
|
|
private function applyPostTypeFilter(Builder $query): Builder |
85
|
|
|
{ |
86
|
|
|
if (!empty($this->getPostType())) { |
|
|
|
|
87
|
|
|
return $query->where('post_type', '=', $this->getPostType()); |
88
|
|
|
} |
89
|
|
|
return $query; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getPagination(): Pagination |
93
|
|
|
{ |
94
|
|
|
return $this->pagination; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|