-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTitleCapitalizerPlugin.php
More file actions
159 lines (138 loc) · 4.38 KB
/
Copy pathTitleCapitalizerPlugin.php
File metadata and controls
159 lines (138 loc) · 4.38 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @file TitleCapitalizerPlugin.php
*
* Title Capitalizer Plugin for OJS/OMP 3.4
* Adds an 'aA' button to capitalize submission titles.
*/
namespace APP\plugins\generic\titlecapitalizer;
use APP\core\Application;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class TitleCapitalizerPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null): bool
{
if (!parent::register($category, $path, $mainContextId)) {
return false;
}
if ($this->getEnabled($mainContextId)) {
Hook::add('TemplateManager::display', [$this, 'injectJS']);
}
return true;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.titlecapitalizer.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription(): string
{
return __('plugins.generic.titlecapitalizer.description');
}
/**
* Inject JS into the backend — fires once per page request via a static guard.
*/
public function injectJS(string $hookName, array $args): bool
{
static $injected = false;
if ($injected) {
return Hook::CONTINUE;
}
$injected = true;
$templateMgr = $args[0];
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context ? $context->getId() : Application::CONTEXT_SITE;
$style = $this->getSetting($contextId, 'style') ?: 'chicago';
$pluginUrl = $request->getBaseUrl() . '/' . $this->getPluginPath();
$templateMgr->addJavaScript(
'titleCapitalizer',
$pluginUrl . '/js/titleCapitalizer.js',
[
'contexts' => 'backend',
'priority' => STYLE_SEQUENCE_LAST,
]
);
$templateMgr->addHeader(
'titleCapitalizerStyle',
'<script>window.titleCapitalizerStyle=' . json_encode($style) . ';</script>'
);
return Hook::CONTINUE;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb): array
{
$actions = parent::getActions($request, $verb);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
$url = $router->url(
$request,
null,
null,
'manage',
null,
[
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic',
]
);
array_unshift(
$actions,
new LinkAction(
'settings',
new AjaxModal($url, $this->getDisplayName()),
__('manager.plugins.settings')
)
);
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request): JSONMessage
{
if ($request->getUserVar('verb') !== 'settings') {
return parent::manage($args, $request);
}
$context = $request->getContext();
$contextId = $context ? $context->getId() : Application::CONTEXT_SITE;
if ($request->getUserVar('save')) {
$this->updateSetting($contextId, 'style', $request->getUserVar('style'));
return new JSONMessage(true);
}
$styles = [
'chicago' => 'Title Case (Chicago)',
'apa' => 'APA Style',
'mla' => 'MLA Style',
'sentence' => 'Sentence case',
'uppercase' => 'ALL CAPS',
'lowercase' => 'lowercase',
];
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign([
'style' => $this->getSetting($contextId, 'style') ?: 'chicago',
'styles' => $styles,
'pluginName' => $this->getName(),
'saveFormID' => 'titleCapitalizerSettings',
]);
return new JSONMessage(true, $templateMgr->fetch($this->getTemplateResource('settings.tpl')));
}
}