<?php
// $Id: views_content.module,v 1.4.2.2 2010/07/20 00:17:08 merlinofchaos Exp $

/**
 * @file views_content.module
 *
 * Provides views as panels content, configurable by the administrator.
 * Each view provided as panel content must be configured in advance,
 * but once configured, building panels with views is a little bit simpler.
 */

/**
 * Implementation of hook_menu().
 */
function views_content_menu() {
  $items = array();

  if (!module_exists('panels')) {
    $items['admin/settings/content-views'] = array(
      'title' => 'Views panes',
      'access arguments' => array('administer views content plugin'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('views_content_admin_page'),
      'description' => 'Configure Views to be used as CTools content.',
      'type' => MENU_NORMAL_ITEM,
    );
  }

  return $items;
}

/**
 * Implementation of hook_ctools_plugin_dierctory() to let the system know
 * where our content_type plugins are.
 */
function views_content_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'ctools') {
    return 'plugins/' . $plugin_type;
  }
}

/**
 * Don't show Views' blocks; we expose them already.
 */
function views_ctools_block_info($module, $delta, &$info) {
  if (strlen($delta) == 32) {
    $hashes = variable_get('views_block_hashes', array());
    if (!empty($hashes[$delta])) {
      $delta = $hashes[$delta];
    }
  }

  if (substr($delta, 0, 1) != '-') {
    // Right now, 'special' blocks are all exposed forms. Let's
    // call those widgets.
    $info['category'] = t('Widgets');
    $info['icon'] = 'icon_views_block_legacy.png';
    $info['path'] = drupal_get_path('module', 'views_content');
  }
  else {
    $info['category'] = t('Views');
    $info['icon'] = 'icon_views_block_legacy.png';
    $info['path'] = drupal_get_path('module', 'views_content');
  }
}

/**
 * Implementation of hook_views_api().
 *
 * This one is used as the base to reduce errors when updating.
 */
function views_content_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'views_content') . '/plugins/views',
  );
}

/**
 * Page callback to provide the basic administration form.
 */
function views_content_admin_page() {
  $form = array();
  views_content_admin_form($form);

  return system_settings_form($form);
}

function views_content_admin_form(&$form) {
  $form['ctools_content_all_views'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make all views available as panes'),
    '#description' => t("If checked, all views will be made available as content panes to be added to content types. If not checked, only Views that have a 'Content pane' display will be available as content panes. Uncheck this if you want to be able to more carefully control what view content is available to users using the panels layout UI."),
    '#default_value' => variable_get('ctools_content_all_views', TRUE),
  );
}

/**
 * API function to get the view.
 */
function views_content_context_get_view(&$context) {
  if (empty($context->view) || get_class($context->view) == '__PHP_Incomplete_Class') {
    $context->view = views_get_view($context->data['name']);
    if ($context->view) {
      $context->view->set_display($context->data['display']);
    }
  }

  return $context->view;
}

/**
 * API function to get the view.
 */
function views_content_context_get_output(&$context) {
  if (empty($context->output)) {
    $view = views_content_context_get_view($context);
    $context->output = $view->execute_display($context->data['display']);
  }

  return $context->output;
}
