'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 ); $items['diggthis/top-stories'] = array( 'title' => 'Digg Top Stories', 'page callback' => 'diggthis_top_stories', 'access arguments' => array('access digg stories'), 'type' => MENU_NORMAL_ITEM ); return $items; } /** * Implementation of hook_theme */ function diggthis_theme() { return array( 'diggthis_button' => array( 'arguments' => array('js_code' => NULL) ), 'diggthis_top_stories' => array( 'arguments' => array('data' => NULL) ) ); } /** * Implementation of hook_perm */ function diggthis_perm() { return array('access digg stories'); } /** * Admin settings form for the diggthis module */ function diggthis_admin_settings() { $form = array(); $form['diggthis'] = array( '#type' => 'fieldset', '#title' => t('diggthis settings') ); $form['diggthis']['diggthis_button_skin'] = array( '#type' => 'select', '#title' => t('Button skin'), '#default_value' => variable_get('diggthis_button_skin', 'standard'), '#options' => array( 'standard' => t('standard'), 'compact' => t('compact') ), '#description' => t('The Button skin option controls the look at the button. If set to standard the button defaults to a standard digg button (much like the one you see on Digg itself). If specified as compact, then a smaller horizontal visual design is used that will fit better into a list of links.') ); $form['diggthis']['diggthis_button_bgcolor'] = array( '#type' => 'textfield', '#title' => t('Button background color'), '#default_value' => variable_get('diggthis_button_bgcolor', ''), '#size' => 7, '#maxlength' => 7, '#description' => t('Enter a hexadecimal color value here, e.g. #ff9900. Include the leading # and enter 6 numbers/digits') ); $form['diggthis']['diggthis_button_weight'] = array( '#type' => 'select', '#title' => t('Weight'), '#default_value' => variable_get('diggthis_button_weight', 10), '#options' => drupal_map_assoc(range(-20, 20)), '#description' => t('Specifies the position of the Diggthis button. A low weight, e.g. -20 will display the button above the content and a high weight, e.g. 20 below the content.') ); $form['diggthis']['diggthis_node_types'] = array( '#type' => 'checkboxes', '#title' => t('Node Types'), '#default_value' => variable_get('diggthis_node_types', array()), '#options' => node_get_types('names'), '#description' => t('activate the node types in where the digg button shall be displayed') ); $form['diggthis_api'] = array( '#type' => 'fieldset', '#title' => t('diggthis API settings') ); $form['diggthis_api']['diggthis_stories_domain'] = array( '#type' => 'textfield', '#title' => t('Digg Stories Domain'), '#default_value' => variable_get('diggthis_stories_domain', ''), '#size' => 32, '#maxlength' => 128, '#description' => t('Enter the domain, e.g. mydomain.com, that will be used to show top Digg stories for.') ); $period = drupal_map_assoc(array(21600, 43200, 86400), 'format_interval'); $form['diggthis_api']['diggthis_cache_lifetime'] = array( '#type' => 'select', '#title' => t('Minimum cache lifetime'), '#default_value' => variable_get('diggthis_cache_lifetime', 43200), '#options' => $period, '#description' => t('The time the response from the Digg API is cached before being requested again.') ); return system_settings_form($form); } /** * 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; $bg_string digg_skin = '$skin'; 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; } /** * 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; }