forked from DBCDK/ting_relation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ting_relation.module
202 lines (184 loc) · 5.97 KB
/
ting_relation.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
<?php
/**
* @file
* Handles relations for Ting entities.
*/
// Load Field module hooks.
module_load_include('field.inc', 'ting_relation');
/**
* Implements hook_theme().
*/
function ting_relation_theme() {
return array(
'ting_relation' => array(
'template' => 'ting_relation',
'render element' => 'elements',
'file' => 'ting_relation.theme.inc',
),
);
}
/**
* kind of a hook - called from ting.module
*/
function ting_relation_anchor_info() {
$relations = ting_relation_inline();
// 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->getAcSource() != 'Ebrary') {
unset($relations['dbcaddi:hasOnlineAccess']);
}
unset($relations['dbcaddi:isReviewOf']);
return $relations;
}
/**
* Inline relation types and their titles.
*/
function ting_relation_inline() {
static $types;
if (!$types) {
$types = array(
'dbcaddi:hasCreatorDescription' => t('Author portrait'),
'dbcaddi:hasReview' => t('Review'),
'dbcaddi:hasSubjectDescription' => t('Subject description'),
'dbcbib:isPartOfManifestation' => t('Is part of'),
'isReviewOf' => t('Is review of'),
'hasOnlineAccess' => t('Article from Ebrary'),
);
}
return $types;
}
/**
* Implements hook_form_FORM_ID_alter()
* Adding ebrary url form field to Ting configurating form
* Set required to FALSE - this is a part of the relation-module, and not everybody wants or has access to ebrary
* TODO this field requires configuration. Maybe it should be moved to a module of its own?
* this url is restricted ;'http://site.ebrary.com/lib/metropol/docDetail.action?docID=';
* - probably for metropol access.
*/
function ting_relation_form_ting_admin_ting_settings_alter(&$form, &$form_state) {
$form['ting']['ting_ebrary_url'] = array(
'#type' => 'textfield',
'#title' => t(' URL to ebrary '),
'#description' => t('BASE URL to site.ebrary.com e.g.https://site.ebrary.com/lib/librarytitles/docDetail.action?docID='),
'#required' => FALSE,
'#default_value' => variable_get('ting_ebrary_url', ''),
);
}
/**
* Implements hook_ding_entity_buttons().
*/
function ting_relation_ding_entity_buttons($type, $entity) {
if ($entity instanceof TingEntity) {
$relation = NULL;
$markup_url = NULL;
$use_absolute = FALSE;
$markup_text = t('See online');
$class = array(
'btn',
'btn-artesis-turquoise',
'see-online',
);
// Seek for a online version of related material.
foreach ($entity->getRelations() as $rel) {
if ($rel->type == 'dbcaddi:hasOnlineAccess') {
$relation = $rel;
break;
}
}
$ebrary_url = variable_get('ting_ebrary_url', '');
// Replace the online material link to related stuff if present.
if ($relation) {
if (strstr($relation->uri, '[URL]http')) {
$markup_url = str_replace('[URL]', '', htmlspecialchars_decode($relation->uri));
$use_absolute = TRUE;
}
elseif (strstr($relation->uri, '[URL]')) {
$markup_url = str_replace('[URL]', $ebrary_url, $relation->uri);
}
elseif (strstr($relation->uri, '[useraccessinfomedia]')) {
$markup_url = 'ting/infomedia/ajax/' . $entity->getLocalId();
$class[] = 'use-ajax';
}
}
$knowledge_source = $entity->getAcSource() === 'UC Viden' ;
// Get link of the entity with type film that has online access.
$film_online_access = $entity->getType() === 'Film (net)' && $relation->type === 'dbcaddi:hasOnlineAccess';
if (!$markup_url && $relation && ($knowledge_source || $film_online_access)) {
$markup_url = htmlspecialchars_decode($relation->uri);
$use_absolute = TRUE;
}
$entity_url = $entity->getOnlineUrl();
if (!$markup_url && $entity_url) {
$settings = variable_get('ting_url_labels', _ting_default_url_labels());
$type = drupal_strtolower($entity->getType());
$label = !empty($settings[$type]) ? $settings[$type] : $settings['_default'];
$markup_text = t($label);
$markup_url = $entity->getOnlineUrl();
// Make sure the url is not array.
if (is_array($markup_url)) {
$markup_url = reset($markup_url);
}
preg_match('/ebrary.*id(?>=|\/)([^&\/]+)/i', $markup_url, $matches);
if(isset($matches[1]) && !empty($matches[1])) {
$markup_url = $ebrary_url . $matches[1];
}
$use_absolute = TRUE;
}
elseif(!$markup_url && !$entity_url && $relation && $relation->uri) {
$markup_url = $relation->uri;
}
if (!empty($markup_url)) {
$altered_url = module_invoke_all('ting_online_url_alter', $markup_url);
$markup_url = urldecode($altered_url[0]);
return array(
array(
'#theme' => 'link',
'#text' => '<i class="icon-white icon-globe"></i> ' . $markup_text,
'#path' => $markup_url,
'#options' => array(
'attributes' => array(
'class' => $class,
'target' => '_blank',
),
'html' => TRUE,
'absolute' => $use_absolute,
),
'#weight' => 107,
),
);
}
}
}
/**
* Get relation title by type.
*
* @param string $type
* Relation type.
* @return string
* Relation title.
*/
function _ting_relation_title($type) {
$titles = ting_relation_inline();
if (!empty($titles[$type])) {
return $titles[$type];
}
return NULL;
}
/**
* Implements hook_field_group_pre_render().
*/
function ting_relation_field_group_pre_render(&$element, $group, &$form) {
foreach ($element as $elem) {
if (!isset($elem['#object'])) {
continue;
}
$id = $group->group_name . '_' . str_ireplace(':', '-', $elem['#object']->id);
break;
}
if ($group->format_type == 'fieldset') {
$add['#prefix'] = '<div id="' . $id . '" class="field-group-format ' . $group->group_name . ' toggle">';
$add['#suffix'] = '</div>';
$element += $add;
}
}