-
Notifications
You must be signed in to change notification settings - Fork 611
/
Announcements.php
142 lines (132 loc) · 4.57 KB
/
Announcements.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
136
137
138
139
140
141
142
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\base\PluginInterface;
use craft\db\Query;
use craft\db\Table;
use craft\helpers\Db;
use craft\helpers\Html;
use craft\helpers\Queue;
use craft\i18n\Translation;
use craft\queue\jobs\Announcement;
use DateTime;
use yii\base\Component;
use yii\helpers\Markdown;
/**
* Announcements service.
*
* An instance of the service is available via [[\craft\base\ApplicationTrait::getAnnouncements()|`Craft::$app->announcements`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.7.0
*/
class Announcements extends Component
{
/**
* Pushes a new announcement out to all control panel users.
*
* ::: tip
* Run the heading and body through [[\craft\i18n\Translation::prep()]] rather than [[\yii\BaseYii::t()|Craft::t()]]
* so they can be lazy-translated for users’ preferred languages rather that the current app language.
* :::
*
* @param string $heading The announcement heading.
* @param string $body The announcement body.
* @param string|null $pluginHandle The plugin handle, if this announcement belongs to a plugin
* @param bool $adminsOnly Whether only admin users should receive the announcement
*/
public function push(string $heading, string $body, ?string $pluginHandle = null, bool $adminsOnly = false): void
{
Queue::push(new Announcement([
'heading' => $heading,
'body' => $body,
'pluginHandle' => $pluginHandle,
'adminsOnly' => $adminsOnly,
]));
}
/**
* Returns any announcements for the logged-in user.
*
* @return array
* @since 3.7.0
*/
public function get(): array
{
$userId = Craft::$app->getUser()->getId();
if (!$userId) {
return [];
}
$query = (new Query())
->select(['a.id', 'a.heading', 'a.body', 'a.unread'])
->from(['a' => Table::ANNOUNCEMENTS])
->orderBy(['a.dateCreated' => SORT_DESC])
->where(['userId' => $userId])
->andWhere([
'or',
['a.unread' => true],
['>', 'a.dateRead', Db::prepareDateForDb(new DateTime('7 days ago'))],
]);
// Any enabled plugins?
$pluginsService = Craft::$app->getPlugins();
$enabledPluginHandles = array_map(
fn(PluginInterface $plugin) => $plugin->getHandle(),
$pluginsService->getAllPlugins(),
);
if (!empty($enabledPluginHandles)) {
$query
->addSelect(['pluginHandle' => 'p.handle'])
->leftJoin(['p' => Table::PLUGINS], '[[p.id]] = [[a.pluginId]]')
->andWhere([
'or',
['p.id' => null],
['p.handle' => $enabledPluginHandles],
]);
} else {
$query->andWhere(['a.pluginId' => null]);
}
return array_map(function(array $result) use ($pluginsService) {
$plugin = !empty($result['pluginHandle']) ? $pluginsService->getPlugin($result['pluginHandle']) : null;
if ($plugin) {
$icon = $pluginsService->getPluginIconSvg($plugin->getHandle());
$label = $plugin->name;
} else {
$icon = file_get_contents(Craft::getAlias('@appicons/craft-cms.svg'));
$label = 'Craft CMS';
}
return [
'id' => (int)$result['id'],
'icon' => $icon,
'label' => $label,
'heading' => Html::widont(Html::encode(Translation::translate($result['heading']))),
'body' => Html::widont(Markdown::processParagraph(Html::encode(Translation::translate($result['body'])))),
'unread' => (bool)$result['unread'],
];
}, $query->all());
}
/**
* Marks the user’s announcements as read.
*
* @param int[] $ids
*/
public function markAsRead(array $ids): void
{
if (empty($ids)) {
return;
}
$userId = Craft::$app->getUser()->getId();
if (!$userId) {
return;
}
Craft::$app->getDb()->createCommand()
->update(Table::ANNOUNCEMENTS, [
'unread' => false,
'dateRead' => Db::prepareDateForDb(new DateTime()),
], ['id' => $ids, 'userId' => $userId])
->execute();
}
}