? .cvsignore
? station_catalog.module.patch
Index: station_catalog.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/catalog/station_catalog.module,v
retrieving revision 1.37
diff -u -p -r1.37 station_catalog.module
--- station_catalog.module	27 Jun 2010 18:33:04 -0000	1.37
+++ station_catalog.module	22 Sep 2010 07:32:44 -0000
@@ -79,11 +79,26 @@ function station_catalog_album_next_numb
  * Implementation of hook_prepare().
  */
 function station_catalog_album_prepare(&$node) {
+  _station_catalog_musicbrainz_set_album(&$node);
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function station_catalog_nodeapi(&$node, $op) {
+  if ($op == 'presave') {
+    _station_catalog_musicbrainz_set_album($node);
+    station_catalog_album_insert($node);
+  }
+}
+
+function _station_catalog_musicbrainz_set_album($node){
   if (isset($node->album['artist']) && isset($node->album['album'])) {
-    if ($mb_info = _station_catalog_musicbrainz_album($node->album['artist'], $node->album['album'])) {
+    if ($mb_info = _station_catalog_musicbrainz_get_album($node->album['artist'], $node->album['album'])) {
       $fields = array(
         'artist' => t('Artist'),
         'album' => t('Album'),
+        'label' => t('Label'),
         'year' => t('Year'),
         'mb_release_id' => t('Music Brainz ID'),
         'asin' => t('ASIN'),
@@ -96,6 +111,7 @@ function station_catalog_album_prepare(&
         }
       }
       if ($adjusted) {
+        station_catalog_album_insert($node);
         drupal_set_message(t('Values for @fields fields were loaded from the MusicBrainz data.', array('@fields' => station_anded_list($adjusted))));
       }
     }
@@ -138,7 +154,6 @@ function station_catalog_album_form($nod
     '#type' => 'textfield',
     '#title' => t('Year'),
     '#default_value' => isset($node->album['year']) ? $node->album['year'] : '',
-    '#required' => TRUE,
     '#description' => t("The year the album was released."),
   );
   $form['album']['label'] = array(
@@ -184,6 +199,7 @@ function station_catalog_album_form($nod
 
 /**
  * Implementation of hook_validate().
+ * @todo Don't allow duplicate album names for an artist
  */
 function station_catalog_album_validate(&$node, &$form) {
   if ($node->album['number'] != '') {
@@ -358,7 +374,7 @@ function _station_catalog_get_vid() {
   return $vid;
 }
 
-function _station_catalog_musicbrainz_album($artist, $title) {
+function _station_catalog_musicbrainz_get_album($artist, $title) {
   $args = array(
     'type' => 'xml',
     'artist' => $artist,
@@ -375,13 +391,11 @@ function _station_catalog_musicbrainz_al
     $xpath = new DOMXPath($doc);
     // Can't use the default namespace, must explicitly set one...
     $xpath->registerNameSpace('mb', 'http://musicbrainz.org/ns/mmd-1.0#');
-#dvm($doc->saveXML());
     // Only return exact matches.
     $result = $xpath->query('//mb:release-list/mb:release[@ext:score="100"]');
     if ($result && $result->length) {
       foreach ($result as $item) {
         $release = simplexml_import_dom($result->item(0));
-#        dvm($release);
       }
       $release = simplexml_import_dom($result->item(0));
       $ret['mb_release_id'] = (string) $release['id'];
@@ -390,35 +404,22 @@ function _station_catalog_musicbrainz_al
       $ret['asin'] = (string) $release->asin;
       // Release year is a bit of a pain, the tag name isn't a valid PHP
       // identifier and we need to use a regular expression to match the
-      // year portion of the ate.
+      // year portion of the date. It gets even trickier with reissues
+      // and albums with multiple release dates across countries.
       $event = 'release-event-list';
-      preg_match('/.*US (\d{4}).*/', $release->$event->event['date'], $matches);
-      $ret['year'] = (string) $matches[1];
-
-      // Now that we've got the release id, we need to make another call to get the label.
-#      $label = _station_catalog_musicbrainz_label($ret['mb_release_id']);
-      $ret['label'] = '';
+      if (strlen($release->$event->event['date']) == 4) {
+        $ret['year'] = (string) $release->$event->event['date'];
+      } elseif (preg_match('/.*US (\d{4}).*/', $release->$event->event['date'], $matches)) {
+        $ret['year'] = (string) $matches[1];
+      } elseif (preg_match('/(\d{4})-\d{2}-\d{2}/', $release->$event->event['date'], $matches)) {
+        $ret['year'] = (string) $matches[1];
+      }
+      $ret['label'] = (string) $release->$event->event->label->name;
     }
   }
   return $ret;
 }
 
-function _station_catalog_musicbrainz_label($release_id) {
-  $args = array(
-    'type' => 'xml',
-    'inc' => 'labels',
-  );
-  $url = url('http://musicbrainz.org/ws/1/release/'. $release_id, array('query' => drupal_query_string_encode($args)));
-dvm($url);
-  $ret = array();
-  $response = drupal_http_request($url);
-  if ($response->code == 200) {
-    $doc = DOMDocument::loadXML($response->data);
-dvm($doc->savexml());
-  }
-  return $ret;
-}
-
 /**
  * Implementation of hook_view_api().
  */
