You are here

vote_storylink.module in Vote Up/Down 5

File

vote_storylink.module
View source
<?php

/*
 * @file
 * vote_storylink defines a "storylink" node type.
 * It's based upon "links_weblink.module".
 */

/**
 * Implementation of hook_help().
 */
function vote_storylink_help($section) {
  switch ($section) {
    case 'admin/help#vote_storylink':
      return t('<p>This module is used to create articles that links to other resources -- websites, pages, documents, etc., part of Vote up/down.</p>');
  }
}

/**
 * Implementation of hook_node_info().
 */
function vote_storylink_node_info() {
  return array(
    'storylink' => array(
      'name' => t('Storylink'),
      'module' => 'vote_storylink',
      'has_title' => TRUE,
      'has_body' => TRUE,
      'description' => t('A story link is an article whose main purpose is to create links to other resources -- websites, pages, documents, etc.'),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function vote_storylink_perm() {
  return array(
    'create storylinks',
    'edit own storylinks',
    'view storylinks',
  );
}

/**
 * Implementation of hook_access().
 */
function vote_storylink_access($op, $node) {
  global $user;
  switch ($op) {
    case 'view':
      return $node->status && user_access('view storylinks');
    case 'create':
      return user_access('create storylinks');
    case 'update':
    case 'delete':
      return user_access('edit own storylinks') && $user->uid == $node->uid;
  }
}

/**
 * Implementation of hook_settings().

function vote_storylink_admin_settings() {
  if (!module_exists('links')) {
    drupal_set_message(t('The &quot;links&quot; module is disabled or not installed. Storylinks will not function until this is corrected. Check the availability of
that module, and enable if needed, in the !modules.',array('!modules'=>l(t('modules administration page'),'admin/modules'))),'error');
  }

  return system_settings_form($form);
} */

/**
 * Implementation of hook_menu().
 */
function vote_storylink_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'storylink',
      'title' => t('Story links'),
      'callback' => 'vote_storylink_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'storylink/' . $user->uid,
      'title' => t('My story links'),
      'access' => user_access('create storylinks'),
      'type' => MENU_DYNAMIC_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_user().
 */
function vote_storylink_user($type, &$edit, &$account, $category = NULL) {
  if ($type == 'view' && user_access('create storylinks', $account)) {
    $items[] = array(
      'title' => t('Story links'),
      'value' => l(t('view recent story links'), "storylink/{$account->uid}", array(
        'title' => t("Read @username's latest story links.", array(
          '@username' => $account->name,
        )),
      )),
      'class' => 'storylink',
    );
    return array(
      t('History') => $items,
    );
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function vote_storylink_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'rss item':
      if ($node->type == 'storylink' && variable_get('feed_item_length', 'teaser') == 'teaser') {
        $node->teaser .= '<p><a href="' . check_url($node->vote_storylink_url) . '" title="' . t('Go to the original news source.') . '">' . t('Original news source') . '</a></p>';
      }
      elseif ($node->type == 'storylink' && variable_get('feed_item_length', 'teaser') == 'fulltext') {
        $node->body .= '<p><a href="' . check_url($node->vote_storylink_url) . '" title="' . t('Go to the original news source.') . '">' . t('Original news source') . '</a></p>';
      }
      break;
  }
}

/**
 * Implementation of hook_form().
 */
function vote_storylink_form(&$node, &$param) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#maxlength' => 96,
    '#description' => t("Title of the story the link goes to, max 96 characters."),
    '#weight' => -18,
  );
  $form['vote_storylink_url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#default_value' => $node->vote_storylink[0]['url'],
    '#maxlength' => 256,
    '#required' => TRUE,
    '#description' => t('The URL/address of the story.'),
    '#validate' => array(
      '_vote_storylink_valid_url' => array(
        'vote_storylink_url',
      ),
    ),
    '#weight' => -17,
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $node->body,
    '#required' => FALSE,
    '#rows' => 5,
    '#maxlength' => 600,
    '#resizable' => TRUE,
    '#description' => t('A short description of the story. It should be around 2 to 4 sentences.'),
    '#weight' => -16,
  );
  $form['body_filter']['filter'] = filter_form($node->format);
  return $form;
}

/**
 * Implementation of hook_view().
 */
function vote_storylink_view($node, $teaser = FALSE, $page = FALSE) {
  if ($page) {

    // Breadcrumb navigation
    $breadcrumb[] = array(
      'path' => 'storylink',
      'title' => t('Story links'),
    );
    $breadcrumb[] = array(
      'path' => 'storylink/' . $node->uid,
      'title' => t("@name's story links", array(
        '@name' => $node->name,
      )),
    );
    $breadcrumb[] = array(
      'path' => 'node/' . $node->nid,
    );
    menu_set_location($breadcrumb);
  }
  $node = node_prepare($node, $teaser);
  return $node;
}

/**
 * Implementation of hook_load().
 */
function vote_storylink_load($node) {
  $links = links_load_links_for_node($node->nid, 'vote_storylink', 0, TRUE);
  return array(
    'vote_storylink_url' => $links[0]['url'],
    'vote_storylink' => $links,
  );
}

/**
 * Implementation of hook_prepare().
 */
function vote_storylink_prepare(&$node) {

  // Allow the following fields to be initialized via $_GET (e.g. for use
  // with a "submit it" bookmarklet):
  foreach (array(
    'title',
    'body',
  ) as $field) {
    if ($_GET['edit'][$field]) {
      $node->{$field} = $_GET['edit'][$field];
    }
  }
  if (isset($_POST['edit']['vote_storylink_url'])) {
    $node->vote_storylink_url = $_POST['edit']['vote_storylink_url'];
  }
  else {
    if ($_GET['edit']['url']) {
      $node->vote_storylink_url = check_url($_GET['edit']['url']);
    }
    else {
      if (empty($node->vote_storylink_url)) {
        $node->vote_storylink_url = 'http://';
      }
    }
  }
  vote_storylink_node_build($node);
}

/**
 * Implementation of hook_validate().
 */
function vote_storylink_validate(&$node) {
  vote_storylink_node_build($node);
  if ($nid = db_result(db_query("SELECT ln.nid FROM {links} l INNER JOIN {links_node} ln ON l.lid = ln.lid WHERE ln.nid != %d AND LOWER(l.url) = LOWER('%s')", $node->nid, $node->vote_storylink_url))) {
    form_set_error('vote_storylink_url', t('This story is already submitted. !clickhere to view and vote on it.', array(
      '!clickhere' => l('Click here', 'node/' . $nid),
    )));
  }
}

/**
 * Implementation of hook_delete().
 */
function vote_storylink_delete(&$node) {

  // Delete the values from a node that is being erased
  // Don't care if it's "supposed" to have links -- delete if found anyway
  vote_storylink_node_build($node);
  links_delete_links_for_node($node, 'vote_storylink');
}

/**
 * Implementation of hook_insert().
 */
function vote_storylink_insert(&$node) {
  vote_storylink_node_build($node);
  links_save_links_for_node($node, 'vote_storylink');
}

/**
 * Implementation of hook_update().
 */
function vote_storylink_update(&$node) {
  vote_storylink_node_build($node);
  links_save_links_for_node($node, 'vote_storylink');
}

/**
 * Implementation of hook_link().
 */
function vote_storylink_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  if ($teaser && $type == 'node' && $node->type == 'storylink') {
    $links['vote_storylink'] = array(
      'title' => t('More info'),
      'href' => 'node/' . $node->nid,
      'attributes' => array(
        'title' => t('More information about this post.'),
      ),
    );
  }
  return $links;
}

