You are here

function noderelationships_erd_diagram in Node Relationships 6

Build the Entity Relationship Diagram (ERD).

1 call to noderelationships_erd_diagram()
noderelationships_admin_page in ./noderelationships.admin.inc
Menu callback; relationships administration.

File

./noderelationships.admin.inc, line 34
Implementation of the administration pages of the module.

Code

function noderelationships_erd_diagram($type) {

  // Load information about relationships.
  $relationships = noderelationships_get_relationships();

  // If this type does not have relations, then we're done here.
  if (!isset($relationships['referrer'][$type->type]) && !isset($relationships['referred'][$type->type])) {
    drupal_set_message(t('Could not find relations for this content type.'), 'warning');
    return '';
  }
  $referrer_types = isset($relationships['referred'][$type->type]) ? $relationships['referred'][$type->type]['referrer_types'] : array();
  $referred_types = isset($relationships['referrer'][$type->type]) ? $relationships['referrer'][$type->type]['referred_types'] : array();
  $nodereference_fields = isset($relationships['referrer'][$type->type]['fields']) ? $relationships['referrer'][$type->type]['fields'] : array();
  $destination = drupal_get_destination();
  $content_types = content_types();
  $type_url_str = str_replace('_', '-', $type->type);
  $current_self_referred = FALSE;
  $referrer_entities = array();
  $referred_entities = array();
  $current_entity_fields = array();
  foreach ($referrer_types as $referrer_type => $field_names) {
    if ($referrer_type == $type->type) {
      $current_self_referred = TRUE;
      continue;
    }
    $referrer_url_str = str_replace('_', '-', $referrer_type);
    $type_label = noderelationships_get_localized_content_type_name($referrer_type);
    $type_label = $type->type == $referrer_type ? check_plain($type_label) : l($type_label, 'admin/content/node-type/' . $referrer_url_str . '/relationships');
    $type_items = array();
    foreach ($field_names as $field_name) {
      $type_items[] = array(
        'data' => l($content_types[$referrer_type]['fields'][$field_name]['widget']['label'], 'admin/content/node-type/' . $referrer_url_str . '/fields/' . $field_name, array(
          'query' => $destination,
        )),
        'attributes' => array(
          'class' => 'noderelationships-erd-noderef',
        ),
      );
    }
    $referrer_entities[] = array(
      'label' => $type_label,
      'items' => $type_items,
    );
  }
  if (!empty($referred_types[$type->type])) {
    foreach ($content_types[$type->type]['fields'] as $field_name => $field) {
      if (in_array($field_name, $referred_types[$type->type])) {
        unset($nodereference_fields[$field_name]);
        $current_entity_fields[] = array(
          'data' => l($field['widget']['label'], 'admin/content/node-type/' . $type_url_str . '/fields/' . $field_name, array(
            'query' => $destination,
          )),
          'attributes' => array(
            'class' => 'noderelationships-erd-noderef',
          ),
        );
      }
    }
  }
  foreach ($referred_types as $referred_type => $field_names) {
    if ($referred_type == $type->type) {
      continue;
    }
    $referred_url_str = str_replace('_', '-', $referred_type);
    $type_label = noderelationships_get_localized_content_type_name($referred_type);
    $type_label = $type->type == $referred_type ? check_plain($type_label) : l($type_label, 'admin/content/node-type/' . $referred_url_str . '/relationships');
    $type_items = array();
    foreach ($field_names as $field_name) {
      unset($nodereference_fields[$field_name]);
      $type_items[] = array(
        'data' => l($content_types[$type->type]['fields'][$field_name]['widget']['label'], 'admin/content/node-type/' . $type_url_str . '/fields/' . $field_name, array(
          'query' => $destination,
        )),
        'attributes' => array(
          'class' => 'noderelationships-erd-noderef',
        ),
      );
    }
    $referred_entities[] = array(
      'label' => $type_label,
      'items' => $type_items,
    );
  }
  foreach ($nodereference_fields as $field_name => $types) {
    $field = $content_types[$type->type]['fields'][$field_name];
    $current_entity_fields[] = array(
      'data' => l($field['widget']['label'], 'admin/content/node-type/' . $type_url_str . '/fields/' . $field_name, array(
        'query' => $destination,
      )),
      'attributes' => array(
        'class' => 'noderelationships-erd-noderef-any',
      ),
    );
  }
  $diagram = '';
  if (!empty($referrer_entities)) {
    $diagram .= theme('noderelationships_erd_group', 'referrer', $referrer_entities);
    $diagram .= theme('noderelationships_erd_relation', t('Referred from'));
  }
  $current_label = array(
    'data' => check_plain($type->name),
  );
  if ($current_self_referred) {
    $current_label['attributes'] = array(
      'class' => 'noderelationships-erd-self',
    );
  }
  $diagram .= theme('noderelationships_erd_current', $current_label, $current_entity_fields);
  if (!empty($referred_entities)) {
    $diagram .= theme('noderelationships_erd_relation', t('Refers to'));
    $diagram .= theme('noderelationships_erd_group', 'referred', $referred_entities);
  }
  $admin_help_text = t('This panel displays an Entity Relationship Diagram (ERD) relative to the content type %type.', array(
    '%type' => $type->name,
  ));
  $output = theme('noderelationships_admin_help', $admin_help_text);
  $output .= theme('noderelationships_erd_diagram', $diagram);
  return $output;
}