You are here

function mostpopular_addthis_refresh_shared in Drupal Most Popular 7

Implements the 'refresh_delta' callback for the AddThis.com analytics service.

Parameters

object $service The service definition.:

object $block The block definition. :

integer $span The number of seconds over which to search.:

integer $last_run the timestamp of the last time this service was run.:

File

modules/mostpopular_addthis/mostpopular_addthis.module, line 42
Uses the AddThis.com Analytics API to provide Most Popular data.

Code

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;
}