<?php

/**
 * @file
 * Workbench module file for editorial workspaces.
 */

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

  $items['admin/config/workbench'] = array(
    'title' => 'Workbench',
    'description' => 'Workbench',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer workbench'),
    'position' => 'right',
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/workbench'] = array(
    'title' => 'My Workbench',
    'description' => 'My Workbench area',
    'page callback' => 'workbench_content',
    'access arguments' => array('access workbench'),
    'weight' => -20,
    'file' => 'workbench.pages.inc',
  );
  $items['admin/workbench/content'] = array(
    'title' => 'My content',
    'page callback' => 'workbench_content',
    'access arguments' => array('access workbench'),
    'weight' => -20,
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'workbench.pages.inc',
  );
  $items['admin/workbench/create'] = array(
    'title' => 'Create content',
    'page callback' => 'workbench_create',
    'access arguments' => array('access workbench'),
    'weight' => -15,
    'type' => MENU_LOCAL_TASK,
    'file' => 'workbench.pages.inc',
  );

  // Create redirect urls for node add pages.
  // This is helpful for the admin_menu module and could have other uses.
  foreach (node_type_get_types() as $type) {
    $type_url_str = str_replace('_', '-', $type->type);
    $items['admin/workbench/create/node/add/' . $type_url_str] = array(
      'title' => $type->name,
      'title callback' => 'check_plain',
      'page callback' => 'drupal_goto',
      'page arguments' => array('node/add/' . $type_url_str),
      'access callback' => 'node_access',
      'access arguments' => array('create', $type->type),
      'description' => $type->description,
    );
  }

  return $items;
}

/**
 * Implements hook_permission().
 */
function workbench_permission() {
  $permissions = array(
    'administer workbench' => array(
      'title' => t('Administer Workbench settings'),
    ),
    'access workbench' => array(
      'title' => t('Access My Workbench'),
    ),
  );
  return $permissions;
}

/**
 * Implements hook_block_info().
 *
 * Register a block that other modules may hook into.
 */
function workbench_block_info() {
  $blocks['block'] = array(
    'info' => t('Workbench information'),
    'weight' => -99,
    'status' => 1,
    'region' => 'content',
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function workbench_block_view($delta = '') {
  $items = module_invoke_all('workbench_block');
  if (empty($items)) {
    return;
  }
  $block = array(
    'subject' => '',
    'content' => array(
      '#markup' => theme('container', array('element' => array('#children' => implode('<br />', $items), '#attributes' => array('class' => 'workbench-info-block')))),
      '#attached' => array(
        'css' => array(drupal_get_path('module', 'workbench') . '/css/workbench.block.css'),
      ),
    ),
  );
  return $block;
}

/**
 * Implements hook_views_api().
 */
function workbench_views_api() {
  return array('api' => 2);
}

/**
 * Implements hook_views_default_views().
 */
function workbench_views_default_views() {
  return workbench_load_all_exports('workbench', 'views', 'view.inc', 'view');
}

/**
 * Fetches an array of exportables from files.
 *
 * @param $module
 *   The module invoking this request. (Can be called by other modules.)
 * @param $directory
 *   The subdirectory in the custom module.
 * @param $extension
 *   The file extension.
 * @param $name
 *   The name of the variable found in each file. Defaults to the same as
 *   $extension.
 *
 * @return
 *   Array of $name objects.
 */
function workbench_load_all_exports($module, $directory, $extension, $name = NULL) {
  if (!$name) {
    $name = $extension;
  }

  $return = array();
  // Find all the files in the directory with the correct extension.
  $files = file_scan_directory(drupal_get_path('module', $module) . "/$directory", "/\.{$extension}$/");
  foreach ($files as $path => $file) {
    require $path;
    if (isset($$name)) {
      $return[$$name->name] = $$name;
    }
  }

  return $return;
}

/**
 * Implements hook_theme().
 *
 * Provides a theme function for use with the RenderAPI that uses the #title and
 * #attributes properties of a render array, if they're present.
 */
function workbench_theme($existing, $type, $theme, $path) {
  return array(
    'workbench_element' => array(
      'render element' => 'element',
      'file' => 'workbench.theme.inc',
    ),
  );
}

/**
 * Implementation of hook_preprocess_views_view_table().
 *
 * This is done to convert the "type" field to a thumbnail for image files.
 *
 */
function workbench_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];

  if ($view->name == 'workbench_current_user'  && $variables['field']->definition['handler'] == 'views_handler_field_user_picture') {
    if (empty($variables['output'])) {
      // We could put the default picture here
      $variables['output'] = theme('image', array(
        'path' => drupal_get_path('module', 'workbench') . '/images/profile_default.png',
        'attributes' => array(
          'width' => '100px',
         ),
      ));
    }
  }
}

/**
 * Implements hook_ctools_plugin_directory() to let the system know
 * where our task and task_handler plugins are.
 */
function workbench_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'page_manager') {
    return 'plugins/page_manager/' . $plugin_type;
  }
}

/**
 * Implements hook_ctools_plugin_api().
 */
function workbench_ctools_plugin_api($module, $api) {
  // This includes a check for whether Panels is enabled since the Page Manager
  // export is for Panels.
  if ($module == 'page_manager' && $api == 'pages_default' && module_exists('panels')) {
    return array('version' => 1);
  }
}
