-
Notifications
You must be signed in to change notification settings - Fork 11
/
Module.php
65 lines (53 loc) · 1.81 KB
/
Module.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
<?php
namespace humhub\modules\breakingnews;
use Yii;
use yii\helpers\Url;
class Module extends \humhub\components\Module
{
/**
* @return bool
*/
public static function showBreakingNews()
{
if (Yii::$app->user->isGuest) {
return false;
}
if (!Yii::$app->request->isConsoleRequest && Yii::$app->controller->layout === '@humhub/modules/user/views/layouts/main') {
return false;
}
/** @var Module $module */
$module = Yii::$app->getModule('breakingnews');
if ($module === null || !$module->settings->get('active')) {
return false;
}
if ($expires = $module->settings->get('expiresAt')) {
$now = new \DateTime('now', new \DateTimeZone('UTC'));
$expires = new \DateTime($expires, new \DateTimeZone('UTC'));
if ($expires < $now) {
return false;
}
}
// Check group restrictions
$activeGroups = $module->settings->getSerialized('activeGroups');
// If no group is ticked, everyone will see this breaking news
if ($activeGroups) {
$userGroups = array_map(static function ($group) {
return $group->id;
}, Yii::$app->user->identity->groups);
// The user must be a member of at least one of the active groups
if (!array_intersect($userGroups, $activeGroups)) {
return false;
}
}
$lastSeenTS = $module->settings->user()->get('timestamp');
$currentNewsTS = $module->settings->get('timestamp');
return $currentNewsTS != null && $lastSeenTS != $currentNewsTS;
}
/**
* @inheridoc
*/
public function getConfigUrl()
{
return Url::to(['/breakingnews/admin/index']);
}
}