Project

General

Profile

Download (9.71 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Media theming functions.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 */
15

    
16
/**
17
 * @todo Please document this function.
18
 * @see http://drupal.org/node/1354
19
 */
20
function media_content_type_dir($media_representation, $default = FALSE) {
21
  if ($media_representation->mimeType) {
22
    return substr($media_representation->mimeType, 0, stripos($media_representation->mimeType, '/'));
23
  }
24
  else {
25
    return $default;
26
  }
27
}
28

    
29
/**
30
 * @todo Please document this function.
31
 * @see http://drupal.org/node/1354
32
 */
33
function getimagesize_remote($image_url) {
34
  $response = cdm_http_request($image_url);
35
  $contents = NULL;
36
  if (isset($response->data)) {
37
    $contents = $response->data;
38
  } else {
39
    return FALSE;
40
  }
41

    
42
  $last_level = error_reporting (E_ERROR); // suppress warnings from the imagecreatefromstring() method,
43
  $im = imagecreatefromstring($contents);
44
  error_reporting($last_level); // reset the error_reporting level
45

    
46
  if (!$im) {
47
    return FALSE;
48
  }
49
  $gis[0] = ImageSX($im);
50
  $gis[1] = ImageSY($im);
51
  // Array member 3 is used below to keep with current getimagesize standards.
52
  $gis[3] = "width={$gis[0]} height={$gis[1]}";
53
  ImageDestroy($im);
54
  return $gis;
55
}
56

    
57
/**
58
 * Creates the markup for the media associated a DescriptionElement instance.
59
 *
60
 * @param $descriptionElement
61
 *    the DescriptionElement instance
62
 * @param $mimeTypePreference array
63
 *    An array of mime type strings. the order of the mimetypes is the order of preference.
64
 *    E.g.: array('application/pdf','image/jpeg')
65
 *
66
 * @return string
67
 *    The markup
68
 *
69
 * FIXME: move to descriptions.inc
70
 */
71
function cdm_description_element_media($descriptionElement, $mimeTypePreference) {
72

    
73
  $out = '';
74

    
75
  _add_js_thickbox();
76

    
77
  $feature = $descriptionElement->feature;
78
  $medias = $descriptionElement->media;
79

    
80
  foreach ($medias as $media) {
81
    $prefRepresentations = cdm_preferred_media_representations($media, $mimeTypePreference, 300, 400);
82
    $mediaRepresentation = array_shift($prefRepresentations);
83
    if ($mediaRepresentation) {
84

    
85
      $contentTypeDirectory = media_content_type_dir($mediaRepresentation);
86

    
87
      $out .= theme('cdm_media_mime_' . $contentTypeDirectory, array('mediaRepresentation' => $mediaRepresentation, 'feature' => $feature));
88
    }
89
    else {
90
      // Media has empty or corrupt representation
91
      if(user_is_logged_in()){
92
        drupal_set_message('The media entity (' . l($media->uuid, path_to_media($media->uuid)) .') has empty or corrupt representation parts. Maybe the URI is empty.' , 'warning');
93
      }
94
    }
95
  }
96
  return $out;
97
}
98

    
99
/**
100
 * @todo Please document this function.
101
 * @see http://drupal.org/node/1354
102
 */
103
function theme_cdm_media_mime_application($variables) {
104
  $mediaRepresentation = $variables['mediaRepresentation'];
105
  $feature = $variables['feature'];
106
  $out = '';
107
  foreach ($mediaRepresentation->parts as $part) {
108
    $attributes = array(
109
      'title' => theme('cdm_feature_name', array('feature_name' => $feature->representation_L10n)),
110
      'target' => '_blank',
111
    );
112
    /*
113
    $attributes = array('title'=>$feature->representation_L10n,'target'=>'_blank');
114
    $attributes = array('title'=>'original publication', 'target'=>'_blank');
115
    */
116
    $out .= l(media_feature_icon($feature, $part->uri), $part->uri, array(
117
      'attributes' => $attributes,
118
      'absolute' => TRUE,
119
      'html' => TRUE,
120
    ));
121
  }
122
  return $out;
123
}
124

    
125

    
126
/**
127
 * Creates the markup for a CDM Media instance.
128
 *
129
 * (This method is currently only called from within theme_cdm_media())
130
 *
131
 * @param $variables
132
 *    An associative array
133
 * @return string
134
 *    The markup
135
 */
136
function theme_cdm_media_mime_image($variables) {
137
  $media_representation = $variables['mediaRepresentation'];
138
  $feature = $variables['feature'];
139
  $out = '';
140
  // TODO thickbox is not used anymore -> delete?
141
  $attributes = array(
142
    'class' => 'thickbox',
143
    'rel' => 'representation-' . $media_representation->uuid,
144
    'title' => $feature->representation_L10n,
145
  );
146
  for ($i = 0; $part = $media_representation->parts[$i]; $i++) {
147
    if ($i == 0) {
148
      $out .= l(media_feature_icon($feature, $part->uri), $part->uri, array(
149
        'attributes' => $attributes,
150
        'absolute' => TRUE,
151
        'html' => TRUE,
152
      ));
153
    }
154
    else {
155
      $out .= l('', $part->uri, array(
156
        'attributes' => $attributes,
157
        'absolute' => TRUE,
158
      ));
159
    }
160
  }
161
  return $out;
162
}
163

    
164
/**
165
 * @todo Please document this function.
166
 * @see http://drupal.org/node/1354
167
 */
168
function theme_cdm_media_mime_text($variables) {
169
  $representation = $variables['mediaRepresentation'];
170
  $feature = $variables['feature'];
171
  $out = '';
172
  if (!empty($representation->parts)) {
173
    foreach ($representation->parts as $part) {
174
      $attributes = array(
175
        'title' => theme('cdm_feature_name', array('feature_name' => $feature->representation_L10n)),
176
        'target' => '_blank',
177
      );
178
      // $attributes = array('title'=>t('original publication'),
179
      // 'target'=>'_blank');
180
      $out .= l(media_feature_icon($feature, $part->uri), $part->uri, array(
181
        'attributes' => $attributes,
182
        'absolute' => TRUE,
183
        'html' => TRUE,
184
      ));
185
    }
186
  }
187
  return $out;
188
}
189

    
190

    
191
/**
192
 * Theme function for captions of media elements. This method is usually called from
193
 * within the theme_cdm_media_gallerie() function or indirectly via AHAH.
194
 *
195
 * @param array $variables
196
 *   an associative array with the following elements:
197
 *   - 'media': the cdm media object to show the captions for
198
 *   - 'elements':
199
 *         an array which defining the caption elements to show up
200
 *         example:
201
 *          Show 'title', 'description', 'file', 'filename' in the caption:
202
 *          array('title', 'description', 'file', 'filename')
203
 *
204
 * @return string
205
 *   the themed html output
206
 *
207
 * TODO turn into compose method
208
 */
209
function theme_cdm_media_caption($variables) {
210

    
211
  $media = $variables['media'];
212
  $elements = $variables['elements'];
213
  $sources_as_content = $variables['sources_as_content'];
214
  $media_metadata = read_media_metadata($media);
215

    
216
  $doTitle = !$elements || array_search('title', $elements) !== FALSE;
217
  $doDescription = !$elements || array_search('description', $elements) !== FALSE;
218
  $doArtist = !$elements || array_search('artist', $elements) !== FALSE;
219
  $doMediacreated = true; //!$elements || array_search('mediacreated', $elements) !== FALSE;
220
  $doLocation = !$elements || array_search('location', $elements) !== FALSE;
221
  $doRights = !$elements || array_search('rights', $elements) !== FALSE;
222

    
223
  $descriptionPrefix = "";
224
  $footnote_list_key = 'media-' . $media->uuid;
225

    
226
  // handle sources and annotations
227
  cdm_lazyload_array_field('media', 'annotations', $media);
228
  RenderHints::setAnnotationsAndSourceConfig([
229
      'sources_as_content' => $sources_as_content,
230
      'link_to_name_used_in_source' => TRUE,
231
      'link_to_reference' => FALSE,
232
      'add_footnote_keys' => !$sources_as_content,
233
      'bibliography_aware' => FALSE
234
    ]);
235
  $annotations_and_sources = handle_annotations_and_sources(
236
    $media, NULL, $footnote_list_key
237
  );
238

    
239
  $out = '';
240

    
241
  // Title.
242
  if ($doTitle) {
243
    $title_string = $media_metadata['title']
244
      . $annotations_and_sources->footNoteKeysMarkup(); // placing the footnote keys here is not optimal, see #6329 A.1.
245
    $title_string = trim($title_string);
246
    if (empty($title_string) && !($doDescription && $media_metadata['description'])) {
247
      // Use filename as fallback option if no description and no source citations are available.
248
      $title_string = $media_metadata['filename'];
249
    }
250
    $out .= '<div class="title">' . $title_string . '</div>';
251
  }
252

    
253
  // Description.
254
  if ($media_metadata['description'] && $doDescription) {
255
    $out .= '<p class="description">' . $media_metadata['description'] . '</p>';
256
  }
257

    
258
  $groups = array();
259

    
260
  $out .= '<dl class="media-caption">';
261
  // Artist.
262
  if ($media_metadata['artist'] && $doArtist) {
263
    _description_list_group_add($groups, t('Artist') . ':', $media_metadata['artist'] );
264
  }
265
  // MediaCreated
266
  if ($media_metadata['mediacreated'] && $doMediacreated) {
267
    _description_list_group_add($groups, t('Created') . ':', $media_metadata['mediacreated'] );
268
  }
269
  // Location.
270
  if ($doLocation) {
271
    $location = '';
272
    $location .= $media_metadata['location']['sublocation'];
273
    if ($location && $media_metadata['location']['city']) {
274
      $location .= ', ';
275
    }
276
    $location .= $media_metadata['location']['city'];
277
    if ($location && $media_metadata['location']['province']) {
278
      $location .= ', ';
279
    }
280
    $location .= $media_metadata['location']['province'];
281
    if ($location && $media_metadata['location']['country']) {
282
      $location .= ' (' . $media_metadata['location']['country'] . ')';
283
    }
284
    else {
285
      $location .= $media_metadata['location']['country'];
286
    }
287
    if ($location) {
288
      _description_list_group_add($groups, t('Location') . ':', $location );
289
    }
290
  }
291
  // Rights.
292
  if ($doRights) {
293
    $groups = array_merge($groups, cdm_rights_as_dl_groups($media_metadata['rights']));
294
  }
295

    
296
  if($annotations_and_sources->hasSourceReferences()){
297
    $sources_label = count($annotations_and_sources->getSourceReferences()) > 1 ? t('Sources') : t('Source');
298
    _description_list_group_add($groups, $sources_label . ':', markup_to_render_array(join('; ', $annotations_and_sources->getSourceReferences())) );
299
  }
300

    
301
  // TODO add all other metadata elements generically.
302

    
303
  $description_list_item = array(
304
    '#theme' => 'description_list',
305
    '#groups' => $groups,
306
    '#attributes' => array('class' => 'media-caption')
307
  );
308
  $out .= drupal_render($description_list_item);
309

    
310
  $out .= render_footnotes($footnote_list_key);
311

    
312
  return $out;
313
}
314

    
(3-3/8)