'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 ); return $items; } /** * Implementaiton of hook_theme */ function diggthis_theme() { return array( 'diggthis_button' => array( 'arguments' => array('js_code' => 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') ); 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; }