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' => '/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'), ), '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 = ''; $output .= ''; $output .= ''; $output .= ''. t('Timeline: @title', array('@title' => $node->title)) .''; $output .= ''; $output .= timelinejs($nid, $width, $height); $output .= ''; // 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; } return theme('timelinejs', array( 'width' => $width, 'height' => $height, 'source' => $source, 'css' => $css, '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"), '
', str_replace('"', "'", $string)); }