/**
 * Implementation of hook_block().
 */
function vote_storylink_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Top story links');
    $blocks[1]['info'] = t('User navigation story links');
    return $blocks;
  }
  else {
    if ($op == 'view') {
      if (user_access('access content')) {
        switch ($delta) {
          case 0:
            $title = t('Top stories');
            $items = array();
            $items[] = l(t('This day'), 'storylink/top/day');
            $items[] = l(t('This week'), 'storylink/top/week');
            $items[] = l(t('This month'), 'storylink/top/month');
            $items[] = l(t('This year'), 'storylink/top/year');
            $items[] = l(t('All time'), 'storylink/top');
            break;
          case 1:
            global $user;
            if ($user->uid) {

              //userpoints integration
              if (module_exists('userpoints')) {
                $title = $user->name . ' (' . userpoints_get_current_points($user->uid) . ')';
              }
              else {
                $title = $user->name;
              }
              $items = array();
              $items[] = l(t('Submit new story'), 'node/add/storylink');
              $items[] = l(t('My account'), 'user/' . $user->uid);
              $items[] = l(t('My story links'), 'storylink/' . $user->uid);
              $items[] = l(t('Log out'), 'logout');
            }
            break;
        }
        if ($items) {
          $block['subject'] = check_plain($title);
          $block['content'] = theme('item_list', $items);
        }
        return $block;
      }
    }
  }
}

