<?php

/**
 * Implements hook_menu().
 */
function bean_all_menu() {
  $items = array();

  $items['admin/config/system/bean-all'] = array(
    'title' => 'Bean Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bean_all_settings_form'),
    'access arguments' => array('administer bean settings'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implements hook_bean_types().
 */
function bean_all_bean_types() {
  $running = &drupal_static(__FUNCTION__ . "_running", FALSE);

  $plugins = array();

  if (!$running) {
    $running = TRUE;
    foreach (module_implements('block_info') as $module) {
      if ($module !== 'bean') {
        _bean_all_bean_types($plugins, $module);
      }
    }
    $running = FALSE;
  }

  return $plugins;
}

function _bean_all_bean_types(&$plugins, $module) {
  $default = array(
    'file' => 'all.inc',
    'path' => drupal_get_path('module', 'bean_all') . '/plugins',
    'handler' => array(
      'class' => 'BeanAll',
      'parent' => 'bean',
      'file' => 'all.inc',
      'path' => drupal_get_path('module', 'bean_all') . '/plugins',
    ),
  );

  $module_blocks = module_invoke($module, 'block_info');

  if (!empty($module_blocks)) {
    bean_all_filter_blocks($module_blocks, $module);
    foreach ($module_blocks as $delta => $block) {
      $block = (object) $block;
      $block->module = $module;
      $block->delta = $delta;
      $name = "{$module}_{$delta}";
      $plugins[$name] = array(
        'label' => $block->info,
        'description' => $block->info,
        'name' => "$module-$delta",
        'block' => $block
      ) + $default;
    }
  }
}

/**
 * Implements hook_bean_types_api_info().
 */
function bean_all_bean_types_api_info() {
  return array(
    'api' => 5,
  );
}

/**
 * Get the block blacklist
 *
 * @param $module
 *  The module to pull the blacklist of
 *
 * @return array
 *  if the array is empty then no blocks are allowed
 */
function bean_all_get_blacklist($module) {
  $variable_name = bean_all_build_variable_name($module);

  return variable_get($variable_name);
}

function bean_all_build_variable_name($module) {
  $variable_name = "bean_all_blacklist";

  if (isset($module)) {
    $variable_name .= "_{$module}";
  }

  return $variable_name;
}


/**
 * Implements hook_block_info_alter().
 *
 * Remove the ability to place the blocks in admin/structure/blocks
 */
function bean_all_block_info_alter(&$blocks, $theme, $code_blocks) {
  foreach ($blocks as $module => $module_blocks) {
    bean_all_filter_blocks($module_blocks, $module);
    foreach ($module_blocks as $delta => $block) {
      $blocks[$module][$delta]['status'] = FALSE;
      $blocks[$module][$delta]['disabled'] = FALSE;
    }
  }
}

/**
 * Filter a list of blocks for a module
 */
function bean_all_filter_blocks(&$blocks, $module) {
  $black_list = bean_all_get_blacklist($module);

  if ($black_list === TRUE) {
    $blocks = array();
  }
  elseif (is_array($black_list)) {
    $black_list = array_combine($black_list, $black_list);
    $blocks = array_diff_key($blocks, $black_list);
  }
}

function bean_all_settings_form($form, &$form_state) {
  $form = array();
  $module_info = system_rebuild_module_data();

  drupal_set_title(t('Select Blocks to not appear as Block Types'));

  foreach (module_implements('block_info') as $module) {
    if ($module !== 'bean') {
      $module_blocks = module_invoke($module, 'block_info');
      $variable_name = bean_all_build_variable_name($module);
      $form[$variable_name] = array(
        '#title' => $module_info[$module]->info['name'],
        '#type' => 'vertical_tabs',
      );
      $form[$variable_name][$variable_name] = array(
        '#type' => 'checkboxes',
        '#default_value' => variable_get($variable_name, array()),
      );

      $options = array();
      foreach ($module_blocks as $delta => $block) {
        $options[$delta] = $block['info'];
      }

      $form[$variable_name][$variable_name]['#options'] = $options;
    }
  }

  return system_settings_form($form);
}
