<?php
// $Id: views_bonus.module,v 1.5.2.10 2007/01/19 04:53:28 merlinofchaos Exp $

// -----------------------------------------------------------------------
// Drupal hooks

function views_bonus_help($section = NULL) {
  switch ($section) {
    case 'admin/modules#description':
      return t('A collection of plugins and default views for the views module.');
  }
}

function views_bonus_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/views/views_bonus',
      'title' => t('views bonus pack'),
      'callback' => 'views_bonus_settings_page',
      'access' => user_access('administer views'),
      'weight' => 10,
      'type' => MENU_LOCAL_TASK);
  }
  return $items;
}

// -----------------------------------------------------------------------
// Views hooks

function views_bonus_views_style_plugins() {
  return views_bonus_load_includes('style_plugins');
}

function views_bonus_views_default_views() {
  return views_bonus_load_includes('default_views');
}

function views_bonus_views_arguments() {
  return views_bonus_load_includes('arguments');
}

function views_bonus_views_query_alter(&$query, &$view, $summary, $level) {
  // unfortunately duplicated code but necessary due to the references.
  $files = views_bonus_get_modules();

  $current = variable_get('views_bonus_active_plugins', array());
  foreach ($files as $file) {
    if (!$current[$file->name]) {
      continue;
    }

    require_once('./' . $file->filename);
    $function = 'views_bonus_' . $file->name . '_' . 'query_alter';
    if (function_exists($function)) {
      $function($query, $view, $summary, $level);
    }
  }
}

// -----------------------------------------------------------------------
// Plugin manager components

function views_bonus_get_modules() {
  static $files = NULL;
  if ($files === NULL) {
    $path = drupal_get_path('module', 'views_bonus') . '/plugins/';
    $files = system_listing('.inc$', $path, 'name', 0);
  }
  return $files;
}

function views_bonus_load_includes($callback, $all = FALSE) {
  // Load all our module 'on behalfs'.
//  $path = drupal_get_path('module', 'views_bonus') . '/plugins/';
//  $files = system_listing('.inc$', $path, 'name', 0);
  $files = views_bonus_get_modules();

  $current = variable_get('views_bonus_active_plugins', array());
  $output = array();

  foreach ($files as $file) {
    if (!$all && !$current[$file->name]) {
      continue;
    }

    require_once('./' . $file->filename);
    if ($callback) {
      $function = 'views_bonus_' . $file->name . '_' . $callback;
      if (function_exists($function)) {
        $result = $function();
        if (isset($result) && is_array($result)) {
          $output = array_merge($output, $result);
        }
      }
    }
  }
  return $output;
}

function views_bonus_settings_page() {
  // true to load even those that are not yet active.
  $info = views_bonus_load_includes('info', TRUE);
  $current = variable_get('views_bonus_active_plugins', array());

  $form['boxes']['#tree'] = TRUE;

  foreach ($info as $plugin => $data) {
    $form['boxes'][$plugin] = array(
      '#type' => 'checkbox',
      '#vb_title' => $data['name'],
      '#vb_description' => $data['description'],
      '#vb_requires' => $data['requires'] ? implode(', ', $data['requires']) : t('None'),
      '#default_value' => $current[$plugin],
    );
    foreach ((array) $data['requires'] as $module) {
      if (!module_exist($module)) {
        $form['boxes'][$plugin]['#attributes'] = array('disabled' => TRUE);
        $form['boxes'][$plugin]['#default_value'] = FALSE;
        break;
      }
    }
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return drupal_get_form('views_bonus_settings', $form);
}

function theme_views_bonus_settings($form) {
  $output = '<p>' . t('Choose which plugins you would like to have active on your system. Most plugins provide new view types which may be accessed from the "Type" dropdown in the "Page" and "Block" sections of the view edit page.') . '</p>';
  $output .= '<p>' . t('<b>IMPORTANT:</b> Many of these plugins do NOT provide new default views!! They only provide new output types that you can use on views you create!') . '</p>';
  $kids = element_children($form['boxes']);
  foreach ($kids as $element) {
    $rows[] = array(
      form_render($form['boxes'][$element]),
      $form['boxes'][$element]['#vb_title'],
      $form['boxes'][$element]['#vb_description'],
      $form['boxes'][$element]['#vb_requires'],
    );
  }
  $output .= theme('table', array('', t('Plugin'), t('Description'), t('Required modules')), $rows);
  $output .= form_render($form);
  return $output;
}

function views_bonus_settings_submit($form_id, $form) {
  variable_set('views_bonus_active_plugins', $form['boxes']);
  views_invalidate_cache();
  drupal_set_message(t('The configuration options have been saved.'));
}

/**
 * Ensure all plugins are loaded during the query building phase
 */
function views_bonus_views_pre_query($view) {
  views_bonus_load_includes(NULL);
}

/**
 * When the modules page changes, we must go through our plugins and
 * ensure that plugins are deactivated if a required module is no
 * longer active.
 */
function views_bonus_form_alter() {

}