/**
 * Menu callback; displays a Drupal page containing recent story links entries.
 */
function vote_storylink_page($a = NULL, $b = NULL, $c = NULL) {
  if (is_numeric($a)) {

    // $a is a user ID
    if ($b == 'feed') {
      return vote_storylink_feed_user($a);
    }
    else {
      return vote_storylink_page_user($a);
    }
  }
  else {
    switch ($a) {
      case 'new':
        if ($b == 'feed') {
          return vote_storylink_feed_new();
        }
        else {
          return vote_storylink_page_new();
        }
        break;
      case 'queue':
        if ($b == 'feed') {
          return vote_storylink_feed_queue();
        }
        else {
          return vote_storylink_page_queue();
        }
        break;
      case 'top':
        if ($b == 'feed') {
          return vote_storylink_feed_top();
        }
        else {
          if ($b == ('day' || 'week' || 'month' || 'year')) {
            if ($c == 'feed') {
              return vote_storylink_feed_interval_top($b);
            }
            else {
              return vote_storylink_page_interval_top($b);
            }
          }
          else {
            return vote_storylink_page_top();
          }
        }
        break;
      default:
        return vote_storylink_page_new();
    }
  }
}

/**
 * Display views of the storylinks
 */
function vote_storylink_page_new() {
  drupal_set_title($title = t('New story links'));
  $sql = db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'storylink' AND n.status = 1 ORDER BY n.created DESC");
  $result = pager_query($sql, variable_get('default_nodes_main', 10));
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
  }
  $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  $output .= theme('feed_icon', url('storylink/new/feed'));
  $feed_url = url('storylink/new/feed', NULL, NULL, TRUE);
  drupal_add_feed($feed_url, t('RSS - @title', array(
    '@title' => $title,
  )));
  return $output;
}
function vote_storylink_page_queue() {
  drupal_set_title($title = t('Queue story links'));
  $sql = db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE n.type = 'storylink' AND n.status = 1 AND n.promote = 0 ORDER BY n.created DESC");
  $result = pager_query($sql, variable_get('default_nodes_main', 10));
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
  }
  $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  $output .= theme('feed_icon', url('storylink/queue/feed'));
  $feed_url = url('storylink/queue/feed', NULL, NULL, TRUE);
  drupal_add_feed($feed_url, t('RSS - @title', array(
    '@title' => $title,
  )));
  return $output;
}
function vote_storylink_page_top() {
  drupal_set_title($title = t('Popular story links'));
  $sql = db_rewrite_sql("SELECT n.nid, n.created, v.content_id, v.value, v.tag, v.function FROM {node} n INNER JOIN {votingapi_cache} v ON n.nid = v.content_id WHERE n.type = 'storylink' AND n.status = 1 AND v.tag = 'vote' AND v.function = 'sum' AND v.content_type = 'node' ORDER BY v.value DESC, n.created DESC");
  $result = pager_query($sql, variable_get('default_nodes_main', 10));
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
  }
  $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  $output .= theme('feed_icon', url('storylink/top/feed'));
  $feed_url = url('storylink/top/feed', NULL, NULL, TRUE);
  drupal_add_feed($feed_url, t('RSS - @title', array(
    '@title' => $title,
  )));
  return $output;
}
function vote_storylink_page_interval_top($interval) {
  drupal_set_title($title = t('Popular story links this @epoch', array(
    '@epoch' => t($interval),
  )));
  $epoch = strtotime("-1 {$interval}");
  $sql = db_rewrite_sql("SELECT n.nid, n.created, v.content_id, v.value, v.tag, v.function FROM {node} n INNER JOIN {votingapi_cache} v ON n.nid = v.content_id WHERE n.type = 'storylink' AND n.status = 1 AND n.created >= %d AND v.tag = 'vote' AND v.function = 'sum' AND v.content_type = 'node' ORDER BY v.value DESC, n.created DESC");
  $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, NULL, $epoch);
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
  }
  $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
  $output .= theme('feed_icon', url("storylink/top/{$interval}/feed"));
  $feed_url = url("storylink/top/{$interval}/feed", NULL, NULL, TRUE);
  drupal_add_feed($feed_url, t('RSS - @title', array(
    '@title' => $title,
  )));
  return $output;
}

