|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mmikkel\cacheflag\services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Component; |
|
7
|
|
|
use craft\db\Query; |
|
8
|
|
|
use craft\db\Table; |
|
9
|
|
|
use craft\events\ConfigEvent; |
|
10
|
|
|
use craft\events\RebuildConfigEvent; |
|
11
|
|
|
use craft\helpers\Db; |
|
12
|
|
|
|
|
13
|
|
|
use mmikkel\cacheflag\records\Flags; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ProjectConfig |
|
17
|
|
|
* @package mmikkel\cacheflag\services |
|
18
|
|
|
* @since 1.2.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class ProjectConfig extends Component |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ConfigEvent $event |
|
25
|
|
|
* @throws \yii\db\Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function onProjectConfigChange(ConfigEvent $event) |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
$uid = $event->tokenMatches[0]; |
|
31
|
|
|
|
|
32
|
|
|
$id = (new Query()) |
|
33
|
|
|
->select(['id']) |
|
34
|
|
|
->from(Flags::tableName()) |
|
35
|
|
|
->where(['uid' => $uid]) |
|
36
|
|
|
->scalar(); |
|
37
|
|
|
|
|
38
|
|
|
$isNew = empty($id); |
|
39
|
|
|
|
|
40
|
|
|
if ($isNew) { |
|
41
|
|
|
|
|
42
|
|
|
$source = \explode(':', $event->newValue['source']); |
|
43
|
|
|
$sourceKey = $source[0] ?? null; |
|
44
|
|
|
$sourceValue = $source[1] ?? null; |
|
45
|
|
|
|
|
46
|
|
|
if (!$sourceKey || !$sourceValue) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
switch ($sourceKey) { |
|
51
|
|
|
case 'section': |
|
52
|
|
|
$column = 'sectionId'; |
|
53
|
|
|
$value = (int)Db::idByUid(Table::SECTIONS, $sourceValue); |
|
54
|
|
|
break; |
|
55
|
|
|
case 'categoryGroup': |
|
56
|
|
|
$column = 'categoryGroupId'; |
|
57
|
|
|
$value = (int)Db::idByUid(Table::CATEGORYGROUPS, $sourceValue); |
|
58
|
|
|
break; |
|
59
|
|
|
case 'tagGroup': |
|
60
|
|
|
$column = 'tagGroupId'; |
|
61
|
|
|
$value = (int)Db::idByUid(Table::TAGGROUPS, $sourceValue); |
|
62
|
|
|
break; |
|
63
|
|
|
case 'userGroup': |
|
64
|
|
|
$column = 'userGroupId'; |
|
65
|
|
|
$value = (int)Db::idByUid(Table::USERGROUPS, $sourceValue); |
|
66
|
|
|
break; |
|
67
|
|
|
case 'volume': |
|
68
|
|
|
$column = 'volumeId'; |
|
69
|
|
|
$value = (int)Db::idByUid(Table::VOLUMES, $sourceValue); |
|
70
|
|
|
break; |
|
71
|
|
|
case 'globalSet': |
|
72
|
|
|
$column = 'globalSetId'; |
|
73
|
|
|
$value = (int)Db::idByUid(Table::GLOBALSETS, $sourceValue); |
|
74
|
|
|
break; |
|
75
|
|
|
case 'elementType': |
|
76
|
|
|
$column = 'elementType'; |
|
77
|
|
|
$value = $sourceValue; |
|
78
|
|
|
break; |
|
79
|
|
|
default: |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$flags = $event->newValue['flags']; |
|
84
|
|
|
|
|
85
|
|
|
Craft::$app->db->createCommand() |
|
86
|
|
|
->insert(Flags::tableName(), [ |
|
87
|
|
|
'flags' => $flags, |
|
88
|
|
|
$column => $value, |
|
89
|
|
|
'uid' => $uid, |
|
90
|
|
|
]) |
|
91
|
|
|
->execute(); |
|
92
|
|
|
|
|
93
|
|
|
} else { |
|
94
|
|
|
|
|
95
|
|
|
Craft::$app->db->createCommand() |
|
96
|
|
|
->update(Flags::tableName(), [ |
|
97
|
|
|
'flags' => $event->newValue['flags'], |
|
98
|
|
|
], ['id' => $id]) |
|
99
|
|
|
->execute(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param ConfigEvent $event |
|
106
|
|
|
* @throws \yii\db\Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function onProjectConfigDelete(ConfigEvent $event) |
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
$uid = $event->tokenMatches[0]; |
|
112
|
|
|
|
|
113
|
|
|
$id = (new Query()) |
|
114
|
|
|
->select(['id']) |
|
115
|
|
|
->from(Flags::tableName()) |
|
116
|
|
|
->where(['uid' => $uid]) |
|
117
|
|
|
->scalar(); |
|
118
|
|
|
|
|
119
|
|
|
if (!$id) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
Craft::$app->db->createCommand() |
|
124
|
|
|
->delete(Flags::tableName(), ['id' => $id]) |
|
125
|
|
|
->execute(); |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param RebuildConfigEvent $event |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public function onProjectConfigRebuild(RebuildConfigEvent $event) |
|
134
|
|
|
{ |
|
135
|
|
|
|
|
136
|
|
|
Craft::$app->getProjectConfig()->remove('cacheFlags'); |
|
137
|
|
|
|
|
138
|
|
|
$rows = (new Query()) |
|
139
|
|
|
->select(['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'volumeId', 'globalSetId', 'elementType', 'uid']) |
|
140
|
|
|
->from(Flags::tableName()) |
|
141
|
|
|
->all(); |
|
142
|
|
|
|
|
143
|
|
|
foreach ($rows as $row) { |
|
144
|
|
|
|
|
145
|
|
|
$sourceKey = null; |
|
146
|
|
|
$sourceValue = null; |
|
147
|
|
|
|
|
148
|
|
|
if ($row['sectionId']) { |
|
149
|
|
|
$sourceKey = 'section'; |
|
150
|
|
|
$sourceValue = Db::uidById(Table::SECTIONS, $row['sectionId']); |
|
151
|
|
|
} else if ($row['categoryGroupId']) { |
|
152
|
|
|
$sourceKey = 'categoryGroup'; |
|
153
|
|
|
$sourceValue = Db::uidById(Table::CATEGORYGROUPS, $row['categoryGroupId']); |
|
154
|
|
|
} else if ($row['tagGroupId']) { |
|
155
|
|
|
$sourceKey = 'tagGroup'; |
|
156
|
|
|
$sourceValue = Db::uidById(Table::TAGGROUPS, $row['tagGroupId']); |
|
157
|
|
|
} else if ($row['userGroupId']) { |
|
158
|
|
|
$sourceKey = 'userGroup'; |
|
159
|
|
|
$sourceValue = Db::uidById(Table::USERGROUPS, $row['userGroupId']); |
|
160
|
|
|
} else if ($row['volumeId']) { |
|
161
|
|
|
$sourceKey = 'volume'; |
|
162
|
|
|
$sourceValue = Db::uidById(Table::VOLUMES, $row['volumeId']); |
|
163
|
|
|
} else if ($row['globalSetId']) { |
|
164
|
|
|
$sourceKey = 'globalSet'; |
|
165
|
|
|
$sourceValue = Db::uidById(Table::GLOBALSETS, $row['globalSetId']); |
|
166
|
|
|
} else if ($row['elementType']) { |
|
167
|
|
|
$sourceKey = 'elementType'; |
|
168
|
|
|
$sourceValue = $row['elementType']; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (!$sourceKey || !$sourceValue) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$event->config['cacheFlags'][$row['uid']] = [ |
|
176
|
|
|
'source' => "$sourceKey:$sourceValue", |
|
177
|
|
|
'flags' => $row['flags'], |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|