| 1 |
<?php
|
| 2 |
// $Id: usernews.module,v 1.1 2009/10/05 10:11:26 jerdiggity Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Implements per-user news article publishing.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_node_info().
|
| 11 |
*/
|
| 12 |
function usernews_node_info() {
|
| 13 |
return array(
|
| 14 |
'usernews' => array(
|
| 15 |
'name' => t('User News'),
|
| 16 |
'module' => 'usernews',
|
| 17 |
'description' => t('Submit a news update, otherwise known as <em>user news</em>.'),
|
| 18 |
)
|
| 19 |
);
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_perm().
|
| 24 |
*/
|
| 25 |
function usernews_perm() {
|
| 26 |
return array('create user news content', 'delete own user news content', 'delete any user news content', 'edit own user news content', 'edit any user news content');
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implementation of hook_access().
|
| 31 |
*/
|
| 32 |
function usernews_access($op, $node, $account) {
|
| 33 |
switch ($op) {
|
| 34 |
case 'create':
|
| 35 |
return user_access('create user news content', $account) && $account->uid ? TRUE : NULL;
|
| 36 |
case 'update':
|
| 37 |
return user_access('edit any user news content', $account) || (user_access('edit own user news content', $account) && ($node->uid == $account->uid)) ? TRUE : NULL;
|
| 38 |
case 'delete':
|
| 39 |
return user_access('delete any user news content', $account) || (user_access('delete own user news content', $account) && ($node->uid == $account->uid)) ? TRUE : NULL;
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_user().
|
| 45 |
*/
|
| 46 |
function usernews_user($type, &$edit, &$user) {
|
| 47 |
if ($type == 'view' && user_access('create user news content', $user)) {
|
| 48 |
$user->content['summary']['usernews'] = array(
|
| 49 |
'#type' => 'user_profile_item',
|
| 50 |
'#title' => t('News by !name', array('!name' => $user->name)),
|
| 51 |
'#value' => l(t('All news articles by @name', array('@name' => $user->name)), "usernews/$user->uid", array('attributes' => array('title' => t("@username's news articles.", array('@username' => $user->name))))),
|
| 52 |
'#attributes' => array('class' => 'usernews'),
|
| 53 |
'#weight' => -10,
|
| 54 |
);
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Implementation of hook_form().
|
| 60 |
*/
|
| 61 |
function usernews_form(&$node) {
|
| 62 |
global $nid;
|
| 63 |
$iid = isset($_GET['iid']) ? (int)$_GET['iid'] : 0;
|
| 64 |
$type = node_get_types('type', $node);
|
| 65 |
|
| 66 |
|
| 67 |
if (empty($node->body)) {
|
| 68 |
if ($nid && $usernews = node_load($nid)) {
|
| 69 |
$node->body = '<em>'. $usernews->body .'</em> ['. l($usernews->name, "node/$nid") .']';
|
| 70 |
}
|
| 71 |
|
| 72 |
if ($iid && $item = db_fetch_object(db_query('SELECT i.*, f.title as ftitle, f.link as flink FROM {aggregator_item} i, {aggregator_feed} f WHERE i.iid = %d AND i.fid = f.fid', $iid))) {
|
| 73 |
$node->title = $item->title;
|
| 74 |
$node->body = '<a href="'. check_url($item->link) .'">'. check_plain($item->title) .'</a> - <em>'. $item->description .'</em> [<a href="'. check_url($item->flink) .'">'. check_plain($item->ftitle) ."</a>]\n";
|
| 75 |
}
|
| 76 |
|
| 77 |
}
|
| 78 |
|
| 79 |
$form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => !empty($node->title) ? $node->title : NULL, '#weight' => -5);
|
| 80 |
$form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
|
| 81 |
return $form;
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Implementation of hook_view().
|
| 86 |
*/
|
| 87 |
function usernews_view($node, $teaser = FALSE, $page = FALSE) {
|
| 88 |
if ($page) {
|
| 89 |
drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('News articles'), 'usernews'), t('@date', array('@date' => format_date($node->created, 'custom', 'd M Y'))), l(t('@newstitle', array('@newstitle' => $node->title)), 'usernews/'. $node->uid)));
|
| 90 |
}
|
| 91 |
return node_prepare($node, $teaser);
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Implementation of hook_link().
|
| 96 |
*/
|
| 97 |
function usernews_link($type, $node = NULL, $teaser = FALSE) {
|
| 98 |
$links = array();
|
| 99 |
|
| 100 |
if ($type == 'node' && $node->type == 'usernews') {
|
| 101 |
if (arg(0) != 'usernews' || arg(1) != $node->uid) {
|
| 102 |
$links['usernews_usernames_article'] = array(
|
| 103 |
'title' => t('All articles by !name', array('!name' => $node->name)),
|
| 104 |
'href' => "usernews/$node->uid",
|
| 105 |
'attributes' => array('title' => t("All news articles by @user.", array('@user' => $node->name)))
|
| 106 |
);
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
return $links;
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Implementation of hook_menu().
|
| 115 |
*/
|
| 116 |
function usernews_menu() {
|
| 117 |
$items['usernews'] = array(
|
| 118 |
'title' => 'News articles',
|
| 119 |
'page callback' => 'usernews_page_last',
|
| 120 |
'access arguments' => array('access content'),
|
| 121 |
'type' => MENU_SUGGESTED_ITEM,
|
| 122 |
'file' => 'usernews.pages.inc',
|
| 123 |
);
|
| 124 |
$items['usernews/%user_uid_optional'] = array(
|
| 125 |
'title' => 'My news articles',
|
| 126 |
'page callback' => 'usernews_page_user',
|
| 127 |
'page arguments' => array(1),
|
| 128 |
'access callback' => 'usernews_page_user_access',
|
| 129 |
'access arguments' => array(1),
|
| 130 |
'file' => 'usernews.pages.inc',
|
| 131 |
);
|
| 132 |
$items['usernews/%user/feed'] = array(
|
| 133 |
'title' => 'News articles',
|
| 134 |
'page callback' => 'usernews_feed_user',
|
| 135 |
'page arguments' => array(1),
|
| 136 |
'access callback' => 'usernews_page_user_access',
|
| 137 |
'access arguments' => array(1),
|
| 138 |
'type' => MENU_CALLBACK,
|
| 139 |
'file' => 'usernews.pages.inc',
|
| 140 |
);
|
| 141 |
$items['usernews/feed'] = array(
|
| 142 |
'title' => 'News articles',
|
| 143 |
'page callback' => 'usernews_feed_last',
|
| 144 |
'access arguments' => array('access content'),
|
| 145 |
'type' => MENU_CALLBACK,
|
| 146 |
'file' => 'usernews.pages.inc',
|
| 147 |
);
|
| 148 |
|
| 149 |
return $items;
|
| 150 |
}
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Access control for user news.
|
| 154 |
*/
|
| 155 |
function usernews_page_user_access($account) {
|
| 156 |
return $account->uid && user_access('access content') && (user_access('create user news content', $account) || _usernews_post_exists($account));
|
| 157 |
}
|
| 158 |
|
| 159 |
function _usernews_post_exists($account) {
|
| 160 |
return (bool)db_result(db_query_range(db_rewrite_sql("SELECT 1 FROM {node} n WHERE n.type = 'usernews' AND n.uid = %d AND n.status = 1"), $account->uid, 0, 1));
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Implementation of hook_block().
|
| 165 |
*/
|
| 166 |
function usernews_block($op = 'list', $delta = 0) {
|
| 167 |
global $user;
|
| 168 |
if ($op == 'list') {
|
| 169 |
$block[0]['info'] = t('User News - latest articles');
|
| 170 |
return $block;
|
| 171 |
}
|
| 172 |
else if ($op == 'view') {
|
| 173 |
if (user_access('access content')) {
|
| 174 |
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'usernews' AND n.status = 1 ORDER BY n.created DESC"), 0, 10);
|
| 175 |
if ($node_title_list = node_title_list($result)) {
|
| 176 |
$block['content'] = $node_title_list;
|
| 177 |
$block['content'] .= theme('more_link', url('usernews'), t('More | @site', array('@site' => variable_get('site_name', 'Drupal'))));
|
| 178 |
$block['subject'] = t('Latest news articles');
|
| 179 |
return $block;
|
| 180 |
}
|
| 181 |
}
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|