-
Notifications
You must be signed in to change notification settings - Fork 611
/
Announcement.php
115 lines (99 loc) · 2.79 KB
/
Announcement.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\queue\jobs;
use Craft;
use craft\db\Table;
use craft\elements\User;
use craft\enums\CmsEdition;
use craft\helpers\Db;
use craft\i18n\Translation;
use craft\queue\BaseJob;
use DateTime;
use yii\base\Exception;
/**
* Announcement job
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.7.0
*/
class Announcement extends BaseJob
{
/**
* @var string The announcement heading
*/
public string $heading;
/**
* @var string The announcement body
*/
public string $body;
/**
* @var string|null The plugin handle
*/
public ?string $pluginHandle = null;
/**
* @var bool Whether only admins should receive the announcement.
* @since 4.5.6
*/
public bool $adminsOnly = false;
/**
* @inheritdoc
* @throws Exception
*/
public function execute($queue): void
{
if (isset($this->pluginHandle)) {
$pluginInfo = Craft::$app->getPlugins()->getStoredPluginInfo($this->pluginHandle);
if ($pluginInfo === null) {
Craft::warning("Couldn’t push announcement because the plugin handle was invalid: $this->pluginHandle", __METHOD__);
return;
}
$pluginId = $pluginInfo['id'];
} else {
$pluginId = null;
}
// Fetch all of the control panel users
$userQuery = User::find();
if (Craft::$app->edition === CmsEdition::Pro) {
$userQuery->can('accessCp');
}
if ($this->adminsOnly) {
$userQuery->admin();
}
$totalUsers = $userQuery->count();
$batchSize = 100;
$dateCreated = Db::prepareDateForDb(new DateTime());
$db = Craft::$app->getDb();
foreach (Db::batch($userQuery, $batchSize) as $batchIndex => $batch) {
/** @var User[] $batch */
$this->setProgress($queue, ($batchIndex * $batchSize) / $totalUsers);
$rows = [];
foreach ($batch as $user) {
$rows[] = [
$user->id,
$pluginId,
$this->heading,
$this->body,
$dateCreated,
];
}
$db->createCommand()->batchInsert(Table::ANNOUNCEMENTS, [
'userId',
'pluginId',
'heading',
'body',
'dateCreated',
], $rows)->execute();
}
}
/**
* @inheritdoc
*/
protected function defaultDescription(): ?string
{
return Translation::prep('app', 'Pushing announcement to control panel users');
}
}