<?php
// $Id$

/**
 * @file
 * CTools primary module file.
 *
 * Most of the CTools tools are in their own .inc files. This contains
 * nothing more than a few convenience functions and some hooks that
 * must be implemented in the module file.
 */
/**
 * Include ctools .inc files as necessary.
 */
function ctools_include($file) {
  static $used = array();
  if (!isset($used[$file])) {
    require_once './' . drupal_get_path('module', 'ctools') . "/includes/$file.inc";
  }

  $used[$file] = TRUE;
}

/**
 * Provide the proper path to a CTools image
 */
function ctools_image_path($image) {
  return drupal_get_path('module', 'ctools') . '/images/' . $image;
}

/**
 * Include views .css files.
 */
function ctools_add_css($file) {
  drupal_add_css(drupal_get_path('module', 'ctools') . "/css/$file.css");
}

/**
 * Include views .js files.
 */
function ctools_add_js($file) {
  drupal_add_js(drupal_get_path('module', 'ctools') . "/js/$file.js");
}

/**
 * Provide a hook passthrough to included files.
 *
 * To organize things neatly, each CTools tool gets its own toolname.$type.inc
 * file. If it exists, it's loaded and ctools_$tool_$type() is executed.
 * To save time we pass the $items array in so we don't need to do array
 * addition. It modifies the array by reference and doesn't need to return it.
 */
function _ctools_passthrough(&$items, $type = 'theme') {
  $files = drupal_system_listing('.' . $type . '.inc$', drupal_get_path('module', 'ctools') . '/includes', 'name', 0);
  foreach ($files as $file) {
    require_once './' . $file->filename;
    list($tool) = explode('.', $file->name, 2);

    $function = 'ctools_' . $tool . '_' . $type;
    if (function_exists($function)) {
      $function($items);
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function ctools_theme() {
  $items = array();
  _ctools_passthrough($items, 'theme');
  return $items;
}

/**
 * Implementation of hook_menu().
 */
function ctools_menu() {
  $items = array();
  _ctools_passthrough($items, 'menu');
  return $items;
}

/**
 * Implementation of hook_ctools_plugin_dierctory() to let the system know
 * we implement task and task_handler plugins.
 */
function ctools_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Get a list of roles in the system.
 */
function ctools_get_roles() {
  static $roles = NULL;
  if (!isset($roles)) {
    $roles = array();
    $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
    while ($obj = db_fetch_object($result)) {
      $roles[$obj->rid] = $obj->name;
    }
  }

  return $roles;
}

/**
 * Determine if the current user has access via a plugin.
 *
 * This function is meant to be embedded in the Drupal menu system, and
 * therefore is in the .module file since sub files can't be loaded, and
 * takes arguments a little bit more haphazardly than ctools_access().
 *
 * @param $plugin_name
 *   The access plugin to use. If empty or 'none' then no access control
 *   is being used and this function returns TRUE. If the plugin can't be
 *   found otherwise, this function automatically returns FALSE.
 * @param $settings
 *   An array of settings theoretically set by the user.
 * @param ...
 *   zero or more context arguments generated from argument plugins. These
 *   contexts must have an 'id' attached to them so that they can be
 *   properly associated. The argument plugin system should set this, but
 *   if the context is coming from elsewhere it will need to be set manually.
 *
 * @return
 *   TRUE if access is granted, false if otherwise.
 */
function ctools_access_menu($plugin_name, $settings) {
  $contexts = array();
  foreach (func_get_args() as $arg) {
    if (is_object($arg) && get_class($arg) == 'ctools_context') {
      $contexts[$arg->id] = $arg;
    }
  }

  global $user;
  return ctools_access($plugin_name, $settings, $contexts, $user);
}

/**
 * Determine if the current user has access via  plugin.
 *
 * @param $plugin_name
 *   The access plugin to use. If empty or 'none' then no access control
 *   is being used and this function returns TRUE. If the plugin can't be
 *   found otherwise, this function automatically returns FALSE.
 * @param $settings
 *   An array of settings theoretically set by the user.
 * @param $contexts
 *   An array of zero or more contexts that may be used to determine if
 *   the user has access.
 * @param $account
 *   The account to test against. If NULL the logged in user will be tested.
 *
 * @return
 *   TRUE if access is granted, false if otherwise.
 */
function ctools_access($plugin_name, $settings, $contexts = array(), $account = NULL) {
  if (!$account) {
    global $user;
    $account = $user;
  }

  if (empty($plugin_name) || $plugin_name == 'none') {
    return TRUE;
  }

  ctools_include('context');
  $plugin = ctools_get_access_plugin($plugin_name);
  if (!$plugin) {
    return FALSE;
  }

  if ($function = ctools_plugin_get_function($plugin, 'callback')) {
    return $function($settings, $contexts, $account);
  }
}