You are here

function bean_usage_output in Bean (for Drupal 7) 7

Displays a table of Beans and their usage

Return value

string - the HTML for the table

1 string reference to 'bean_usage_output'
bean_usage_menu in bean_usage/bean_usage.module
Implements hook_menu()

File

bean_usage/bean_usage.module, line 91
Bean Admin Functions and Display

Code

function bean_usage_output($bean = NULL) {
  module_load_include('inc', 'bean_usage', 'includes/bean_usage.forms');
  $output = '';
  $bean_delta = NULL;
  $delta_specific = $delta_found = FALSE;
  $bean_usage_form = drupal_get_form('bean_usage_filters');
  if (!empty($bean) && is_object($bean)) {
    $delta_specific = TRUE;
  }
  else {
    if (is_numeric($bean)) {
      $delta_specific = TRUE;
      $bean = bean_load($bean);
    }
  }
  if (is_object($bean)) {
    drupal_set_title($bean->label . ' Usage');
    $bean_delta = $bean->delta;
  }

  // Get all fields that are of type blockreference and not deleted
  $fields = bean_usage_blockreference_fields();

  // If we have something to work with
  if (!empty($fields)) {
    $filters = drupal_get_query_parameters();
    if ($delta_specific) {
      $delta_found = FALSE;
    }
    $entity_types = bean_usage_blockrefence_entity_types();
    $bundles = bean_usage_blockreference_bundles();
    $query = '';
    foreach ($fields as $i => $field) {
      if (!$delta_found) {
        $field_name = $field->name;
        $data_table = 'field_data_' . $field_name;
        if ($i == 0) {
          $query = _bean_usage_field_data($data_table, $i, $field_name, $entity_types, $bundles, $filters, $delta_specific, $bean);
        }
        else {
          $query
            ->union(_bean_usage_field_data($data_table, $i, $field_name, $entity_types, $bundles, $filters, $delta_specific, $bean));
        }
      }
    }

    // Get the data
    $data = $query
      ->execute()
      ->fetchAll(0);
    if (!empty($data)) {

      // Sort the data - default sort is by label
      usort($data, 'bean_usage_sort_by_label');
      if (!empty($_GET['order'])) {
        $sort_by = empty($_GET['order']) ? '' : str_replace('bean ', '', strtolower($_GET['order']));
        switch ($sort_by) {
          case 'label':
            usort($data, 'bean_usage_sort_by_label');
            break;
          case 'title':
            usort($data, 'bean_usage_sort_by_title');
            break;
          case 'type':
            usort($data, 'bean_usage_sort_by_type');
            break;
        }
      }
      if (!empty($_GET['sort']) && $_GET['sort'] == 'desc') {
        $data = array_reverse($data);
      }
      $content = array();
      foreach ($data as $bean_data) {

        // We want to link to display the entity type title/name
        $entity_title = '';
        switch ($bean_data->entity_type) {

          // If its a node, get the node title
          case 'node':
            $entity_title = _bean_usage_entity_display('node', $bean_data->entity_id, 'nid', 'title');
            break;

          // If its a user, get the user name
          case 'user':
            $entity_title = _bean_usage_entity_display('user', $bean_data->entity_id, 'uid', 'name');
            break;
        }
        $content[$bean_data->label][$bean_data->entity_type][$bean_data->entity_id]['bean_title'] = $bean_data->title;
        $content[$bean_data->label][$bean_data->entity_type][$bean_data->entity_id]['bean_type'] = $bean_data->type;
        $content[$bean_data->label][$bean_data->entity_type][$bean_data->entity_id]['entity_id'] = $bean_data->entity_id;
        $content[$bean_data->label][$bean_data->entity_type][$bean_data->entity_id]['entity_title'] = $entity_title;
        if ($delta_specific) {
          $delta_found = TRUE;
        }
      }
    }

    // If we have any usage data create and display the table of data
    if (!empty($content)) {
      $rows = count($content);
      $results_per_page = variable_get('bean_usage_results_per_page', 30);

      // set up pager variables
      pager_default_initialize($rows, $results_per_page, $element = 0);

      // Set up output table
      $bean_table_data = array(
        'attributes' => array(
          // set table attributes
          'width' => '100%',
        ),
        'header' => _bean_usage_table_headers($bean_delta),
        'rows' => array(),
      );
      if ($filters) {
        $bean_table_data['caption'] = t('Filters: ') . $filters;
      }

      // Get the result subset for the current page
      $page = empty($_GET['page']) ? 0 : $_GET['page'];
      $range_start = $page * $results_per_page;
      $usage = array_slice($content, $range_start, $results_per_page);

      // If the user is on a higher page and then filters, the page
      if (!empty($filters) && $page != 0 && count($usage) == 0) {
        while (count($usage) == 0) {
          $page--;
          $range_start = $page * $results_per_page;
          $usage = array_slice($content, $range_start, $results_per_page);
        }
      }
      foreach ($usage as $bean_label => $entity_type) {
        foreach ($entity_type as $type => $delta) {
          $rowspan = count($delta);
          $bean_label_cell = '';
          if (empty($bean_delta)) {
            $bean_label_cell = array(
              'data' => $bean_label,
              'rowspan' => $rowspan,
              'width' => '30%',
            );
          }

          // we create the row with the bean name first and set the rowspan to however many deltas there are
          foreach ($delta as $entity) {
            $bean_title_cell = array(
              'data' => empty($entity['bean_title']) ? '<em>' . t('No title set') . '</em>' : $entity['bean_title'],
              'rowspan' => $rowspan,
              'width' => '30%',
            );
            $bean_type_cell = array(
              'data' => $entity['bean_type'],
              'rowspan' => $rowspan,
              'width' => '10%',
            );

            // We want to prefix the entity title with the entity id
            // We also want the entity title to link back the to the entity page
            $link_prefix = '';
            $text_prefix = '';
            switch ($type) {
              case 'node':
                $text_prefix = '[nid:' . $entity['entity_id'] . '] ';
                $link_prefix = 'node/';
                break;
              case 'user':
                $text_prefix = '[uid:' . $entity['entity_id'] . '] ';
                $link_prefix = 'user/';
                break;
            }

            // switch ($entity_type)
            // add the usage data to the table
            // We only need to add the Bean label once per delta, Since we set it above we check for its emptiness
            if (!empty($bean_label_cell)) {
              $bean_table_data['rows'][] = array(
                0 => $bean_label_cell,
                1 => $bean_title_cell,
                2 => $bean_type_cell,
                3 => array(
                  'data' => $text_prefix . l($entity['entity_title'], $link_prefix . $entity['entity_id']),
                  'width' => '30%',
                  'no_striping' => TRUE,
                ),
              );
            }
            else {
              $bean_table_data['rows'][] = array(
                $text_prefix . l(t($entity['entity_title']), $link_prefix . $entity['entity_id'], array(
                  'attributes' => array(
                    'title' => $entity['entity_title'],
                    'target' => '_blank',
                  ),
                )),
              );
            }

            // else
            // empty out the $bean_row so that it doesn't display more than one if there are multiple deltas for the block
            $bean_label_cell = '';
          }

          // foreach ($delta)
        }

        //foreach ($entity_type)
      }

      // foreach ($usage)
      // Set the output using theme_table with the header and rows created above
      if (!$delta_specific) {
        $output .= drupal_render($bean_usage_form);
      }
      $output .= theme('table', $bean_table_data);
      $output .= theme('pager');
    }
    else {
      if ($delta_specific) {
        $output = '<p>' . t('This bean is not used anywhere on the site.') . '</p>';
      }
      else {
        $output .= $bean_usage_form;
        $output .= 'There is no bean usage to report for ' . $filters;
      }
    }

    // else
  }

  // if (!empty($fields))
  // return the output for page rendering
  return $output;
}