'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 content'), '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) ) ); } /** * Admin settings form for the diggthis module */ function diggthis_admin_settings() { $form = array(); $form['diggthis'] = array( '#type' => 'fieldset', '#title' => 'diggthis '. t('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') ); $period = drupal_map_assoc(array(21600, 43200, 86400), 'format_interval'); $form['diggthis']['diggthis_cache_lifetime'] = array( '#type' => 'select', '#title' => t('Minimum cache lifetime'), '#default_value' => variable_get('diggthis_cache_lifetime', 43200), '#options' => $period, '#required' => TRUE, '#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': $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 = diggthis_cut($title, 60); $teaser = drupal_to_js(diggthis_cut($teaser, 350)); // 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 .'
'; } /* * Helper function for shortening a string * to the desired length * * @param $string * The string that will be shortened * * @param $length * An integer to set the length of the resulting string */ function diggthis_cut($string, $length) { if (strlen($string) > $length) { $string = substr($string, 0, $length); $string = preg_replace("/\s+\S+$/", "...", $string); } return $string; } /** * Do the Digg API request and retrieve the XML feed * with the top Digg stories for a given domain. * * @return string $xml */ function diggthis_request($options) { $api_url = 'http://services.digg.com/stories'; $request_url = url($api_url, $options); $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); // 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)); } return $xml; } /** * Do the Digg API request and retrieve the XML feed * with the top Digg stories for a given domain. * * @return String themed HTML of top Digg stories * * @todo replace hard coded urls */ function diggthis_top_stories() { $options = array( 'query' => array('count' => 10, 'appkey' => 'http://www.seo-expert-blog.com', 'domain' => 'www.seo-expert-blog.com', 'sort' => 'digg_count-desc'), 'absolute' => TRUE ); $xml = diggthis_request($options); return theme('diggthis_top_stories', simplexml_load_string($xml)); } /** * Theme function for the digg this story page * * @param Object $data * @return String $html */ 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; }