/
ElementAdministrationPlugin.php
266 lines (229 loc) · 9.89 KB
/
ElementAdministrationPlugin.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
<?php
/**
*
* @todo integrate with SimpleVocab
*
* @author Joe Corall <jcorall@kent.edu>
*/
if (!defined('ELEMENT_ADMINISTRATION_DIR')) {
define('ELEMENT_ADMINISTRATION_DIR', dirname(__FILE__));
}
class ElementAdministrationPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'after_save_collection',
'define_acl',
'initialize',
'install',
'uninstall'
);
protected $_filters = array(
'admin_navigation_main',
'display_elements',
);
private $_settings = array();
/**
* Install the plugin.
*/
public function hookInstall()
{
// Create the table.
$db = $this->_db;
$sql = "
CREATE TABLE `$db->ElementAdministrationSettings` (
`id` int(10) unsigned NOT NULL,
`collection_id` int(10) unsigned DEFAULT '0',
`required` tinyint(1) NOT NULL DEFAULT '0',
`multiple` tinyint(1) NOT NULL DEFAULT '1',
`html` tinyint(1) NOT NULL DEFAULT '1',
`brief_display` tinyint(1) NOT NULL DEFAULT '0',
`hidden_form` tinyint(1) NOT NULL DEFAULT '0',
`hidden_public` tinyint(1) NOT NULL DEFAULT '0',
`form_label` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`public_label` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`default_value` mediumtext COLLATE utf8_unicode_ci,
UNIQUE KEY `id` (`id`,`collection_id`),
KEY `brief_display` (`html`),
KEY `html` (`html`),
KEY `multiple` (`multiple`),
KEY `required` (`required`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$db->query($sql);
$this->_installOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
// Drop the table.
$db = $this->_db;
$sql = "DROP TABLE IF EXISTS `$db->ElementAdministrationSettings`";
$db->query($sql);
$this->_uninstallOptions();
}
public function hookInitialize() {
$db = get_db();
// get all the settings in the system
$sql = "SELECT e.id, IF(length(item_type.name), 'Item Type Metadata', set.name) AS set_name, e.name, e.description, e.order, s.*
FROM `$db->Elements` e
INNER JOIN {$db->prefix}element_administration_settings s ON s.id = e.id
INNER JOIN `$db->ElementSets` `set` ON set.id = e.element_set_id
LEFT JOIN {$db->prefix}item_types_elements type_element ON type_element.element_id = e.id
LEFT JOIN {$db->prefix}item_types item_type ON item_type.id = type_element.item_type_id";
$result = $db->query($sql);
// add a per-element filter for every element that has a setting defined by this plugin
while ($setting = $result->fetchObject()) {
$this->_settings[($setting->id)][($setting->collection_id)] = $setting;
add_filter(array('ElementForm', 'Item', $setting->set_name, $setting->name), array($this, 'addAdminFormSettings'));
add_filter(array('ElementInput', 'Item', $setting->set_name, $setting->name), array($this, 'addAdminInputSettings'));
}
// if this is the admin edit form
// add some javascript so client-side events (e.g. selecting a collection)
// trigger the correct settings from the plugin
if (strpos($_SERVER['REQUEST_URI'], '/admin/items/') !== FALSE) {
queue_js_string('if (!Omeka) {var Omeka = {};}Omeka.element_administration = ' . json_encode($this->_settings) . ';');
queue_js_url(url('plugins/ElementAdministration/views/admin/javascripts/items.js'));
}
}
public function hookAfterSaveCollection($args)
{
// if creating a new collection
if (!empty($args['insert'])) {
// get all settings for "All Collections"
$db = $this->_db;
$table = $db->getTable('ElementAdministrationSettings');
$select = $table->getSelect()->where('collection_id = 0');
$element_settings = $table->fetchObjects($select);
// add each setting for this new collection
foreach ($element_settings as $setting) {
// this will be the database record inserted
// make the collection ID the new collection being added
$insert = array(
'collection_id' => $args['record']->id,
);
// copy over all the element admin settings to this new record
$properties = array(
'id',
'required',
'multiple',
'html',
'brief_display',
'form_label',
'hidden_form',
'hidden_public',
'public_label',
'default_value'
);
foreach ($properties as $property) {
$insert[$property] = $setting->{$property};
}
$db->insert('ElementAdministrationSettings', $insert);
}
}
}
public function filterAdminNavigationMain($nav) {
$nav[] = array(
'label' => __('Element Administration'),
'uri' => url('element-administration'),
'resource' => 'ElementAdministration_Index',
'privilege' => 'index'
);
return $nav;
}
public function filterDisplayElements($elementSets)
{
$_elementSets = array();
$item = get_current_record('item', false);
if (!$item && strpos(current_url(), 'admin/items/add') !== FALSE) {
return;
}
if (empty($item->collection_id)) {
if (!is_object($item)) {
$item = new stdClass();
}
$item->collection_id = 0;
}
foreach ($elementSets as $set => $elements) {
$_elementSets[$set] = array();
foreach ($elements as $key => $element) {
if (isset($this->_settings[($element->id)][$item->collection_id]->public_label)
&& strlen($this->_settings[($element->id)][$item->collection_id]->public_label) > 0) {
$key = $this->_settings[($element->id)][$item->collection_id]->public_label;
}
if (empty($this->_settings[($element->id)][$item->collection_id]->hidden_public)) {
$element->hide = empty($this->_settings[($element->id)][$item->collection_id]->brief_display);
$_elementSets[$set][$key] = $element;
}
}
}
return $_elementSets;
}
public function hookDefineAcl($args)
{
// Restrict access to super and admin users.
$args['acl']->addResource('ElementAdministration_Index');
}
public function addAdminInputSettings($components, $args)
{
// get the collection ID for this item
$collection_id = isset($args['record']->collection_id) ? $args['record']->collection_id : 0;
// if no collection ID set, see if its set in the URL
$collection_id = isset($_GET['collection_id']) ? $_GET['collection_id'] : $collection_id;
// get this elements setting
$setting = isset($this->_settings[($args['element']->id)][$collection_id]) ? $this->_settings[($args['element']->id)][$collection_id] : new stdClass();
// if this element is hidden, no need to do anyt processing
// it'll get hidden in addAdminFormSettings()
if (!empty($setting->hidden_form)) {
return $components;
}
$attributes = array(
'rows' => 3,
'cols' => 50,
);
// set required if needed
if (!empty($setting->required)) {
$attributes['required'] = 'required';
}
// set default value if there is one
if (empty($args['record']->id) && strlen($setting->default_value)) {
$args['value'] = $setting->default_value;
}
$components['input'] = get_view()->formTextArea($args['input_name_stem'] . '[text]', $args['value'], $attributes);
// remove the html checkbox if that option is set
if (isset($setting->html) && $setting->html == 0) {
$components['html_checkbox'] = FALSE;
}
return $components;
}
public function addAdminFormSettings($components, $args)
{
// get the collection ID for this item
$collection_id = isset($args['record']->collection_id) ? $args['record']->collection_id : 0;
// if no collection ID set, see if its set in the URL
$collection_id = isset($_GET['collection_id']) ? $_GET['collection_id'] : $collection_id;
// get this elements setting
$setting = isset($this->_settings[($args['element']->id)][$collection_id]) ? $this->_settings[($args['element']->id)][$collection_id] : new stdClass();
// if this element is set to be hidden on the admin from, hide it
if (!empty($setting->hidden_form)) {
foreach ($components as $key => $input) {
$components[$key] = '';
}
$components['input'] = "<style>#element-{$setting->id} {display:none}</style>";
}
else {
// override the label if needed
$label = isset($setting->form_label) && strlen($setting->form_label) ? $setting->form_label : $components['label'];
$components['label'] = '<label> ' . strip_tags($label);
if (!empty($setting->required)) {
$components['label'] .= ' <span class="required"></span>';
}
$components['label'] .= '</label>';
// remove the "Add Input" button if that option is set
if (isset($setting->multiple) && $setting->multiple == 0) {
$components['add_input'] = '';
}
}
return $components;
}
}