blog.module 7.22 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
   
Dries committed
4
5
/**
 * @file
6
 * Enables multi-user blogs.
Dries's avatar
   
Dries committed
7
8
 */

Dries's avatar
   
Dries committed
9
/**
10
 * Implements hook_node_info().
Dries's avatar
   
Dries committed
11
 */
12
function blog_node_info() {
13
14
  return array(
    'blog' => array(
15
      'name' => t('Blog entry'),
16
      'base' => 'blog',
17
      'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
18
19
    )
  );
Dries's avatar
   
Dries committed
20
21
}

Dries's avatar
   
Dries committed
22
/**
23
 * Implements hook_user_view().
Dries's avatar
   
Dries committed
24
 */
25
26
27
function blog_user_view($account) {
  if (user_access('create blog content', $account)) {
    $account->content['summary']['blog'] =  array(
28
29
      '#type' => 'user_profile_item',
      '#title' => t('Blog'),
30
      '#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
31
      '#attributes' => array('class' => array('blog')),
32
    );
33
34
35
  }
}

Dries's avatar
   
Dries committed
36
/**
37
 * Implements hook_help().
Dries's avatar
   
Dries committed
38
 */
39
40
function blog_help($path, $arg) {
  switch ($path) {
Dries's avatar
   
Dries committed
41
    case 'admin/help#blog':
42
      $output = '<h3>' . t('About') . '</h3>';
43
      $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>. By default, the blog entries are displayed by creation time in descending order, with comments enabled, and are promoted to the site's front page. For more information, see the online handbook entry for <a href='@blog'>Blog module</a>.", array('@blog' => 'http://drupal.org/handbook/modules/blog/')) . '</p>';
44
45
46
47
48
49
50
51
52
53
54
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Single-user blogs') . '</dt>';
      $output .= '<dd>' . t("Each user's blog entries are automatically displayed with a link to the user's main blog page. You can create as many single-user blogs as you have site users with permission to create blog content.") . '</dd>';
      $output .= '<dt>' . t('Multi-user blogs') . '</dt>';
      $output .= '<dd>' . t("Blog entries from each single-user blog are also aggregated into one central multi-user blog, which displays the blog content of all users in a single listing.") . '</dd>';
      $output .= '<dt>' . t('Navigation') . '</dt>';
      $output .= '<dd>' . 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.") . '</dd>';
      $output .= '<dt>' . t('Blocks') . '</dt>';
      $output .= '<dd>' . t('The Blog module also creates a default <em>Recent blog posts</em> block that may be enabled at the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
      $output .= '</dl>';
55
      return $output;
Dries's avatar
   
Dries committed
56
  }
Dries's avatar
   
Dries committed
57
}
Dries's avatar
   
Dries committed
58

Dries's avatar
   
Dries committed
59
/**
60
 * Implements hook_form().
Dries's avatar
   
Dries committed
61
 */
62
function blog_form($node, $form_state) {
63
  return node_content_form($node, $form_state);
Dries's avatar
 
Dries committed
64
65
}

Dries's avatar
   
Dries committed
66
/**
67
 * Implements hook_view().
Dries's avatar
   
Dries committed
68
 */
69
function blog_view($node, $view_mode) {
70
  if (node_is_page($node)) {
71
    // Breadcrumb navigation.
72
    drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'), l(t("!name's blog", array('!name' => format_username($node))), 'blog/' . $node->uid)));
Dries's avatar
   
Dries committed
73
  }
74
  return $node;
Dries's avatar
   
Dries committed
75
76
}

Dries's avatar
   
Dries committed
77
/**
78
 * Implements hook_node_view().
Dries's avatar
   
Dries committed
79
 */
80
81
function blog_node_view($node, $view_mode = 'full') {
  if ($view_mode != 'rss') {
82
    if ($node->type == 'blog' && arg(0) != 'blog' || arg(1) != $node->uid) {
83
      $links['blog_usernames_blog'] = array(
84
        'title' => t("!username's blog", array('!username' => format_username($node))),
85
        'href' => "blog/$node->uid",
86
        'attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($node)))),
87
      );
88
      $node->content['links']['blog'] = array(
89
        '#theme' => 'links__blog_node',
90
        '#links' => $links,
91
        '#attributes' => array('class' => array('links', 'inline')),
92
      );
Dries's avatar
   
Dries committed
93
    }
Dries's avatar
   
Dries committed
94
  }
Dries's avatar
 
Dries committed
95
96
}

Dries's avatar
   
Dries committed
97
/**
98
 * Implements hook_menu().
Dries's avatar
   
Dries committed
99
 */
100
101
function blog_menu() {
  $items['blog'] = array(
102
    'title' => 'Blogs',
103
    'page callback' => 'blog_page_last',
104
105
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
106
    'file' => 'blog.pages.inc',
107
  );
108
  $items['blog/%user_uid_optional'] = array(
109
    'title' => 'My blog',
110
    'page callback' => 'blog_page_user',
111
    'page arguments' => array(1),
112
113
    'access callback' => 'blog_page_user_access',
    'access arguments' => array(1),
114
    'file' => 'blog.pages.inc',
115
116
117
118
  );
  $items['blog/%user/feed'] = array(
    'title' => 'Blogs',
    'page callback' => 'blog_feed_user',
119
    'page arguments' => array(1),
120
121
    'access callback' => 'blog_page_user_access',
    'access arguments' => array(1),
122
    'type' => MENU_CALLBACK,
123
    'file' => 'blog.pages.inc',
124
125
126
127
128
129
  );
  $items['blog/feed'] = array(
    'title' => 'Blogs',
    'page callback' => 'blog_feed_last',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
130
    'file' => 'blog.pages.inc',
131
  );
Dries's avatar
   
Dries committed
132

Dries's avatar
   
Dries committed
133
134
135
  return $items;
}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * Implements hook_menu_local_tasks_alter().
 */
function blog_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  // Add action link to 'node/add/blog' on 'blog' page.
  if ($root_path == 'blog') {
    $item = menu_get_item('node/add/blog');
    if ($item['access']) {
      $item['title'] = t('Create new blog entry');
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

153
154
155
156
157
158
159
/**
 * Access callback for user blog pages.
 */
function blog_page_user_access($account) {
  // The visitor must be able to access the site's content.
  // For a blog to 'exist' the user must either be able to
  // create new blog entries, or it must have existing posts.
160
  return $account->uid && user_access('access content') && (user_access('create blog content', $account) || _blog_post_exists($account));
161
162
163
164
165
166
}

/**
 * Helper function to determine if a user has blog posts already.
 */
function _blog_post_exists($account) {
167
168
169
170
171
172
173
174
175
  return (bool)db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('type', 'blog')
    ->condition('uid', $account->uid)
    ->condition('status', 1)
    ->range(0, 1)
    ->addTag('node_access')
    ->execute()
    ->fetchField();
176
177
}

Dries's avatar
   
Dries committed
178
/**
179
 * Implements hook_block_info().
180
 */
181
function blog_block_info() {
182
183
184
185
186
  $block['recent']['info'] = t('Recent blog posts');
  return $block;
}

/**
187
 * Implements hook_block_view().
Dries's avatar
   
Dries committed
188
189
190
 *
 * Displays the most recent 10 blog titles.
 */
191
function blog_block_view($delta = '') {
Dries's avatar
   
Dries committed
192
  global $user;
193
194

  if (user_access('access content')) {
195
196
197
198
199
200
201
202
203
    $result = db_select('node', 'n')
      ->fields('n', array('nid', 'title', 'created'))
      ->condition('type', 'blog')
      ->condition('status', 1)
      ->orderBy('created', 'DESC')
      ->range(0, 10)
      ->addTag('node_access')
      ->execute();

204
205
    if ($node_title_list = node_title_list($result)) {
      $block['content'] = $node_title_list;
206
      $block['content'] .= theme('more_link', array('url' => url('blog'), 'title' => t('Read the latest blog entries.')));
207
208
      $block['subject'] = t('Recent blog posts');
      return $block;
209
    }
Dries's avatar
 
Dries committed
210
211
  }
}
212

213
/**
214
 * Implements hook_rdf_mapping().
215
216
217
218
219
220
221
 */
function blog_rdf_mapping() {
  return array(
    array(
      'type' => 'node',
      'bundle' => 'blog',
      'mapping' => array(
222
        'rdftype' => array('sioc:Post', 'sioct:BlogPost'),
223
      ),
224
225
    ),
  );
226
}