-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModerationQuery.php
96 lines (86 loc) · 2.34 KB
/
ModerationQuery.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
<?php
namespace yii2mod\moderation;
use Yii;
use yii\db\ActiveQuery;
use yii2mod\moderation\enums\Status;
/**
* ModerationQuery adds the ability of getting only approved, rejected, postponed or pending models.
*
* This class provides the following methods:
*
* ```php
* Post::find()->approved()->all() // It will return all Approved Posts
*
* Post::find()->rejected()->all() // It will return all Rejected Posts
*
* Post::find()->postponed()->all() // It will return all Postponed Posts
*
* Post::find()->pending()->all() // It will return all Pending Posts
*
* Post::find()->approvedWithPending()->all() // It will return all Approved and Pending Posts
* ```
*
* @author Igor Chepurnoy <[email protected]>
*
* @since 1.0
*/
class ModerationQuery extends ActiveQuery
{
/**
* @var string status attribute
*/
protected $statusAttribute;
/**
* @inheritdoc
*/
public function init(): void
{
parent::init();
$this->statusAttribute = Yii::$container->get($this->modelClass)->statusAttribute;
}
/**
* Get a new active query object that includes approved resources.
*
* @return ActiveQuery
*/
public function approved(): ActiveQuery
{
return $this->andWhere([$this->statusAttribute => Status::APPROVED]);
}
/**
* Get a new active query object that includes rejected resources.
*
* @return ActiveQuery
*/
public function rejected(): ActiveQuery
{
return $this->andWhere([$this->statusAttribute => Status::REJECTED]);
}
/**
* Get a new active query object that includes postponed resources.
*
* @return ActiveQuery
*/
public function postponed(): ActiveQuery
{
return $this->andWhere([$this->statusAttribute => Status::POSTPONED]);
}
/**
* Get a new active query object that includes pending resources.
*
* @return ActiveQuery
*/
public function pending(): ActiveQuery
{
return $this->andWhere([$this->statusAttribute => Status::PENDING]);
}
/**
* Get a new active query object that includes approved and pending resources.
*
* @return ActiveQuery
*/
public function approvedWithPending(): ActiveQuery
{
return $this->andWhere([$this->statusAttribute => [Status::APPROVED, Status::PENDING]]);
}
}