-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTaxonomyPlugin.php
199 lines (175 loc) · 6.11 KB
/
TaxonomyPlugin.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
<?php
class TaxonomyPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'initialize',
'admin_head',
);
protected $_filters = array(
'admin_navigation_main',
'element_types_info',
);
public function hookInstall()
{
$db = $this->_db;
$sql = "
CREATE TABLE IF NOT EXISTS {$db->Taxonomy} (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS {$db->TaxonomyTerm} (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
taxonomy_id int(10) unsigned NOT NULL,
code varchar(255) NOT NULL,
value varchar(255) NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY (taxonomy_id) REFERENCES {$db->Taxonomy} (id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
";
$db->query($sql);
}
public function hookUninstall()
{
$db = $this->_db;
$db->query("DROP TABLE IF EXISTS {$db->TaxonomyTerm}");
$db->query("DROP TABLE IF EXISTS {$db->Taxonomy}");
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
public function hookAdminHead($args)
{
queue_js_file('taxonomy');
}
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Taxonomy'),
'uri' => url('taxonomy/taxonomy/list'),
);
return $nav;
}
public function filterElementTypesInfo($types) {
$types['taxonomy-term'] = array(
'label' => __('Taxonomy term'),
'filters' => array(
'ElementInput' => array($this, 'filterElementInput'),
'Save' => array($this, 'filterSave'),
'Display' => array($this, 'filterDisplay'),
),
'hooks' => array(
'OptionsForm' => array($this, 'hookOptionsForm'),
),
);
return $types;
}
public function filterElementInput($components, $args)
{
$view = get_view();
$db = $this->_db;
$element = $args['element'];
$element_id = $element->id;
$index = $args['index'];
$name = "Elements[$element_id][$index][text]";
$taxonomy_id = $args['element_type_options']['taxonomy_id'];
$open = !empty($args['element_type_options']['open']);
if ($taxonomy_id) {
$terms = $db->getTable('TaxonomyTerm')->listByTaxonomy($taxonomy_id);
$options = array('' => '') + $terms;
if ($open) {
$options['insert_new_term'] = '> ' . __('Add a new code') . ' <';
}
$components['input'] = $view->formSelect($name, $args['value'],
array('class' => 'taxonomy taxonomy-open'), $options);
if ($open) {
$components['input'] .= $view->formText(
'taxonomy_input_' . $element_id,
'',
array(
'placeholder' => __('New code'),
'class' => 'taxonomy taxonomy-open',
'style' => 'display: none',
));
$components['input'] .= $view->formButton(
'taxonomy_insert_' . $element_id,
__('Enter new code'),
array(
'class' => 'taxonomy taxonomy-open button blue small',
'style' => 'display: none',
));
$components['input'] .= $view->formNote(null,
'<p class="taxonomy-open description help-block" style="display: none;">'
. html_escape(__('Note: Only the selected entry will be permanently added to the list.'))
. '</p>');
}
$components['html_checkbox'] = null;
}
return $components;
}
public function filterSave($text, $args)
{
$text = trim($text);
if (!strlen($text)) {
return $text;
}
if ($text == 'insert_new_term') {
return '';
}
$closed = empty($args['element_type_options']['open']);
if ($closed) {
return $text;
}
// Check if the term doesn't exist (prevent some issues).
$taxonomy_id = $args['element_type_options']['taxonomy_id'];
if (!$taxonomy_id) {
return $text;
}
$db = $this->_db;
$term = $db->getTable('TaxonomyTerm')->findByCode($taxonomy_id, $text);
if ($term) {
return $text;
}
// TODO The text should be already filtered by Omeka.
$term = new TaxonomyTerm();
$term->taxonomy_id = $taxonomy_id;
$term->code = $text;
$term->value = $text;
$term->save();
return $text;
}
public function filterDisplay($text, $args)
{
$db = $this->_db;
$taxonomy_id = $args['element_type_options']['taxonomy_id'];
if ($taxonomy_id) {
$term = $db->getTable('TaxonomyTerm')->findByCode($taxonomy_id, $text);
if ($term) {
$text = $term->value;
}
}
return $text;
}
public function hookOptionsForm($args)
{
$view = get_view();
$db = $this->_db;
$options = $args['element_type']['element_type_options'];
$taxonomies = $db->getTable('Taxonomy')->findAll();
$taxonomy_options = array('' => '');
foreach ($taxonomies as $taxonomy) {
$taxonomy_options[$taxonomy->id] = $taxonomy->name;
}
echo $view->partial('taxonomy/option-form.php', array(
'options' => $options,
'taxonomy_options' => $taxonomy_options,
));
}
}