-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEhriDataPlugin.php
106 lines (83 loc) · 3.03 KB
/
EhriDataPlugin.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
<?php
require_once(__DIR__.'/vendor/autoload.php');
class EhriDataPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array('public_head');
private $twig;
private $TEMPLATES = array(
'Repository' => 'institution.twig',
'HistoricalAgent' => 'authority.twig',
'DocumentaryUnit' => 'unit.twig',
'VirtualUnit' => 'virtual.twig',
'Country' => 'country.twig'
);
const API_MIMETYPE = 'application/vnd.api+json';
const DEFAULT_API_BASE = 'https://portal.ehri-project.eu/api/v1/';
const DEBUG = true;
public function __construct()
{
Requests::register_autoloader();
$loader = new Twig_Loader_Filesystem(array(
dirname(__FILE__) . '/views/public/templates'
));
$this->twig = new Twig_Environment($loader, array(
'debug' => self::DEBUG,
'cache' => self::DEBUG ? false : dirname(__FILE__) . '/cache',
));
$this->twig->addExtension(new Twig_Extensions_Extension_Text());
$this->twig->addExtension(new Twig_Extensions_Extension_Date());
}
public function hookPublicHead($args)
{
queue_css_file('ehri-shortcode');
}
public function setUp()
{
add_shortcode('ehri_item_data', array($this, 'ehri_item_data'));
add_plugin_hook('config_form', array($this, 'ehri_shortcode_config_form'));
add_plugin_hook('config', array($this, 'ehri_shortcode_config'));
parent::setUp();
}
public function ehri_shortcode_config_form()
{
include(dirname(__FILE__) . '/views/admin/config_form.php');
}
public function ehri_shortcode_config()
{
set_option('ehri_shortcode_uri_configuration', trim($_POST['ehri_shortcode_uri_configuration']));
}
/**
* @param $args
* @param $view
* @return string
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function ehri_item_data($args, $view)
{
$id = $args["id"];
$headers = ['Accept' => self::API_MIMETYPE];
$base = get_option('ehri_shortcode_uri_configuration', self::DEFAULT_API_BASE);
try {
$response = Requests::get($base . $id, $headers);
} catch (Exception $e) {
error_log($e->getTraceAsString());
return '<pre>Error requesting EHRI API data.</pre>';
}
if (!$response->success) {
return '<pre>Error requesting EHRI API data: [' . $response->status_code . ']: ' . $response->body . '</pre>';
}
$json = json_decode($response->body, true);
$type = $json['data']['type'];
$data = $json['data'];
// If there is 'included' data at the top level, move it
// into the main data array...
if (array_key_exists("included", $json)) {
$data["included"] = $json["included"];
}
return array_key_exists($type, $this->TEMPLATES)
? $this->twig->render($this->TEMPLATES[$type], $data)
: "";
}
}