Code coverage for /20081101/modules/blog/blog.module

Line #Times calledCode
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
 */
12243
function blog_node_info() {
13
  return array(
14
    'blog' => array(
15200
      'name' => t('Blog entry'),
16200
      'base' => 'blog',
17200
      'description' => t('A <em>blog entry</em> is a single post to an
online journal, or <em>blog</em>.'),
18
    )
19200
  );
200
}
21
22
/**
23
 * Implementation of hook_perm().
24
 */
25243
function blog_perm() {
263
  return node_list_permissions('blog');
270
}
28
29
/**
30
 * Implementation of hook_access().
31
 */
32243
function blog_access($op, $node, $account) {
33
  switch ($op) {
34181
    case 'create':
35
      // Anonymous users cannot post even if they have the permission.
36171
      return user_access('create blog content', $account) && $account->uid;
3731
    case 'update':
3822
      return user_access('edit any blog content', $account) ||
(user_access('edit own blog content', $account) && ($node->uid ==
$account->uid));
3925
    case 'delete':
4016
      return user_access('delete any blog content', $account) ||
(user_access('delete own blog content', $account) && ($node->uid ==
$account->uid));
410
  }
4216
}
43
44
/**
45
 * Implementation of hook_user_view().
46
 */
47243
function blog_user_view(&$edit, &$user, $category) {
4820
  if (user_access('create blog content', $user)) {
497
    $user->content['summary']['blog'] =  array(
507
      '#type' => 'user_profile_item',
517
      '#title' => t('Blog'),
527
      '#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))))),
537
      '#attributes' => array('class' => 'blog'),
54
    );
557
  }
5620
}
57
58
/**
59
 * Implementation of hook_help().
60
 */
61243
function blog_help($path, $arg) {
62
  switch ($path) {
63155
    case 'admin/help#blog':
642
      $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>';
652
      $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>';
662
      $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>';
672
      $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>';
682
      return $output;
692
  }
700
}
71155
72
/**
73
 * Implementation of hook_form().
74
 */
75
function blog_form(&$node) {
76243
  global $nid;
778
  $type = node_get_types('type', $node);
788
798
  $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);
818
  return $form;
82
}
83
841
/**
850
 * Implementation of hook_view().
860
 */
87
function blog_view($node, $teaser = FALSE, $page = FALSE) {
881
  if ($page) {
890
    // 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)));
910
  }
920
931
  return node_prepare($node, $teaser);
94
}
958
968
/**
97
 * Implementation of hook_link().
988
 */
990
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) {
104243
      $links['blog_usernames_blog'] = array(
1059
        'title' => t("!username's blog", array('!username' =>
$node->name)),
106
        'href' => "blog/$node->uid",
1079
        'attributes' => array('title' => t("Read !username's latest blog
entries.", array('!username' => $node->name))),
1089
      );
109
    }
1109
  }
1110
112
  return $links;
113
}
114
115
/**
116243
 * Implementation of hook_menu().
11715
 */
118
function blog_menu() {
11915
  $items['blog'] = array(
1209
    'title' => 'Blogs',
1219
    'page callback' => 'blog_page_last',
1229
    'access arguments' => array('access content'),
1239
    'type' => MENU_SUGGESTED_ITEM,
1249
  );
125
  $items['blog/%user_uid_optional'] = array(
1269
    'title' => 'My blog',
1279
    'page callback' => 'blog_page_user',
128
    'page arguments' => array(1),
12915
    'access callback' => 'blog_page_user_access',
1300
    'access arguments' => array(1),
131
  );
132
  $items['blog/%user/feed'] = array(
133
    'title' => 'Blogs',
134
    'page callback' => 'blog_feed_user',
135243
    'page arguments' => array(1),
1365
    'access callback' => 'blog_page_user_access',
1375
    'access arguments' => array(1),
1385
    'type' => MENU_CALLBACK,
1395
  );
1405
  $items['blog/feed'] = array(
141
    'title' => 'Blogs',
1425
    'page callback' => 'blog_feed_last',
1435
    'access arguments' => array('access content'),
1445
    'type' => MENU_CALLBACK,
1455
  );
1465
1475
  return $items;
148
}
1495
1505
/**
1515
 * Access callback for user blog pages.
1525
 */
1535
function blog_page_user_access($account) {
1545
  // The visitor must be able to access the site's content.
1555
  // For a blog to 'exist' the user must either be able to
156
  // create new blog entries, or it must have existing posts.
1575
  return $account->uid && user_access('access content') &&
(user_access('create blog content', $account) ||
_blog_post_exists($account));
1585
}
1595
1605
/**
1615
 * Helper function to determine if a user has blog posts already.
162
 */
163
function _blog_post_exists($account) {
1645
  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));
1650
}
166
167
/**
168
 * Implementation of hook_block().
169
 *
170243
 * Displays the most recent 10 blog titles.
171
 */
172
function blog_block($op = 'list', $delta = '') {
173
  global $user;
17422
  if ($op == 'list') {
1750
    $block['recent']['info'] = t('Recent blog posts');
176
    return $block;
177
  }
178
  elseif ($op == 'view') {
179
    if (user_access('access content')) {
180243
      $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);
1816
      if ($node_title_list = node_title_list($result)) {
1820
        $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
  }
189243
}
19051
19151