2
2
3
3
namespace Statamic \Eloquent \Entries ;
4
4
5
+ use Illuminate \Support \Collection as IlluminateCollection ;
5
6
use Illuminate \Support \Str ;
6
7
use Statamic \Contracts \Entries \QueryBuilder ;
7
8
use Statamic \Eloquent \Entries \Entry as EloquentEntry ;
9
+ use Statamic \Eloquent \QueriesJsonColumns ;
8
10
use Statamic \Entries \EntryCollection ;
9
11
use Statamic \Facades \Blink ;
10
12
use Statamic \Facades \Collection ;
11
13
use Statamic \Facades \Entry ;
12
14
use Statamic \Facades \Taxonomy ;
15
+ use Statamic \Fields \Field ;
13
16
use Statamic \Query \EloquentQueryBuilder ;
14
17
use Statamic \Stache \Query \QueriesEntryStatus ;
15
18
use Statamic \Stache \Query \QueriesTaxonomizedEntries ;
16
19
17
20
class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder
18
21
{
19
22
use QueriesEntryStatus,
23
+ QueriesJsonColumns,
20
24
QueriesTaxonomizedEntries;
21
25
22
26
private $ selectedQueryColumns ;
@@ -28,92 +32,6 @@ class EntryQueryBuilder extends EloquentQueryBuilder implements QueryBuilder
28
32
'date ' , 'collection ' , 'created_at ' , 'updated_at ' , 'order ' , 'blueprint ' ,
29
33
];
30
34
31
- public function orderBy ($ column , $ direction = 'asc ' )
32
- {
33
- $ actualColumn = $ this ->column ($ column );
34
-
35
- if (Str::contains ($ actualColumn , 'data-> ' )) {
36
- $ wheres = collect ($ this ->builder ->getQuery ()->wheres );
37
-
38
- if ($ wheres ->where ('column ' , 'collection ' )->count () == 1 ) {
39
- $ collectionWhere = $ wheres ->firstWhere ('column ' , 'collection ' );
40
- if (isset ($ collectionWhere ['values ' ]) && count ($ collectionWhere ['values ' ]) == 1 ) {
41
- $ collectionWhere ['value ' ] = $ collectionWhere ['values ' ][0 ];
42
- }
43
-
44
- if (isset ($ collectionWhere ['value ' ])) {
45
- if ($ collection = Collection::find ($ collectionWhere ['value ' ])) {
46
- $ blueprintField = $ collection ->entryBlueprints ()
47
- ->flatMap (function ($ blueprint ) {
48
- return $ blueprint ->fields ()
49
- ->all ()
50
- ->filter (function ($ field ) {
51
- return in_array ($ field ->type (), ['float ' , 'integer ' , 'date ' ]);
52
- });
53
- })
54
- ->filter ()
55
- ->get ($ column );
56
-
57
- if ($ blueprintField ) {
58
- $ castType = '' ;
59
- $ fieldType = $ blueprintField ->type ();
60
-
61
- $ grammar = $ this ->builder ->getConnection ()->getQueryGrammar ();
62
- $ actualColumn = $ grammar ->wrap ($ actualColumn );
63
-
64
- if (in_array ($ fieldType , ['float ' ])) {
65
- $ castType = 'float ' ;
66
- } elseif (in_array ($ fieldType , ['integer ' ])) {
67
- $ castType = 'float ' ; // bit sneaky but mysql doesnt support casting as integer, it wants unsigned
68
- } elseif (in_array ($ fieldType , ['date ' ])) {
69
- // Take time into account when enabled
70
- if ($ blueprintField ->get ('time_enabled ' )) {
71
- $ castType = 'datetime ' ;
72
- } else {
73
- $ castType = 'date ' ;
74
- }
75
-
76
- // take range into account
77
- if ($ blueprintField ->get ('mode ' ) == 'range ' ) {
78
- $ actualColumnStartDate = $ grammar ->wrap ($ this ->column ($ column ).'->start ' );
79
- $ actualColumnEndDate = $ grammar ->wrap ($ this ->column ($ column ).'->end ' );
80
- if (str_contains (get_class ($ grammar ), 'SQLiteGrammar ' )) {
81
- $ this ->builder
82
- ->orderByRaw ("datetime( {$ actualColumnStartDate }) {$ direction }" )
83
- ->orderByRaw ("datetime( {$ actualColumnEndDate }) {$ direction }" );
84
- } else {
85
- $ this ->builder
86
- ->orderByRaw ("cast( {$ actualColumnStartDate } as {$ castType }) {$ direction }" )
87
- ->orderByRaw ("cast( {$ actualColumnEndDate } as {$ castType }) {$ direction }" );
88
- }
89
-
90
- return $ this ;
91
- }
92
-
93
- // sqlite casts dates to year, which is pretty unhelpful
94
- if (str_contains (get_class ($ grammar ), 'SQLiteGrammar ' )) {
95
- $ this ->builder ->orderByRaw ("datetime( {$ actualColumn }) {$ direction }" );
96
-
97
- return $ this ;
98
- }
99
- }
100
-
101
- if ($ castType ) {
102
- $ this ->builder ->orderByRaw ("cast( {$ actualColumn } as {$ castType }) {$ direction }" );
103
-
104
- return $ this ;
105
- }
106
- }
107
- }
108
- }
109
- }
110
- }
111
-
112
- parent ::orderBy ($ column , $ direction );
113
-
114
- return $ this ;
115
- }
116
-
117
35
protected function transform ($ items , $ columns = [])
118
36
{
119
37
$ items = EntryCollection::make ($ items )->map (function ($ model ) use ($ columns ) {
@@ -292,4 +210,27 @@ private function getKeysForTaxonomyWhereIn($where)
292
210
->pluck ('id ' );
293
211
});
294
212
}
213
+
214
+ protected function getJsonCasts (): IlluminateCollection
215
+ {
216
+ $ wheres = collect ($ this ->builder ->getQuery ()->wheres );
217
+ $ collectionWhere = $ wheres ->firstWhere ('column ' , 'collection ' );
218
+
219
+ if (
220
+ ! $ collectionWhere
221
+ || ! isset ($ collectionWhere ['value ' ])
222
+ || ! ($ collection = Collection::find ($ collectionWhere ['value ' ]))
223
+ ) {
224
+ return [];
225
+ }
226
+
227
+ return $ collection ->entryBlueprints ()
228
+ ->flatMap (function ($ blueprint ) {
229
+ return $ blueprint ->fields ()
230
+ ->all ()
231
+ ->filter (fn ($ field ) => in_array ($ field ->type (), ['float ' , 'integer ' , 'date ' ]))
232
+ ->map (fn (Field $ field ) => $ this ->toCast ($ field ));
233
+ })
234
+ ->filter ();
235
+ }
295
236
}
0 commit comments