/
MailingListElementQuery.php
executable file
·135 lines (112 loc) · 3.83 KB
/
MailingListElementQuery.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
<?php
/**
* @copyright Copyright (c) PutYourLightsOn
*/
namespace putyourlightson\campaign\elements\db;
use craft\db\Table;
use craft\elements\db\ElementQuery;
use craft\helpers\Db;
use putyourlightson\campaign\elements\MailingListElement;
use putyourlightson\campaign\models\MailingListTypeModel;
use putyourlightson\campaign\records\MailingListTypeRecord;
use yii\db\Connection;
/**
* @method MailingListElement[]|array all($db = null)
* @method MailingListElement|array|null one($db = null)
* @method MailingListElement|array|null nth(int $n, Connection $db = null)
*/
class MailingListElementQuery extends ElementQuery
{
/**
* @var int|int[]|null The mailing list type ID(s) that the resulting mailing lists must have.
*/
public array|int|null $mailingListTypeId = null;
/**
* @var int|int[]|null The synced user group ID(s) that the resulting mailing lists must have.
*/
public array|int|null $syncedUserGroupId = null;
/**
* @var bool Whether to only return mailing lists that have synced user groups.
*/
public bool $synced = false;
/**
* @inheritdoc
*/
public function __set($name, $value): void
{
switch ($name) {
case 'mailingListType':
$this->mailingListType($value);
break;
default:
parent::__set($name, $value);
}
}
/**
* Sets the [[mailingListType]] property.
*/
public function mailingListType(array|MailingListTypeModel|string|null $value): static
{
if ($value instanceof MailingListTypeModel) {
$this->mailingListTypeId = $value->id;
} elseif ($value !== null) {
$this->mailingListTypeId = MailingListTypeRecord::find()
->select(['id'])
->where(Db::parseParam('handle', $value))
->column();
} else {
$this->mailingListTypeId = null;
}
return $this;
}
/**
* Sets the [[mailingListTypeId]] property.
*/
public function mailingListTypeId(array|int|null $value): static
{
$this->mailingListTypeId = $value;
return $this;
}
/**
* Sets the [[syncedUserGroupId]] property.
*/
public function syncedUserGroupId(array|int|null $value): static
{
$this->syncedUserGroupId = $value;
return $this;
}
/**
* Sets the [[synced]] property.
*/
public function synced(bool $value = null): static
{
$value = $value ?? true;
$this->synced = $value;
return $this;
}
/**
* @inheritdoc
*/
protected function beforePrepare(): bool
{
$this->joinElementTable('campaign_mailinglists');
$this->query->select([
'campaign_mailinglists.mailingListTypeId',
'campaign_mailinglists.syncedUserGroupId',
]);
if ($this->mailingListTypeId) {
$this->subQuery->andWhere(Db::parseParam('campaign_mailinglists.mailingListTypeId', $this->mailingListTypeId));
}
$this->subQuery->innerJoin(MailingListTypeRecord::tableName() . ' campaign_mailinglisttypes', '[[campaign_mailinglisttypes.id]] = [[campaign_mailinglists.mailingListTypeId]]');
$this->subQuery->select(['mailingListType' => 'campaign_mailinglisttypes.name']);
$this->subQuery->innerJoin(Table::SITES . ' sites', '[[sites.id]] = [[campaign_mailinglisttypes.siteId]]');
$this->subQuery->andWhere(['[[sites.dateDeleted]]' => null]);
if ($this->syncedUserGroupId) {
$this->subQuery->andWhere(Db::parseParam('campaign_mailinglists.syncedUserGroupId', $this->syncedUserGroupId));
}
if ($this->synced === true) {
$this->subQuery->andWhere(['not', ['syncedUserGroupId' => null]]);
}
return parent::beforePrepare();
}
}