Index: timeline.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/timeline/timeline.module,v retrieving revision 1.3.2.7.2.6 diff -u -r1.3.2.7.2.6 timeline.module --- timeline.module 19 Oct 2007 18:38:05 -0000 1.3.2.7.2.6 +++ timeline.module 30 Oct 2007 10:41:34 -0000 @@ -324,6 +324,22 @@ '#description' => t('Whether to display filtering and highlighting controls under the timeline. Not recommended for block mode.'), ); + // Group by field. + $view = views_get_view($vid); + $fields = _views_get_fields(); + $group_options = array('Do not group'); + foreach ($view->field as $field) { + $group_options[$field['fullname']] = $fields[$field['fullname']]['name']; + } + $form['group'] = array('#type' => 'fieldset', '#title' => t('Group by'), '#collapsible' => TRUE, '#collapsed' => FALSE); + $form['group']['group_by_field'] = array( + '#type' => 'select', + '#title' => t('Group field'), + '#default_value' => timeline_variable_get($vid, $mode, 'group_by_field', ''), + '#options' => $group_options, + '#description' => t('Select a field which events should be grouped by. Intervals within the same group will show up in the same color.'), + ); + $form['submit'] = array('#type' => 'submit', '#value' => t('Save timeline settings')); return $form; } @@ -332,7 +348,7 @@ * */ function timeline_form_submit() { - $fields = array('width', 'height', 'focus', 'band1_unit', 'band2_unit', 'controls'); + $fields = array('width', 'height', 'focus', 'band1_unit', 'band2_unit', 'controls', 'group_by_field'); $values = $GLOBALS['form_values']; $config = array(); foreach ($fields as $field) { @@ -388,7 +404,8 @@ _timeline_set_headers('application/json'); $view_args = empty($_REQUEST['args']) ? array() : unserialize($_REQUEST['args']); - $events = timeline_data($view, $view_args); + $group_by_field = empty($_REQUEST['group_by_field']) ? FALSE : $_REQUEST['group_by_field']; + $events = timeline_data($view, $view_args, $group_by_field); print drupal_to_js(array('dateTimeFormat' => 'iso8601', 'events' => $events)); die(); // Prevent other modules (e.g. devel.module) from interfering with our output @@ -407,7 +424,8 @@ _timeline_set_headers('text/xml'); $view_args = empty($_REQUEST['args']) ? array() : unserialize($_REQUEST['args']); - $events = timeline_data($view, $view_args); + $group_by_field = empty($_REQUEST['group_by_field']) ? FALSE : $_REQUEST['group_by_field']; + $events = timeline_data($view, $view_args, $group_by_field); $elements = array(); foreach ($events as $event) { @@ -467,7 +485,7 @@ * * @return an associative array of timeline events */ -function timeline_data($view, $view_args = array(), $teasers = TRUE, $links = TRUE) { +function timeline_data($view, $view_args = array(), $group_by_field = FALSE, $teasers = TRUE, $links = TRUE) { $items = (object)views_build_view('items', $view, $view_args, FALSE, $view->nodes_per_page); // Check if this is an event field and get start and end. @@ -493,6 +511,7 @@ $body_field = isset($view->field[2]['field']) ? $view->field[2]['queryname'] : NULL; $format_field = isset($body_field) ? preg_replace('/_value$/', '_format', $body_field) : NULL; + $fields = _views_get_fields(); $events = array(); foreach ($items->items as $item) { $node = node_load(array('nid' => $item->nid)); @@ -503,12 +522,22 @@ $body_value = !empty($body_field) && !empty($item->$body_field) ? $item->$body_field : $node->teaser; $body_format = !empty($format_field) && !empty($item->$format_field) ? $item->$format_field : $node->format; + if ($group_by_field) { + foreach ($view->field as $field) { + if ($field['fullname'] == $group_by_field) { + $group = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view); + $color = timeline_generate_color($group); + } + } + } + $event = array( 'title' => $title_value, 'link' => url('node/' . $node->nid), 'start' => timeline_format_iso8601_date($start_value), 'end' => $end_value ? timeline_format_iso8601_date($end_value) : NULL, //'isDuration' => $end_value ? 'true' : 'false', // NOTE: broken with JSON when explicit + 'color' => isset($color) ? $color : timeline_generate_color('empty'), 'description' => check_markup($body_value, $body_format, FALSE), ); if (is_null($event['end']) || $event['start'] == $event['end']) unset($event['end']); @@ -555,6 +584,24 @@ } /** + * Generate a color for the given group. + */ +function timeline_generate_color($group) { + static $groups_colors = array(); + static $count = 0; + if (!isset($groups_colors[$group])) { + $colors = array('#507EA1', '#406480', '#CFDCE6', '#3082BF', '#E6AC73', '#806040', '#E6DACF', '#BF7830', '#E6D573', '#807740', '#E6E2CF', '#BFAB30', '#5F458A', '#584080', '#D7CFE6', '#6730BF'); + // We have just 16 colors, so reset counter if we go beyond that number. + if ($count > 16) { + $count = 0; + } + $groups_colors[$group] = $colors[$count]; + $count++; + } + return $groups_colors[$group]; +} + +/** * @return a string in the format "Thu Jan 01 1970 1:00:00 GMT+0100" */ function timeline_format_initial_date($timestamp) { Index: timeline.theme.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/timeline/timeline.theme.inc,v retrieving revision 1.1.2.8.2.2 diff -u -r1.1.2.8.2.2 timeline.theme.inc --- timeline.theme.inc 9 Apr 2007 00:22:07 -0000 1.1.2.8.2.2 +++ timeline.theme.inc 30 Oct 2007 10:41:34 -0000 @@ -73,6 +73,7 @@ 'band1_unit' => timeline_variable_get($view->vid, $view->build_type, 'band1_unit', 'day'), 'band2_unit' => timeline_variable_get($view->vid, $view->build_type, 'band2_unit', 'month'), 'controls' => timeline_variable_get($view->vid, $view->build_type, 'controls', FALSE), + 'group_by_field' => timeline_variable_get($view->vid, $view->build_type, 'group_by_field', FALSE), )); } @@ -144,6 +145,11 @@ } $query = 'view=' . $timeline->view_name . (empty($timeline->view_args) ? '' : '&args=' . urlencode(serialize($timeline->view_args))); + + if ($timeline->group_by_field) { + $query .= '&group_by_field='. $timeline->group_by_field; + } + $url = url('timeline/' . TIMELINE_FEED_TYPE, $query, NULL, FALSE); $args = array($timeline->id , '%O', $timeline->initial_date, $timeline->timezone, array('%U1', '%U2'), $url, TIMELINE_FEED_TYPE); $script = 'createTimelineWidget(' . implode(', ', array_map('drupal_to_js', $args)) . ');';