annotate modules/blog/blog.module @ 7:fff6d4c8c043 6.3

Drupal 6.3
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:30:28 +0100
parents 2427550111ae
children
rev   line source
webmaster@1 1 <?php
webmaster@7 2 // $Id: blog.module,v 1.297.2.3 2008/05/19 07:27:35 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Enables keeping an easily and regularly updated web page or a blog.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * Implementation of hook_node_info().
webmaster@1 11 */
webmaster@1 12 function blog_node_info() {
webmaster@1 13 return array(
webmaster@1 14 'blog' => array(
webmaster@1 15 'name' => t('Blog entry'),
webmaster@1 16 'module' => 'blog',
webmaster@1 17 'description' => t('A <em>blog entry</em> is a single post to an online journal, or <em>blog</em>.'),
webmaster@1 18 )
webmaster@1 19 );
webmaster@1 20 }
webmaster@1 21
webmaster@1 22 /**
webmaster@1 23 * Implementation of hook_perm().
webmaster@1 24 */
webmaster@1 25 function blog_perm() {
webmaster@1 26 return array('create blog entries', 'delete own blog entries', 'delete any blog entry', 'edit own blog entries', 'edit any blog entry');
webmaster@1 27 }
webmaster@1 28
webmaster@1 29 /**
webmaster@1 30 * Implementation of hook_access().
webmaster@1 31 */
webmaster@1 32 function blog_access($op, $node, $account) {
webmaster@1 33 switch ($op) {
webmaster@1 34 case 'create':
webmaster@1 35 // Anonymous users cannot post even if they have the permission.
webmaster@7 36 return user_access('create blog entries', $account) && $account->uid ? TRUE : NULL;
webmaster@1 37 case 'update':
webmaster@7 38 return user_access('edit any blog entry', $account) || (user_access('edit own blog entries', $account) && ($node->uid == $account->uid)) ? TRUE : NULL;
webmaster@1 39 case 'delete':
webmaster@7 40 return user_access('delete any blog entry', $account) || (user_access('delete own blog entries', $account) && ($node->uid == $account->uid)) ? TRUE : NULL;
webmaster@1 41 }
webmaster@1 42 }
webmaster@1 43
webmaster@1 44 /**
webmaster@1 45 * Implementation of hook_user().
webmaster@1 46 */
webmaster@1 47 function blog_user($type, &$edit, &$user) {
webmaster@1 48 if ($type == 'view' && user_access('create blog entries', $user)) {
webmaster@1 49 $user->content['summary']['blog'] = array(
webmaster@1 50 '#type' => 'user_profile_item',
webmaster@1 51 '#title' => t('Blog'),
webmaster@7 52 '#value' => l(t('View recent blog entries'), "blog/$user->uid", array('attributes' => array('title' => t("Read @username's latest blog entries.", array('@username' => $user->name))))),
webmaster@1 53 '#attributes' => array('class' => 'blog'),
webmaster@1 54 );
webmaster@1 55 }
webmaster@1 56 }
webmaster@1 57
webmaster@1 58 /**
webmaster@1 59 * Implementation of hook_help().
webmaster@1 60 */
webmaster@1 61 function blog_help($path, $arg) {
webmaster@1 62 switch ($path) {
webmaster@1 63 case 'admin/help#blog':
webmaster@1 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>';
webmaster@1 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>';
webmaster@1 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>';
webmaster@1 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>';
webmaster@1 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>';
webmaster@1 69 return $output;
webmaster@1 70 }
webmaster@1 71 }
webmaster@1 72
webmaster@1 73 /**
webmaster@1 74 * Implementation of hook_form().
webmaster@1 75 */
webmaster@1 76 function blog_form(&$node) {
webmaster@1 77 global $nid;
webmaster@1 78 $iid = isset($_GET['iid']) ? (int)$_GET['iid'] : 0;
webmaster@1 79 $type = node_get_types('type', $node);
webmaster@1 80
webmaster@1 81
webmaster@1 82 if (empty($node->body)) {
webmaster@1 83 // If the user clicked a "blog it" link, we load the data from the
webmaster@1 84 // database and quote it in the blog.
webmaster@1 85 if ($nid && $blog = node_load($nid)) {
webmaster@1 86 $node->body = '<em>'. $blog->body .'</em> ['. l($blog->name, "node/$nid") .']';
webmaster@1 87 }
webmaster@1 88
webmaster@1 89 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))) {
webmaster@1 90 $node->title = $item->title;
webmaster@1 91 // Note: $item->description has been validated on aggregation.
webmaster@1 92 $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";
webmaster@1 93 }
webmaster@1 94
webmaster@1 95 }
webmaster@1 96
webmaster@1 97 $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => !empty($node->title) ? $node->title : NULL, '#weight' => -5);
webmaster@1 98 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
webmaster@1 99 return $form;
webmaster@1 100 }
webmaster@1 101
webmaster@1 102 /**
webmaster@1 103 * Implementation of hook_view().
webmaster@1 104 */
webmaster@1 105 function blog_view($node, $teaser = FALSE, $page = FALSE) {
webmaster@1 106 if ($page) {
webmaster@1 107 // Breadcrumb navigation
webmaster@1 108 drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Blogs'), 'blog'), l(t("@name's blog", array('@name' => $node->name)), 'blog/'. $node->uid)));
webmaster@1 109 }
webmaster@1 110 return node_prepare($node, $teaser);
webmaster@1 111 }
webmaster@1 112
webmaster@1 113 /**
webmaster@1 114 * Implementation of hook_link().
webmaster@1 115 */
webmaster@1 116 function blog_link($type, $node = NULL, $teaser = FALSE) {
webmaster@1 117 $links = array();
webmaster@1 118
webmaster@1 119 if ($type == 'node' && $node->type == 'blog') {
webmaster@1 120 if (arg(0) != 'blog' || arg(1) != $node->uid) {
webmaster@1 121 $links['blog_usernames_blog'] = array(
webmaster@1 122 'title' => t("@username's blog", array('@username' => $node->name)),
webmaster@1 123 'href' => "blog/$node->uid",
webmaster@1 124 'attributes' => array('title' => t("Read @username's latest blog entries.", array('@username' => $node->name)))
webmaster@1 125 );
webmaster@1 126 }
webmaster@1 127 }
webmaster@1 128
webmaster@1 129 return $links;
webmaster@1 130 }
webmaster@1 131
webmaster@1 132 /**
webmaster@1 133 * Implementation of hook_menu().
webmaster@1 134 */
webmaster@1 135 function blog_menu() {
webmaster@1 136 $items['blog'] = array(
webmaster@1 137 'title' => 'Blogs',
webmaster@1 138 'page callback' => 'blog_page_last',
webmaster@1 139 'access arguments' => array('access content'),
webmaster@1 140 'type' => MENU_SUGGESTED_ITEM,
webmaster@1 141 'file' => 'blog.pages.inc',
webmaster@1 142 );
webmaster@5 143 $items['blog/%user_uid_optional'] = array(
webmaster@1 144 'title' => 'My blog',
webmaster@1 145 'page callback' => 'blog_page_user',
webmaster@1 146 'page arguments' => array(1),
webmaster@5 147 'access callback' => 'blog_page_user_access',
webmaster@5 148 'access arguments' => array(1),
webmaster@1 149 'file' => 'blog.pages.inc',
webmaster@1 150 );
webmaster@1 151 $items['blog/%user/feed'] = array(
webmaster@1 152 'title' => 'Blogs',
webmaster@1 153 'page callback' => 'blog_feed_user',
webmaster@1 154 'page arguments' => array(1),
webmaster@5 155 'access callback' => 'blog_page_user_access',
webmaster@5 156 'access arguments' => array(1),
webmaster@1 157 'type' => MENU_CALLBACK,
webmaster@1 158 'file' => 'blog.pages.inc',
webmaster@1 159 );
webmaster@1 160 $items['blog/feed'] = array(
webmaster@1 161 'title' => 'Blogs',
webmaster@1 162 'page callback' => 'blog_feed_last',
webmaster@1 163 'access arguments' => array('access content'),
webmaster@1 164 'type' => MENU_CALLBACK,
webmaster@1 165 'file' => 'blog.pages.inc',
webmaster@1 166 );
webmaster@1 167
webmaster@1 168 return $items;
webmaster@1 169 }
webmaster@1 170
webmaster@1 171 /**
webmaster@5 172 * Access callback for user blog pages.
webmaster@5 173 */
webmaster@5 174 function blog_page_user_access($account) {
webmaster@5 175 // The visitor must be able to access the site's content.
webmaster@5 176 // For a blog to 'exist' the user must either be able to
webmaster@5 177 // create new blog entries, or it must have existing posts.
webmaster@5 178 return $account->uid && user_access('access content') && (user_access('create blog entries', $account) || _blog_post_exists($account));
webmaster@5 179 }
webmaster@5 180
webmaster@5 181 /**
webmaster@5 182 * Helper function to determine if a user has blog posts already.
webmaster@5 183 */
webmaster@5 184 function _blog_post_exists($account) {
webmaster@5 185 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));
webmaster@5 186 }
webmaster@5 187
webmaster@5 188 /**
webmaster@1 189 * Implementation of hook_block().
webmaster@1 190 *
webmaster@1 191 * Displays the most recent 10 blog titles.
webmaster@1 192 */
webmaster@1 193 function blog_block($op = 'list', $delta = 0) {
webmaster@1 194 global $user;
webmaster@1 195 if ($op == 'list') {
webmaster@1 196 $block[0]['info'] = t('Recent blog posts');
webmaster@1 197 return $block;
webmaster@1 198 }
webmaster@1 199 else if ($op == 'view') {
webmaster@1 200 if (user_access('access content')) {
webmaster@1 201 $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);
webmaster@1 202 if ($node_title_list = node_title_list($result)) {
webmaster@1 203 $block['content'] = $node_title_list;
webmaster@1 204 $block['content'] .= theme('more_link', url('blog'), t('Read the latest blog entries.'));
webmaster@1 205 $block['subject'] = t('Recent blog posts');
webmaster@1 206 return $block;
webmaster@1 207 }
webmaster@1 208 }
webmaster@1 209 }
webmaster@1 210 }
webmaster@1 211