'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 site configuration'), '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'); } /** * 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) ); } } } /** * Implementaiton 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 * * @param $node * an optional node object */ function diggthis_button_create($node = false) { // we are in node view if ($node) { $path = 'node/'. $node->nid; $title = check_plain($node->title); $teaser = strip_tags($node->teaser); } 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); } } // get the absolute URL of the current page $url = url($path, array('absolute' => TRUE)); // make sure strings are not to long $title = truncate_utf8($title, 60, TRUE, FALSE); $teaser = drupal_to_js(truncate_utf8($teaser, 350, TRUE, FALSE)); // only change the background color if configured $bgcolor = variable_get('diggthis_button_bgcolor', ''); if ($bgcolor) { $bg_string = "digg_bgcolor = '$bgcolor';"; } $skin = variable_get('diggthis_button_skin', 'standard'); $digg_js =<< digg_url = '$url'; digg_title = '$title'; digg_bodytext = $teaser; digg_skin = '$skin'; $bg_string EOF; return theme('diggthis_button', $digg_js); } /** * Theme function for button display * * @param $js_code * The JavaScript code to display the diggthis button */ function theme_diggthis_button($js_code) { return '
'. $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; } /** * 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; }