-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAnnouncementsBlockPluginSettingsForm.php
98 lines (87 loc) · 3.17 KB
/
AnnouncementsBlockPluginSettingsForm.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
<?php
namespace APP\plugins\blocks\announcementsBlock;
use APP\core\Application;
use \PKP\classes\form\Form;
use APP\template\TemplateManager;
use APP\notification\NotificationManager;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorPost;
class AnnouncementsBlockPluginSettingsForm extends \PKP\form\Form
{
public $plugin;
public function __construct($plugin)
{
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$this->plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
/**
* Load settings already saved in the database
*
* Settings are stored by context, so that each journal or press
* can have different settings.
*/
public function initData()
{
$context = Application::get()->getRequest()->getContext();
$contextId = ($context && $context->getId()) ? $context->getId() : CONTEXT_SITE;
$this->setData(
'announcementsAmount',
$this->plugin->getSetting($contextId, 'announcementsAmount') == null ? 2 : $this->plugin->getSetting($contextId, 'announcementsAmount')
);
$this->setData('truncateNum', $this->plugin->getSetting($contextId, 'truncateNum'));
$announcementsAlignItems = [
'left' => "plugins.blocks.announcements.align.left",
'right' => "plugins.blocks.announcements.align.right",
'center' => "plugins.blocks.announcements.align.center",
'justify' => "plugins.blocks.announcements.align.justify",
];
$this->setData('announcementsAlign', $this->plugin->getSetting($contextId, 'announcementsAlign'));
$this->setData('announcementsAlignItems', $announcementsAlignItems);
parent::initData();
}
/**
* Load data that was submitted with the form
*/
public function readInputData()
{
$this->readUserVars(['announcementsAmount', 'truncateNum', 'announcementsAlign']);
parent::readInputData();
}
/**
* Fetch any additional data needed for your form.
*
* Data assigned to the form using $this->setData() during the
* initData() or readInputData() methods will be passed to the
* template.
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* Save the settings
* @param mixed ...$functionArgs
* @return mixed|null
*/
public function execute(...$functionArgs)
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = ($context && $context->getId()) ? $context->getId() : CONTEXT_SITE;
$this->plugin->updateSetting($contextId, 'announcementsAmount', $this->getData('announcementsAmount'));
$this->plugin->updateSetting($contextId, 'truncateNum', $this->getData('truncateNum'));
$this->plugin->updateSetting($contextId, 'announcementsAlign', $this->getData('announcementsAlign'));
// import('classes.notification.NotificationManager');
$notificationMgr = new NotificationManager();
$notificationMgr->createTrivialNotification(
$request->getUser()->getId(),
NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
return parent::execute();
}
}