| 1 | | <?php |
| 2 | | // $Id: blog.module,v 1.312 2008/11/01 18:23:12 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * Enables multi-user blogs. |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Implementation of hook_node_info(). |
| 11 | | */ |
| 12 | 243 | function blog_node_info() { |
| 13 | | return array( |
| 14 | | 'blog' => array( |
| 15 | 200 | 'name' => t('Blog entry'), |
| 16 | 200 | 'base' => 'blog', |
| 17 | 200 | 'description' => t('A <em>blog entry</em> is a single post to an
online journal, or <em>blog</em>.'), |
| 18 | | ) |
| 19 | 200 | ); |
| 20 | 0 | } |
| 21 | | |
| 22 | | /** |
| 23 | | * Implementation of hook_perm(). |
| 24 | | */ |
| 25 | 243 | function blog_perm() { |
| 26 | 3 | return node_list_permissions('blog'); |
| 27 | 0 | } |
| 28 | | |
| 29 | | /** |
| 30 | | * Implementation of hook_access(). |
| 31 | | */ |
| 32 | 243 | function blog_access($op, $node, $account) { |
| 33 | | switch ($op) { |
| 34 | 181 | case 'create': |
| 35 | | // Anonymous users cannot post even if they have the permission. |
| 36 | 171 | return user_access('create blog content', $account) && $account->uid; |
| 37 | 31 | case 'update': |
| 38 | 22 | return user_access('edit any blog content', $account) ||
(user_access('edit own blog content', $account) && ($node->uid ==
$account->uid)); |
| 39 | 25 | case 'delete': |
| 40 | 16 | return user_access('delete any blog content', $account) ||
(user_access('delete own blog content', $account) && ($node->uid ==
$account->uid)); |
| 41 | 0 | } |
| 42 | 16 | } |
| 43 | | |
| 44 | | /** |
| 45 | | * Implementation of hook_user_view(). |
| 46 | | */ |
| 47 | 243 | function blog_user_view(&$edit, &$user, $category) { |
| 48 | 20 | if (user_access('create blog content', $user)) { |
| 49 | 7 | $user->content['summary']['blog'] = array( |
| 50 | 7 | '#type' => 'user_profile_item', |
| 51 | 7 | '#title' => t('Blog'), |
| 52 | 7 | '#markup' => l(t('View recent blog entries'), "blog/$user->uid",
array('attributes' => array('title' => t("Read !username's latest blog
entries.", array('!username' => $user->name))))), |
| 53 | 7 | '#attributes' => array('class' => 'blog'), |
| 54 | | ); |
| 55 | 7 | } |
| 56 | 20 | } |
| 57 | | |
| 58 | | /** |
| 59 | | * Implementation of hook_help(). |
| 60 | | */ |
| 61 | 243 | function blog_help($path, $arg) { |
| 62 | | switch ($path) { |
| 63 | 155 | case 'admin/help#blog': |
| 64 | 2 | $output = '<p>' . t('The blog module allows registered users to
maintain an online journal, or <em>blog</em>. Blogs are made up of
individual <em>blog entries</em>, and the blog entries are most often
displayed in descending order by creation time.') . '</p>'; |
| 65 | 2 | $output .= '<p>' . t('There is an (optional) <em>Blogs</em> menu item
added to the Navigation menu, which displays all blogs available on your
site, and a <em>My blog</em> item displaying the current user\'s blog
entries. The <em>Blog entry</em> menu item under <em>Create content</em>
allows new blog entries to be created.') . '</p>'; |
| 66 | 2 | $output .= '<p>' . t('Each blog entry is displayed with an automatic
link to other blogs created by the same user. By default, blog entries have
comments enabled and are automatically promoted to the site front page. The
blog module also creates a <em>Recent blog posts</em> block that may be
enabled at the <a href="@blocks">blocks administration page</a>.',
array('@blocks' => url('admin/build/block'))) . '</p>'; |
| 67 | 2 | $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@blog">Blog module</a>.', array('@blog' =>
'http://drupal.org/handbook/modules/blog/')) . '</p>'; |
| 68 | 2 | return $output; |
| 69 | 2 | } |
| 70 | 0 | } |
| 71 | 155 | |
| 72 | | /** |
| 73 | | * Implementation of hook_form(). |
| 74 | | */ |
| 75 | | function blog_form(&$node) { |
| 76 | 243 | global $nid; |
| 77 | 8 | $type = node_get_types('type', $node); |
| 78 | 8 | |
| 79 | 8 | $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 | 8 | return $form; |
| 82 | | } |
| 83 | | |
| 84 | 1 | /** |
| 85 | 0 | * Implementation of hook_view(). |
| 86 | 0 | */ |
| 87 | | function blog_view($node, $teaser = FALSE, $page = FALSE) { |
| 88 | 1 | if ($page) { |
| 89 | 0 | // Breadcrumb navigation. |
| 90 | | drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'),
l(t("!name's blog", array('!name' => $node->name)), 'blog/' .
$node->uid))); |
| 91 | 0 | } |
| 92 | 0 | |
| 93 | 1 | return node_prepare($node, $teaser); |
| 94 | | } |
| 95 | 8 | |
| 96 | 8 | /** |
| 97 | | * Implementation of hook_link(). |
| 98 | 8 | */ |
| 99 | 0 | function blog_link($type, $node = NULL, $teaser = FALSE) { |
| 100 | | $links = array(); |
| 101 | | |
| 102 | | if ($type == 'node' && $node->type == 'blog') { |
| 103 | | if (arg(0) != 'blog' || arg(1) != $node->uid) { |
| 104 | 243 | $links['blog_usernames_blog'] = array( |
| 105 | 9 | 'title' => t("!username's blog", array('!username' =>
$node->name)), |
| 106 | | 'href' => "blog/$node->uid", |
| 107 | 9 | 'attributes' => array('title' => t("Read !username's latest blog
entries.", array('!username' => $node->name))), |
| 108 | 9 | ); |
| 109 | | } |
| 110 | 9 | } |
| 111 | 0 | |
| 112 | | return $links; |
| 113 | | } |
| 114 | | |
| 115 | | /** |
| 116 | 243 | * Implementation of hook_menu(). |
| 117 | 15 | */ |
| 118 | | function blog_menu() { |
| 119 | 15 | $items['blog'] = array( |
| 120 | 9 | 'title' => 'Blogs', |
| 121 | 9 | 'page callback' => 'blog_page_last', |
| 122 | 9 | 'access arguments' => array('access content'), |
| 123 | 9 | 'type' => MENU_SUGGESTED_ITEM, |
| 124 | 9 | ); |
| 125 | | $items['blog/%user_uid_optional'] = array( |
| 126 | 9 | 'title' => 'My blog', |
| 127 | 9 | 'page callback' => 'blog_page_user', |
| 128 | | 'page arguments' => array(1), |
| 129 | 15 | 'access callback' => 'blog_page_user_access', |
| 130 | 0 | 'access arguments' => array(1), |
| 131 | | ); |
| 132 | | $items['blog/%user/feed'] = array( |
| 133 | | 'title' => 'Blogs', |
| 134 | | 'page callback' => 'blog_feed_user', |
| 135 | 243 | 'page arguments' => array(1), |
| 136 | 5 | 'access callback' => 'blog_page_user_access', |
| 137 | 5 | 'access arguments' => array(1), |
| 138 | 5 | 'type' => MENU_CALLBACK, |
| 139 | 5 | ); |
| 140 | 5 | $items['blog/feed'] = array( |
| 141 | | 'title' => 'Blogs', |
| 142 | 5 | 'page callback' => 'blog_feed_last', |
| 143 | 5 | 'access arguments' => array('access content'), |
| 144 | 5 | 'type' => MENU_CALLBACK, |
| 145 | 5 | ); |
| 146 | 5 | |
| 147 | 5 | return $items; |
| 148 | | } |
| 149 | 5 | |
| 150 | 5 | /** |
| 151 | 5 | * Access callback for user blog pages. |
| 152 | 5 | */ |
| 153 | 5 | function blog_page_user_access($account) { |
| 154 | 5 | // The visitor must be able to access the site's content. |
| 155 | 5 | // For a blog to 'exist' the user must either be able to |
| 156 | | // create new blog entries, or it must have existing posts. |
| 157 | 5 | return $account->uid && user_access('access content') &&
(user_access('create blog content', $account) ||
_blog_post_exists($account)); |
| 158 | 5 | } |
| 159 | 5 | |
| 160 | 5 | /** |
| 161 | 5 | * Helper function to determine if a user has blog posts already. |
| 162 | | */ |
| 163 | | function _blog_post_exists($account) { |
| 164 | 5 | return (bool)db_result(db_query_range(db_rewrite_sql("SELECT 1 FROM
{node} n WHERE n.type = 'blog' AND n.uid = %d AND n.status = 1"),
$account->uid, 0, 1)); |
| 165 | 0 | } |
| 166 | | |
| 167 | | /** |
| 168 | | * Implementation of hook_block(). |
| 169 | | * |
| 170 | 243 | * Displays the most recent 10 blog titles. |
| 171 | | */ |
| 172 | | function blog_block($op = 'list', $delta = '') { |
| 173 | | global $user; |
| 174 | 22 | if ($op == 'list') { |
| 175 | 0 | $block['recent']['info'] = t('Recent blog posts'); |
| 176 | | return $block; |
| 177 | | } |
| 178 | | elseif ($op == 'view') { |
| 179 | | if (user_access('access content')) { |
| 180 | 243 | $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title,
n.created FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY
n.created DESC"), 0, 10); |
| 181 | 6 | if ($node_title_list = node_title_list($result)) { |
| 182 | 0 | $block['content'] = $node_title_list; |
| 183 | | $block['content'] .= theme('more_link', url('blog'), t('Read the
latest blog entries.')); |
| 184 | | $block['subject'] = t('Recent blog posts'); |
| 185 | | return $block; |
| 186 | | } |
| 187 | | } |
| 188 | | } |
| 189 | 243 | } |
| 190 | 51 | |
| 191 | 51 | |