/**
 * Displays a Drupal page containing recent storylink entries of a given user.
 */
function vote_storylink_page_user($uid) {
  global $user;
  $account = user_load(array(
    is_numeric($uid) ? 'uid' : 'name' => $uid,
    'status' => 1,
  ));
  if ($account->uid) {
    drupal_set_title($title = t("@name's story links", array(
      '@name' => $account->name,
    )));
    $sql = db_rewrite_sql("SELECT n.nid, n.created FROM {node} n WHERE type = 'storylink' AND n.uid = %d AND n.status = 1 ORDER BY n.created DESC");
    $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, NULL, $account->uid);
    while ($node = db_fetch_object($result)) {
      $output .= node_view(node_load($node->nid), 1);
    }
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10));
    $output .= theme('feed_icon', url("storylink/{$account->uid}/feed"));
    $feed_url = url("storylink/{$account->uid}/feed", NULL, NULL, TRUE);
    drupal_add_feed($feed_url, t('RSS - @title', array(
      '@title' => $title,
    )));
    return $output;
  }
  else {
    drupal_not_found();
  }
}

/**
 * Displays an RSS feed containing recent storylink entries of all users.
 */
function vote_storylink_feed_new() {
  $sql = db_rewrite_sql("SELECT n.nid, n.title, n.created, r.teaser FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid WHERE n.type = 'storylink' AND n.status = 1 ORDER BY n.created DESC");
  $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
  $channel['title'] = variable_get('site_name', 'drupal') . ' new';
  $channel['link'] = url('storylink/new', NULL, NULL, TRUE);
  $channel['description'] = t('New storylinks');
  node_feed($result, $channel);
}
function vote_storylink_feed_queue() {
  $sql = db_rewrite_sql("SELECT n.nid, n.title, n.created, r.teaser FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid WHERE n.type = 'storylink' AND n.status = 1 AND n.promote = 0 ORDER BY n.created DESC");
  $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
  $channel['title'] = variable_get('site_name', 'drupal') . ' queue';
  $channel['link'] = url('storylink/queue', NULL, NULL, TRUE);
  $channel['description'] = t('Queued storylinks');
  node_feed($result, $channel);
}
function vote_storylink_feed_top() {
  $sql = db_rewrite_sql("SELECT n.nid, n.created, n.title, r.teaser, v.content_id, v.value, v.tag, v.function FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid INNER JOIN {votingapi_cache} v ON n.nid = v.content_id WHERE n.type = 'storylink' AND n.status = 1 AND v.tag = 'vote' AND v.function = 'sum' AND v.content_type = 'node' ORDER BY v.value DESC, n.created DESC");
  $result = db_query_range($sql, 0, variable_get('feed_default_items', 10));
  $channel['title'] = variable_get('site_name', 'drupal') . ' popular';
  $channel['link'] = url('storylink/top', NULL, NULL, TRUE);
  $channel['description'] = t('Popular storylinks');
  node_feed($result, $channel);
}
function vote_storylink_feed_interval_top($interval) {
  $epoch = strtotime("-1 {$interval}");
  $sql = db_rewrite_sql("SELECT n.nid, n.created, n.title, r.teaser, v.content_id, v.value, v.tag, v.function FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid INNER JOIN {votingapi_cache} v ON n.nid = v.content_id WHERE n.type = 'storylink' AND n.status = 1 AND n.created >= %d AND v.tag = 'vote' AND v.function = 'sum' AND v.content_type = 'node' ORDER BY v.value DESC, n.created DESC");
  $result = db_query_range($sql, $epoch, 0, variable_get('feed_default_items', 10));
  $channel['title'] = variable_get('site_name', 'drupal') . ' popular story links this ' . $interval;
  $channel['link'] = url("storylink/top/{$interval}", NULL, NULL, TRUE);
  $channel['description'] = t('Popular storylinks');
  node_feed($result, $channel);
}
function vote_storylink_feed_user($uid = 0) {
  global $user;
  if ($uid) {
    $account = user_load(array(
      'uid' => $uid,
      'status' => 1,
    ));
  }
  else {
    $account = $user;
  }
  $sql = db_rewrite_sql("SELECT n.nid, n.title, n.created, r.teaser, u.name, u.uid FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid INNER JOIN {users} u ON n.uid = u.uid WHERE n.type = 'storylink' AND u.uid = %d AND n.status = 1 ORDER BY n.created DESC");
  $result = db_query_range($sql, $uid, 0, variable_get('feed_default_items', 10));
  $channel['title'] = $account->name . "'s story links";
  $channel['link'] = url("storylink/{$uid}", NULL, NULL, TRUE);
  $channel['description'] = t('User storylinks');
  node_feed($result, $channel);
}

