[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/catalog/ -> station_catalog.module (source)

   1  <?php
   2  // $Id: station_catalog.module,v 1.37 2010/06/27 18:33:04 drewish Exp $
   3  
   4  /**
   5   * Implementation of hook_help().
   6   */
   7  function station_catalog_help($path, $arg) {
   8    switch ($path) {
   9      case 'admin/settings/station/catalog':
  10        return t("These settings allow you to configure the station catalog.");
  11    }
  12  }
  13  
  14  /**
  15   * Implementation of hook_node_info().
  16   */
  17  function station_catalog_node_info() {
  18    return array(
  19      'station_album' => array(
  20        'name' => t('Album'),
  21        'module' => 'station_catalog_album',
  22        'description' => t("An album in the station's library."),
  23        'has_title' => FALSE,
  24        'title_label' => '',
  25        'has_body' => TRUE,
  26        'body_label' => t('Description'),
  27      )
  28    );
  29  }
  30  
  31  /**
  32   * Implementation of hook_perm().
  33   */
  34  function station_catalog_perm() {
  35    return array(
  36      'administer station catalog',
  37      'view station album content',
  38      'create station album content',
  39      'edit any station album content',
  40      'delete any station album content',
  41    );
  42  }
  43  
  44  /**
  45   * Implementation of hook_access().
  46   */
  47  function station_catalog_album_access($op, $node, $account) {
  48    // Admins can do anything.
  49    if (user_access('administer station catalog', $account)) {
  50      return TRUE;
  51    }
  52  
  53    switch ($op) {
  54      case 'view':
  55        return user_access('view station album content', $account);
  56  
  57      case 'create':
  58        return user_access('create station album content', $account);
  59  
  60      case 'update':
  61        return user_access('edit any station album content', $account);
  62  
  63      case 'delete':
  64        return user_access('delete any station album content', $account);
  65    }
  66  }
  67  
  68  /**
  69   * Returns the the next unused album number.
  70   *
  71   * @return
  72   *   Integer with the next, unused album number.
  73   */
  74  function station_catalog_album_next_number() {
  75    return db_result(db_query('SELECT MAX(number) + 1 FROM {station_catalog}'));
  76  }
  77  
  78  /**
  79   * Implementation of hook_prepare().
  80   */
  81  function station_catalog_album_prepare(&$node) {
  82    if (isset($node->album['artist']) && isset($node->album['album'])) {
  83      if ($mb_info = _station_catalog_musicbrainz_album($node->album['artist'], $node->album['album'])) {
  84        $fields = array(
  85          'artist' => t('Artist'),
  86          'album' => t('Album'),
  87          'year' => t('Year'),
  88          'mb_release_id' => t('Music Brainz ID'),
  89          'asin' => t('ASIN'),
  90        );
  91        $adjusted = array();
  92        foreach ($fields as $field => $name) {
  93          if (empty($node->album[$field])) {
  94            $adjusted[] = $name;
  95            $node->album[$field] = $mb_info[$field];
  96          }
  97        }
  98        if ($adjusted) {
  99          drupal_set_message(t('Values for @fields fields were loaded from the MusicBrainz data.', array('@fields' => station_anded_list($adjusted))));
 100        }
 101      }
 102    }
 103  }
 104  
 105  /**
 106   * Implementation of hook_form().
 107   */
 108  function station_catalog_album_form($node) {
 109    $type = node_get_types('type', $node);
 110  
 111    $form['album'] = array(
 112      '#type' => 'fieldset',
 113      '#title' => t('Album information'),
 114      '#tree' => TRUE,
 115      '#weight' => -4,
 116    );
 117    $form['album']['number'] = array(
 118      '#type' => 'textfield',
 119      '#title' => t('Number'),
 120      '#default_value' => isset($node->album['number']) ? $node->album['number'] : '',
 121      '#description' => t("The album's log number must be numeric and unique."),
 122    );
 123    $form['album']['artist'] = array(
 124      '#type' => 'textfield',
 125      '#title' => t('Artist'),
 126      '#default_value' => isset($node->album['artist']) ? $node->album['artist'] : '',
 127      '#required' => TRUE,
 128      '#maxlength' => 255,
 129    );
 130    $form['album']['album'] = array(
 131      '#type' => 'textfield',
 132      '#title' => t('Album title'),
 133      '#default_value' => isset($node->album['album']) ? $node->album['album'] : '',
 134      '#required' => TRUE,
 135      '#maxlength' => 255,
 136    );
 137    $form['album']['year'] = array(
 138      '#type' => 'textfield',
 139      '#title' => t('Year'),
 140      '#default_value' => isset($node->album['year']) ? $node->album['year'] : '',
 141      '#required' => TRUE,
 142      '#description' => t("The year the album was released."),
 143    );
 144    $form['album']['label'] = array(
 145      '#type' => 'textfield',
 146      '#title' => t('Label'),
 147      '#default_value' => isset($node->album['label']) ? $node->album['label'] : '',
 148      '#required' => FALSE,
 149      '#maxlength' => 255,
 150      '#description' => t("Name of the label that released the album."),
 151    );
 152    $form['album']['mb_release_id'] = array(
 153      '#type' => 'textfield',
 154      '#title' => t('Music Brainz Release ID'),
 155      '#default_value' => isset($node->album['mb_release_id']) ? $node->album['mb_release_id'] : '',
 156      '#required' => FALSE,
 157      '#maxlength' => 255,
 158      '#description' => t("MusicBrainz ID for this release."),
 159    );
 160    $form['album']['asin'] = array(
 161      '#type' => 'textfield',
 162      '#title' => t('Amazon Standard Identification Number'),
 163      '#default_value' => isset($node->album['asin']) ? $node->album['asin'] : '',
 164      '#required' => FALSE,
 165      '#maxlength' => 255,
 166      '#description' => t("Amazon product ID for this album."),
 167    );
 168  
 169    if ($type->has_body) {
 170      $form['body_filter']['body'] = array(
 171        '#type' => 'textarea',
 172        '#title' => check_plain($type->body_label),
 173        '#default_value' => isset($node->body) ? $node->body : '',
 174        '#rows' => 10,
 175        '#required' => ($type->min_word_count > 0),
 176      );
 177      $form['body_filter']['format'] = filter_form($node->format);
 178    }
 179  
 180    $form['#submit'][] = 'station_catalog_album_node_form_submit';
 181  
 182    return $form;
 183  }
 184  
 185  /**
 186   * Implementation of hook_validate().
 187   */
 188  function station_catalog_album_validate(&$node, &$form) {
 189    if ($node->album['number'] != '') {
 190      if (!is_numeric($node->album['number'])) {
 191        form_set_error('album][number', t('Album number must be a numeric value.'));
 192      }
 193      elseif ($node->album['number']) {
 194        // look for duplicate numbers
 195        if ($node->nid) {
 196          $query = db_query('SELECT sc.nid, n.title FROM {station_catalog} sc INNER JOIN {node} n ON sc.nid = n.nid WHERE sc.number = %d AND sc.nid <> %d', $node->album['number'], $node->nid);
 197        }
 198        else {
 199          $query = db_query('SELECT sc.nid, n.title FROM {station_catalog} sc INNER JOIN {node} n ON sc.nid = n.nid WHERE sc.number = %d', $node->album['number']);
 200        }
 201        if ($other_node = db_fetch_object($query)) {
 202          form_set_error('album][number', t('Another album <a href="@link">%title</a> already has this number.', array('@link' => url('node/'. $other_node->nid), '%title' => $other_node->title))
 203          );
 204        }
 205      }
 206    }
 207  
 208    if (!empty($node->album['year']) && !is_numeric($node->album['year'])) {
 209      form_set_error('album][year', t('Year must be a numeric value.'));
 210    }
 211  }
 212  
 213  /**
 214   * Form submit handler to set the node's title().
 215   */
 216  function station_catalog_album_node_form_submit($form, &$form_state) {
 217    // Compute the title.
 218    $form_state['values']['title'] = $form_state['values']['album']['artist'] .' - '. $form_state['values']['album']['album'];
 219    $form_state['values']['album']['number'] = ($form_state['values']['album']['number'] != '') ? $form_state['values']['album']['number'] : station_catalog_album_next_number();
 220  }
 221  
 222  /**
 223   * Implementation of hook_load().
 224   */
 225  function station_catalog_album_load($node) {
 226    $result = db_query('SELECT number, artist, album, year, label, mb_release_id, asin FROM {station_catalog} WHERE nid = %d', $node->nid);
 227    return array('album' => db_fetch_array($result));
 228  }
 229  
 230  /**
 231   * Implementation of hook_insert().
 232   */
 233  function station_catalog_album_insert($node) {
 234    $record = array_merge($node->album, array('nid' => $node->nid));
 235    drupal_write_record('station_catalog', $record);
 236  }
 237  
 238  /**
 239   * Implementation of hook_delete().
 240   */
 241  function station_catalog_album_delete($node) {
 242    db_query("DELETE FROM {station_catalog} WHERE nid = %d", $node->nid);
 243  }
 244  
 245  /**
 246   * Implementation of hook_update().
 247   */
 248  function station_catalog_album_update($node) {
 249    db_query("DELETE FROM {station_catalog} WHERE nid = %d", $node->nid);
 250    db_query("INSERT INTO {station_catalog} (nid, number, artist, album, year, label, mb_release_id, asin) VALUES (%d, %d, '%s', '%s', %d, '%s', '%s', '%s')", $node->nid, $node->album['number'], $node->album['artist'], $node->album['album'], $node->album['year'], $node->album['label'], $node->album['mb_release_id'], $node->album['asin']);
 251  }
 252  
 253  /**
 254   * Implementation of hook_view().
 255   */
 256  function station_catalog_album_view(&$node, $teaser = FALSE, $page = FALSE) {
 257    $node = node_prepare($node, $teaser);
 258  
 259    if ($page) {
 260      $breadcrumb = drupal_get_breadcrumb();
 261      $breadcrumb[] = l(t('Station'), 'station');
 262      $breadcrumb[] = l(t('Catalog'), 'station/catalog/search');
 263      drupal_set_breadcrumb($breadcrumb);
 264    }
 265  
 266    $node->content['album'] = array(
 267      'number' => array(
 268        '#type' => 'item',
 269        '#title' => t('Number'),
 270        '#value' => $node->album['number'],
 271        '#weight' => -6,
 272      ),
 273      'artist' => array(
 274        '#type' => 'item',
 275        '#title' => t('Artist'),
 276        '#value' => $node->album['artist'],
 277        '#weight' => -5,
 278      ),
 279      'album' => array(
 280        '#type' => 'item',
 281        '#title' => t('Album'),
 282        '#value' => $node->album['album'],
 283        '#weight' => -4,
 284      ),
 285      'year' => array(
 286        '#type' => 'item',
 287        '#title' => t('Year'),
 288        '#value' => $node->album['year'] ? $node->album['year'] : '',
 289        '#weight' => -3,
 290      ),
 291      'label' => array(
 292        '#type' => 'item',
 293        '#title' => t('Label'),
 294        '#value' => $node->album['label'],
 295        '#weight' => -2,
 296      ),
 297    );
 298  
 299    if (!empty($node->album['mb_release_id'])) {
 300      $node->content['album']['mb_release_id'] = array(
 301        '#type' => 'item',
 302        '#title' => t('MusicBrainz'),
 303        '#value' => l($node->album['mb_release_id'], "http://musicbrainz.org/release/{$node->album['mb_release_id']}.html"),
 304        '#weight' => 0,
 305      );
 306    }
 307    if (!empty($node->album['asin'])) {
 308      $node->content['album']['asin'] = array(
 309        '#type' => 'item',
 310        '#title' => t('Amazon'),
 311        '#value' => l($node->album['asin'], 'http://www.amazon.com/gp/product/'. $node->album['asin']),
 312        '#weight' => 0,
 313      );
 314    }
 315  
 316    return $node;
 317  }
 318  
 319  /**
 320   * Implementation of hook_taxonomy().
 321   *
 322   * Delete our vocabulary variable if the vocabulary is deleted.
 323   */
 324  function station_catalog_taxonomy($op, $type, $object = NULL) {
 325    if ($op == 'delete' && $type == 'vocabulary' && $object->vid == _station_catalog_get_vid())  {
 326      variable_del('station_catalog_vocabulary');
 327    }
 328  }
 329  
 330  
 331  /**
 332   * Find or create a station catalog vocabulary ID.
 333   *
 334   * @return
 335   *   Vocabulary ID.
 336   */
 337  function _station_catalog_get_vid() {
 338    $vid = variable_get('station_catalog_vocabulary', '');
 339    if (empty($vid)) {
 340      // Check to see if a a  vocabulary exists
 341      $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", 'station_catalog'));
 342      if (!$vid) {
 343        $vocabulary = array(
 344          'name' => t('Station album genres'),
 345          'description' => t("Select the appropriate genre's for this album."),
 346          'multiple' => '1',
 347          'required' => '1',
 348          'hierarchy' => '0',
 349          'relations' => '0',
 350          'module' => 'station_catalog',
 351          'nodes' => array('station_album' => 1),
 352        );
 353        taxonomy_save_vocabulary($vocabulary);
 354        $vid = $vocabulary['vid'];
 355      }
 356      variable_set('station_catalog_vocabulary', $vid);
 357    }
 358    return $vid;
 359  }
 360  
 361  function _station_catalog_musicbrainz_album($artist, $title) {
 362    $args = array(
 363      'type' => 'xml',
 364      'artist' => $artist,
 365      'title' => $title
 366    );
 367    $url = url('http://musicbrainz.org/ws/1/release/', array('query' => drupal_query_string_encode($args)));
 368  
 369    $ret = array();
 370  
 371    $response = drupal_http_request($url);
 372    if ($response->code == 200) {
 373      $doc = DOMDocument::loadXML($response->data);
 374  
 375      $xpath = new DOMXPath($doc);
 376      // Can't use the default namespace, must explicitly set one...
 377      $xpath->registerNameSpace('mb', 'http://musicbrainz.org/ns/mmd-1.0#');
 378  #dvm($doc->saveXML());
 379      // Only return exact matches.
 380      $result = $xpath->query('//mb:release-list/mb:release[@ext:score="100"]');
 381      if ($result && $result->length) {
 382        foreach ($result as $item) {
 383          $release = simplexml_import_dom($result->item(0));
 384  #        dvm($release);
 385        }
 386        $release = simplexml_import_dom($result->item(0));
 387        $ret['mb_release_id'] = (string) $release['id'];
 388        $ret['artist'] = (string) $release->artist->name;
 389        $ret['album'] = (string) $release->title;
 390        $ret['asin'] = (string) $release->asin;
 391        // Release year is a bit of a pain, the tag name isn't a valid PHP
 392        // identifier and we need to use a regular expression to match the
 393        // year portion of the ate.
 394        $event = 'release-event-list';
 395        preg_match('/.*US (\d{4}).*/', $release->$event->event['date'], $matches);
 396        $ret['year'] = (string) $matches[1];
 397  
 398        // Now that we've got the release id, we need to make another call to get the label.
 399  #      $label = _station_catalog_musicbrainz_label($ret['mb_release_id']);
 400        $ret['label'] = '';
 401      }
 402    }
 403    return $ret;
 404  }
 405  
 406  function _station_catalog_musicbrainz_label($release_id) {
 407    $args = array(
 408      'type' => 'xml',
 409      'inc' => 'labels',
 410    );
 411    $url = url('http://musicbrainz.org/ws/1/release/'. $release_id, array('query' => drupal_query_string_encode($args)));
 412  dvm($url);
 413    $ret = array();
 414    $response = drupal_http_request($url);
 415    if ($response->code == 200) {
 416      $doc = DOMDocument::loadXML($response->data);
 417  dvm($doc->savexml());
 418    }
 419    return $ret;
 420  }
 421  
 422  /**
 423   * Implementation of hook_view_api().
 424   */
 425  function station_catalog_views_api() {
 426    return array(
 427      'api' => 2.0,
 428    );
 429  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7