<?php
// $Id: views_bonus_panels.module,v 1.4 2009/06/24 08:20:35 neclimdul Exp $
/**
 * @file
 * Base module file for views bonus panels plugins.
 */

/**
 * Implementation of hook_views_api().
 */
function views_bonus_panels_views_api() {
  return array(
    'api' => 2,
  );
}

function theme_views_bonus_panels_render($view, $options, $rows, $title) {
  panels_load_include('display-render');
  list($panel_name, $content) = views_bonus_panels_shared_preprocess($view->plugin_name, $rows);
  return panels_print_layout($panel_name, $content);
}

function template_preprocess_views_bonus_panels_threecol_term(&$vars) {
  //
}

/**
 * Shared preprocess to devide out rendered rows into panel sections.
 *
 * @param plugin_name
 * 
 * @param $rows
 * An array of rendered views rows.
 * @return
 * Panels content array.
 */
function views_bonus_panels_shared_preprocess($plugin_name, $rows) {
  switch ($plugin_name) {
    case 'panels_threecol':
      $panel_name = 'threecol_33_34_33';
      $stacked = false;
      $columns = 3;
      break;
    case 'panels_threecol_stack':
      $panel_name = 'threecol_33_34_33_stacked';
      $stacked = true;
      $columns = 3;
      break;
    case 'panels_twocol':
      $panel_name = 'twocol';
      $stacked = false;
      $columns = 2;
      break;
    case 'panels_twocol_stack':
      $panel_name = 'twocol_stacked';
      $stacked = true;
      $columns = 2;
      break;
  }

  $content = array(
    'left' => '',
    'right' => '',
  );

  // If this is stacked, drop the first entry into the top slot.
  if ($stacked) {
    $content['top'] = array_shift($rows);
  }

  $col_names[] = 'left';
  if ($columns != 2) {
    // Assume its 3 and add the middle column.
    $col_names[] = 'middle';
    $content['middle'] = '';
  }
  $col_names[] = 'right';

  // iterate over remaining rows and fill columns.
  foreach ($rows as $offset => $row) {
    $r = $offset % $columns;
    $content[$col_names[$r]] .= $row;
  }

  return array($panel_name, $content);
}