/**
 * Theme the display of (via example.com).
 */
function theme_vote_storylink_via($link_url) {
  $link_url = parse_url($link_url);
  $output = '<div class="vote-up-down-via">(' . t('via @domain', array(
    '@domain' => $link_url['host'],
  )) . ')</div>';
  return $output;
}

/**
 * Validate the URL.
 */
function _vote_storylink_valid_url($formelement, $fieldname) {
  $url = $formelement['#value'];
  if (!preg_match('/^(http|https):\\/\\/[a-z0-9]+([\\-\\.]{1,2}[a-z0-9]+)*\\.[a-z]{2,6}((:[0-9]{1,5})?\\/.*)?$/i', $url)) {
    form_set_error($fieldname, t('The URL is not valid.'));
  }
}

/**
 * To use the links API, we sometimes need to populate the internal data
 * structure $node->vote_storylink, which is an array of link-definition
 * arrays. In this case, the outer array always contains exactly one element
 * because we are interested only in the primary URL for the storylink. If
 * this site allows "related links" for storylink nodes, that's handled
 * by the links_related module, not here.
 */
function vote_storylink_node_build(&$node, $url = '') {
  if (empty($url)) {
    $url = links_normalize_url($node->vote_storylink_url);
  }
  else {
    $url = links_normalize_url($url);
  }
  $node->vote_storylink_url = $url;
  $link = array(
    'url' => $url,
    'link_title' => $node->title,
    'weight' => 0,
  );

  // Wrap $link as the [0] element of a trivial outer array.
  $node->vote_storylink = array(
    $link,
  );
}

Functions

Namesort descending Description
theme_vote_storylink_via Theme the display of (via example.com).
vote_storylink_access Implementation of hook_access().
vote_storylink_block Implementation of hook_block().
vote_storylink_delete Implementation of hook_delete().
vote_storylink_feed_interval_top
vote_storylink_feed_new Displays an RSS feed containing recent storylink entries of all users.
vote_storylink_feed_queue
vote_storylink_feed_top
vote_storylink_feed_user
vote_storylink_form Implementation of hook_form().
vote_storylink_help Implementation of hook_help().
vote_storylink_insert Implementation of hook_insert().
vote_storylink_link Implementation of hook_link().
vote_storylink_load Implementation of hook_load().
vote_storylink_menu Implementation of hook_menu().
vote_storylink_nodeapi Implementation of hook_nodeapi().
vote_storylink_node_build To use the links API, we sometimes need to populate the internal data structure $node->vote_storylink, which is an array of link-definition arrays. In this case, the outer array always contains exactly one element because we are interested only in…
vote_storylink_node_info Implementation of hook_node_info().
vote_storylink_page Menu callback; displays a Drupal page containing recent story links entries.
vote_storylink_page_interval_top
vote_storylink_page_new Display views of the storylinks
vote_storylink_page_queue
vote_storylink_page_top
vote_storylink_page_user Displays a Drupal page containing recent storylink entries of a given user.
vote_storylink_perm Implementation of hook_perm().
vote_storylink_prepare Implementation of hook_prepare().
vote_storylink_update Implementation of hook_update().
vote_storylink_user Implementation of hook_user().
vote_storylink_validate Implementation of hook_validate().
vote_storylink_view Implementation of hook_view().
_vote_storylink_valid_url Validate the URL.