This repository was archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTeiDisplayPlugin.php
309 lines (251 loc) · 7.24 KB
/
TeiDisplayPlugin.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
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4; */
/**
* Plugin manager class.
*
* @package omeka
* @subpackage teidisplay
* @copyright 2012 Rector and Board of Visitors, University of Virginia
* @license http://www.apache.org/licenses/LICENSE-2.0.html
*/
class TeiDisplayPlugin extends Omeka_Plugin_AbstractPlugin
{
// Hooks.
protected $_hooks = array(
'install',
'uninstall',
'define_routes',
'after_save_item',
'config_form',
'config'
);
// Filters.
protected $_filters = array(
'admin_navigation_main',
'admin_items_form_tabs',
'action_contexts',
'response_contexts'
);
// XML mime types.
protected $_xmlMimeTypes = array(
'application/xml',
'text/xml'
);
/**
* Get tables.
*
* @return void.
*/
public function __construct()
{
parent::__construct();
$this->_texts = $this->_db->getTable('TeiDisplayText');
}
// ------
// Hooks.
// ------
/**
* Create tables.
*
* @return void.
*/
public function hookInstall()
{
// Stylesheets table.
$tableName = $this->_db->TeiDisplayStylesheet;
$sql = "CREATE TABLE IF NOT EXISTS `$tableName` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` tinytext collate utf8_unicode_ci,
`xslt` TEXT COLLATE utf8_unicode_ci NULL,
`modified` TIMESTAMP NULL,
PRIMARY KEY (`id`)
) ENGINE=innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$this->_db->query($sql);
// Texts table.
$tableName = $this->_db->TeiDisplayText;
$sql = "CREATE TABLE IF NOT EXISTS `$tableName` (
`id` int(10) unsigned NOT NULL auto_increment,
`item_id` int(10) unsigned NOT NULL,
`file_id` int(10) unsigned NOT NULL,
`sheet_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$this->_db->query($sql);
// Whitelist XML extension.
$extensions = get_option('file_extension_whitelist');
if (strpos($extensions, 'xml') === false) {
set_option('file_extension_whitelist',
get_option('file_extension_whitelist').',xml');
}
// Whitelist XML mime type.
$mimes = get_option('file_mime_type_whitelist');
if (strpos($mimes, 'application/xml') === false) {
set_option('file_mime_type_whitelist',
get_option('file_mime_type_whitelist').',application/xml');
}
}
/**
* Drop tables.
*
* @return void.
*/
public function hookUninstall()
{
// Drop stylesheets.
$tableName = $this->_db->TeiDisplayStylesheet;
$sql = "DROP TABLE IF EXISTS `$tableName`";
$this->_db->query($sql);
// Drop texts.
$tableName = $this->_db->TeiDisplayText;
$sql = "DROP TABLE IF EXISTS `$tableName`";
$this->_db->query($sql);
}
/**
* Register routes.
*
* @param object $router Router passed in by the front controller.
*
* @return void.
*/
public function hookDefineRoutes($args)
{
// Default stylesheets routes.
$args['router']->addRoute(
'teiDisplayStylesheetsDefault',
new Zend_Controller_Router_Route(
'tei/stylesheets/:action',
array(
'module' => 'tei-display',
'controller' => 'stylesheets',
'action' => 'browse'
)
)
);
// Stylesheet-specific routes.
$args['router']->addRoute(
'teiDisplayStylesheetsId',
new Zend_Controller_Router_Route(
'tei/stylesheets/:action/:id',
array(
'module' => 'tei-display',
'controller' => 'stylesheets'
),
array(
'id' => '\d+'
)
)
);
}
/**
* Render configuration form.
*
* @return void.
*/
public function hookConfigForm()
{
$form = new TeiDisplay_Form_Header();
$form->removeDecorator('form');
echo $form;
}
/**
* Save configuration form.
*
* @return void.
*/
public function hookConfig($args)
{
$this->_texts->saveTeiMappings($args['post']);
}
/**
* Process Item add/edit TEI tab.
*
* @param array $args The hook arguments, with keys 'record'
* and 'post'.
*
* @return void.
*/
public function hookAfterSaveItem($args)
{
// If post is defined.
if ($args['post']) { // TODO: wtf?
// Create or update the text.
$text = $this->_texts->createOrUpdate($args['record'],
(int) $args['record']['teistylesheet'],
(int) $args['record']['teitext']
);
// Import header.
if ((bool) $args['record']['teiimport']) {
$this->_texts->importTeiHeader($args['record']);
}
}
}
// --------
// Filters.
// --------
/**
* Add TEI tab to main admin menu bar.
*
* @param array $tabs This is an array of label => URI pairs.
*
* @return array The tabs array with the TEI tab.
*/
public function filterAdminNavigationMain($tabs)
{
$tabs[] = array('label' => 'TEI', 'uri' => url('tei/stylesheets'));
return $tabs;
}
/**
* Add TEI tab to item add/edit form.
*
* @param array $tabs This is an array of label => URI pairs.
*
* @return array The tabs array with the TEI tab.
*/
public function filterAdminItemsFormTabs($tabs)
{
// Construct the form.
$item = get_current_record('item');
$form = new TeiDisplay_Form_Text(array('item' => $item));
$form->removeDecorator('form');
// If the item exists.
if ($item->exists()) {
// Try to get a text.
$text = $this->_texts->findByItem($item);
// Populate fields.
if ($text) $form->populate(array(
'teitext' => $text->file_id,
'teistylesheet' => $text->sheet_id
));
}
// Add tab.
$tabs['TEI'] = $form;
return $tabs;
}
/**
* Register `tei` action context.
*
* @param array $contexts The action contexts.
*
* @return array $contexts The modified array.
*/
public function filterActionContexts($contexts)
{
$contexts['show'][] = 'tei';
return $contexts;
}
/**
* Register the `tei` response context.
*
* @param array $context The current context.
*
* @return array $context The modified context.
*/
public function filterResponseContexts($contexts)
{
$contexts['tei'] = array(
'suffix' => 'tei',
'headers' => array('Content-Type' => 'text/html')
);
return $contexts;
}
}