You are here

function organigrams_compose_organigrams in Organigrams 7

Generate an renderable item list array for the specified organigram.

Parameters

object $organigram: An organigrams entity.

mixed $langcode: The language in which the organigram needs to be rendered.

Return value

array A renderable array for the specified organigram if found, otherwise NULL.

1 call to organigrams_compose_organigrams()
organigrams_entity_view in ./organigrams.module
Implements hook_entity_view().

File

./organigrams.module, line 1123
Defines the organigrams functions and entity types.

Code

function organigrams_compose_organigrams($organigram, $langcode = NULL) {

  // Default output.
  $output = array();

  // Validate loaded organigram.
  if (!$organigram) {
    return $output;
  }

  // Construct the orgchart settings.
  $organigram_settings = array(
    'organigram_settings' => (object) array(
      'canvas_width' => $organigram->canvas_width,
      'canvas_height' => $organigram->canvas_height,
      'center' => $organigram->center,
      'fit' => $organigram->fit,
      'node_width' => $organigram->node_width,
      'node_height' => $organigram->node_height,
      'top_radius' => $organigram->top_radius,
      'bottom_radius' => $organigram->bottom_radius,
      'shadow_offset' => $organigram->shadow_offset,
      'horizontal_space' => $organigram->horizontal_space,
      'vertical_space' => $organigram->vertical_space,
      'horizontal_offset' => $organigram->horizontal_offset,
      'line_color' => $organigram->line_color,
      'border_color' => $organigram->border_color,
      'border_color_hover' => $organigram->border_color_hover,
      'background_color' => $organigram->background_color,
      'background_color_hover' => $organigram->background_color_hover,
      'font_color' => $organigram->font_color,
      'font_color_hover' => $organigram->font_color_hover,
      'font_name' => $organigram->font_name,
      'font_size' => $organigram->font_size,
      'line_height' => $organigram->line_height,
      'vertical_alignment' => $organigram->vertical_alignment,
    ),
    'nodes' => array(),
  );

  // Retrieve the organigram tree.
  $organigram_tree = organigrams_get_tree($organigram->oid, 0, NULL, TRUE);

  // Validate if the organigram has any items.
  if (empty($organigram_tree)) {
    return $output;
  }

  // Create a hierarchical list from all organigram items.
  $hierarchical_list = array();

  // Iterate through the organigram tree.
  foreach ($organigram_tree as $tree_item) {

    // Set the parent ID.
    $parent = 0;
    if (!empty($tree_item->parent)) {
      $parent = $tree_item->parent;
    }

    // Create a new array for this parent if it doesn't exist.
    if (!isset($hierarchical_list[$parent])) {
      $hierarchical_list[$parent] = array();
    }

    // Add the item to this parent.
    $hierarchical_list[$parent][] = $tree_item;
  }

  // Generate the list. This will be displayed if the JavaScript cannot be
  // loaded and is used as data source for the orgchart library.
  $output = array(
    '#theme' => 'item_list',
    '#type' => 'ul',
    '#items' => organigrams_generate_items_list($hierarchical_list),
  );

  // Include the excanvas library if it exists.
  $output['#attached']['libraries_load'][] = array(
    'excanvas',
  );

  // Include the orgchart library.
  $output['#attached']['library'][] = array(
    'organigrams',
    'orgchart',
  );

  // Include the organigram content loader.
  $output['#attached']['js'][] = drupal_get_path('module', 'organigrams') . '/js/organigrams.js';

  // Add the organigram to the organigrams list.
  $output['#attached']['js'][] = array(
    'data' => array(
      'organigrams' => array(
        'organigrams' => array(
          $organigram->unique_id => $organigram_settings,
        ),
      ),
    ),
    'type' => 'setting',
  );
  return $output;
}