<?php
// $Id: grid.inc,v 1.1.2.2 2007/01/19 05:15:30 merlinofchaos Exp $
// $Name: DRUPAL-4-7--1-1 $

function views_bonus_grid_info() {
  $items['grid'] = array(
    'name' => t('Grid View'),
    'description' => t('View type that outputs a LIST view in a simple grid. In your PHP argument code, you can use &lt;?php $view->gridcount = SOME_NUMBER; ?&gt; to change the column count'),
    'requires' => array('panels'),
  );
  return $items;
}

function views_bonus_grid_style_plugins() {
  $items['bonus_grid'] = array(
    'name' => t('Bonus: Grid View'),
    'theme' => 'views_bonus_view_grid',
    'validate' => 'views_ui_plugin_validate_list',
    'needs_fields' => true,
  );
  return $items;
}

function theme_views_bonus_view_grid($view, $nodes, $type) {
  theme_add_style(drupal_get_path('module', 'views_bonus') .'/views_bonus.css');
  $fields = _views_get_fields();
  $content = '<table class="view-grid view-grid-' . $view->name . '">';
  
  // set default count.
  $cols = $view->gridcount ? $view->gridcount : 4;

  $count = 0;
  $total = count($nodes);
  foreach ($nodes as $node) {
    $item = '';
    if ($count % $cols == 0) { 
      $content .= '<tr>'; 
    }

    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        if ($field['label']) {
          $item .= "<div class='view-label view-label-$field[queryname]'>" . $field['label'] . "</div>";
        }
        $item .= "<div class='view-field view-data-$field[queryname]'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "</div>";
      }
    }
    $content .= "<td class='view-grid-item'><div class='view-item view-item-$view->name'>$item</div></td>\n"; 

    $count++;
    if ($count % $cols == 0 || $count == $total) {
      $content .= '</tr>';
    }
  }
  $content .= '</table>';
  
  if ($content) {
    return $content;
  }
}

