forked from omeka/plugin-SimplePages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplePagesPlugin.php
342 lines (298 loc) · 11 KB
/
SimplePagesPlugin.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Simple Pages
*
* @copyright Copyright 2008-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*/
require_once dirname(__FILE__) . '/helpers/SimplePageFunctions.php';
/**
* Simple Pages plugin.
*/
class SimplePagesPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array('install', 'uninstall', 'upgrade', 'initialize',
'define_acl', 'define_routes', 'config_form', 'config',
'html_purifier_form_submission');
/**
* @var array Filters for the plugin.
*/
protected $_filters = array('admin_navigation_main',
'public_navigation_main', 'search_record_types', 'page_caching_whitelist',
'page_caching_blacklist_for_record');
/**
* @var array Options and their default values.
*/
protected $_options = array(
'simple_pages_filter_page_content' => '0'
);
/**
* Install the plugin.
*/
public function hookInstall()
{
// Create the table.
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS `$db->SimplePagesPage` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`modified_by_user_id` int(10) unsigned NOT NULL,
`created_by_user_id` int(10) unsigned NOT NULL,
`is_published` tinyint(1) NOT NULL,
`title` tinytext COLLATE utf8_unicode_ci NOT NULL,
`slug` tinytext COLLATE utf8_unicode_ci NOT NULL,
`text` text COLLATE utf8_unicode_ci,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`inserted` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`order` int(10) unsigned NOT NULL,
`parent_id` int(10) unsigned NOT NULL,
`template` tinytext COLLATE utf8_unicode_ci NOT NULL,
`use_tiny_mce` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `is_published` (`is_published`),
KEY `inserted` (`inserted`),
KEY `updated` (`updated`),
KEY `created_by_user_id` (`created_by_user_id`),
KEY `modified_by_user_id` (`modified_by_user_id`),
KEY `order` (`order`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
// Save an example page.
$page = new SimplePagesPage;
$page->modified_by_user_id = current_user()->id;
$page->created_by_user_id = current_user()->id;
$page->is_published = 1;
$page->parent_id = 0;
$page->title = 'About';
$page->slug = 'about';
$page->text = '<p>This is an example page. Feel free to replace this content, or delete the page and start from scratch.</p>';
$page->save();
$this->_installOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
// Drop the table.
$db = $this->_db;
$sql = "DROP TABLE IF EXISTS `$db->SimplePagesPage`";
$db->query($sql);
$this->_uninstallOptions();
}
/**
* Upgrade the plugin.
*
* @param array $args contains: 'old_version' and 'new_version'
*/
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
$db = $this->_db;
if ($oldVersion < '1.0') {
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `is_published` )";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `inserted` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `updated` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `add_to_public_nav` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `created_by_user_id` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `modified_by_user_id` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD `order` INT UNSIGNED NOT NULL ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `order` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD `parent_id` INT UNSIGNED NOT NULL ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD INDEX ( `parent_id` ) ";
$db->query($sql);
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD `template` TINYTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ";
$db->query($sql);
}
if ($oldVersion < '1.3') {
$sql = "ALTER TABLE `$db->SimplePagesPage` ADD `use_tiny_mce` TINYINT(1) NOT NULL";
$db->query($sql);
}
if ($oldVersion < '2.0') {
$db->query("ALTER TABLE `$db->SimplePagesPage` DROP `add_to_public_nav`");
delete_option('simple_pages_home_page_id');
}
}
/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
/**
* Define the ACL.
*
* @param Omeka_Acl
*/
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$indexResource = new Zend_Acl_Resource('SimplePages_Index');
$pageResource = new Zend_Acl_Resource('SimplePages_Page');
$acl->add($indexResource);
$acl->add($pageResource);
$acl->allow(array('super', 'admin'), array('SimplePages_Index', 'SimplePages_Page'));
$acl->allow(null, 'SimplePages_Page', 'show');
$acl->deny(null, 'SimplePages_Page', 'show-unpublished');
}
/**
* Add the routes for accessing simple pages by slug.
*
* @param Zend_Controller_Router_Rewrite $router
*/
public function hookDefineRoutes($args)
{
// Don't add these routes on the admin side to avoid conflicts.
if (is_admin_theme()) {
return;
}
$router = $args['router'];
// Add custom routes based on the page slug.
$pages = get_db()->getTable('SimplePagesPage')->findAll();
foreach ($pages as $page) {
$router->addRoute(
'simple_pages_show_page_' . $page->id,
new Zend_Controller_Router_Route(
$page->slug,
array(
'module' => 'simple-pages',
'controller' => 'page',
'action' => 'show',
'id' => $page->id
)
)
);
}
}
/**
* Display the plugin config form.
*/
public function hookConfigForm()
{
require dirname(__FILE__) . '/config_form.php';
}
/**
* Set the options from the config form input.
*/
public function hookConfig()
{
set_option('simple_pages_filter_page_content', (int)(boolean)$_POST['simple_pages_filter_page_content']);
}
/**
* Filter the 'text' field of the simple-pages form, but only if the
* 'simple_pages_filter_page_content' setting has been enabled from within the
* configuration form.
*
* @param array $args Hook args, contains:
* 'request': Zend_Controller_Request_Http
* 'purifier': HTMLPurifier
*/
public function hookHtmlPurifierFormSubmission($args)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$purifier = $args['purifier'];
// If we aren't editing or adding a page in SimplePages, don't do anything.
if ($request->getModuleName() != 'simple-pages' or !in_array($request->getActionName(), array('edit', 'add'))) {
return;
}
// Do not filter HTML for the request unless this configuration directive is on.
if (!get_option('simple_pages_filter_page_content')) {
return;
}
$post = $request->getPost();
$post['text'] = $purifier->purify($post['text']);
$request->setPost($post);
}
/**
* Add the Simple Pages link to the admin main navigation.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Simple Pages'),
'uri' => url('simple-pages'),
'resource' => 'SimplePages_Index',
'privilege' => 'browse'
);
return $nav;
}
/**
* Add the pages to the public main navigation options.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
public function filterPublicNavigationMain($nav)
{
$navLinks = simple_pages_get_links_for_children_pages(0, 0, 'order', true);
$nav = array_merge($nav, $navLinks);
return $nav;
}
/**
* Add SimplePagesPage as a searchable type.
*/
public function filterSearchRecordTypes($recordTypes)
{
$recordTypes['SimplePagesPage'] = __('Simple Page');
return $recordTypes;
}
/**
* Specify the default list of urls to whitelist
*
* @param $whitelist array An associative array urls to whitelist,
* where the key is a regular expression of relative urls to whitelist
* and the value is an array of Zend_Cache front end settings
* @return array The whitelist
*/
public function filterPageCachingWhitelist($whitelist)
{
// Add custom routes based on the page slug.
$pages = get_db()->getTable('SimplePagesPage')->findAll();
foreach($pages as $page) {
$whitelist['/' . trim($page->slug, '/')] = array('cache'=>true);
}
return $whitelist;
}
/**
* Add pages to the blacklist
*
* @param $blacklist array An associative array urls to blacklist,
* where the key is a regular expression of relative urls to blacklist
* and the value is an array of Zend_Cache front end settings
* @param $record
* @param $args Filter arguments. contains:
* - record: the record
* - action: the action
* @return array The blacklist
*/
public function filterPageCachingBlacklistForRecord($blacklist, $args)
{
$record = $args['record'];
$action = $args['action'];
if ($record instanceof SimplePagesPage) {
$page = $record;
if ($action == 'update' || $action == 'delete') {
$blacklist['/' . trim($page->slug, '/')] = array('cache'=>false);
}
}
return $blacklist;
}
}