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

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

/**
 * Implements hook_node_view().
 */
function timelinejs_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'timeline' && $view_mode == 'full') {
    $node->content['timeline']['#markup'] = timelinejs($node->nid);
    $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(
        'width'         => NULL,
        'height'        => NULL,
        'source'        => NULL,
        'start_at_end'  => 'false',
        'hash_bookmark' => 'true',
        'css'           => NULL,
        'js'            => '/'. libraries_get_path('timelinejs') .'/compiled/js/timeline.js',
        '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'),
    ),
    'timelinejs_print' => array(
      'template'  => 'timelinejs_print',
      'variables' => array('headline', 'date', 'text', 'media', 'events'),
    ),
    'timelinejs_print_event' => array(
      'template'  => 'timelinejs_print_event',
      'variables' => array('event'),
    ),
  );
}

/**
 * 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);

  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));
    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) {
  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><head>';
  $output .= '<title>'. t('Timeline: @title', array('@title' => $node->title)) .'</title>';
  $output .= '</head><body class="widget timeline-widget">';
  $output .= timelinejs($nid, $width, $height);
  $output .= '</body></html>';

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

/**
 * Menu callback.
 */
function timelinejs($nid = NULL, $width = '960px', $height = '500px') {
  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 = libraries_get_path('timelinejs');

  // 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%';
  }

  return theme('timelinejs', array(
    'width'        => $width,
    'height'       => $height,
    'source'       => $source,
    'css'          => $css,
    'js'           => '/'. $library_path .'/compiled/js/timeline-min.js',
    'lang'         => '/'. $library_path .'/compiled/js/locale/'. $language->language .'.js',
    'library_path' => $library_path,
  ));
}

/**
 * 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));
}