You are here

function querypath_examples_show_musicbrainz_form_validate in QueryPath 6

Same name and namespace in other branches
  1. 7.3 querypath_examples.module \querypath_examples_show_musicbrainz_form_validate()
  2. 7.2 querypath_examples.module \querypath_examples_show_musicbrainz_form_validate()

File

./querypath_examples.module, line 245
The main file for querypath_examples.

Code

function querypath_examples_show_musicbrainz_form_validate($form, &$form_state) {
  $artist_name = trim($form_state['values']['artist_name']);
  $artist_name = urlencode($artist_name);
  $artist_url = 'http://musicbrainz.org/ws/1/artist/?type=xml&name=' . $artist_name;
  $album_url = 'http://musicbrainz.org/ws/1/release/?type=xml&artistid=';
  try {
    drupal_set_message('URL: ' . $artist_url, 'status');
    $artist = qp($artist_url, 'artist:first');
    if ($artist
      ->size() > 0) {
      $id = $artist
        ->attr('id');
      $out .= '<p>The best match we found was for ' . $artist
        ->children('name')
        ->text() . '</p>';
      $out .= '<p>Artist ID: ' . $id . '</p>';
      $out .= '<p>Albums for this artist' . '</p><ul>';
      $out .= $album_url . urlencode($id);
      $albums = qp($album_url . urlencode($id));

      //->writeXML();
      $xml = $albums
        ->xml();
      foreach ($albums
        ->find('release') as $album) {
        $title = check_plain($album
          ->find('title')
          ->text());
        $asin = check_plain($album
          ->next('asin')
          ->text());
        $us_release = check_plain($album
          ->parent()
          ->find('release-event-list>event[country=US]')
          ->attr('date'));
        $label = check_plain($album
          ->attr('label'));
        $out .= '<li><strong>' . $title . '</strong><br/>';
        if (strlen($label) > 0 && $label != '-') {
          $out .= ' (' . $label . ')</li>';
        }
        if (!empty($us_release)) {
          $out .= ' <em>Release Date:' . $us_release . '</em>';
        }
        if (!empty($asin) && $asin != '-') {
          $out .= '<br/><a href="http://www.amazon.com/gp/product/' . $asin . '">Buy</a>';
        }
      }
      $out .= '<h2>The XML looked like this:</h2>';
      $out .= '<blockquote>';
      $out .= htmlentities($xml);
      $out .= '</blockquote>';
      $_SESSION['musicbrainz_data'] = $out;
    }
    else {
      drupal_set_message('No artists found for ' . $artist_name, 'status');
    }
  } catch (Exception $e) {
    drupal_set_message('Error: ' . $e
      ->getMessage(), 'status');
  }
}