-
Notifications
You must be signed in to change notification settings - Fork 646
/
Copy pathEntryQuery.php
1024 lines (960 loc) · 29.6 KB
/
EntryQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\elements\db;
use Craft;
use craft\db\Query;
use craft\db\QueryAbortedException;
use craft\db\Table;
use craft\elements\Entry;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use craft\helpers\StringHelper;
use craft\models\EntryType;
use craft\models\Section;
use craft\models\UserGroup;
use yii\base\InvalidConfigException;
use yii\db\Connection;
/**
* EntryQuery represents a SELECT SQL statement for entries in a way that is independent of DBMS.
*
* @property string|string[]|Section $section The handle(s) of the section(s) that resulting entries must belong to.
* @property string|string[]|EntryType $type The handle(s) of the entry type(s) that resulting entries must have.
* @property string|string[]|UserGroup $authorGroup The handle(s) of the user group(s) that resulting entries’ authors must belong to.
* @method Entry[]|array all($db = null)
* @method Entry|array|null one($db = null)
* @method Entry|array|null nth(int $n, Connection $db = null)
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
* @supports-structure-params
* @supports-site-params
* @supports-enabledforsite-param
* @supports-title-param
* @supports-slug-param
* @supports-status-param
* @supports-uri-param
* @supports-draft-params
* @supports-revision-params
* @replace {element} entry
* @replace {elements} entries
* @replace {twig-method} craft.entries()
* @replace {myElement} myEntry
* @replace {element-class} \craft\elements\Entry
*/
class EntryQuery extends ElementQuery
{
// General parameters
// -------------------------------------------------------------------------
/**
* @var bool Whether to only return entries that the user has permission to edit.
* @used-by editable()
*/
public $editable = false;
/**
* @var int|int[]|null The section ID(s) that the resulting entries must be in.
* ---
* ```php
* // fetch entries in the News section
* $entries = \craft\elements\Entry::find()
* ->section('news')
* ->all();
* ```
* ```twig
* {# fetch entries in the News section #}
* {% set entries = craft.entries()
* .section('news')
* .all() %}
* ```
* @used-by section()
* @used-by sectionId()
*/
public $sectionId;
/**
* @var int|int[]|null The entry type ID(s) that the resulting entries must have.
* ---
* ```php{4}
* // fetch Article entries in the News section
* $entries = \craft\elements\Entry::find()
* ->section('news')
* ->type('article')
* ->all();
* ```
* ```twig{4}
* {# fetch entries in the News section #}
* {% set entries = craft.entries()
* .section('news')
* .type('article')
* .all() %}
* ```
* @used-by EntryQuery::type()
* @used-by typeId()
*/
public $typeId;
/**
* @var int|int[]|null The user ID(s) that the resulting entries’ authors must have.
* @used-by authorId()
*/
public $authorId;
/**
* @var int|int[]|null The user group ID(s) that the resulting entries’ authors must be in.
* ---
* ```php
* // fetch entries authored by people in the Authors group
* $entries = \craft\elements\Entry::find()
* ->authorGroup('authors')
* ->all();
* ```
* ```twig
* {# fetch entries authored by people in the Authors group #}
* {% set entries = craft.entries()
* .authorGroup('authors')
* .all() %}
* ```
* @used-by authorGroup()
* @used-by authorGroupId()
*/
public $authorGroupId;
/**
* @var mixed The Post Date that the resulting entries must have.
* ---
* ```php
* // fetch entries written in 2018
* $entries = \craft\elements\Entry::find()
* ->postDate(['and', '>= 2018-01-01', '< 2019-01-01'])
* ->all();
* ```
* ```twig
* {# fetch entries written in 2018 #}
* {% set entries = craft.entries()
* .postDate(['and', '>= 2018-01-01', '< 2019-01-01'])
* .all() %}
* ```
* @used-by postDate()
*/
public $postDate;
/**
* @var string|array|\DateTime The maximum Post Date that resulting entries can have.
* ---
* ```php
* // fetch entries written before 4/4/2018
* $entries = \craft\elements\Entry::find()
* ->before('2018-04-04')
* ->all();
* ```
* ```twig
* {# fetch entries written before 4/4/2018 #}
* {% set entries = craft.entries()
* .before('2018-04-04')
* .all() %}
* ```
* @used-by before()
*/
public $before;
/**
* @var string|array|\DateTime The minimum Post Date that resulting entries can have.
* ---
* ```php
* // fetch entries written in the last 7 days
* $entries = \craft\elements\Entry::find()
* ->after((new \DateTime())->modify('-7 days'))
* ->all();
* ```
* ```twig
* {# fetch entries written in the last 7 days #}
* {% set entries = craft.entries()
* .after(now|date_modify('-7 days'))
* .all() %}
* ```
* @used-by after()
*/
public $after;
/**
* @var mixed The Expiry Date that the resulting entries must have.
* @used-by expiryDate()
*/
public $expiryDate;
/**
* @inheritdoc
*/
protected $defaultOrderBy = ['entries.postDate' => SORT_DESC];
/**
* @inheritdoc
*/
public function __construct($elementType, array $config = [])
{
// Default status
if (!isset($config['status'])) {
$config['status'] = ['live'];
}
parent::__construct($elementType, $config);
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
switch ($name) {
case 'section':
$this->section($value);
break;
case 'type':
$this->type($value);
break;
case 'authorGroup':
$this->authorGroup($value);
break;
default:
parent::__set($name, $value);
}
}
/**
* @inheritdoc
*/
public function init()
{
if ($this->withStructure === null) {
$this->withStructure = true;
}
parent::init();
}
/**
* Sets the [[$editable]] property.
*
* @param bool $value The property value (defaults to true)
* @return static self reference
* @uses $editable
*/
public function editable(bool $value = true)
{
$this->editable = $value;
return $this;
}
/**
* Narrows the query results based on the sections the entries belong to.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'foo'` | in a section with a handle of `foo`.
* | `'not foo'` | not in a section with a handle of `foo`.
* | `['foo', 'bar']` | in a section with a handle of `foo` or `bar`.
* | `['not', 'foo', 'bar']` | not in a section with a handle of `foo` or `bar`.
* | a [[Section|Section]] object | in a section represented by the object.
*
* ---
*
* ```twig
* {# Fetch entries in the Foo section #}
* {% set {elements-var} = {twig-method}
* .section('foo')
* .all() %}
* ```
*
* ```php
* // Fetch entries in the Foo section
* ${elements-var} = {php-method}
* ->section('foo')
* ->all();
* ```
*
* @param string|string[]|Section|null $value The property value
* @return static self reference
* @uses $sectionId
*/
public function section($value)
{
// If the value is a section handle, swap it with the section
if (is_string($value) && ($section = Craft::$app->getSections()->getSectionByHandle($value))) {
$value = $section;
}
if ($value instanceof Section) {
$this->sectionId = [$value->id];
if ($value->structureId) {
$this->structureId = $value->structureId;
} else {
$this->withStructure = false;
}
} else if ($value !== null) {
$this->sectionId = (new Query())
->select(['id'])
->from([Table::SECTIONS])
->where(Db::parseParam('handle', $value))
->column();
} else {
$this->sectionId = null;
}
return $this;
}
/**
* Narrows the query results based on the sections the entries belong to, per the sections’ IDs.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `1` | in a section with an ID of 1.
* | `'not 1'` | not in a section with an ID of 1.
* | `[1, 2]` | in a section with an ID of 1 or 2.
* | `['not', 1, 2]` | not in a section with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch entries in the section with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .sectionId(1)
* .all() %}
* ```
*
* ```php
* // Fetch entries in the section with an ID of 1
* ${elements-var} = {php-method}
* ->sectionId(1)
* ->all();
* ```
*
* @param int|int[]|null $value The property value
* @return static self reference
* @uses $sectionId
*/
public function sectionId($value)
{
$this->sectionId = $value;
return $this;
}
/**
* Narrows the query results based on the entries’ entry types.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'foo'` | of a type with a handle of `foo`.
* | `'not foo'` | not of a type with a handle of `foo`.
* | `['foo', 'bar']` | of a type with a handle of `foo` or `bar`.
* | `['not', 'foo', 'bar']` | not of a type with a handle of `foo` or `bar`.
* | an [[EntryType|EntryType]] object | of a type represented by the object.
*
* ---
*
* ```twig
* {# Fetch entries in the Foo section with a Bar entry type #}
* {% set {elements-var} = {twig-method}
* .section('foo')
* .type('bar')
* .all() %}
* ```
*
* ```php
* // Fetch entries in the Foo section with a Bar entry type
* ${elements-var} = {php-method}
* ->section('foo')
* ->type('bar')
* ->all();
* ```
*
* @param string|string[]|EntryType|null $value The property value
* @return static self reference
* @uses $typeId
*/
public function type($value)
{
if ($value instanceof EntryType) {
$this->typeId = [$value->id];
} else if ($value !== null) {
$this->typeId = (new Query())
->select(['id'])
->from([Table::ENTRYTYPES])
->where(Db::parseParam('handle', $value))
->column();
} else {
$this->typeId = null;
}
return $this;
}
/**
* Narrows the query results based on the entries’ entry types, per the types’ IDs.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `1` | of a type with an ID of 1.
* | `'not 1'` | not of a type with an ID of 1.
* | `[1, 2]` | of a type with an ID of 1 or 2.
* | `['not', 1, 2]` | not of a type with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch entries of the entry type with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .typeId(1)
* .all() %}
* ```
*
* ```php
* // Fetch entries of the entry type with an ID of 1
* ${elements-var} = {php-method}
* ->typeId(1)
* ->all();
* ```
*
* @param int|int[]|null $value The property value
* @return static self reference
* @uses $typeId
*/
public function typeId($value)
{
$this->typeId = $value;
return $this;
}
/**
* Narrows the query results based on the entries’ authors.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `1` | with an author with an ID of 1.
* | `'not 1'` | not with an author with an ID of 1.
* | `[1, 2]` | with an author with an ID of 1 or 2.
* | `['not', 1, 2]` | not with an author with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch entries with an author with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .authorId(1)
* .all() %}
* ```
*
* ```php
* // Fetch entries with an author with an ID of 1
* ${elements-var} = {php-method}
* ->authorId(1)
* ->all();
* ```
*
* @param int|int[]|null $value The property value
* @return static self reference
* @uses $authorId
*/
public function authorId($value)
{
$this->authorId = $value;
return $this;
}
/**
* Narrows the query results based on the user group the entries’ authors belong to.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'foo'` | with an author in a group with a handle of `foo`.
* | `'not foo'` | not with an author in a group with a handle of `foo`.
* | `['foo', 'bar']` | with an author in a group with a handle of `foo` or `bar`.
* | `['not', 'foo', 'bar']` | not with an author in a group with a handle of `foo` or `bar`.
* | a [[UserGroup|UserGroup]] object | with an author in a group represented by the object.
*
* ---
*
* ```twig
* {# Fetch entries with an author in the Foo user group #}
* {% set {elements-var} = {twig-method}
* .authorGroup('foo')
* .all() %}
* ```
*
* ```php
* // Fetch entries with an author in the Foo user group
* ${elements-var} = {php-method}
* ->authorGroup('foo')
* ->all();
* ```
*
* @param string|string[]|UserGroup|null $value The property value
* @return static self reference
* @uses $authorGroupId
*/
public function authorGroup($value)
{
if ($value instanceof UserGroup) {
$this->authorGroupId = $value->id;
} else if ($value !== null) {
$this->authorGroupId = (new Query())
->select(['id'])
->from([Table::USERGROUPS])
->where(Db::parseParam('handle', $value))
->column();
} else {
$this->authorGroupId = null;
}
return $this;
}
/**
* Narrows the query results based on the user group the entries’ authors belong to, per the groups’ IDs.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `1` | with an author in a group with an ID of 1.
* | `'not 1'` | not with an author in a group with an ID of 1.
* | `[1, 2]` | with an author in a group with an ID of 1 or 2.
* | `['not', 1, 2]` | not with an author in a group with an ID of 1 or 2.
*
* ---
*
* ```twig
* {# Fetch entries with an author in a group with an ID of 1 #}
* {% set {elements-var} = {twig-method}
* .authorGroupId(1)
* .all() %}
* ```
*
* ```php
* // Fetch entries with an author in a group with an ID of 1
* ${elements-var} = {php-method}
* ->authorGroupId(1)
* ->all();
* ```
*
* @param int|int[]|null $value The property value
* @return static self reference
* @uses $authorGroupId
*/
public function authorGroupId($value)
{
$this->authorGroupId = $value;
return $this;
}
/**
* Narrows the query results based on the entries’ post dates.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'>= 2018-04-01'` | that were posted on or after 2018-04-01.
* | `'< 2018-05-01'` | that were posted before 2018-05-01
* | `['and', '>= 2018-04-04', '< 2018-05-01']` | that were posted between 2018-04-01 and 2018-05-01.
*
* ---
*
* ```twig
* {# Fetch entries posted last month #}
* {% set start = date('first day of last month')|atom %}
* {% set end = date('first day of this month')|atom %}
*
* {% set {elements-var} = {twig-method}
* .postDate(['and', ">= #{start}", "< #{end}"])
* .all() %}
* ```
*
* ```php
* // Fetch entries posted last month
* $start = (new \DateTime('first day of last month'))->format(\DateTime::ATOM);
* $end = (new \DateTime('first day of this month'))->format(\DateTime::ATOM);
*
* ${elements-var} = {php-method}
* ->postDate(['and', ">= {$start}", "< {$end}"])
* ->all();
* ```
*
* @param mixed $value The property value
* @return static self reference
* @uses $postDate
*/
public function postDate($value)
{
$this->postDate = $value;
return $this;
}
/**
* Narrows the query results to only entries that were posted before a certain date.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'2018-04-01'` | that were posted before 2018-04-01.
* | a [[\DateTime|DateTime]] object | that were posted before the date represented by the object.
*
* ---
*
* ```twig
* {# Fetch entries posted before this month #}
* {% set firstDayOfMonth = date('first day of this month') %}
*
* {% set {elements-var} = {twig-method}
* .before(firstDayOfMonth)
* .all() %}
* ```
*
* ```php
* // Fetch entries posted before this month
* $firstDayOfMonth = new \DateTime('first day of this month');
*
* ${elements-var} = {php-method}
* ->before($firstDayOfMonth)
* ->all();
* ```
*
* @param string|\DateTime $value The property value
* @return static self reference
* @uses $before
*/
public function before($value)
{
$this->before = $value;
return $this;
}
/**
* Narrows the query results to only entries that were posted on or after a certain date.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'2018-04-01'` | that were posted after 2018-04-01.
* | a [[\DateTime|DateTime]] object | that were posted after the date represented by the object.
*
* ---
*
* ```twig
* {# Fetch entries posted this month #}
* {% set firstDayOfMonth = date('first day of this month') %}
*
* {% set {elements-var} = {twig-method}
* .after(firstDayOfMonth)
* .all() %}
* ```
*
* ```php
* // Fetch entries posted this month
* $firstDayOfMonth = new \DateTime('first day of this month');
*
* ${elements-var} = {php-method}
* ->after($firstDayOfMonth)
* ->all();
* ```
*
* @param string|\DateTime $value The property value
* @return static self reference
* @uses $after
*/
public function after($value)
{
$this->after = $value;
return $this;
}
/**
* Narrows the query results based on the entries’ expiry dates.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `':empty:'` | that don’t have an expiry date.
* | `':notempty:'` | that have an expiry date.
* | `'>= 2020-04-01'` | that will expire on or after 2020-04-01.
* | `'< 2020-05-01'` | that will expire before 2020-05-01
* | `['and', '>= 2020-04-04', '< 2020-05-01']` | that will expire between 2020-04-01 and 2020-05-01.
*
* ---
*
* ```twig
* {# Fetch entries expiring this month #}
* {% set nextMonth = date('first day of next month')|atom %}
*
* {% set {elements-var} = {twig-method}
* .expiryDate("< #{nextMonth}")
* .all() %}
* ```
*
* ```php
* // Fetch entries expiring this month
* $nextMonth = (new \DateTime('first day of next month'))->format(\DateTime::ATOM);
*
* ${elements-var} = {php-method}
* ->expiryDate("< {$nextMonth}")
* ->all();
* ```
*
* @param mixed $value The property value
* @return static self reference
* @uses $expiryDate
*/
public function expiryDate($value)
{
$this->expiryDate = $value;
return $this;
}
/**
* Narrows the query results based on the entries’ statuses.
*
* Possible values include:
*
* | Value | Fetches entries…
* | - | -
* | `'live'` _(default)_ | that are live.
* | `'pending'` | that are pending (enabled with a Post Date in the future).
* | `'expired'` | that are expired (enabled with an Expiry Date in the past).
* | `'disabled'` | that are disabled.
* | `['live', 'pending']` | that are live or pending.
*
* ---
*
* ```twig
* {# Fetch disabled entries #}
* {% set {elements-var} = {twig-method}
* .status('disabled')
* .all() %}
* ```
*
* ```php
* // Fetch disabled entries
* ${elements-var} = {element-class}::find()
* ->status('disabled')
* ->all();
* ```
*/
public function status($value)
{
return parent::status($value);
}
/**
* @inheritdoc
*/
protected function beforePrepare(): bool
{
// See if 'section', 'type', or 'authorGroup' were set to invalid handles
if ($this->sectionId === [] || $this->typeId === [] || $this->authorGroupId === []) {
return false;
}
$this->joinElementTable('entries');
$this->query->select([
'entries.sectionId',
'entries.typeId',
'entries.authorId',
'entries.postDate',
'entries.expiryDate',
]);
if ($this->postDate) {
$this->subQuery->andWhere(Db::parseDateParam('entries.postDate', $this->postDate));
} else {
if ($this->before) {
$this->subQuery->andWhere(Db::parseDateParam('entries.postDate', $this->before, '<'));
}
if ($this->after) {
$this->subQuery->andWhere(Db::parseDateParam('entries.postDate', $this->after, '>='));
}
}
if ($this->expiryDate) {
$this->subQuery->andWhere(Db::parseDateParam('entries.expiryDate', $this->expiryDate));
}
$this->_normalizeTypeId();
if ($this->typeId) {
$this->subQuery->andWhere(['entries.typeId' => $this->typeId]);
}
if (Craft::$app->getEdition() === Craft::Pro) {
if ($this->authorId) {
$this->subQuery->andWhere(Db::parseParam('entries.authorId', $this->authorId));
}
if ($this->authorGroupId) {
$this->subQuery
->innerJoin(['usergroups_users' => Table::USERGROUPS_USERS], '[[usergroups_users.userId]] = [[entries.authorId]]')
->andWhere(Db::parseParam('usergroups_users.groupId', $this->authorGroupId));
}
}
$this->_applyEditableParam();
$this->_applySectionIdParam();
$this->_applyRefParam();
return parent::beforePrepare();
}
/**
* @inheritdoc
*/
protected function statusCondition(string $status)
{
$currentTimeDb = Db::prepareDateForDb(new \DateTime(), true);
switch ($status) {
case Entry::STATUS_LIVE:
return [
'and',
[
'elements.enabled' => true,
'elements_sites.enabled' => true
],
['<=', 'entries.postDate', $currentTimeDb],
[
'or',
['entries.expiryDate' => null],
['>', 'entries.expiryDate', $currentTimeDb]
]
];
case Entry::STATUS_PENDING:
return [
'and',
[
'elements.enabled' => true,
'elements_sites.enabled' => true,
],
['>', 'entries.postDate', $currentTimeDb]
];
case Entry::STATUS_EXPIRED:
return [
'and',
[
'elements.enabled' => true,
'elements_sites.enabled' => true
],
['not', ['entries.expiryDate' => null]],
['<=', 'entries.expiryDate', $currentTimeDb]
];
default:
return parent::statusCondition($status);
}
}
/**
* Applies the 'editable' param to the query being prepared.
*
* @throws QueryAbortedException
*/
private function _applyEditableParam()
{
if (!$this->editable) {
return;
}
$user = Craft::$app->getUser()->getIdentity();
if (!$user) {
throw new QueryAbortedException();
}
// Limit the query to only the sections the user has permission to edit
$this->subQuery->andWhere([
'entries.sectionId' => Craft::$app->getSections()->getEditableSectionIds()
]);
// Enforce the editPeerEntries permissions for non-Single sections
foreach (Craft::$app->getSections()->getEditableSections() as $section) {
if ($section->type != Section::TYPE_SINGLE && !$user->can('editPeerEntries:' . $section->uid)) {
$this->subQuery->andWhere([
'or',
['not', ['entries.sectionId' => $section->id]],
['entries.authorId' => $user->id]
]);
}
}
}
/**
* Normalizes the typeId param to an array of IDs or null
*
* @throws InvalidConfigException
*/
private function _normalizeTypeId()
{
if (empty($this->typeId)) {
$this->typeId = null;
} else if (is_numeric($this->typeId)) {
$this->typeId = [$this->typeId];
} else if (!is_array($this->typeId) || !ArrayHelper::isNumeric($this->typeId)) {
throw new InvalidConfigException('Invalid typeId param value');
}
}
/**
* Applies the 'sectionId' param to the query being prepared.
*/
private function _applySectionIdParam()
{
$this->_normalizeSectionId();
if ($this->sectionId) {
$this->subQuery->andWhere(['entries.sectionId' => $this->sectionId]);
// Should we set the structureId param?
if ($this->structureId === null && count($this->sectionId) === 1) {
$structureId = (new Query())
->select(['structureId'])
->from([Table::SECTIONS])
->where(Db::parseParam('id', $this->sectionId))
->andWhere(['type' => Section::TYPE_STRUCTURE])
->scalar();
$this->structureId = (int)$structureId ?: false;
}
}
}
/**
* Normalizes the groupId param to an array of IDs or null
*/
private function _normalizeSectionId()
{
if (empty($this->sectionId)) {
$this->sectionId = null;
} else if (is_numeric($this->sectionId)) {
$this->sectionId = [$this->sectionId];
} else if (!is_array($this->sectionId) || !ArrayHelper::isNumeric($this->sectionId)) {
$this->sectionId = (new Query())
->select(['id'])
->from([Table::SECTIONS])
->where(Db::parseParam('id', $this->sectionId))
->column();
}
}
/**
* Applies the 'ref' param to the query being prepared.
*/
private function _applyRefParam()
{
if (!$this->ref) {
return;
}
$refs = $this->ref;
if (!is_array($refs)) {
$refs = is_string($refs) ? StringHelper::split($refs) : [$refs];
}
$joinSections = false;
$condition = ['or'];
foreach ($refs as $ref) {
$parts = array_filter(explode('/', $ref));
if (!empty($parts)) {
if (count($parts) == 1) {
$condition[] = Db::parseParam('elements_sites.slug', $parts[0]);
} else {
$condition[] = [
'and',
Db::parseParam('sections.handle', $parts[0]),
Db::parseParam('elements_sites.slug', $parts[1])
];
$joinSections = true;
}
}
}
$this->subQuery->andWhere($condition);
if ($joinSections) {