You are here

mostpopular_addthis.module in Drupal Most Popular 7

Same filename and directory in other branches
  1. 6 modules/mostpopular_addthis/mostpopular_addthis.module

Uses the AddThis.com Analytics API to provide Most Popular data.

File

modules/mostpopular_addthis/mostpopular_addthis.module
View source
<?php

// $Id$



/**
 * @file
 * Uses the AddThis.com Analytics API to provide Most Popular data.
 */

/**
 * Implements hook_mostpopular_service_info().
 *
 * @see hook_mostpopular_service_info()
 */
function mostpopular_addthis_mostpopular_service_info() {
  $info = array();
  $info['shared'] = array(
    'name' => t('AddThis.com Most Shared'),
    'title' => t('Shared'),
    'entity_types' => TRUE,
  );
  return $info;
}

/**
 * Implements the 'refresh_delta' callback for the AddThis.com analytics service.
 * 
 * @param object $service The service definition.
 * @param object $block The block definition. 
 * @param integer $span The number of seconds over which to search.
 * @param integer $last_run the timestamp of the last time this service was run.
 */
function mostpopular_addthis_refresh_shared($service, $block, $span, $last_run) {
  $addthis = new MostPopularAddThis($service->data['auth']['username'], $service->data['auth']['password'], $service->data['auth']['pubid'], $service->data['auth']['domain']);
  $params = array(
    'service' => implode(',', $service->data['services']),
  );
  if ($span <= 60 * 60 * 24) {
    $params['period'] = 'day';
  }
  elseif ($span <= 60 * 60 * 24 * 7) {
    $params['period'] = 'week';
  }
  else {
    $params['period'] = 'month';
  }

  // Get the data from AddThis.com
  $data = $addthis
    ->fetchJson('url', $params);
  $limit = $block->count;
  $out = array();

  // If there was an error, report it.
  if ($data === FALSE) {
    return FALSE;
  }
  $status = '';
  foreach ($data as $v) {
    $count = $v['shares'];
    $url = $v['url'];

    // Match the URL to a node
    $obj = mostpopular_match_result_nodes($url, $count, $service->data);
    if (isset($obj)) {
      $out[] = $obj;
      $status .= t('@url (@count)', array(
        '@url' => $url,
        '@count' => $count,
      ));
      if (isset($obj->entity_type)) {
        $status .= t(' is %entity: %id', array(
          '%entity' => $obj->entity_type,
          '%id' => $obj->entity_id,
        ));
      }
      $status .= '<br>';
    }

    // Return only the first results
    if (count($out) >= $limit) {
      break;
    }
  }
  watchdog('mostpopular_addthis', 'Found %num items (of %count results) for @services<br/>!status', array(
    '%num' => count($out),
    '%count' => count($data),
    '@services' => implode(', ', $service->data['services']),
    '!status' => $status,
  ), WATCHDOG_DEBUG);
  return $out;
}

/**
 * Implements the 'config_form' callback for the AddThis.com service.
 * 
 * @param object $service The service definition.
 * @param array $form_state The current state of the form.
 */
function mostpopular_addthis_config_form($service, &$form_state) {
  $form = array();
  $form['auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('AddThis.com login credentials'),
  );
  $form['auth']['username'] = array(
    '#type' => 'textfield',
    '#title' => t('User Name'),
    '#required' => TRUE,
    '#default_value' => !empty($service->data['auth']['username']) ? $service->data['auth']['username'] : '',
  );
  $form['auth']['password'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#required' => TRUE,
    '#default_value' => !empty($service->data['auth']['password']) ? $service->data['auth']['password'] : '',
  );
  $form['auth']['pubid'] = array(
    '#type' => 'textfield',
    '#title' => t('Publisher Profile ID'),
    '#default_value' => !empty($service->data['auth']['pubid']) ? $service->data['auth']['pubid'] : '',
  );
  $form['auth']['domain'] = array(
    '#type' => 'textfield',
    '#title' => t('Domain'),
    '#default_value' => !empty($service->data['auth']['domain']) ? $service->data['auth']['domain'] : '',
    '#description' => t('Limit response data only on the specified domain.'),
  );
  $form['services'] = array(
    '#type' => 'select',
    '#title' => t('Sharing services'),
    '#description' => t('Choose one or more sharing services you would like included in these metrics.'),
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#default_value' => !empty($service->data['services']) ? $service->data['services'] : 'email,mailto',
    '#options' => mostpopular_addthis_service_options(),
  );
  return $form;
}

/**
 * Returns a list of the available services for getting metrics from AddThis.com.
 * You can alter this list by implementing hook_mostpopular_addthis_service_options_alter().
 * 
 * @return array
 *   An array of sharing services.  Each value must match one of the service keys
 *   at http://www.addthis.com/services/list
 */
function mostpopular_addthis_service_options() {
  $services = array(
    'bitly' => t('Bit.ly'),
    'delicious' => t('Del.icio.us'),
    'digg' => t('Digg'),
    'email,mailto' => t('Email'),
    'facebook,facebook_like' => t('Facebook'),
    'google_plusone' => t('Google +1'),
    'kindleit' => t('Kindle It'),
    'print' => t('Print'),
    'reddit' => t('Reddit'),
    'twitter' => t('Twitter'),
  );
  drupal_alter('mostpopular_addthis_service_options', $services);
  asort($services);
  return $services;
}

/**
 * Implements the 'next_run' callback for the AddThis emailed mostpopular service.
 * 
 * Returns the timestamp at which to next refresh the data for this interval.
 * 
 * @param array $service The service definition
 * @param integer $span The number of seconds representing the current interval.
 * @param integer $last_run The timestamp at which this service was last run for this interval.
 */
function mostpopular_addthis_next_run($service, $span, $last_run) {

  // If the interval is 2 days or less, refresh once per hour
  if ($span <= 60 * 60 * 24 * 2) {
    return strtotime('1 hour', $last_run);
  }
  elseif ($span >= 60 * 60 * 24 * 365) {
    return strtotime('1 week', $last_run);
  }

  // Otherwise, refresh once per day.
  return strtotime('1 day', $last_run);
}

Functions

Namesort descending Description
mostpopular_addthis_config_form Implements the 'config_form' callback for the AddThis.com service.
mostpopular_addthis_mostpopular_service_info Implements hook_mostpopular_service_info().
mostpopular_addthis_next_run Implements the 'next_run' callback for the AddThis emailed mostpopular service.
mostpopular_addthis_refresh_shared Implements the 'refresh_delta' callback for the AddThis.com analytics service.
mostpopular_addthis_service_options Returns a list of the available services for getting metrics from AddThis.com. You can alter this list by implementing hook_mostpopular_addthis_service_options_alter().