blog.module 7.41 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 6 7 8
/**
 * @file
 * Enables keeping an easily and regularly updated web page or a blog.
 */

Dries's avatar
 
Dries committed
9
/**
10
 * Implementation of 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
      'module' => 'blog',
17
      'description' => t('A <em>blog entry</em> is a single post to an online journal, or <em>blog</em>.'),
18 19
    )
  );
Dries's avatar
 
Dries committed
20 21
}

Dries's avatar
 
Dries committed
22 23 24
/**
 * Implementation of hook_perm().
 */
Dries's avatar
 
Dries committed
25
function blog_perm() {
Dries's avatar
 
Dries committed
26
  return array('edit own blog');
Dries's avatar
 
Dries committed
27 28
}

Dries's avatar
 
Dries committed
29 30 31
/**
 * Implementation of hook_access().
 */
32
function blog_access($op, $node, $account) {
Dries's avatar
 
Dries committed
33
  if ($op == 'create') {
34
    return user_access('edit own blog', $account) && $account->uid;
Dries's avatar
 
Dries committed
35 36
  }

Dries's avatar
 
Dries committed
37
  if ($op == 'update' || $op == 'delete') {
38
    if (user_access('edit own blog', $account) && ($node->uid == $account->uid)) {
Dries's avatar
 
Dries committed
39 40
      return TRUE;
    }
Dries's avatar
 
Dries committed
41 42 43
  }
}

Dries's avatar
 
Dries committed
44 45 46
/**
 * Implementation of hook_user().
 */
47
function blog_user($type, &$edit, &$user) {
Dries's avatar
 
Dries committed
48
  if ($type == 'view' && user_access('edit own blog', $user)) {
49 50 51 52 53
    $user->content['summary']['blog'] =  array(
      '#type' => 'user_profile_item',
      '#title' => t('Blog'),
      '#value' => l(t('View recent blog entries'), "blog/$user->uid", array('title' => t("Read @username's latest blog entries.", array('@username' => $user->name)))),
      '#attributes' => array('class' => 'blog'),
54
    );
55 56 57
  }
}

Dries's avatar
 
Dries committed
58 59 60
/**
 * Implementation of hook_help().
 */
61 62
function blog_help($path, $arg) {
  switch ($path) {
Dries's avatar
 
Dries committed
63
    case 'admin/help#blog':
64
      $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
      $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
      $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
      $output .= '<p>'. t('When using the aggregator module an automatic <em>blog it</em> icon is displayed next to the items in a feed\'s <em>latest items</em> block. Clicking this icon populates a <em>blog entry</em> with a title (the title of the feed item) and body (a link to the source item on its original site and illustrative content suitable for use in a block quote). Blog authors can use this feature to easily comment on items of interest that appear in aggregator feeds from other sites. To use this feature, be sure to <a href="@modules">enable</a> the aggregator module, <a href="@feeds">add and configure</a> a feed from another site, and <a href="@blocks">position</a> the feed\'s <em>latest items</em> block.', array('@modules' => url('admin/build/modules'), '@feeds' => url('admin/content/aggregator'), '@blocks' => url('admin/build/block'))) .'</p>';
68
      $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>';
69
      return $output;
Dries's avatar
 
Dries committed
70
  }
Dries's avatar
 
Dries committed
71
}
Dries's avatar
 
Dries committed
72

Dries's avatar
 
Dries committed
73 74 75
/**
 * Implementation of hook_form().
 */
Dries's avatar
 
Dries committed
76
function blog_form(&$node) {
Dries's avatar
 
Dries committed
77
  global $nid;
78
  $iid = isset($_GET['iid']) ? (int)$_GET['iid'] : 0;
79
  $type = node_get_types('type', $node);
Dries's avatar
 
Dries committed
80

81

Dries's avatar
 
Dries committed
82
  if (empty($node->body)) {
Dries's avatar
 
Dries committed
83 84 85 86 87
    /*
    ** If the user clicked a "blog it" link, we load the data from the
    ** database and quote it in the blog:
    */

88
    if ($nid && $blog = node_load($nid)) {
Dries's avatar
 
Dries committed
89
      $node->body = '<em>'. $blog->body .'</em> ['. l($blog->name, "node/$nid") .']';
Dries's avatar
 
Dries committed
90 91
    }

92
    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))) {
93
      $node->title = $item->title;
94 95
      // Note: $item->description has been validated on aggregation.
      $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";
Dries's avatar
 
Dries committed
96
    }
