Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
413 lines (348 sloc) 11.1 KB
<?php
/**
* @file
* Band-related CRUD callbacks
*/
/**
* Implementation of band page view callback
*
* Ideally this should be batched as we don't know how many albums might be imported
* but for now the import process occurs in a single request
*/
function band_page_view($band, $view_mode = 'full') {
$band->content = array();
$output = '';
$releases = array();
$result = db_query("
SELECT rid FROM {bandcamp_release} WHERE bid=:bid
", array(':bid' => $band->bid));
$rids = array();
foreach($result as $release) {
$rids[] = $release->rid;
}
$releases = entity_load('release', $rids);
foreach($releases as $release) {
// get release greased up for display
$release = entity_get_controller('release')->view($release, $band);
$releases[$release->rid]->tracks = array();
}
$result = db_query("
SELECT tid FROM {bandcamp_track} WHERE bid=:bid
", array(':bid' => $band->bid));
$tids = array();
foreach($result as $track) {
$tids[] = $track->tid;
}
$tracks = entity_load('track', $tids);
foreach($tracks as $track) {
// get track greased up for display
$release = $releases[$track->rid];
$track = entity_get_controller('track')->view($track, $band, $release);
$releases[$track->rid]->tracks[$track->bandcamp_track_number['und'][0]['value']] = $track;
}
// release data now compiled; display
$output = '';
foreach($releases as $release) {
$output .= theme('bandcamp_release', array('release' => $release, 'band' => $band));
}
if($output) {
$band->content['releases'] = array('#markup' => $output);
return $band->content;
}
else {
return '<p>No releases found for this band.</p>';
}
}
/**
* Band creation callback
*/
function band_add() {
$band = (object) array(
'bid' => '',
'type' => 'band',
'api_key' => '',
'band_id' => '',
'band_name' => '',
'band_url_name' => '',
);
return drupal_get_form('band_add_form', $band);
}
/**
* Band entity add form
*/
function band_add_form($form, &$form_state, $band) {
$form['band_name'] = array(
'#type' => 'textfield',
'#title' => t('Band name'),
'#description' => t("Enter your band's name"),
'#required' => true,
);
$form['band_url_name'] = array(
'#type' => 'textfield',
'#title' => t('Band URL name'),
'#description' => t("Enter your band's name as it appears in the bandcamp url e.g. <em>&lt;bandname&gt;.bandcamp.com</em>"),
'#required' => true,
);
$form['band_id'] = array(
'#type' => 'textfield',
'#title' => t('Band ID'),
'#description' => t('Enter numeric Bandcamp band id (if known)'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
field_attach_form('band', $band, $form, $form_state);
return $form;
}
/**
* Band form validation hook
*/
function band_add_form_validate($form, &$form_state) {
$band_submission = (object) $form_state['values'];
// get band id if not extant
if(!$band_submission->band_id) {
require_once 'bandcamp.api.php';
$key = variable_get('bandcamp_api_key', '');
if(!$key) {
form_set_error('', t('No Bandcamp API key configured. You cannot add bands at this time'));
return;
}
try {
$bandcamp_conn = new bandcampConnection($key, $band_submission->band_url_name);
}
catch(Exception $e) {
form_set_error('band_id', t('Could not retrieve band id from Bandcamp: ' . check_plain($e->getMessage())));
return;
}
try {
$form_state['values']['band_id'] = $bandcamp_conn->getBandId();
}
catch(Exception $e) {
form_set_error('band_id', t('Could not retrieve band id from Bandcamp: ' . check_plain($e->message)));
}
}
field_attach_form_validate('band', $band_submission, $form, $form_state);
}
/**
* Band form submit hook
*/
function band_add_form_submit($form, &$form_state) {
$band_submission = (object) $form_state['values'];
field_attach_submit('band', $band_submission, $form, $form_state);
$band = band_save($band_submission);
# $form_state['redirect'] = "band/$band->bid";
$form_state['redirect'] = "band-import/$band->bid";
}
/**
* Band save helper function
*/
function band_save(&$band) {
return entity_get_controller('band')->save($band);
}
/**
* This essentially copies node_load()
*/
function band_load($nid = NULL, $vid = NULL, $reset = FALSE) {
$nids = (isset($nid) ? array($nid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$band = band_load_multiple($nids, $conditions, $reset);
return $band ? reset($band) : FALSE;
}
/**
* ...And this essentially copies node_load_multiple()
*/
function band_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('band', $nids, $conditions, $reset);
}
/**
* Set band page title
*/
function band_page_title($band) {
return t('!band: releases', array('!band' => $band->band_name));
}
/**
* Callback for importing release and track data for a band
*
* User is redirected to this page after creating band
*/
function band_import($band, $reimport = false) {
if($band->imported && !$reimport) {
drupal_set_message("This band's data has already been imported from Bandcamp", 'error');
return drupal_not_found();
}
// if we're reimporting data, we'd best remove everything we've currently got
if($reimport) {
db_delete('bandcamp_release')
->condition('bid', $band->bid)
->execute();
db_delete('bandcamp_track')
->condition('bid', $band->bid)
->execute();
}
// first get API key
$key = variable_get('bandcamp_api_key', '');
if(!$key) {
return t('Bandcamp API key not found');
}
require_once('bandcamp.api.php');
try {
$bc = new bandcampConnection($key, $band->band_name, $band->band_id);
$discography = $bc->getDiscography();
foreach($discography as $disc) {
// first validate disc
$release = entity_get_controller('release')->prepare($disc, $band);
if(!entity_get_controller('release')->validate($release)) {
watchdog('bandcamp', t('Release !name by !band could not be imported', array('!name' => $release->title, '!band' => $band->band_name)));
}
else {
// prepare release (including field data structures
release_save($release);
// ok, now onto the tracks
$tracks = array();
foreach($disc->tracks as $raw_track) {
$track = entity_get_controller('track')->prepare($raw_track, $release, $band);
try {
entity_get_controller('track')->validate($track);
}
catch(Exception $e) {
// we'd best move on then
watchdog('bandcamp', t('Track !name by !band could not be imported', array('!name' => $track->title, '!band' => $band->band_name)));
continue;
}
track_save($track);
bandcamp_report(t('Saved track %track_name from album %album_name', array('%track_name' => $raw_track->title, '%album_name' => $disc->title)));
}
}
}
// we set 'imported' value on $band so that we can't reimport all the guff again
db_update('bandcamp_band')
->fields(array('imported' => 1))
->condition('bid', $band->bid)
->execute();
// message back to user the import results
bandcamp_report();
}
catch(Exception $e) {
return $e->getMessage();
}
if(!$reimport) {
drupal_goto('band/' . $band->bid);
}
}
/**
* Confirmation form for band data reimport
*/
function band_reimport_confirm($form, &$form_state, $band) {
$form['#band'] = $band;
// Always provide entity id in the same form key as in the entity edit form.
$form['bid'] = array(
'#type' => 'value',
'#value' => $band->bid,
);
return confirm_form($form,
t('Are you sure you want to reimport data for band %title?', array('%title' => $band->band_name)),
'band/' . $band->bid,
t('This action cannot be undone.'),
t('Reimport band data'),
t('Cancel')
);
}
/**
* Submit function for band_reimport_confirm form
*/
function band_reimport_confirm_submit(&$form, &$form_state) {
// pull band out of form_state bits
$band = $form_state['build_info']['args'][0];
// set reimport to override not reimport behaviour
band_import($band, true);
drupal_set_message('Band data reimported from Bandcamp');
$form_state['redirect'] = 'band/' . $band->bid;
}
/**
* Form builder for confirming a band deletion
*
* Essentially a copy of node_delete_confirm()
*/
function band_delete_confirm($form, &$form_state, $band) {
$form['#band'] = $band;
// Always provide entity id in the same form key as in the entity edit form.
$form['bid'] = array(
'#type' => 'value',
'#value' => $band->bid,
);
return confirm_form($form,
t('Are you sure you want to delete band %title?', array('%title' => $band->band_name)),
'band/' . $band->bid,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Submit function for band_delete_confirm form. Deletes band on confirmation
*
* Pretty much verbatim copy of node_delete_confirm_submit
*/
function band_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$band = band_load($form_state['values']['bid']);
band_delete($form_state['values']['bid']);
watchdog('content', '@type: deleted %title.', array('%title' => $band->band_name));
drupal_set_message(t('Band %title has been deleted.', array('%title' => $band->band_name)));
}
$form_state['redirect'] = '<front>';
}
/**
* Wrapper function for deleting band entities.
*
* Copy of node_delete()
*/
function band_delete($bid) {
band_delete_multiple(array($bid));
}
/**
* Delete band entities and child release and track entities
*
* Copy of node_delete_multiple
*/
function band_delete_multiple($bids) {
$transaction = db_transaction();
if (!empty($bids)) {
$bands = band_load_multiple($bids, array());
try {
foreach ($bands as $bid => $band) {
// Call the band-specific callback (if any):
module_invoke_all('band_delete', $band);
module_invoke_all('entity_delete', $band, 'band');
field_attach_delete('band', $band);
// Remove this band from the search index if needed.
// This code is implemented in band module rather than in search module,
// because band module is implementing search module's API, not the other
// way around.
// TODO: bandcamp module should probably do search.
/* if (module_exists('search')) {
search_reindex($bid, 'band');
} */
}
// Delete after calling hooks so that they can query band tables as needed.
// Remove child releases and tracks for band
db_delete('bandcamp_band')
->condition('bid', $bids, 'IN')
->execute();
db_delete('bandcamp_release')
->condition('bid', $bids, 'IN')
->execute();
db_delete('bandcamp_track')
->condition('bid', $bids, 'IN')
->execute();
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('bandcamp', $e);
throw $e;
}
// Clear the page and block and band_load_multiple caches.
entity_get_controller('band')->resetCache();
}
}
Something went wrong with that request. Please try again.