You are here

farm_dashboard.module in farmOS 7

Farm dashboard module.

File

modules/farm/farm_dashboard/farm_dashboard.module
View source
<?php

/**
 * @file
 * Farm dashboard module.
 */

/**
 * Implements hook_hook_info().
 */
function farm_dashboard_hook_info() {
  $hooks['farm_dashboard_panes'] = array(
    'group' => 'farm_dashboard',
  );
  return $hooks;
}

/**
 * Implements hook_permission().
 */
function farm_dashboard_permission() {
  return array(
    'access farm dashboard' => array(
      'title' => t('Access farm dashboard'),
      'description' => t('Access the farm dashboard.'),
    ),
  );
}

/**
 * Implements hook_farm_access_perms().
 */
function farm_dashboard_farm_access_perms($role) {
  $perms = array();

  // Access farm dashboard.
  $perms[] = 'access farm dashboard';
  return $perms;
}

/**
 * Implements hook_menu().
 */
function farm_dashboard_menu() {
  $items['farm'] = array(
    'title' => 'Farm',
    'page callback' => 'farm_dashboard_page_callback',
    'access arguments' => array(
      'access farm dashboard',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['farm/dashboard'] = array(
    'title' => 'Dashboard',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -100,
  );

  // Root farm configuration path.
  $items['admin/config/farm'] = array(
    'title' => 'Farm',
    'description' => 'Configure settings for your farm.',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file path' => drupal_get_path('module', 'system'),
    'file' => 'system.admin.inc',
  );
  return $items;
}

/**
 * Farm dashboard page callback.
 */
function farm_dashboard_page_callback() {

  // Set the page title.
  drupal_set_title('Dashboard');

  // Start a build array.
  $build = array();

  // Ask modules for dashboard panes.
  $panes = module_invoke_all('farm_dashboard_panes');

  // Iterate through the panes and add them to the dashboard.
  foreach ($panes as $pane) {

    // Set defaults for title, output, group, and weight.
    $output = '';
    $title = '';
    $group = 'general';
    $weight = 0;

    // Use the provided title, if available.
    if (!empty($pane['title'])) {
      $title = $pane['title'];
    }

    // If a 'view' and 'view_display_id' are provided, load the view, generate
    // the preview, and set the title automatically.
    if (!empty($pane['view']) && !empty($pane['view_display_id'])) {
      $view = views_get_view($pane['view']);
      $preview = $view
        ->preview($pane['view_display_id']);
      $title = $view
        ->get_title();
      $output = $preview;
    }
    elseif (!empty($pane['callback']) && function_exists($pane['callback'])) {
      $output = call_user_func($pane['callback']);
    }

    // If a group is not provided, assign it to a general default group.
    if (!empty($pane['group'])) {
      $group = $pane['group'];
    }

    // If a weight is not provided, default to 0.
    if (!empty($pane['weight'])) {
      $weight = $pane['weight'];
    }

    // Create a container for the pane.
    $container = array(
      '#type' => 'container',
      '#weight' => $weight,
    );

    // If a title is set, make it a fieldset.
    if (!empty($title)) {
      $container['#type'] = 'fieldset';
      $container['#title'] = check_plain($title);
    }

    // Assemble a content markup item inside the container.
    $container['content'] = array(
      '#type' => 'markup',
      '#markup' => $output,
    );

    // Add the container to the build array.
    $build[$group][] = $container;
  }

  // Return the build array.
  return $build;
}