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( '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, ), ), '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) { 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 .= ''; $output .= timelinejs($nid, $width, $height); $output .= ''; // 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%'; } 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', 'font' => variable_get('timelinejs_font', 'Bevan-PotanoSans'), '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)); }