'. $js_code .'
';
}
/**
* Do the Digg API request and retrieve the XML feed
* with the top Digg stories for a given domain.
*
* @return $xml
* The XML string as returned by the Digg Web Service
*/
function diggthis_request($query_string) {
$api_url = 'http://services.digg.com/stories';
//$request_url = url($api_url, $options);
$request_url = $api_url . $query_string;
$xml = '';
$cache = cache_get($request_url, 'cache');
if(is_object($cache) && $cache->expire > time()) {
$xml = $cache->data;
}
else {
cache_clear_all($request_url, 'cache');
$response = drupal_http_request($request_url);
if (200 == $response->code) {
// cache the XML string since unserializing simplexml objects does not work
$xml = $response->data;
cache_set($request_url, $xml, 'cache', time() + variable_get('diggthis_cache_lifetime', 43200));
}
else {
watchdog('diggthis', 'Digg top stories could not be retrieved. Server response: "%response"',
array('%response' => var_export($response, TRUE)), WATCHDOG_WARNING);
return FALSE;
}
}
return $xml;
}
/**
* Do the Digg API request and retrieve the XML feed
* with the top Digg stories for a given domain.
*
* @return $html
* An HTML string with markup for the Digg stories or
* an empty string if an error occurred
*
* @todo replace hard coded urls
*/
function diggthis_top_stories() {
$html = '';
$domain = trim(variable_get('diggthis_stories_domain', ''));
if (empty($domain)) {
drupal_set_message(t('There is no domain entered in the diggthis settings.'));
return $html;
}
$appkey = url($_GET['q'], array('absolute' => TRUE));
$domain = drupal_urlencode($domain);
//$options = array(
// 'query' => array('count' => 10,
// 'appkey' => $appkey,
// 'domain' => $domain,
// 'type' => 'php',
// 'sort' => 'digg_count-desc'),
// 'absolute' => TRUE
//);
// ugly but calling url() twice does not work due to urlencodeing
$query_string = '?count=10&sort=digg_count-desc&type=xml&appkey=' . $appkey . '&domain=' . $domain;
$xml = diggthis_request($query_string);
if ($xml) {
$html = theme('diggthis_top_stories', simplexml_load_string($xml));
}
else {
drupal_set_message(t('An error occurred. Digg top stories could not be retrieved.'));
}
return $html;
}
/**
* Theme function for the digg this story page
*
* @param $data
* A PHP SimpleXML object
*
* @return $html
* An HTML string with markup for the Digg stories
*/
function theme_diggthis_top_stories($data) {
$html = '';
foreach ($data->story as $story) {
$digg_link = l(t('Diggs received: !diggs', array('!diggs' => $story['diggs'])),
$story['href'], array('absolute' => TRUE));
$story_link = l($story->title, $story['link'], array('absolute' => TRUE));
$html .= '';
$html .= '
' . $story_link . '
';
$html .= '
' . $story->description . '
';
$html .= '
' . $digg_link . '
';
$html .= '
';
}
return $html;
}