-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathSystemMessagesController.php
More file actions
108 lines (92 loc) Β· 2.91 KB
/
SystemMessagesController.php
File metadata and controls
108 lines (92 loc) Β· 2.91 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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\enums\CmsEdition;
use craft\filters\UtilityAccess;
use craft\models\SystemMessage;
use craft\utilities\SystemMessages;
use craft\web\Controller;
use yii\web\Response;
/**
* The SystemMessagesController class is a controller that handles various email message tasks such as saving email
* messages.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class SystemMessagesController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors(): array
{
return array_merge(parent::behaviors(), [
[
'class' => UtilityAccess::class,
'utility' => SystemMessages::class,
],
]);
}
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
Craft::$app->requireEdition(CmsEdition::Pro);
return true;
}
/**
* Returns the HTML for a system email message modal.
*
* @return Response
*/
public function actionGetMessageModal(): Response
{
$this->requireAcceptsJson();
$key = $this->request->getRequiredBodyParam('key');
$language = $this->request->getBodyParam('language');
if (!$language) {
$language = Craft::$app->getSites()->getPrimarySite()->language;
}
$message = Craft::$app->getSystemMessages()->getMessage($key, $language);
return $this->asJson([
'body' => $this->getView()->renderTemplate('_components/utilities/SystemMessages/message-modal.twig', [
'message' => $message,
'language' => $language,
]),
]);
}
/**
* Saves a system email message.
*
* @return Response
*/
public function actionSaveMessage(): Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();
$message = new SystemMessage();
$message->key = $this->request->getRequiredBodyParam('key');
$message->subject = $this->request->getRequiredBodyParam('subject');
$message->body = $this->request->getRequiredBodyParam('body');
if (Craft::$app->getIsMultiSite()) {
$language = $this->request->getBodyParam('language');
} else {
$language = Craft::$app->getSites()->getPrimarySite()->language;
}
if (!Craft::$app->getSystemMessages()->saveMessage($message, $language)) {
return $this->asFailure(Craft::t('app', 'There was a problem saving your message.'));
}
return $this->asSuccess();
}
}