-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHideElementsPlugin.php
176 lines (154 loc) · 5.71 KB
/
HideElementsPlugin.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
<?php
/**
* @package HideElements
* @copyright Copyright 2013, John Flatness
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 or any later version
*/
class HideElementsPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array('initialize', 'config', 'config_form',
'install', 'uninstall', 'upgrade');
protected $_filters = array('display_elements', 'elements_select_options');
protected $_settings;
public function hookInstall()
{
$defaults = array(
'override' => array(),
'form' => array(),
'admin' => array(),
'public' => array(),
'search' => array(),
);
set_option('hide_elements_settings', json_encode($defaults));
}
public function hookUninstall()
{
delete_option('hide_elements_settings');
}
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$settings = json_decode(get_option('hide_elements_settings'), true);
if (version_compare($oldVersion, '1.2', '<')) {
$settings['override'] = array();
$settings['search'] = array();
}
// Convert old-style search hide settings to ID-based storage
if (version_compare($oldVersion, '1.3', '<')) {
$newSearch = array();
$elementTable = $this->_db->getTable('Element');
foreach ($settings['search'] as $set => $elements) {
foreach ($elements as $element => $enabled) {
$element = $elementTable->findByElementSetNameAndElementName($set, $element);
$newSearch[$set][$element->id] = $enabled;
}
}
$settings['search'] = $newSearch;
}
set_option('hide_elements_settings', json_encode($settings));
}
public function hookInitialize()
{
$this->_settings = json_decode(get_option('hide_elements_settings'), true);
$elementUsers = array('Item', 'File', 'Collection');
foreach (array_keys($this->_settings['form']) as $elementSet) {
foreach ($elementUsers as $record) {
add_filter(array('ElementSetForm', $record, $elementSet), array($this, 'filterElementSetForm'));
}
}
}
public function hookConfigForm()
{
$settings = $this->_settings;
$table = get_db()->getTable('Element');
$select = $table->getSelect()
->order('elements.element_set_id')
->order('ISNULL(elements.order)')
->order('elements.order');
$elements = $table->fetchObjects($select);
include 'config-form.php';
}
public function hookConfig($args)
{
$post = $args['post'];
$settings = array(
'override' => isset($post['override']) ? $post['override'] : array(),
'form' => isset($post['form']) ? $post['form'] : array(),
'admin' => isset($post['admin']) ? $post['admin'] : array(),
'public' => isset($post['public']) ? $post['public'] : array(),
'search' => isset($post['search']) ? $post['search'] : array(),
);
set_option('hide_elements_settings', json_encode($settings));
}
public function filterDisplayElements($elementsBySet)
{
if ($this->_overrideFilter()) {
return $elementsBySet;
}
$key = is_admin_theme() ? 'admin' : 'public';
$itemTypeSetName = ElementSet::ITEM_TYPE_NAME;
// Account for the renamed Item Type Metadata set.
foreach ($this->_settings[$key] as $elementSet => $elements) {
if ($elementSet == $itemTypeSetName) {
foreach (array_keys($elementsBySet) as $currentSet) {
if (substr_compare($currentSet, $itemTypeSetName,
-strlen($itemTypeSetName), strlen($itemTypeSetName))
=== 0
) {
$elementSet = $currentSet;
break;
}
}
}
foreach (array_keys($elements) as $element) {
unset($elementsBySet[$elementSet][$element]);
}
}
return $elementsBySet;
}
public function filterElementSetForm($elements, $args)
{
if ($this->_overrideFilter()) {
return $elements;
}
$set = $args['element_set_name'];
if (isset($this->_settings['form'][$set])) {
$hideElements = array_keys($this->_settings['form'][$set]);
foreach ($elements as $key => $element) {
if (in_array($element->name, $hideElements)) {
unset($elements[$key]);
}
}
}
return $elements;
}
public function filterElementsSelectOptions($options)
{
if ($this->_overrideFilter() || !isset($this->_settings['search'])) {
return $options;
}
$elementSetHeadings = get_option('show_element_set_headings');
$optgroups = $elementSetHeadings || version_compare(OMEKA_VERSION, '2.2', '<');
foreach ($this->_settings['search'] as $elementSet => $elements) {
foreach ($elements as $id => $hidden) {
if ($optgroups) {
unset($options[__($elementSet)][$id]);
} else {
unset($options[$id]);
}
}
}
return $options;
}
/**
* Override filters for configured user roles.
*/
protected function _overrideFilter()
{
$user = current_user();
if ($user && in_array($user->role, $this->_settings['override'])) {
return true;
}
return false;
}
}