View source
<?php
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>');
}
}
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.'),
),
);
}
function vote_storylink_perm() {
return array(
'create storylinks',
'edit own storylinks',
'view storylinks',
);
}
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;
}
}
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;
}
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,
);
}
}
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;
}
}
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;
}
function vote_storylink_view($node, $teaser = FALSE, $page = FALSE) {
if ($page) {
$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;
}
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,
);
}
function vote_storylink_prepare(&$node) {
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);
}
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),
)));
}
}
function vote_storylink_delete(&$node) {
vote_storylink_node_build($node);
links_delete_links_for_node($node, 'vote_storylink');
}
function vote_storylink_insert(&$node) {
vote_storylink_node_build($node);
links_save_links_for_node($node, 'vote_storylink');
}
function vote_storylink_update(&$node) {
vote_storylink_node_build($node);
links_save_links_for_node($node, 'vote_storylink');
}
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;
}
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) {
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;
}
}
}
}
function vote_storylink_page($a = NULL, $b = NULL, $c = NULL) {
if (is_numeric($a)) {
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();
}
}
}
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;
}
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();
}
}
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);
}
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;
}
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.'));
}
}
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,
);
$node->vote_storylink = array(
$link,
);
}