97

Dries's avatar
 
Dries committed
98
  }
Dries's avatar
 
Dries committed
99

100
  $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => !empty($node->title) ? $node->title : NULL, '#weight' => -5);
101
  $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
102
  return $form;
Dries's avatar
 
Dries committed
103 104
}

Dries's avatar
 
Dries committed
105 106 107
/**
 * Implementation of hook_view().
 */
108
function blog_view($node, $teaser = FALSE, $page = FALSE) {
Dries's avatar
 
Dries committed
109
  if ($page) {
Dries's avatar
 
Dries committed
110
    // Breadcrumb navigation
111
    drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'), l(t("@name's blog", array('@name' => $node->name)), 'blog/'. $node->uid)));
Dries's avatar
 
Dries committed
112
  }
113
  return node_prepare($node, $teaser);
Dries's avatar
 
Dries committed
114 115
}

Dries's avatar
 
Dries committed
116 117 118
/**
 * Implementation of hook_link().
 */
119
function blog_link($type, $node = NULL, $teaser = FALSE) {
Dries's avatar
 
Dries committed
120 121
  $links = array();

Dries's avatar
 
Dries committed
122
  if ($type == 'node' && $node->type == 'blog') {
123
    if (arg(0) != 'blog' || arg(1) != $node->uid) {
124
      $links['blog_usernames_blog'] = array(
125
        'title' => t("@username's blog", array('@username' => $node->name)),
126
        'href' => "blog/$node->uid",
127
        'attributes' => array('title' => t("Read @username's latest blog entries.", array('@username' => $node->name)))
128
      );
Dries's avatar
 
Dries committed
129
    }
Dries's avatar
 
Dries committed
130 131
  }

Dries's avatar
 
Dries committed
132
  return $links;
Dries's avatar
 
Dries committed
133 134
}

Dries's avatar
 
Dries committed
135 136 137
/**
 * Implementation of hook_menu().
 */
138 139
function blog_menu() {
  $items['blog'] = array(
140
    'title' => 'Blogs',
141
    'page callback' => 'blog_page_last',
142 143
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
144 145
    'file' => 'blog.pages.inc',
  );
146
  $items['blog/%user_current'] = array(
147
    'title' => 'My blog',
148
    'page callback' => 'blog_page_user',
149
    'page arguments' => array(1),
150 151
    'access callback' => 'user_access',
    'access arguments' => array('edit own blog', 1),
152 153 154 155 156
    'file' => 'blog.pages.inc',
  );
  $items['blog/%user/feed'] = array(
    'title' => 'Blogs',
    'page callback' => 'blog_feed_user',
157
    'page arguments' => array(1),
158 159 160 161 162 163 164 165 166 167
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'blog.pages.inc',
  );
  $items['blog/feed'] = array(
    'title' => 'Blogs',
    'page callback' => 'blog_feed_last',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'blog.pages.inc',
168
  );
Dries's avatar
 
Dries committed
169

Dries's avatar
 
Dries committed
170 171 172
  return $items;
}

Dries's avatar
 
Dries committed
173 174 175 176 177 178
/**
 * Implementation of hook_block().
 *
 * Displays the most recent 10 blog titles.
 */
function blog_block($op = 'list', $delta = 0) {
Dries's avatar
 
Dries committed
179
  global $user;
Dries's avatar
 
Dries committed
180
  if ($op == 'list') {
Dries's avatar
 
Dries committed
181
    $block[0]['info'] = t('Recent blog posts');
Dries's avatar
 
Dries committed
182
    return $block;
Dries's avatar
 
Dries committed
183
  }
184
  else if ($op == 'view') {
Dries's avatar
 
Dries committed
185
    if (user_access('access content')) {
186
      $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);
187 188
      if ($node_title_list = node_title_list($result)) {
        $block['content'] = $node_title_list;
189
        $block['content'] .= theme('more_link', url('blog'), t('Read the latest blog entries.'));
190 191 192
        $block['subject'] = t('Recent blog posts');
        return $block;
      }
193
    }
Dries's avatar
 
Dries committed
194 195
  }
}
196