-
Notifications
You must be signed in to change notification settings - Fork 6
/
ting_infomedia.module
293 lines (252 loc) · 6.96 KB
/
ting_infomedia.module
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
<?php
/**
* @file
* @TODO: Missing file description.
*/
// Load field hooks.
module_load_include('inc', 'ting_infomedia', 'includes/ting_infomedia.field');
module_load_include('inc', 'ting_infomedia' , 'includes/ting_infomedia.wrapper');
/**
* Implements hook_menu
*/
function ting_infomedia_menu() {
$items = array();
$items['ting/object/infomedia/%'] = array(
'title' => 'Infomedia',
'page callback' => 'ting_infomedia_view',
'page arguments' => array(3),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback function
*
* Return a render array
*/
function ting_infomedia_view($id) {
global $user;
if (!ding_user_is_provider_user($user)) {
// It's a requirement that the user is a user in the library system (the credentials will be checked in the Infomedia webservice).
drupal_set_message(t('Please log in to read infomedia articles'), 'warning');
return array(
'#prefix' => '<div class="infomedia_error">',
'#suffix' => '</div>',
'#theme' => 'status_messages',
);
}
$render_array = ting_infomedia_parse_article($id);
return array(
'#markup' => '<div id="article-wrapper">' . render($render_array). '</div>',
'#theme' => 'ting_infomedia_article',
'#attached' => array(
'css' => array(drupal_get_path('module', 'ting_infomedia') . '/css/ting_infomedia_article_view.css'),
),
);
}
/**
* Implements hook_theme().
*
* Set template for viewing ting_infomedia articles.
* Set theme-function for displaying a link (field) to ting_infomedia.
*/
function ting_infomedia_theme($existing, $type, $theme, $path) {
$render_array = array();
// Page view definitions - view articles.
$render_array['ting_infomedia_article'] = array(
'template' => 'ting_infomedia_article_view',
'render element' => 'element',
);
return $render_array;
}
/**
* Implements hook_ting_online_url().
*
* @see ting.entities.inc::getOnline_url *
*/
function ting_infomedia_ting_online_url_alter(&$url, $entity) {
if (strpos($url, '[useraccessinfomedia]') === 0) {
$url = ting_infomedia_parse_url($url);
}
}
/**
* Alter infomedia url.
*
* @param $url
* The original url.
* @return string
* The modified url.
*/
function ting_infomedia_parse_url($url) {
global $user;
$parts = array();
$url = parse_url($url, PHP_URL_QUERY);
parse_str($url, $parts);
if (!ding_user_is_provider_user($user)) {
// IF user is not logged in the login-box should be shown.
return url('ting/object/infomedia/' . $parts['faust'], array('fragment' => 'login', 'absolute' => TRUE));
}
return url('ting/object/infomedia/' . $parts['faust'], array('absolute' => TRUE));
}
/**
* Return stripped path.
*
* E.g /ting/infomedia/33514212 -> /ting/infomedia.
*
* @param string $path
* The path to strip.
*
* @return mixed
* The strip path or FALSE.
*/
function ting_infomedia_strip_path($path) {
$parts = explode('/', $path);
if (isset($parts[0]) && isset($parts[1])) {
return $parts[0] . '/' . $parts[1];
}
return FALSE;
}
/**
* Inline relation types and their titles.
*/
function ting_infomedia_get_ting_relations() {
static $types;
if (!$types) {
$types = array(
'dbcaddi:hasOnlineAccess' => t('Article from Infomedia'),
);
}
return $types;
}
/**
* Implements hook_ding_anchor_info().
*/
function ting_infomedia_ding_anchor_info() {
$relations = ting_infomedia_get_ting_relations();
// We need to know what kind of hasOnlineAccess
// is provided with this object and return relevant anchor information.
$object = menu_get_object('ting_object', 2);
if ($object->getAc_source() != 'Artikler') {
unset($relations['dbcaddi:hasOnlineAccess']);
}
return $relations;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adding infomedia url form field to ting configuration form.
*/
function ting_infomedia_form_ting_admin_ting_settings_alter(&$form, &$form_state) {
$form['ting']['ting_infomedia_url'] = array(
'#type' => 'textfield',
'#title' => t('Infomedia web service URL'),
'#description' => t('URL to the infomedia webservice (access to infomedia article base) , e.g. http://useraccessinfomedia.addi.dk/1.1/'),
'#default_value' => variable_get('ting_infomedia_url', ''),
);
}
/**
* Retrieve article from infomedia.
*
* Parse for errors, clean html and add html for error handling if needed.
*
* @return mixed
* The parsed HTML or a render array with error or FALSE if all fails.
*/
function ting_infomedia_parse_article($id) {
$obj = ting_infomedia_get_article($id);
$html = ting_infomedia_parse_for_errors($obj);
if(!$html) {
foreach($obj->parts as $part=>$val) {
if ($message = ting_infomedia_parse_for_article_error($val)) {
$val['article'] = $message;
}
$html = $val['article'];
// For now simply return the first article.
// @TODO: handle more articles
ting_infomedia_clean_html($html);
}
}
return $html;
}
/**
* Parse for general (user and/or library) errors.
*
* Take appropriate action if errors are found.
*
* @param TingClientInfomediaResult $result
* A ting client infomedia result.
*
* @return mixed
* FALSE if no error else the error as an render array.
*/
function ting_infomedia_parse_for_errors(TingClientInfomediaResult $result) {
$error = FALSE;
if ($result->error) {
watchdog('infomedia', $result->error);
drupal_set_message(t('Internal error please try again later'), 'warning');
$error = array(
'#prefix' => '<div class="infomedia_error">',
'#suffix' => '</div>',
'#theme' => 'status_messages',
);
}
return $error;
}
/**
* Parse for error on article-level.
*
* Not verified, empty etc.
*
* @param array $part
* Infomedia article in array form.
*
* @return mixed
* FALSE is not verified or article else translated string.
*/
function ting_infomedia_parse_for_article_error($part) {
$ret = FALSE;
if (!$part['verified']) {
$ret = t('Article not verified');
}
if (empty($part['article'])) {
$ret = t('Article not found on infomedia');
}
return $ret;
}
/**
* Clean HTML.
*
* @param string $html
* HTML string to clean.
*/
function ting_infomedia_clean_html(&$html) {
$patterns = array();
$patterns[0] = '/<p id="[.+]">/';
$patterns[1] = '/<hl2>/';
$patterns[2] = '/<\/hl2>/';
$replacements = array();
$replacements[0] = '<p>';
$replacements[1] = '<h4>';
$replacements[2] = '</h4>';
preg_replace($patterns, $replacements, $html);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ting_infomedia_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implement hook_ctools_plugin_api().
*/
function ting_infomedia_ctools_plugin_api($module, $api) {
if ($module == 'panels_mini' && $api == 'panels_default') {
return array('version' => 1);
}
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 1);
}
}