-
Notifications
You must be signed in to change notification settings - Fork 39
/
StaticPagesPlugin.php
234 lines (210 loc) · 7.24 KB
/
StaticPagesPlugin.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @file StaticPagesPlugin.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @package plugins.generic.staticPages
*
* @class StaticPagesPlugin
*
* @brief Static pages plugin main class
*/
namespace APP\plugins\generic\staticPages;
use APP\core\Application;
use APP\plugins\generic\staticPages\classes\StaticPagesDAO;
use APP\plugins\generic\staticPages\controllers\grid\StaticPageGridHandler;
use APP\template\TemplateManager;
use PKP\core\Registry;
use PKP\db\DAORegistry;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\RedirectAction;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class StaticPagesPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.staticPages.displayName');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
$description = __('plugins.generic.staticPages.description');
if (!$this->isTinyMCEInstalled()) {
$description .= __('plugins.generic.staticPages.requirement.tinymce');
}
return $description;
}
/**
* Check whether or not the TinyMCE plugin is installed.
*
* @return bool True iff TinyMCE is installed.
*/
public function isTinyMCEInstalled()
{
$application = Application::get();
$products = $application->getEnabledProducts('plugins.generic');
return (isset($products['tinymce']));
}
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
// Register the static pages DAO.
$staticPagesDao = new StaticPagesDAO();
DAORegistry::registerDAO('StaticPagesDAO', $staticPagesDao);
Hook::add('Template::Settings::website', $this->callbackShowWebsiteSettingsTabs(...));
// Intercept the LoadHandler hook to present
// static pages when requested.
Hook::add('LoadHandler', $this->callbackHandleContent(...));
// Register the components this plugin implements to
// permit administration of static pages.
Hook::add('LoadComponentHandler', $this->setupGridHandler(...));
}
return true;
}
return false;
}
/**
* Extend the website settings tabs to include static pages
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackShowWebsiteSettingsTabs($hookName, $args)
{
$templateMgr = $args[1];
$output = & $args[2];
$request = & Registry::get('request');
$dispatcher = $request->getDispatcher();
$output .= $templateMgr->fetch($this->getTemplateResource('staticPagesTab.tpl'));
// Permit other plugins to continue interacting with this hook
return false;
}
/**
* Declare the handler function to process the actual page PATH
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackHandleContent($hookName, $args)
{
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$page = & $args[0];
$op = & $args[1];
$handler = & $args[3];
/** @var StaticPagesDAO */
$staticPagesDao = DAORegistry::getDAO('StaticPagesDAO');
if ($page == 'pages' && $op == 'preview') {
// This is a preview request; mock up a static page to display.
// The handler class ensures that only managers and administrators
// can do this.
$staticPage = $staticPagesDao->newDataObject();
$staticPage->setContent((array) $request->getUserVar('content'), null);
$staticPage->setTitle((array) $request->getUserVar('title'), null);
} else {
// Construct a path to look for
$path = $page;
if ($op !== 'index') {
$path .= "/{$op}";
}
if ($ops = $request->getRequestedArgs()) {
$path .= '/' . implode('/', $ops);
}
// Look for a static page with the given path
$context = $request->getContext();
$staticPage = $staticPagesDao->getByPath(
$context?->getId() ?? Application::SITE_CONTEXT_ID,
$path
);
}
// Check if this is a request for a static page or preview.
if ($staticPage) {
// Trick the handler into dealing with it normally
$page = 'pages';
$op = 'view';
// It is -- attach the static pages handler.
$handler = new StaticPagesHandler($this, $staticPage);
return true;
}
return false;
}
/**
* Permit requests to the static pages grid handler
*
* @param string $hookName The name of the hook being invoked
*/
public function setupGridHandler($hookName, $params)
{
$component = & $params[0];
$componentInstance = & $params[2];
if ($component == 'plugins.generic.staticPages.controllers.grid.StaticPageGridHandler') {
// Allow the static page grid handler to get the plugin object
$componentInstance = new StaticPageGridHandler($this);
return true;
}
return false;
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs)
{
$dispatcher = $request->getDispatcher();
return array_merge(
$this->getEnabled() ? [
new LinkAction(
'settings',
new RedirectAction($dispatcher->url(
$request,
Application::ROUTE_PAGE,
null,
'management',
'settings',
['website'],
['uid' => uniqid()], // Force reload
'staticPages' // Anchor for tab
)),
__('plugins.generic.staticPages.editAddContent'),
null
),
] : [],
parent::getActions($request, $actionArgs)
);
}
/**
* @copydoc Plugin::getInstallMigration()
*/
public function getInstallMigration()
{
return new StaticPagesSchemaMigration();
}
/**
* Get the JavaScript URL for this plugin.
*/
public function getJavaScriptURL($request)
{
return $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js';
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\staticPages\StaticPagesPlugin', '\StaticPagesPlugin');
}