You are here

function taxonomy_server_vocabulary_page in Taxonomy import/export via XML 7

Display a page showing one or all available vocabularies

Inspects content-negotiation for alt formats or explicit content-type requests

Parameters

$vocabulary object:

1 string reference to 'taxonomy_server_vocabulary_page'
taxonomy_server_menu in taxonomy_server/taxonomy_server.module
Implementation of hook_menu: Define menu links.

File

taxonomy_server/taxonomy_server.module, line 257
Extends taxonomy_xml to publish downloadable or queriable taxonomy vocabularies and terms. Extends the Drupal URLs to make vocabulary information available under /taxonomy/vocabulary and /taxonomy/vocabulary/{vid}.

Code

function taxonomy_server_vocabulary_page($vocabulary = NULL, $format_id = NULL) {
  if (!$vocabulary) {

    // list all vocabs
    $vocabs = taxonomy_get_vocabularies();
    $link_list = array();
    foreach ($vocabs as $vocabulary) {

      #$tree = taxonomy_get_tree($vocabulary->vid);

      #$vocabcount = count($tree);
      $vocabcount = db_query("SELECT count(*) FROM {taxonomy_term_data} WHERE vid = :vid", array(
        ':vid' => $vocabulary->vid,
      ))
        ->fetchField();
      $link_list[$vocabulary->vid] = array(
        'title' => $vocabulary->name . t(" (!count terms)", array(
          '!count' => $vocabcount,
        )),
        'href' => 'taxonomy/vocabulary/' . $vocabulary->vid,
      );
    }
    return theme('links', array(
      'links' => $link_list,
      'attributes' => array(
        'class' => 'vocabulary-list',
      ),
    ));
  }
  else {

    // list the given vocab
    if (is_numeric($vocabulary)) {
      $vocabulary = taxonomy_vocabulary_load($vocabulary);
    }
    if (is_string($vocabulary)) {
      return t('Invalid argument. %vid is not a known vocabulary. Must be an integer.');
    }

    // Check if this request was from a content-negotiation compatible client.
    // Redirect them to the RDF version if so.
    $preferred = taxonomy_server_get_preferred_content($_SERVER["HTTP_ACCEPT"]);
    watchdog('taxonomy_server', "\n      Received a request for <b>{$_SERVER['REQUEST_URI']}</b> .\n      Seeing if I can give it better content via content-negotiation.\n      Client asked for ({$_SERVER['HTTP_ACCEPT']})\n      <br> It seems that the client would prefer '{$preferred}'\n    ");

    # <pre>" . print_r($_SERVER, 1) . "</pre>

    // TODO https & port support
    $rdf_uri = taxonomy_xml_get_vocabulary_uri($vocabulary) . "/rdf";
    if ($preferred == 'rdf') {
      watchdog('taxonomy_server', "Received a <b>content-negotiated</b> request for {$_SERVER['REQUEST_URI']} as {$preferred}. Boinging the request to that version of this page.");
      header("HTTP/1.1 303");
      header("Vary: Accept");
      header("Location: " . $rdf_uri);
      exit;
    }
    if (!empty($format_id)) {

      // this vocab was requested in a specific export format.
      // Use the appropriate export function.
      return taxonomy_xml_export_vocabulary($vocabulary, $format_id);
    }

    // Else, return the normal HTML version!
    // @todo May need to start thinking about depth
    $item_tree = taxonomy_server_get_vocab_as_item_tree($vocabulary->vid);
    drupal_add_css(drupal_get_path('module', 'taxonomy_server') . '/taxonomy_server.css');

    // add a link tag so user-agents can see I have this semantic version avaialable
    drupal_add_html_head_link(array(
      'rel' => 'alternate',
      'type' => 'application/rdf+xml',
      'title' => $vocabulary->name,
      'href' => $rdf_uri,
    ));

    #    <link rel="alternate" type="application/rss+xml" title="semanticweb.org RSS Feed" href="http://semanticweb.org/index.php?title=Special:RecentChanges&amp;feed=rss" />
    return theme('item_list', array(
      'items' => $item_tree,
      'title' => $vocabulary->name,
      'type' => 'ul',
      'attributes' => array(
        'class' => 'term-tree',
      ),
    ));
  }
}