summaryrefslogtreecommitdiff
path: root/timelinejs.module
blob: 1afff833bcf0c28f23ac5aff4f4a7069ee03ae72 (plain)
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
<?php

/**
 * @file
 * TimelineJS module.
 */

/**
 * Implements hook_node_view()
 */
function timelinejs_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'timeline' && $view_mode == 'full') {
    if (isset($node->nid)) {
      $node->content['timeline']['#markup'] = timelinejs($node->nid);
    }
    else {
      $node->content['timeline']['#markup'] = t('Timeline will be rendered just after saving it.');
    }

    $node->content['timeline']['#weight'] = -1;
  }
}

/**
 * Implements hook_menu()
 */
function timelinejs_menu() {
  $items['timelinejs/json'] = array(
    'page callback'    => 'timelinejs_json',
    'access arguments' => array('access content'),
    'type'             => MENU_CALLBACK,
  );

  $items['timeline'] = array(
    'page callback'    => 'timelinejs',
    'access arguments' => array('access content'),
    'type'             => MENU_CALLBACK,
  );

  $items['widgets/timeline'] = array(
    'page callback'    => 'timelinejs_widget',
    'access arguments' => array('access content'),
    'type'             => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_theme()
 */
function timelinejs_theme($existing, $type, $theme, $path) {
  return array(
    'timelinejs' => array(
      'template'  => 'timelinejs',
      'variables' => array(
        'module_path'   => drupal_get_path('module', 'timelinejs'),
        'nid'           => NULL,
        'width'         => NULL,
        'height'        => NULL,
        'source'        => NULL,
        'start_at_end'  => 'false',
        'hash_bookmark' => 'true',
        'css'           => NULL,
        'js'            => '/' . timelinejs_library_path() . '/compiled/js/timeline.js',
        'font'          => NULL,
        'lang'          => NULL,
        'lang_js'       => NULL,
      ),
    ),
    'timelinejs_embed' => array(
      'template'  => 'timelinejs_embed',
      'variables' => array(
        'nid'           => NULL,
        'lang'          => NULL,
      ),
    ),
    'timelinejs_json' => array(
      'template'  => 'timelinejs_json',
      'variables' => array('headline', 'date', 'text', 'media', 'events'),
    ),
    'timelinejs_json_event' => array(
      'template'  => 'timelinejs_json_event',
      'variables' => array('event', 'date_format'),
    ),
    'timelinejs_print' => array(
      'template'  => 'timelinejs_print',
      'variables' => array('headline', 'date', 'text', 'media', 'events'),
    ),
    'timelinejs_print_event' => array(
      'template'  => 'timelinejs_print_event',
      'variables' => array('event', 'date_format'),
    ),
  );
}

/**
 * Determine the library path.
 */
function timelinejs_library_path() {
  // We have to use "timeline" instead of "timelinejs" because of
  // this issue: https://github.com/VeriteCo/TimelineJS/issues/403
  return libraries_get_path('timeline');
}

/**
 * Get a score nid from a timeline nid.
 */
function timelinejs_load_events($nid) {
  $query = db_select('field_data_field_timeline', 't');

  // Basic query.
  $query
    ->fields('t', array('entity_id'))
    ->condition('t.bundle', 'event')
    ->condition('t.field_timeline_target_id', $nid, '=');

  $result = $query->execute();

  return $result->fetchAll();
}

/**
 * Load all events from a timeline.
 */
function timelinejs_fetch_items($nid, $format = 'json') {
  // Sanitization and basic data.
  $nid         = (int) $nid;
  $timeline    = node_load($nid);
  $nodes       = timelinejs_load_events($nid);

  if (isset($timeline->field_date_format['und'][0]['value'])) {
    $date_format = check_plain($timeline->field_date_format['und'][0]['value']);
  }
  else {
    $date_format = '%Y,%m,%d';
  }

  foreach ($nodes as $node) {
    $items[] = node_load($node->entity_id);
  }

  if (empty($items)) {
    $items[0]                                = new stdClass();
    $items[0]->field_date['und'][0]['value'] = NULL;
    $items[0]->title                         = t('No items');
    $items[0]->body['und'][0]['value']       = t('Currently there are no items in this timeline.');
  }

  foreach ($items as $event) {
    if (!isset($event->body['und'])) {
      $event->body['und'] = NULL;
    }

    $output[] = theme('timelinejs_' . $format . '_event', array('event' => $event, 'date_format' => $date_format));
    if ($format == 'json') {
      $events = implode(',', $output);
    }
    else {
      $events = implode($output);
    }
  }

  $text     = $media = NULL;
  $headline = $timeline->title;
  $date     = NULL;

  return array(
    'headline' => $headline,
    'date'     => $date,
    'text'     => $text,
    'media'    => $media,
    'events'   => $events,
  );
}

/**
 * Menu callback.
 */
function timelinejs_json($nid) {
  header('Content-type: application/json; charset=utf-8', true);
  print theme('timelinejs_json', timelinejs_fetch_items($nid));

  // Avoid themable output.
  exit;
}

/**
 * Menu callback.
 */
function timelinejs_widget($nid = NULL, $width = '960px', $height = '500px') {
  // Check parameters.
  if ($nid == NULL) {
    print t('Page not found');
    exit;
  }
  else {
    $node = node_load($nid);

    if ($node == FALSE) {
      print t('Page not found');
      exit;
    }
  }

  // Format output and include timeline.
  $output  = '<!DOCTYPE html>';
  $output .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
  $output .= '<html style="height: 100%;"><head>';
  $output .= '<title>' . t('Timeline: @title', array('@title' => $node->title)) . '</title>';
  $output .= '<script type="text/javascript">timelineWidget = true;</script>';
  $output .= '</head><body class="widget timeline-widget" style="height: 100%; margin: 0px;">';
  $output .= timelinejs($nid, $width, $height);
  $output .= '</body></html>';

  // Avoid themeable output.
  print $output;
  exit;
}

/**
 * Menu callback.
 */
function timelinejs($nid = NULL, $width = '960px', $height = '600px') {
  global $language;
  global $theme;
  global $base_url;

  if ($nid == NULL) {
    drupal_not_found();
  }

  $node         = node_load($nid);
  $theme_path   = drupal_get_path('theme', $theme);
  $library_path = timelinejs_library_path();

  // Determine stylesheet
  if (file_exists($theme_path . '/timeline.css')) {
    $css = '/' . $theme_path . '/timeline.css';
  }
  else {
    $css = '/' . $library_path . '/compiled/css/timeline.css';
  }

  // Determine data source
  if (isset($node->field_source['und'][0]['value'])) {
    $source = $node->field_source['und'][0]['value'];
  }
  else {
    $source = $base_url . '/timelinejs/json/' . (int) $nid;
  }

  // Determine dimensions
  if ($width == 'full') {
    $width = '100%';
  }

  if ($height == 'full') {
    $height = '100%';
  }

  $output = theme('timelinejs', array(
    'nid'          => $nid,
    'lang'         => $language->language,
    'width'        => $width,
    'height'       => $height,
    'source'       => $source,
    'css'          => $css,
    // Use timelinejs from CDN until we fix it's direct inclusion
    // See https://gitlab.com/socioambiental/widgets/issues/2
    'js'           => 'https://cdn.knightlab.com/libs/timeline/latest/js/timeline-min.js',
    //'js'         => '/' . $library_path . '/compiled/js/timeline-min.js',
    'lang_js'      => '/' . $library_path . '/compiled/js/locale/' . $language->language . '.js',
    'font'         => variable_get('timelinejs_font', 'Bevan-PotanoSans'),
    'library_path' => $library_path,
  ));

  $output .= theme('timelinejs_embed', array(
    'nid'  => $nid,
    'lang' => $language->language,
  ));

  return $output;
}

/**
 * Menu callback.
 */
function timelinejs_print($nid) {
  return theme('timelinejs_print', timelinejs_fetch_items($nid, 'print'));
}

/**
 * Strip text so it can be used inside json templates.
 */
function timelinejs_strip($string) {
  return str_replace(array("\r\n", "\n", "\r"), '<br>', str_replace('"', "'", $string));
}