'. t('Display links to social sharing websites like Digg, del.icio.us, reddit, Technorati etc.') .'
'; break; case "admin/modules#description": return ''. t('Control which and where service links should be active.') .'
'; } } /** * Implementation of hook_perm(). */ function service_links_perm() { return array('access service links'); } /** * Implementation of hook_menu(). */ function service_links_menu() { $items = array(); $items['admin/settings/service_links'] = array( 'title' => 'Service links', 'description' => 'Control which and where service links should be active.', 'page callback' => 'drupal_get_form', 'page arguments' => array('service_links_admin_settings'), 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, 'file' => 'service_links.admin.inc', ); $items['admin/settings/service_links/general'] = array( 'title' => 'General Settings', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items['admin/settings/service_links/services'] = array( 'title' => 'Services', 'page callback' => 'drupal_get_form', 'page arguments' => array('service_links_admin_services'), 'access arguments' => array('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'parent' => 'admin/settings/service_links', 'file' => 'service_links.admin.inc', ); return $items; } /** * Implementation of hook_nodeapi(). */ function service_links_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { /*case 'load': if (arg(2) != 'edit') { if (service_links_show($node)) { $node->service_links = service_links_render($node, TRUE); $node->service_links_rendered = theme('service_links_node_format', $node->service_links, variable_get('service_links_label_in_node', 'Bookmark/Search this post with')); } } break;*/ case 'view': //if (isset($node->service_links) && user_access('access service links')) { if ((arg(2) != 'edit') && service_links_show($node) && user_access('access service links')) { $node->service_links = service_links_render($node, TRUE); $node->service_links_rendered = theme('service_links_node_format', $node->service_links, variable_get('service_links_label_in_node', 'Bookmark/Search this post with')); switch (variable_get('service_links_in_node', SERVICE_LINKS_DISABLED)) { case SERVICE_LINKS_IN_TEASER: if ($teaser) { $node->content['service_links'] = array( '#value' => $node->service_links_rendered, '#weight' => variable_get('service_links_weight_in_node', 10), ); } break; case SERVICE_LINKS_IN_FULL: if ($page) { $node->content['service_links'] = array( '#value' => $node->service_links_rendered, '#weight' => variable_get('service_links_weight_in_node', 10), ); } break; case SERVICE_LINKS_IN_BOTH: if ($teaser) { $node->content['service_links'] = array( '#value' => $node->service_links_rendered, '#weight' => variable_get('service_links_weight_in_node', 10), ); } elseif ($page) { $node->content['service_links'] = array( '#value' => $node->service_links_rendered, '#weight' => variable_get('service_links_weight_in_node', 10), ); } break; } } break; } } /** * Implementation of hook_link(). */ function service_links_link($type, $node = NULL, $teaser = FALSE) { $links = array(); if ($type == 'node') { switch (variable_get('service_links_in_links', SERVICE_LINKS_DISABLED)) { case SERVICE_LINKS_DISABLED: $show_links = FALSE; break; case SERVICE_LINKS_IN_TEASER: $show_links = $teaser ? TRUE : FALSE; break; case SERVICE_LINKS_IN_FULL: $show_links = $teaser ? FALSE : TRUE; break; case SERVICE_LINKS_IN_BOTH: $show_links = TRUE; break; default: $show_links = FALSE; break; } if (service_links_show($node) && $show_links && user_access('access service links')) { $links = service_links_render($node, TRUE); } } return $links; } /** * Implementation of hook_block(). */ function service_links_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { $blocks['service_links'] = array( 'info' => t('Service links'), 'cache' => BLOCK_NO_CACHE, ); $blocks['service_links_fisheye'] = array( 'info' => t('Service links with FishEye effect'), 'cache' => BLOCK_NO_CACHE, ); $blocks['service_links_not_node'] = array( 'info' => t('Service links block for not-node pages'), 'cache' => BLOCK_NO_CACHE, ); return $blocks; } elseif ($op == 'view') { $node = menu_get_object('node'); $block = array(); if (user_access('access service links') && (isset($node))) { if (service_links_show($node)) { switch ($delta) { case 'service_links': $block['subject'] = t('Bookmark/Search this post'); $style = variable_get('service_links_block_style', SERVICE_LINKS_STYLE_IMAGE_AND_TEXT); $block['content'] = theme('service_links_block_format', service_links_render($node, FALSE, $style), $style); break; case 'service_links_fisheye': $block['subject'] = t('Bookmark/Search this post'); $block['content'] = theme('service_links_fisheye_format', service_links_render($node, FALSE, SERVICE_LINKS_STYLE_FISHEYE)); break; } } return $block; } elseif (user_access('access service links') && (!isset($node))) { switch ($delta) { case 'service_links_not_node': $block['subject'] = t('Bookmark/Search this post'); $style = variable_get('service_links_block_not_node_style', SERVICE_LINKS_STYLE_IMAGE_AND_TEXT); $block['content'] = theme('service_links_block_format', service_links_render(NULL, FALSE, $style), $style); break; } return $block; } } elseif ($op == 'configure') { $form = array(); switch ($delta) { case 'service_links': $form['service_links_block_style'] = array( '#type' => 'select', '#title' => t('Style'), '#description' => t('How the service links will appear in the block.'), '#default_value' => variable_get('service_links_block_style', SERVICE_LINKS_STYLE_IMAGE_AND_TEXT), '#options' => array( SERVICE_LINKS_STYLE_TEXT => t('Text'), SERVICE_LINKS_STYLE_IMAGE => t('Image'), SERVICE_LINKS_STYLE_IMAGE_AND_TEXT => t('Image and Text'), ), ); break; case 'service_links_fisheye': $form['service_links_path_fisheye'] = array( '#type' => 'textfield', '#title' => t('Alternative icon folder'), '#size' => 60, '#description' => t('If you have alternative icons write here the path without trailing slash'), '#default_value' => service_links_expand_path(NULL, 'fisheye'), ); break; case 'service_links_not_node': $form['service_links_block_not_node_style'] = array( '#type' => 'select', '#title' => t('Style'), '#description' => t('How the service links will appear in the block.'), '#default_value' => variable_get('service_links_block_not_node_style', SERVICE_LINKS_STYLE_IMAGE_AND_TEXT), '#options' => array( SERVICE_LINKS_STYLE_TEXT => t('Text'), SERVICE_LINKS_STYLE_IMAGE => t('Image'), SERVICE_LINKS_STYLE_IMAGE_AND_TEXT => t('Image and Text'), ), ); break; } return $form; } elseif ($op == 'save') { switch ($delta) { case 'service_links': variable_set('service_links_block_style', $edit['service_links_block_style']); break; case 'service_links_fisheye': variable_set('service_links_path_fisheye', $edit['service_links_path_fisheye']); break; case 'service_links_not_node': variable_set('service_links_block_not_node_style', $edit['service_links_block_not_node_style']); break; } } } /** * Implementation of hook_theme(). */ function service_links_theme() { return array( 'service_links_build_link' => array( 'arguments' => array( 'text' => NULL, 'url' => array(), 'image' => NULL, 'nodelink' => FALSE, 'style' => NULL, 'attributes' => array(), ), 'file' => 'service_links.theme.inc', ), 'service_links_node_format' => array( 'arguments' => array('links' => NULL, 'label' => NULL), 'file' => 'service_links.theme.inc', ), 'service_links_block_format' => array( 'arguments' => array('items' => NULL, 'style' => SERVICE_LINKS_STYLE_IMAGE_AND_TEXT), 'file' => 'service_links.theme.inc', ), 'service_links_fisheye_format' => array( 'arguments' => array('items' => NULL), 'file' => 'service_links.theme.inc', ), 'service_links_drag_table' => array( 'arguments' => array('form' => NULL), 'file' => 'service_links.admin.inc', ), ); } /** * Discover all available service links by invoking hook_service_links(). * * @param $services * If NULL, will retrieve all service link information. If an array is passed, * will only obtain information for the given keyed links. * @param $reset * Resets the Service Links cache. * * @return * An array containing information for all the requested services. */ function service_links_get_links($services = NULL, $reset = FALSE) { static $links = NULL; if (!isset($links) || $reset) { // Retrieve the links from the cache. if (!$reset && ($cache = cache_get('service_links_get_links')) && !empty($cache->data)) { $links = $cache->data; } else { // Create the repository of links. $links = array(); foreach (module_implements('service_links') as $module) { $module_links = module_invoke($module, 'service_links'); foreach ($module_links as $name => $link) { $link['module'] = $module; $links[$name] = $link; } } // Allow alteration of the links. drupal_alter('service_links', $links); // Save the links in the cache. cache_set('service_links_get_links', $links); } } // If desired, return only the given services. if (isset($services) && is_numeric(key($services))) { $services = array_combine($services, array_fill(0, count($services), 1)); } return isset($services) ? array_intersect_key($links, $services) : $links; } /** * Create short links using predefined settings. */ function service_links_short_url($url, $nid = NULL) { switch (variable_get('service_links_short_links_type', SERVICE_LINKS_SHORT_URL_TYPE_NODE)) { case SERVICE_LINKS_SHORT_URL_TYPE_NODE: if (empty($nid)) { return $url; } else { // With alias = true doesn't change the path. return url("node/$nid", array('absolute' => TRUE, 'alias' => TRUE)); } case SERVICE_LINKS_SHORT_URL_TYPE_SERVICE: if (module_exists('shorten')) { $turl = shorten_url($url); } else { $turl = drupal_http_request('http://tinyurl.com/api-create.php?url='. $url); $turl = (isset($turl->data) && ($turl->code == 200)) ? $turl->data : $url; } return $turl; case SERVICE_LINKS_SHORT_URL_TYPE_REDIRECT_DOMAIN: $burl = variable_get('service_links_domain_redirect', NULL); return url($url, array('absolute' => TRUE, 'base_url' => $burl)); case SERVICE_LINKS_SHORT_URL_TYPE_REDIRECT_ALL: $burl = variable_get('service_links_domain_redirect', NULL); if (empty($nid)) { return url($url, array('absolute' => TRUE, 'base_url' => $burl)); } else { return url("node/$nid", array('absolute' => TRUE, 'alias' => TRUE, 'base_url' => $burl)); } } } /** * Function that render the service links. * This is the function themers have to call for insert all the * Service Links selected in the admin page. * * @param $node * Contain the current node object. * If NULL will be considered a not-node page. * @param $nodelink * (optional) Decide how to render the services. Default is FALSE. * If TRUE the final render will be an array compatible * with the standard Drupal link section. * @param $style * (optional) When specified, overwrite the style. * * @return * An Array of themed Service Links. */ function service_links_render($node, $nodelink = FALSE, $style = 0) { $links = array('weight' => array(), 'link' => array()); $settings = _service_links_load_settings(); if (empty($settings['link_show'])) { return array(); } _service_links_get_tags($node, $settings); // Services are filtered in _service_links_load_settings(). $services = service_links_get_links($settings['link_show']); if ($style > 0) { $settings['style'] = $style; } foreach ($services as $service_id => $service) { // Load the position. $links['weight'][] = isset($settings['link_weight'][$service_id]) ? $settings['link_weight'][$service_id] : 0; // Render the Service. $links['link'] += _service_links_render($service_id, $service, $settings, $nodelink); } if (!empty($links['link'])) { array_multisort($links['weight'], $links['link']); } return !empty($links['link']) ? $links['link'] : array(); } /** * This function render only the services requested * by their id. * * @param $service_ids * The id of the needed service, can be just a string or an array of ids. * @param $node * the current node (optional). * @param $nodelink * Boolean, if TRUE render the service for use as a link into the node, * default FALSE. * @param $style * Overwrite the default style (optional). * * @return * An Array of HTML links */ function service_links_render_some($service_ids, $node = NULL, $nodelink = FALSE, $style = 0) { if (is_array($service_ids)) { $services = service_links_get_links($service_ids); } else { $services = service_links_get_links(array($service_ids)); } if (empty($services)) { return; } $settings = _service_links_load_settings(); _service_links_get_tags($node, $settings); if ($style > 0) { $settings['style'] = $style; } $links = array(); foreach ($services as $service_id => $service) { $links += _service_links_render($service_id, $service, $settings, $nodelink); } return $links; } /** * The common render function used privately. */ function _service_links_render($service_id, $service, $settings, $nodelink) { $service['url'] = preg_split('/\?/', $service['link']); $subst_id = isset($service['url'][1]) ? 1 : 0; $service['url'][$subst_id] = str_replace($settings['tag'], $settings['subst'], $service['url'][$subst_id]); $service['attributes']['title'] = $service['description']; $class = str_replace(array('][', '_', ' '), '-', 'service_links-'. $service_id); $service['attributes']['class'] = isset($service['attributes']['class']) ? $service['attributes']['class'] . " " . $class : $class; $service['attributes'] += $settings['attributes']; $service['icon'] = isset($service['icon']) ? $service['icon'] : "$service_id.png"; $service_id = str_replace('_', '-', 'service_links_'. $service_id); // Check if a predefined style should be imposed. if (empty($service['style'])) { $service['style'] = $settings['style']; } // Add the related JavaScript and CSS. if (isset($service['javascript'])) { if (strpos($service['javascript'], '://') !== FALSE) { drupal_set_html_head(''); } else { drupal_add_js(service_links_expand_path($service['javascript'], 'javascript')); } } if (isset($service['css'])) { drupal_add_css(service_links_expand_path($service['css'], 'css')); } // Invoke callback function. if (isset($service['callback'])) { if (function_exists($function = $service['callback'])) { $function($service, $settings['subst']); } } // Create the HTML. $link = theme('service_links_build_link', $service['name'], $service['url'], $service['icon'], $nodelink, $service['style'], $service['attributes'] ); return array($service_id => $link); } /** * Fill an array with tags and the content to substitute. */ function _service_links_get_tags($node, &$settings) { if (!empty($node)) { $title = _service_links_get_node_title($node, $settings); $path = _service_links_get_node_path($node); //$url = url("node/$node->nid", array('absolute' => TRUE, 'query' => $settings['text_to_append'])); $url = url($path, array('absolute' => TRUE, 'query' => $settings['text_to_append'])); //The choice of front_page shouldn't affect the query which should point the original page $query = preg_replace('/^\//', '', str_replace(preg_replace('/\/\/|(\?q\=)\/+/', '\1', url('/')), '', url("node/$node->nid"))); $teaser = empty($node->teaser) ? '' : strip_tags($node->teaser); $nid = $node->nid; } else { $title = drupal_get_title(); $url = url($_GET['q'], array('absolute' => TRUE, 'query' => $settings['text_to_append'])); $query = check_plain(arg(0)) . str_replace(url(arg(0)), '', url($_GET['q'])); $teaser = ''; $nid = NULL; } $source = variable_get('site_name', 'Drupal'); switch ($settings['short_links_use']) { case SERVICE_LINKS_SHORT_URL_USE_NEVER: $short_url = $url; break; case SERVICE_LINKS_SHORT_URL_USE_WHEN_REQUESTED: $short_url = service_links_short_url($url, $nid); break; case SERVICE_LINKS_SHORT_URL_USE_ALWAYS: $short_url = service_links_short_url($url, $nid); $url = $short_url; break; } $settings['tag'] = array( 'raw-encoded-title' => '