-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModule.php
More file actions
193 lines (165 loc) · 5.52 KB
/
Copy pathModule.php
File metadata and controls
193 lines (165 loc) · 5.52 KB
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
<?php
namespace app\modules\sermonaudio;
use humhub\modules\content\components\ContentContainerModule;
use humhub\modules\space\models\Space;
use yii\helpers\Url;
use Yii;
class Module extends ContentContainerModule
{
/**
* @inheritdoc
*/
public function getContentContainerTypes()
{
return [
Space::class,
];
}
/**
* @inheritdoc
*/
public function getContentContainerName(\humhub\modules\content\components\ContentContainerActiveRecord $container)
{
return Yii::t('SermonaudioModule.base', 'SermonAudio');
}
/**
* @inheritdoc
*/
public function getContentContainerDescription(\humhub\modules\content\components\ContentContainerActiveRecord $container)
{
return Yii::t('SermonaudioModule.base', 'Automatically posts new sermons from SermonAudio RSS feeds');
}
/**
* @inheritdoc
* No global config - this is a space-level module
*/
public function getConfigUrl()
{
return Url::to(['/sermonaudio/admin']);
}
public function hasApiKey(): bool
{
return (bool) $this->settings->get('apiKey');
}
public function getApiEnabled(): bool
{
return (bool) $this->settings->get('apiEnabled');
}
public function setApiEnabled(bool $enabled): void
{
$this->settings->set('apiEnabled', $enabled ? 1 : 0);
}
public function setApiKey(string $apiKey): void
{
$encryptionKey = $this->getApiEncryptionKey();
$encrypted = Yii::$app->security->encryptByKey($apiKey, $encryptionKey);
$this->settings->set('apiKey', base64_encode($encrypted));
}
public function getApiKey(): ?string
{
$encrypted = $this->settings->get('apiKey');
if (empty($encrypted)) {
return null;
}
$encryptionKey = $this->getApiEncryptionKey();
$decoded = base64_decode($encrypted, true);
if ($decoded === false) {
return null;
}
return Yii::$app->security->decryptByKey($decoded, $encryptionKey);
}
protected function getApiEncryptionKey(): string
{
$key = $this->settings->get('apiEncryptionKey');
if (empty($key)) {
$key = Yii::$app->security->generateRandomString(32);
$this->settings->set('apiEncryptionKey', $key);
}
return $key;
}
/**
* Check if debug mode is enabled (shorter intervals for testing)
*/
public function isDebugModeEnabled(): bool
{
return (bool) $this->settings->get('debugMode');
}
/**
* Set debug mode state
*/
public function setDebugMode(bool $enabled): void
{
$this->settings->set('debugMode', $enabled ? 1 : 0);
}
/**
* Clean up orphaned content records (posts deleted in UI but content record remains)
* This prevents false "already posted" detection
*/
public function cleanupOrphanedContent(): int
{
$deleted = Yii::$app->db->createCommand()->delete(
'{{%content}}',
['and',
['object_model' => 'humhub\modules\post\models\Post'],
['not in', 'object_id', (new \yii\db\Query())->select('id')->from('{{%post}}')]
]
)->execute();
if ($deleted > 0) {
Yii::info("Cleaned up {$deleted} orphaned content records", 'sermonaudio');
}
return $deleted;
}
/**
* Clean up soft-deleted sermon posts
* Permanently hard-deletes posts that were created by this module and are in STATE_DELETED state
* Only deletes posts older than the retention period (default 30 days)
*/
public function cleanupSoftDeletedSermonPosts(): int
{
$retentionDays = (int) $this->settings->get('softDeleteRetentionDays', 30);
$cutoffDate = date('Y-m-d H:i:s', time() - ($retentionDays * 86400));
// Find soft-deleted sermon posts older than retention period
$softDeletedPosts = (new \yii\db\Query())
->select('p.id')
->from('{{%post}} p')
->innerJoin('{{%content}} c', 'c.object_id = p.id AND c.object_model = :model', [':model' => 'humhub\modules\post\models\Post'])
->where(['c.state' => \humhub\modules\content\models\Content::STATE_DELETED])
->andWhere(['<', 'c.updated_at', $cutoffDate])
->column();
if (empty($softDeletedPosts)) {
return 0;
}
$deletedCount = 0;
// Hard delete each post
foreach ($softDeletedPosts as $postId) {
try {
$post = \humhub\modules\post\models\Post::findOne($postId);
if ($post) {
$post->hardDelete();
$deletedCount++;
}
} catch (\Exception $e) {
Yii::warning("Failed to hard-delete sermon post {$postId}: " . $e->getMessage(), 'sermonaudio');
}
}
if ($deletedCount > 0) {
Yii::info("Cleaned up {$deletedCount} soft-deleted sermon posts (retention: {$retentionDays} days)", 'sermonaudio');
}
return $deletedCount;
}
/**
* @inheritdoc
* This is where space-level configuration happens
*/
public function getContentContainerConfigUrl(\humhub\modules\content\components\ContentContainerActiveRecord $container)
{
return $container->createUrl('/sermonaudio/config-container');
}
/**
* @inheritdoc
*/
public function disable()
{
parent::disable();
}
}