-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.php
367 lines (321 loc) · 12.8 KB
/
plugin.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* @version $Id$
* @copyright Center for History and New Media, 2008
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @package Omeka
* @subpackage MyOmeka
**/
// note: MyOmeka can optionally be used in conjunction with the TermsOfService plugin
// Define the plugin version and page path.
define('MY_OMEKA_PAGE_PATH', 'myomeka/');
define('MY_OMEKA_PAGE_TITLE', 'MyOmeka');
define('MY_OMEKA_DISCLAIMER', 'This page contains user generated content and does not necessarily reflect the opinions of this website. For more information please refer to our Terms and Conditions. If you would like to report the content of this page as objectionable, please contact us.');
define('MYOMEKA_USER_ROLE', 'my-omeka');
define('MYOMEKA_TAG_TYPE', 'MyomekaTag');
// Current hack, controllers require access to some of the view helpers
// for generating URLs in emails. Need to refactor helpers to allow access
// within controllers.
require_once HELPER_DIR . DIRECTORY_SEPARATOR . 'all.php';
// Add plugin hooks.
add_plugin_hook('install', 'my_omeka_install');
add_plugin_hook('uninstall', 'my_omeka_uninstall');
add_plugin_hook('config', 'my_omeka_config');
add_plugin_hook('config_form', 'my_omeka_config_form');
add_plugin_hook('define_acl', 'my_omeka_setup_acl');
add_plugin_hook('define_routes', 'my_omeka_define_routes');
add_plugin_hook('public_theme_header', 'my_omeka_css');
add_plugin_hook('admin_theme_header', 'my_omeka_css');
add_plugin_hook('item_browse_sql', 'my_omeka_show_only_my_items');
add_plugin_hook('public_append_to_items_show', 'my_omeka_embed_notes_and_tags');
add_plugin_hook('initialize', 'my_omeka_add_controller_plugin');
add_plugin_hook('before_delete_item', 'my_omeka_delete_myomeka_taggings');
// Special hooks.
add_plugin_hook('html_purifier_form_submission', 'my_omeka_xss_filter');
// Add filters.
add_filter('admin_navigation_main', 'my_omeka_admin_nav');
add_filter('public_navigation_main','my_omeka_public_nav');
/**
* Install the plugin.
*/
function my_omeka_install()
{
set_option('my_omeka_page_path', MY_OMEKA_PAGE_PATH);
set_option('my_omeka_page_title', MY_OMEKA_PAGE_TITLE);
set_option('my_omeka_disclaimer', MY_OMEKA_DISCLAIMER);
// Create new tables to support poster building
$db = get_db();
$db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}posters (
`id` BIGINT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`title` VARCHAR(255) NOT NULL,
`description` TEXT,
`user_id` BIGINT UNSIGNED NOT NULL,
`date_created` TIMESTAMP NOT NULL default '0000-00-00 00:00:00',
`date_modified` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
) ENGINE = MYISAM;");
$db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}posters_items (
`id` BIGINT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`annotation` TEXT,
`poster_id` BIGINT UNSIGNED NOT NULL,
`item_id` BIGINT UNSIGNED NOT NULL,
`ordernum` INT NOT NULL
) ENGINE = MYISAM;");
$db->query("CREATE TABLE IF NOT EXISTS {$db->prefix}notes (
`id` BIGINT UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`note` TEXT NOT NULL,
`user_id` BIGINT UNSIGNED NOT NULL,
`item_id` BIGINT UNSIGNED NOT NULL,
`date_modified` TIMESTAMP NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
) ENGINE = MYISAM;");
}
/**
* Uninstall the plugin
*/
function my_omeka_uninstall()
{
delete_option('my_omeka_page_path');
delete_option('my_omeka_page_title');
delete_option('my_omeka_disclaimer');
$db = get_db();
$db->query("DROP TABLE {$db->prefix}posters");
$db->query("DROP TABLE {$db->prefix}posters_items");
$db->query("DROP TABLE {$db->prefix}notes");
}
/**
* Define the routes, wrapping them in my_omeka_add_route()
*/
function my_omeka_define_routes($router)
{
// get the base path
$bp = get_option('my_omeka_page_path');
if (empty($bp)) {
$bp = MY_OMEKA_PAGE_PATH;
}
$routes = array();
// We may be able to condense this list even more.
$routes['myOmekaDashboard'] = array('', array('controller'=>'my-omeka'));
$routes['myOmekaAction'] = array(':action', array('controller'=>'my-omeka'));
$routes['myOmekaPosterAction'] = array('posters/:action', array('controller'=>'poster'));
$routes['myOmekaPosterActionId'] = array('posters/:action/:id', array('controller'=>'poster'));
$routes['myOmekaPosterBrowse'] = array('posters/browse/:page', array('controller'=>'poster', 'action'=>'browse', 'page'=>1));
$routes['myOmekaAddTag'] = array('tags/add', array('controller'=>'tag', 'action'=>'add'));
$routes['myOmekaTagDelete'] = array('tags/delete/:tag_id/:item_id', array('controller'=>'tag', 'action'=>'delete'));
$routes['myOmekaNoteAction'] = array('note/:action', array('controller'=>'note'));
foreach ($routes as $routeName => $routeValues) {
list($routePath, $routeVars) = $routeValues;
// All of these routes are for the 'my-omeka' module.
$routeVars = array_merge(array('module'=>'my-omeka'), $routeVars);
$router->addRoute(
$routeName,
new Zend_Controller_Router_Route(
// Attach the base path to every defined route.
$bp . $routePath,
$routeVars));
}
}
/**
* Add the Poster Administration link to the admin main navigation.
*
* @param array Navigation array.
* @return array Filtered navigation array.
*/
function my_omeka_admin_nav($navArray)
{
return $navArray += array('Posters'=> uri(array('action'=>'browse'), 'myOmekaPosterAction'));
}
function my_omeka_css($request)
{
$module = $request->getModuleName();
if ($module == 'my-omeka' || ($module == 'default' && !is_admin_theme())) {
queue_css('myomeka');
}
}
/**
* This allows the user to pass arbitrary parameters in the query string when
* browsing items so that we only retrieve items that were tagged using MyOmeka.
*
* @return void
**/
function my_omeka_show_only_my_items($select, $params)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
if( ($user = current_user()) and ($myTagId = (int)$request->getParam('myTag'))) {
$entity_id = (int) $user->entity_id;
$db = get_db();
// Join against the taggings table to only select items that have been
// tagged using the MyOmeka interface.
$select->joinInner(array('my_tg'=>$db->Taggings), 'my_tg.relation_id = i.id', array());
$select->where('my_tg.type = "' . MYOMEKA_TAG_TYPE . '" AND my_tg.tag_id = ?', $myTagId);
}
}
/**
* Echo this function in your items/show.php of your public themes to allow users to add and remove notes and tags
*/
function my_omeka_embed_notes_and_tags()
{
$item = get_current_item();
$user = current_user();
$html = '';
if ($user) {
$html .= '<div id="myomeka-notes-tags">';
$html .= my_omeka_add_notes($item);
$html .= my_omeka_add_tags($item);
$html .= my_omeka_items_show_navigation();
$html .= '</div>';
}
return $html;
}
/**
* Call this function in your public themes to allow users to add notes to an item.
*/
function my_omeka_add_notes($item)
{
if ($user = current_user()) {
// Check if the user has already added notes to the item
$note = get_db()->getTable('MyOmekaNote')->findByUserIdAndItemId($user->id, $item->id);
// Render the addNotes template
common("add-notes", compact("note","item"));
}
}
/**
* Call this function in your public themes to allow users to add and remove tags.
*/
function my_omeka_add_tags($item)
{
$user = current_user();
// Select MyomekaTag tags.
$tagSelect = get_db()->getTable('Tag')->getSelectForFindBy(array('user'=>$user->id, 'type'=>'MyomekaTag'));
// A hackaround for retrieving MyOmeka tags.
$tagSelect->where('tg.relation_id = ?', $item->id);
$tags = get_db()->getTable('Tag')->fetchObjects($tagSelect);
common("add-tags", compact("item","tags"));
}
function my_omeka_items_show_navigation()
{
$helpUrl = uri(array('action'=>'help'), 'myOmekaAction');
$dashboardUrl = uri(array(), 'myOmekaDashboard');
?>
<ul class="navigation"><li><a href="<?php echo html_escape($helpUrl); ?>" class="myomeka-help-link">Help</a></li>
<li><a class="dashboard-link" href="<?php echo html_escape($dashboardUrl); ?>">Go to My Dashboard</a></li></ul>
<?php
}
function my_omeka_poster_icon_html()
{
$html = item_thumbnail();
if (!$html) {
$html = "<img alt='no image available' src='".html_escape(img('noThumbnail.png'))."'/>";
}
return $html;
}
function my_omeka_user_status()
{
$user = current_user();
if ($user) {
$html = '<p>logged in as <a href="'
. html_escape(uri(array(), 'myOmekaDashboard'))
. '">'
. html_escape($user->username)
. '</a> | <a href="'
. html_escape(uri(array('action'=>'logout', 'controller'=>'users'), 'default'))
. '">Logout</a></p>';
} else {
$html = '<p><a href="' . html_escape(uri(array(), 'myOmekaDashboard')) . '">Login</a> | <a href="' . html_escape(uri(array('action'=>'register'), 'myOmekaAction')) . '">Register</a></p>';
}
return $html;
}
function my_omeka_config($post)
{
$path = trim(trim($post['my_omeka_page_path']), '/');
if (!$path) {
$path = MY_OMEKA_PAGE_PATH;
} else {
$path .= '/';
}
set_option('my_omeka_page_path', $path);
set_option('my_omeka_page_title', $post['my_omeka_page_title']);
set_option('my_omeka_disclaimer', $post['my_omeka_disclaimer']);
if(get_option('my_omeka_page_title') == '') {
set_option('my_omeka_page_title', MY_OMEKA_PAGE_TITLE);
}
$requireTOS = (strtolower($post['my_omeka_require_terms_of_service']) == 'checked') ? 1 : 0;
set_option('my_omeka_require_terms_of_service', $requireTOS);
}
function my_omeka_config_form()
{
include "config_form.php";
}
/**
* Define the ACL.
*
* @param Omeka_Acl
*/
function my_omeka_setup_acl($acl)
{
$acl->addRole(new Zend_Acl_Role(MYOMEKA_USER_ROLE));
// All logged in users have permission to delete their own posters.
// As of 0.10, admin & super are automatically given new permissions, so
// they will be able to edit and delete other users' posters by default.
//
// Note that privileges must be 'editAny' instead of 'edit' b/c the latter
// will control all access to the editAction, whereas we want to allow partial
// access based on criteria such as ownership.
$acl->loadResourceList(array('MyOmeka_Poster'=>array('editAny', 'deleteAny')));
}
function my_omeka_add_controller_plugin()
{
require_once 'MyOmekaControllerPlugin.php';
Zend_Controller_Front::getInstance()->registerPlugin(new MyOmekaControllerPlugin);
}
function my_omeka_xss_filter($request, $purifier)
{
if ($request->getModuleName() == 'my-omeka') {
if ($request->getActionName() == 'save') {
$post = $request->getPost();
foreach ($post as $key => $value) {
// Filter the description, annotation- and itemID- fields.
switch (true) {
case $key == 'description':
$post['description'] = $purifier->purify($value);
break;
case strstr($key, 'annotation'):
$post[$key] = $purifier->purify($value);
break;
}
}
$request->setPost($post);
}
}
}
function my_omeka_get_note_for_item($item)
{
$user = current_user();
return get_db()->getTable('MyOmekaNote')->findByUserIdAndItemId($user->id, $item->id);
}
/*
* Since, MyOmeka hacks the record type for taggings from 'Item' to MYOMEKA_TAG_TYPE,
* the core will not automatically remove MYOMEKA_TAG_TYPE taggings when an Item is deleted.
* So we need to delete MyOmeka taggings when items are deleted. Hence, this is another hack.
* All of this should be removed when we remake MyOmeka for collaborative tagging between users.
**/
function my_omeka_delete_myomeka_taggings($item)
{
$taggings = get_db()->getTable('Taggings')->findBySql(
'relation_id = ? AND type = ?',
array($item->id, MYOMEKA_TAG_TYPE));
foreach ($taggings as $tagging) {
$tagging->delete();
}
}
function my_omeka_public_nav($nav)
{
if(!($user = current_user())){
$nav[get_option('my_omeka_page_title').' Register']
= uri(get_option('my_omeka_page_path').'register');
$nav['Login'] = uri('users/login');
} else {
$nav[get_option('my_omeka_page_title')]
= uri(get_option('my_omeka_page_path'));
$nav['Logout'] = uri('users/logout');
}
return $nav;
}