forked from omeka/plugin-UserTags
/
UserTagsPlugin.php
153 lines (137 loc) · 5.49 KB
/
UserTagsPlugin.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
<?php
define('USER_TAGS_PLUGIN_DIR', PLUGIN_DIR . '/UserTags');
class UserTagsPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'config_form',
'config',
'public_items_show',
'public_head',
'add_item_tag',
'remove_item_tag',
'items_browse_sql'
);
public function hookInstall()
{
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS `$db->UserRecordsTag` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`record_id` int(10) unsigned NOT NULL,
`record_type` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`tag_id` int(10) unsigned NOT NULL,
`owner_id` int(10) unsigned NOT NULL,
`added` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `tag` (`record_type`,`record_id`,`tag_id`, `owner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
";
$db->query($sql);
set_option('user-tags-private-roles', serialize(array()));
}
public function hookConfigForm($args)
{
include(USER_TAGS_PLUGIN_DIR . '/config_form.php');
}
public function hookConfig($args)
{
$post = $args['post'];
set_option('user-tags-private-roles', serialize($post['user_tags_private_roles']));
}
public function hookPublicHead($args)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
if($request->getControllerName() == 'items' && $request->getActionName() == 'show') {
//add javascript and css for dealing with the user tags only to item show pages
$view = $args['view'];
//don't know why $view->item isn't giving me the object, but it isn't, so go via request
$itemId = $request->getParam('id');
queue_css_file('user-tags');
queue_css_file('jquery-ui');
queue_js_file('user-tags');
$js = "UserTags.webRoot = '" . WEB_ROOT . "'; ";
$js .= "UserTags.itemId = " . $itemId . "; ";
queue_js_string($js);
}
}
public function hookPublicItemsShow($args)
{
$user = current_user();
if ($user) {
$view = $args['view'];
$item = $args['item'];
$myTags = $this->_db->getTable('UserRecordsTag')->findTagsBy(array('record'=>$item, 'user'=>$user));
//need to remove itemTags that are mine from the list to display
$myTagsIds = array();
foreach($myTags as $tag) {
$myTagsIds[] = $tag->id;
}
$itemTags = $item->Tags;
echo $view->partial('index/tags.php', array('tags' => $myTags, 'link'=>true, 'itemTags'=>$itemTags));
}
}
/**
* Add the UserRecordTag when a tag is added. If the user role's tags are private, also delete the records_tags row
* @param array $args
*/
public function hookAddItemTag($args)
{
$user = current_user();
if($user && in_array($user->role, unserialize(get_option('user-tags-private-roles')))) {
$recordsTagsTable = $this->_db->getTable('RecordsTags');
$item = $args['record'];
$added = $args['added'];
foreach($added as $tag) {
$userRecordsTag = new UserRecordsTag();
$userRecordsTag->tag_id = $tag->id;
$userRecordsTag->record_type = 'Item';
$userRecordsTag->record_id = $item->id;
$userRecordsTag->save();
//kill the record from records_tags. Oh records_tags row, we hardly knew ye
$recordsTag = $recordsTagsTable->findForRecordAndTag($item, $tag);
if($recordsTag) {
$recordsTag->delete();
}
}
}
}
/**
* Deletes the UserRecordTag
* @param unknown_type $args
*/
public function hookRemoveItemTag($args)
{
$user = current_user();
if($user && in_array($user->role, unserialize(get_option('user-tags-private-roles')))) {
$item = $args['record'];
$removed = $args['removed'];
$userRecordsTagTable = $this->_db->getTable('UserRecordsTag');
foreach($removed as $tag) {
$userRecordsTags = $userRecordsTagTable->findUsersRecordsTags($tag, $item);
foreach($userRecordsTags as $userRecordsTag) {
$userRecordsTag->delete();
}
}
}
}
public function hookItemsBrowseSql($args)
{
$select = $args['select'];
$params = $args['params'];
$user = current_user();
if(!$user) {
return;
}
if(isset($params['my-tag'])) {
$tagName = $params['my-tag'];
$db = $this->_db;
$subSelect = new Omeka_Db_Select;
$subSelect->from(array('user_records_tags'=>$db->UserRecordsTags), array('items.id'=>'user_records_tags.record_id'))
->joinInner(array('tags'=>$db->Tag), 'tags.id = user_records_tags.tag_id', array())
->where('tags.name = ? AND user_records_tags.`record_type` = "Item"', trim($tagName));
$subSelect->where('user_records_tags.owner_id = ?', $user->id);
$select->where('items.id IN (' . (string) $subSelect . ')');
}
}
}