'Diggthis', 'description' => 'Enable the node types and set the properties for the diggthis button.', 'page callback' => 'drupal_get_form', 'page arguments' => array('diggthis_admin_settings'), 'access arguments' => array('administer diggthis settings'), 'type' => MENU_NORMAL_ITEM, 'file' => 'diggthis.admin.inc' ); $items['diggthis/top-stories'] = array( 'title' => 'Digg Top Stories', 'page callback' => 'diggthis_top_stories', 'access arguments' => array('access digg stories'), 'type' => MENU_NORMAL_ITEM, 'file' => 'diggthis.pages.inc' ); return $items; } /** * Implementation of hook_theme */ function diggthis_theme() { return array( 'diggthis_button' => array( 'arguments' => array('js_code' => NULL) ), 'diggthis_story' => array( 'arguments' => array('story' => NULL) ) ); } /** * Implementation of hook_perm */ function diggthis_perm() { return array('access digg stories', 'administer diggthis settings'); } /** * Implementation of hook_nodeapi(). */ function diggthis_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { // we're in full node view if ($op == 'view' && !$a3) { if (in_array($node->type, variable_get('diggthis_node_types', array()), TRUE)) { $node->content['diggthis_button'] = array( '#value' => diggthis_button_create($node), '#weight' => variable_get('diggthis_button_weight', 10) ); } } } /** * Implementation of hook_block */ function diggthis_block($op = 'list', $delta = 0, $edit = array()) { $blocks = array(); switch ( $op ) { case 'list': $blocks[0] = array( 'info' => t('Digg This'), 'weight' => 0 ); break; case 'view': if ('admin' != arg(0)) { $blocks['content'] = diggthis_button_create(); $blocks['subject'] = variable_get('diggthis_block_title_'.$delta, t('Digg this')); break; } } return $blocks; } /** * A function that generates the JavaScript * code for embedding hte diggthis button * * @see http://about.digg.com/downloads/button/smart * * @param $node * an optional node object */ function diggthis_button_create($node = false) { $title = $teaser = $url = $digg_url = $a = ''; $attr = array(); $attr['class'] = 'DiggThisButton Digg' . variable_get('diggthis_button_skin', 'Medium'); // we are in node view if ($node) { $path = 'node/'. $node->nid; $title = check_plain($node->title); $teaser = strip_tags($node->teaser); $url = url($path, array('absolute' => TRUE)); } elseif ('admin' != arg(0)) { $path = check_plain($_GET['q']); $title = drupal_get_title(); $teaser = ''; // block display in node view if ('node' == arg(0) && is_numeric(arg(1)) && !arg(2)) { $node = node_load(arg(1)); $teaser = strip_tags($node->teaser); } } if ($teaser) { $teaser = sprintf('%s', $teaser); } if ($url) { $digg_url = 'http://digg.com/submit?' . http_build_query(array( 'url' => $url, 'title' => $title )); } // submission type and topic $rev = array(); $type = variable_get('diggthis_type', ''); if ($type) { array_push($rev, $type); } $topic = variable_get('diggthis_topic', ''); if ($topic) { array_push($rev, $topic); } if (!empty($rev)) { if (2 == count($rev)) { $attr_rev = implode(', ', $rev); } else { $attr_rev = $rev[0]; } $attr['rev'] = $attr_rev; } $a = l($teaser, $digg_url, array('attributes' => $attr, 'html' => TRUE)); $digg_code =<< (function() { var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0]; s.type = 'text/javascript'; s.async = true; s.src = 'http://widgets.digg.com/buttons.js'; s1.parentNode.insertBefore(s, s1); })(); $a EOF; return theme('diggthis_button', $digg_code); } /** * Theme function for button display * * @param $js_code * The JavaScript and HTML code to display the diggthis button */ function theme_diggthis_button($code) { return '
'. $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; } /** * Theme function to markup a single digg story * * @param $data * A PHP SimpleXML object * * @return $html * An HTML string with markup for a Digg story */ function theme_diggthis_story($story) { $html = ''; $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; }