summaryrefslogtreecommitdiff
path: root/timelinejs.module
blob: 731d4591938b800d11fba6eb5173001cc53f6b58 (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
<?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,
  );

  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'            => '/sites/all/libraries/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'),
    ),
  );
}

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

/**
 * Menu callback.
 */
function timelinejs_json($nid) {
  // 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) {
    $output[] = theme('timelinejs_json_event', array('event' => $event));
    $events   = implode(',', $output);
  }

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

  print theme('timelinejs_json', array(
    'headline' => $headline,
    'date'     => $date,
    'text'     => $text,
    'media'    => $media,
    'events'   => $events,
  ));

  # Avoid themable output.
  exit;
}

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

  $path = drupal_get_path('theme', $theme);
  $css  = (file_exists($path .'/timeline.css')) ? '/'. $path .'/timeline.css' : NULL;

  return theme('timelinejs', array(
    'width'  => $width,
    'height' => $height,
    'source' => '/timelinejs/json/'. (int) $nid,
    'css'    => $css,
    'lang'   => '/sites/all/libraries/timelinejs/compiled/js/locale/'. $language->language .'.js',
  ));
}