-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalContextsPlugin.php
More file actions
executable file
·382 lines (334 loc) · 12.9 KB
/
Copy pathLocalContextsPlugin.php
File metadata and controls
executable file
·382 lines (334 loc) · 12.9 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
if (!defined('LOCALCONTEXTS_PLUGIN_DIR')) {
define('LOCALCONTEXTS_PLUGIN_DIR', dirname(__FILE__));
}
if (!defined('LOCALCONTEXTS_HELPERS_DIR')) {
define('LOCALCONTEXTS_HELPERS_DIR', LOCALCONTEXTS_PLUGIN_DIR . '/helpers');
}
if (!defined('LOCALCONTEXTS_FORMS_DIR')) {
define('LOCALCONTEXTS_FORMS_DIR', LOCALCONTEXTS_PLUGIN_DIR . '/forms');
}
require_once LOCALCONTEXTS_PLUGIN_DIR . '/LocalContextsPlugin.php';
/**
* LocalContexts plugin.
*
* @package Omeka\Plugins\LocalContexts
*/
class LocalContextsPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'uninstall',
'admin_head',
'public_head',
'initialize',
'define_routes',
'config',
'config_form',
'before_save_item',
'admin_items_batch_edit_form',
'items_batch_edit_custom',
'public_footer'
);
protected $_filters = array(
'admin_navigation_main',
'admin_items_form_tabs',
'exhibit_layouts'
);
public function hookUninstall()
{
// Delete the plugin options
delete_option('lc_project_id');
delete_option('lc_notices');
delete_option('lc_site_language');
delete_option('lc_content_site');
}
public function hookAdminHead()
{
queue_css_file('local-contexts');
queue_js_file('local-contexts');
}
public function hookPublicHead()
{
queue_css_file('local-contexts');
}
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
public function hookDefineRoutes($args)
{
$router = $args['router'];
$indexRoute = new Zend_Controller_Router_Route('local-contexts',
array(
'module' => 'local-contexts',
'controller' => 'local-contexts',
'action' => 'index'
)
);
$router->addRoute('localContextsIndex', $indexRoute);
$assignRoute = new Zend_Controller_Router_Route('local-contexts/assign',
array(
'module' => 'local-contexts',
'controller' => 'local-contexts',
'action' => 'assign'
)
);
$router->addRoute('localContextsAssign', $assignRoute);
}
public function hookConfig($args)
{
$post = $args['post'];
set_option('lc_site_language', $post['lc_site_language']);
if ($post['lc-content']) {
set_option('lc_content_site', serialize($post['lc-content']));
} else {
delete_option('lc_content_site');
}
}
public function hookConfigForm()
{
$view = get_view();
include 'config_form.php';
}
// Save LC content selected in admin item form (below)
public function hookBeforeSaveItem($args)
{
if (!($post = $args['post'])) {
return;
}
$item = $args['record'];
self::LcContentAddToElement($item, $post);
}
public function hookPublicFooter()
{
if (get_option('lc_content_site')) {
$projects = unserialize(get_option('lc_content_site'));
$lcLanguage = get_option('lc_site_language');
$contentArray = array();
foreach ($projects as $key => $project) {
$project = json_decode($project, true);
$projectArray = array();
foreach ($project as $key => $content) {
if (is_int($key)) {
// Only print content in selected language. If 'All', print everything
if ((isset($content['language']) && $content['language'] == $lcLanguage)
|| (!isset($content['language']) && $lcLanguage == 'English')
|| $lcLanguage == 'All') {
$projectArray[] = $content;
}
}
}
// Don't print project URL if element value array is empty
if (isset($project['project_url']) && $projectArray) {
$projectArray['project_url'] = $project['project_url'];
$projectArray['project_title'] = $project['project_title'];
}
if ($projectArray) {
$lcHtml = self::renderLCNoticeHtml($projectArray, $key, true, false);
$lcArray['label'] = $lcHtml;
$lcArray['value'] = json_encode($project);
$contentArray[] = $lcArray;
}
}
$view = get_view();
echo $view->partial('site-footer.phtml', [
'lc_content' => $contentArray,
]);
}
}
/**
* Add custom fields to the item batch edit form.
*/
public function hookAdminItemsBatchEditForm()
{
$view = get_view();
$lcSiteChecked = get_option('lc_content_site') ? unserialize(get_option('lc_content_site')) : [];
$elementData = get_db()->getTable('Element')->findPairsForSelectForm(array('exclude_item_type' => true));
$lcLanguageOptions = [
'All' => __('All available languages'),
'English' => __('English'),
'French' => __('French'),
'Spanish' => __('Spanish'),
'Māori' => __('Māori'),
];
// Combine available general settings projects with existing site settings projects
$projects = get_option('lc_notices') ? unserialize(get_option('lc_notices')) : [];
foreach($lcSiteChecked as $siteProject) {
$projects[] = json_decode($siteProject, true);
}
$projects = get_option('lc_notices') ? unserialize(get_option('lc_notices')) : [];
foreach (array_unique($projects, SORT_REGULAR) as $key => $project) {
// Collapse many projects for ease of viewing
// Save each project's content as single select value
$lcHtml = self::renderLCNoticeHtml($project, $key, false, true);
$lcHtml = str_replace("lc-content[]", "custom[lc-content][]", $lcHtml);
$lcBatchContent[] = $lcHtml;
}
echo $view->partial('lc-batch-edit.phtml', [
'elementData' => $elementData,
'lcLanguageOptions' => $lcLanguageOptions,
'lc_content' => $lcBatchContent,
]);
}
/**
* Process the item batch edit form.
*
* @param array $args
*/
public function hookItemsBatchEditCustom($args)
{
$item = $args['item'];
if (!($custom = $args['custom'])) {
return;
}
self::LcContentAddToElement($item, $custom);
// Need to manually save batch edited items
$item->save();
}
// Provide LC content to item add/update form
public function filterAdminItemsFormTabs($tabs, $args)
{
$item = $args['item'];
$tabs['Local Contexts'] = [];
$view = get_view();
if (get_option('lc_notices')) {
$projects = unserialize(get_option('lc_notices'));
foreach ($projects as $key => $project) {
// Collapse many projects for ease of viewing
$lcHtml = self::renderLCNoticeHtml($project, $key, false, true);
$contentArray[] = $lcHtml;
}
$elementData = get_db()->getTable('Element')->findPairsForSelectForm(array('exclude_item_type' => true));
$languageData = [
'All' => __('All available languages'),
'English' => __('English'),
'French' => __('French'),
'Spanish' => __('Spanish'),
'Māori' => __('Māori'),
];
$tabs['Local Contexts'] = $view->partial('lc-item-edit.phtml', [
'item' => $item,
'lc_content' => $contentArray,
'element_data' => $elementData,
'language_data' => $languageData,
]);
}
return $tabs;
}
/**
* LocalContexts admin_navigation_main filter.
*
* Adds a button to the admin's main navigation.
*
* @param array $nav
* @return array
*/
public function filterAdminNavigationMain($nav)
{
$nav[] = array(
'label' => __('Local Contexts'),
'uri' => url('local-contexts'),
);
return $nav;
}
/**
* Register an exhibit layout for displaying Local Contexts content.
*
* @param array $layouts Exhibit layout specs.
* @return array
*/
public function filterExhibitLayouts($layouts)
{
$layouts['local-contexts'] = array(
'name' => __('Local Contexts'),
'description' => __('Embed Local Contexts content.')
);
return $layouts;
}
public function lcContentAddToElement($item, $post)
{
$lcContentArray = isset($post['lc-content']) ? $post['lc-content'] : [];
$lcLanguage = isset($post['lc_content_language']) ? $post['lc_content_language'] : '';
$lcElementValueArray = [];
if ($lcContentArray) {
$elementID = $post['lc_content_element'];
$element = get_db()->getTable('Element')->find($elementID);
$elementSet = get_db()->getTable('ElementSet')->find($element->element_set_id);
foreach ($lcContentArray as $project) {
$project = json_decode($project, true);
foreach($project as $key => $content) {
$lcElementArray = [];
if (is_int($key)) {
// Only print content in selected language. If 'English' or 'All',
// print everything (since English doesn't have language element)
if ((isset($content['language']) && $content['language'] == $lcLanguage)
|| (!isset($content['language']) && $lcLanguage == 'English')
|| $lcLanguage == 'All') {
$lcElementArray[] = $content;
}
if (!empty($lcElementArray)) {
if (isset($project['project_title']) && isset($project['project_url'])) {
$lcElementArray['project_title'] = $project['project_title'];
$lcElementArray['project_url'] = $project['project_url'];
}
$lcElementValue = self::renderLCNoticeHtml($lcElementArray, $projectKey = $key, true, true);
$lcElementValueArray[] = ['text' => $lcElementValue, 'html' => true];
}
}
}
}
$elementTexts = array(
$elementSet->name => array(
$element->name => $lcElementValueArray
)
);
$item->addElementTextsByArray($elementTexts);
} else {
// Do nothing if no lc content selected
return;
}
}
public static function renderLCNoticeHtml($project, $projectKey, $isPublic = false, $collapse = false) {
$view = get_view();
$projectTitle = isset($project['project_title']) ? $project['project_title'] : "Project";
$projectUrl = isset($project['project_url']) ? rtrim($project['project_url'], "/") . '/' : '';
$image_urls = array_unique(array_column($project, 'image_url'));
// Save each project's content as single select value
// Only show one image per shared notice group
$projectByImage = [];
foreach ($image_urls as $url) {
// Build new array arranged by notice image url
$noticeByImage = array_filter($project, function($child) use($url) {
if (is_array($child)) { return $child['image_url'] == $url; }
});
$projectByImage[$url] = $noticeByImage;
}
if ($isPublic) {
return $view->partial('lc-project-public.phtml', [
'projectTitle' => $projectTitle,
'projectUrl' => $projectUrl,
'projectByImage' => $projectByImage,
]);
} else if ($collapse) {
$collapseHtml = $view->partial('lc-project-collapse.phtml', [
'project' => $project,
'projectTitle' => $projectTitle,
'projectUrl' => $projectUrl,
'projectKey' => $projectKey,
'projectByImage' => $projectByImage,
]);
return $collapseHtml;
} else {
return $view->partial('lc-project-admin.phtml', [
'project' => $project,
'projectTitle' => $projectTitle,
'projectUrl' => $projectUrl,
'projectKey' => $projectKey,
'projectByImage' => $projectByImage,
]);
}
}
}