You are here

function mostpopular_addthis_mostpopular_service in Drupal Most Popular 6

Implements hook_mostpopular_service().

See also

hook_mostpopular_service()

File

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

Code

function mostpopular_addthis_mostpopular_service($op, $delta = 0, $options = array()) {
  switch ($op) {
    case 'list':
      return array(
        'emailed' => array(
          'name' => t('AddThis.com Most Emailed'),
          'title' => t('Emailed'),
        ),
      );
      break;
    case 'refresh':
      switch ($delta) {
        case 'emailed':
          include_once drupal_get_path('module', 'mostpopular_addthis') . '/mostpopular_addthis.classes.inc';
          $addthis = new AddThis();
          $data = $addthis
            ->getNodeEmailCount($options['ts']);
          $out = array();
          if (empty($data)) {
            return $out;
          }
          foreach ($data as $v) {
            $count = $v['shares'];
            $url = $v['url'];

            // Match the URL to a node
            $obj = mostpopular_match_result_nodes($url, $count);
            if (isset($obj)) {
              $out[] = $obj;
            }

            // Return only the first results
            if (count($out) >= $options['max']) {
              break;
            }
          }
          return $out;
      }
      return FALSE;
    case 'config':
      $form = array();
      $addthisConfig = variable_get('addthis_config', array());
      $pubid = $addthisConfig['pubid'];
      $password = variable_get('addthis_password', '');
      $username = variable_get('addthis_username', '');
      $form['auth'] = array(
        '#type' => 'fieldset',
        '#title' => t('AddThis.com login credentials'),
        'addthis_username' => array(
          '#type' => 'textfield',
          '#title' => t('AddThis Account'),
          '#required' => TRUE,
          '#default_value' => $username,
          '#description' => 'Email to authenticate Analytics with.',
        ),
        'addthis_pubid' => array(
          '#type' => 'item',
          '#title' => 'AddThis Profile ID',
          '#value' => $pubid . '   ' . l('(change)', 'admin/settings/addthis', array(
            'query' => drupal_get_destination(),
          )),
        ),
        'addthis_password' => array(
          '#type' => 'password',
          '#title' => t('Password'),
          '#required' => empty($password),
          '#description' => empty($password) ? t('You must set the password to connect to the AddThis.com service.') : t('To update your AddThis.com password, enter a new password here.'),
          '#attributes' => array(
            'autocomplete' => 'off',
          ),
        ),
      );
      $form['#submit'][] = 'mostpopular_addthis_config_submit';
      return $form;
    case 'throttles':
      switch ($delta) {
        case 'emailed':
          $out = array();
          foreach ($options as $iid => $ts) {

            // Determine the interval
            if ($ts >= strtotime('-1 day -10 minutes')) {
              $out[$iid] = '1 hour';
            }
            elseif ($ts <= strtotime('-1 year')) {
              $out[$iid] = '1 week';
            }
            else {
              $out[$iid] = '1 day';
            }
          }
          return $out;
      }
      return NULL;
  }
}