<?php

/**
 * @file
 * Bean Admin UI
 */

module_load_include('inc', 'bean_admin_ui', 'includes/features');

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

  $items['admin/structure/block-types'] = array(
    'title' => 'Block Types',
    'description' => 'Manage Block Types',
    'page callback' => 'bean_admin_ui_admin_page',
    'access arguments' => array('administer bean types'),
    'file' => 'bean_admin_ui.admin.inc',
  );

  $items['admin/structure/block-types/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  $items['admin/structure/block-types/add'] = array(
    'title' => 'Add block type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bean_admin_ui_type_form'),
    'access arguments' => array('administer bean types'),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'bean_admin_ui.admin.inc',
  );

  $items['admin/structure/block-types/manage/%bean_type'] = array(
    'title' => 'Edit block type',
    'title arguments' => array(4),
    'title callback' => 'bean_admin_ui_page_title',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bean_admin_ui_type_form', 4),
    'access arguments' => array('administer bean types'),
    'file' => 'bean_admin_ui.admin.inc',
  );
  $items['admin/structure/block-types/manage/%bean_type/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/structure/block-types/manage/%bean_type/delete'] = array(
    'title' => 'Delete',
    'page arguments' => array('bean_admin_ui_type_op_confirm', 'delete', 4),
    'access arguments' => array('administer bean types'),
    'file' => 'bean_admin_ui.admin.inc',
  );
  $items['admin/structure/block-types/manage/%bean_type/revert'] = array(
    'title' => 'Delete',
    'page arguments' => array('bean_admin_ui_type_op_confirm', 'revert', 4),
    'access arguments' => array('administer bean types'),
    'file' => 'bean_admin_ui.admin.inc',
  );

  return $items;
}

/**
 * Page title for block types
 */
function bean_admin_ui_page_title($block_type) {
  $beantype = bean_fetch_plugin_info($block_type->type);
  return check_plain($beantype['label']);
}

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

/**
 * Implements hook_bean_types().
 */
function bean_admin_ui_bean_types() {
  $plugins = array();

  // Add in the types defined in the UI.
  $bean_types = bean_admin_ui_get_types();

  foreach ($bean_types as $bean_type) {
    $plugins[$bean_type->name] = array(
      'label' => $bean_type->label,
      'description' => empty($bean_type->description) ? '' : $bean_type->description,
      'type' => $bean_type->name,
      'export_status' => $bean_type->type,
    );

    $plugins[$bean_type->name] += _bean_admin_default_plugin();
  }

  return $plugins;
}

/**
 * Get all Custom Bean Types
 */
function bean_admin_ui_get_types() {
  ctools_include('export');
  return ctools_export_load_object('bean_type');
}

/**
 * Implements hook_bean_cache_clear().
 */
function bean_admin_ui_bean_cache_clear() {
  // Clear ctools cache.
  ctools_include('export');
  ctools_export_load_object_reset('bean_type');

  // Flush caches.
  cache_clear_all('bean_types', 'cache');
}

/**
 * The Default plugin settings
 */
function _bean_admin_default_plugin() {
  return array(
    'label' => '',
    'description' => '',
    'type' => '',
    'editable' => TRUE,
    'handler' => array(
      'info_file' => TRUE,
      'class' => 'BeanCustom',
      'parent' => 'bean',
      'file' => 'BeanCustom.class.php',
      'path' => drupal_get_path('module', 'bean_admin_ui') . '/plugins',
    ),
  );
}

/**
 * Implements hook_features_api().
 */
function bean_admin_ui_features_api() {
  static $api = FALSE;
  if (!$api) {
    module_load_include('inc', 'features', 'includes/features.ctools');
    $api = ctools_component_features_api('bean_admin_ui');
    $api['bean_type']['name'] = 'Bean types';
    $api['bean_type']['file'] = drupal_get_path('module', 'bean_admin_ui') . '/includes/features.inc';
  }
  return $api;
}

/**
 * Implements hook_modules_enabled().
 */
function bean_admin_ui_modules_enabled($modules) {
  $beans_found = FALSE;
  foreach ($modules as $module) {
    if (function_exists($module .'_bean_types')) {
      $beans_found = TRUE;
    }
  }
  // Flush bean caches and statics if any of the enabled modules provide bean types.
  if ($beans_found) {
    bean_reset();
  }
}
