-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserProfilesPlugin.php
More file actions
322 lines (284 loc) · 12.4 KB
/
UserProfilesPlugin.php
File metadata and controls
322 lines (284 loc) · 12.4 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
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
<?php
define('USER_PROFILES_DIR', dirname(__FILE__) );
require_once(USER_PROFILES_DIR . '/helpers/functions.php');
class UserProfilesPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'define_acl',
'config',
'config_form',
'admin_items_show_sidebar',
'public_items_show',
'public_content_top',
'admin_users_browse_each',
'after_delete_user',
'initialize',
'upgrade',
);
protected $_filters = array(
'admin_navigation_main',
'search_record_types',
'api_resources',
'api_import_omeka_adapters'
);
protected $_options = null;
public function setUp()
{
if(plugin_is_active('GuestUser')) {
$this->_filters[] = 'guest_user_links';
}
parent::setUp();
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
public function hookUpgrade($args)
{
$db = get_db();
$old = $args['old_version'];
$new = $args['new_version'];
if (version_compare($old, '1.1.1', '<')) {
$sql = "
ALTER TABLE `$db->UserProfilesProfile` CHANGE `modified` `modified` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT '2000-01-01 00:00:00', CHANGE `added` `added` TIMESTAMP NOT NULL DEFAULT '2000-01-01 00:00:00'
";
$db->query($sql);
}
}
public function hookInstall()
{
$db = get_db();
$sql = "
CREATE TABLE IF NOT EXISTS `$db->UserProfilesProfile` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type_id` int(10) unsigned NOT NULL ,
`owner_id` int(10) unsigned NOT NULL ,
`added` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00',
`modified` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`public` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->UserProfilesType` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`label` text,
`description` text,
`element_set_id` int(10) unsigned NOT NULL,
`required_element_ids` text NOT NULL,
`required_multielement_ids` text NOT NULL,
`public` tinyint(1) NOT NULL,
`required` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->UserProfilesMultiElement` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` text COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`type` enum('radio','select','multiselect','checkbox') COLLATE utf8_unicode_ci NOT NULL,
`options` text COLLATE utf8_unicode_ci NOT NULL,
`element_set_id` int(10) unsigned NOT NULL,
`order` int(11) DEFAULT NULL,
`comment` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='A parallel to Elements for checkboxes, radio, selects ' ;
";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->UserProfilesMultiValue` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`profile_type_id` int(10) unsigned NOT NULL,
`profile_id` int(10) unsigned NOT NULL,
`values` text COLLATE utf8_unicode_ci NOT NULL,
`multi_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
";
$db->query($sql);
set_option('user_profiles_required_elements', serialize(array()));
set_option('user_profiles_required_multielements', serialize(array()));
$plugin = get_db()->getTable('Plugin')->findByDirectoryName('Contribution');
if($plugin) {
set_option('user_profiles_contributors_imported', 0);
}
}
public function hookUninstall()
{
$db = get_db();
//Delete all elements, elementsets, and elementtexts UP is using
$types = $db->getTable('UserProfilesType')->findAll();
foreach($types as $type) {
$type->getElementSet()->delete();
}
$sql = "DROP TABLE IF EXISTS `$db->UserProfilesProfile` ";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `$db->UserProfilesType` ";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `$db->UserProfilesMultiValue` ";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `$db->UserProfilesMultiElement` ";
$db->query($sql);
//don't forget to delete the record relations
$sql = "DELETE FROM `$db->RecordRelationsRelation` WHERE `object_record_type` = 'UserProfilesProfile'" ;
$db->query($sql);
}
public function hookAfterDeleteUser($args)
{
$user = $args['record'];
$profiles = get_db()->getTable('UserProfilesProfile')->findByUserId($user->id);
foreach($profiles as $profile) {
$profile->deleteWithRelation();
}
}
public function filterAdminNavigationMain($tabs)
{
$tabs['User Profiles'] = array('label'=>'User Profiles', 'uri'=>url("user-profiles") );
return $tabs;
}
public function filterGuestUserLinks($links)
{
$user = current_user();
$typesTable = $this->_db->getTable('UserProfilesType');
$incompleteTypes = $typesTable->getIncompleteProfileTypes();
if(!empty($incompleteTypes)) {
$type = $incompleteTypes[0];
$links['UserProfiles'] = array('label'=>'My Profiles', 'uri'=>url("/user-profiles/profiles/user/id/{$user->id}/type/{$type->id}"));
return $links;
}
$firstProfileTypes = $this->_db->getTable('UserProfilesType')->findBy(array(), 1);
if(!empty($firstProfileTypes)) {
$type = $firstProfileTypes[0];
$links['UserProfiles'] = array('label'=>'My Profiles', 'uri'=>url("/user-profiles/profiles/user/id/{$user->id}/type/{$type->id}"));
}
return $links;
}
public function filterSearchRecordTypes($recordTypes)
{
$recordTypes['UserProfilesProfile'] = __('User Profiles');
return $recordTypes;
}
public function filterApiResources($resources)
{
$resources['user_profiles_types'] = array(
'record_type' => 'UserProfilesType',
'actions' => array('index', 'get')
);
$resources['user_profiles'] = array(
'record_type' => 'UserProfilesProfile',
'actions' => array('index', 'get')
);
$resources['user_profiles_multielements'] = array(
'record_type' => 'UserProfilesMultiElement',
'actions' => array('index', 'get'),
);
$resources['user_profiles_multivalues'] = array(
'record_type' => 'UserProfilesMultiValue',
'actions' => array('index', 'get')
);
return $resources;
}
public function filterApiImportOmekaAdapters($adapters, $args)
{
// Sequence is important here. Need the types and their elements mapped in first
// Then the profiles themselves, then bring in the multi-valued elements then their values
$typesAdapter = new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'UserProfilesType');
$typesAdapter->setResourceProperties(array('element_set' => 'ElementSet'));
$adapters['user_profiles_types'] = $typesAdapter;
$elementAdapter = new ApiImport_ResponseAdapter_Omeka_UserProfilesMulti(null, $args['endpointUri'], 'UserProfilesMultiElement');
$elementAdapter->setResourceProperties(array('element_set' => 'ElementSet'));
$adapters['user_profiles_multielements'] = $elementAdapter;
$profileAdapter = new ApiImport_ResponseAdapter_Omeka_UserProfilesProfile(null, $args['endpointUri'], 'UserProfilesProfile');
$adapters['user_profiles'] = $profileAdapter;
$valueAdapter = new ApiImport_ResponseAdapter_Omeka_UserProfilesMulti(null, $args['endpointUri'], 'UserProfilesMultiValue');
$valueAdapter->setResourceProperties(array(
'profile_type' => 'UserProfilesType',
'multi' => 'UserProfilesMultiElement',
'profile' => 'UserProfilesProfile'
));
$adapters['user_profiles_multivalues'] = $valueAdapter;
return $adapters;
}
public function hookPublicItemsShow($args)
{
if(get_option('user_profiles_link_to_owner')) {
$view = $args['view'];
$view->addHelperPath(USER_PROFILES_DIR . '/helpers', 'UserProfiles_View_Helper_');
echo $view->linkToOwnerProfile(array('item' =>$args['item'], 'text'=> __("Added by ")));
}
}
public function hookPublicContentTop($args)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$module = $request->getModuleName();
$action = $request->getActionName();
//check if they are already editing a profile, and if so don't ask them to edit a profile
if($module == 'user-profiles' && ($action == 'user' || $action == 'edit')) {
return;
}
$view = $args['view'];
$view->addHelperPath(USER_PROFILES_DIR . '/helpers', 'UserProfiles_View_Helper_');
echo $view->linksToIncompleteProfiles();
}
public function hookAdminUsersBrowseEach($args)
{
$user = $args['user'];
$view = $args['view'];
$view->addHelperPath(USER_PROFILES_DIR . '/helpers', 'UserProfiles_View_Helper_');
echo $view->linkToOwnerProfile(array('owner'=>$user, 'text'=>"Profile: "));
}
public function hookAdminItemsShowSidebar($args)
{
if(get_option('user_profiles_link_to_owner')) {
$view = $args['view'];
echo "<div class='panel'>";
echo "<h4>Owner Info</h4>";
$view->addHelperPath(USER_PROFILES_DIR . '/helpers', 'UserProfiles_View_Helper_');
echo $view->linkToOwnerProfile(array('item' =>$args['item']));
echo "</div>";
}
}
public function hookConfig($args)
{
$db = get_db();
$post = $args['post'];
foreach($post as $option=>$value) {
set_option($option, $value);
}
if($post['user_profiles_import_contributors'] == 1) {
$importer = new UserProfilesImportContribution(array());
$importer->perform();
}
}
public function hookConfigForm($args)
{
include(USER_PROFILES_DIR . '/config_form.php');
}
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$acl->addResource('UserProfiles_Type');
$acl->addResource('UserProfiles_Profile');
//null as 1st param in allow includes not logged in, so manage roles here
$roles = array('super', 'admin', 'contributor', 'researcher');
if(plugin_is_active('GuestUser')) {
$roles[] = 'guest';
}
$acl->allow(null,
'UserProfiles_Profile',
array('edit', 'delete'),
new Omeka_Acl_Assert_Ownership);
$acl->allow(null, 'UserProfiles_Profile', array('user'));
$acl->allow($roles, 'UserProfiles_Profile', array('add', 'editSelf', 'delete-confirm', 'showSelfNotPublic', 'deleteSelf'));
$acl->allow(array('admin', 'super', 'researcher'), 'UserProfiles_Profile', array('showNotPublic'));
$acl->allow(array('admin', 'super'), 'UserProfiles_Profile', array('deleteAll'));
$acl->deny(null, 'UserProfiles_Type');
$acl->allow(array('super', 'admin'), 'UserProfiles_Type');
//let all logged in people see the types available, but hide non-public ones from searches
//since public/private is managed by Omeka_Db_Select_PublicPermission, this keeps them out of the navigation
$acl->allow($roles, 'UserProfiles_Type', array('showNotPublic'));
}
}