cms/drupal/modules/blog/blog.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Enables multi-user blogs.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_node_info().
       
    10  */
       
    11 function blog_node_info() {
       
    12   return array(
       
    13     'blog' => array(
       
    14       'name' => t('Blog entry'),
       
    15       'base' => 'blog',
       
    16       'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
       
    17     )
       
    18   );
       
    19 }
       
    20 
       
    21 /**
       
    22  * Implements hook_user_view().
       
    23  */
       
    24 function blog_user_view($account) {
       
    25   if (user_access('create blog content', $account)) {
       
    26     $account->content['summary']['blog'] =  array(
       
    27       '#type' => 'user_profile_item',
       
    28       '#title' => t('Blog'),
       
    29       // l() escapes the attributes, so we should not escape !username here.
       
    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 
       
    36 /**
       
    37  * Implements hook_help().
       
    38  */
       
    39 function blog_help($path, $arg) {
       
    40   switch ($path) {
       
    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/documentation/modules/blog/')) . '</p>';
       
    44       $output .= '<h3>' . t('Uses') . '</h3>';
       
    45       $output .= '<dl>';
       
    46       $output .= '<dt>' . t('Single-user blogs') . '</dt>';
       
    47       $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>';
       
    48       $output .= '<dt>' . t('Multi-user blogs') . '</dt>';
       
    49       $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>';
       
    50       $output .= '<dt>' . t('Navigation') . '</dt>';
       
    51       $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>';
       
    52       $output .= '<dt>' . t('Blocks') . '</dt>';
       
    53       $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>';
       
    54       $output .= '</dl>';
       
    55       return $output;
       
    56   }
       
    57 }
       
    58 
       
    59 /**
       
    60  * Implements hook_form().
       
    61  */
       
    62 function blog_form($node, $form_state) {
       
    63   return node_content_form($node, $form_state);
       
    64 }
       
    65 
       
    66 /**
       
    67  * Implements hook_view().
       
    68  */
       
    69 function blog_view($node, $view_mode) {
       
    70   if ($view_mode == 'full' && node_is_page($node)) {
       
    71     // Breadcrumb navigation.  l() escapes title, so we should not escape !name.
       
    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)));
       
    73   }
       
    74   return $node;
       
    75 }
       
    76 
       
    77 /**
       
    78  * Implements hook_node_view().
       
    79  */
       
    80 function blog_node_view($node, $view_mode) {
       
    81   if ($view_mode != 'rss') {
       
    82     if ($node->type == 'blog' && (arg(0) != 'blog' || arg(1) != $node->uid)) {
       
    83       // This goes to l(), which escapes !username in both title and attributes.
       
    84       $links['blog_usernames_blog'] = array(
       
    85         'title' => t("!username's blog", array('!username' => format_username($node))),
       
    86         'href' => "blog/$node->uid",
       
    87         'attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($node)))),
       
    88       );
       
    89       $node->content['links']['blog'] = array(
       
    90         '#theme' => 'links__node__blog',
       
    91         '#links' => $links,
       
    92         '#attributes' => array('class' => array('links', 'inline')),
       
    93       );
       
    94     }
       
    95   }
       
    96 }
       
    97 
       
    98 /**
       
    99  * Implements hook_menu().
       
   100  */
       
   101 function blog_menu() {
       
   102   $items['blog'] = array(
       
   103     'title' => 'Blogs',
       
   104     'page callback' => 'blog_page_last',
       
   105     'access arguments' => array('access content'),
       
   106     'type' => MENU_SUGGESTED_ITEM,
       
   107     'file' => 'blog.pages.inc',
       
   108   );
       
   109   $items['blog/%user_uid_optional'] = array(
       
   110     'title' => 'My blog',
       
   111     'page callback' => 'blog_page_user',
       
   112     'page arguments' => array(1),
       
   113     'access callback' => 'blog_page_user_access',
       
   114     'access arguments' => array(1),
       
   115     'file' => 'blog.pages.inc',
       
   116   );
       
   117   $items['blog/%user/feed'] = array(
       
   118     'title' => 'Blogs',
       
   119     'page callback' => 'blog_feed_user',
       
   120     'page arguments' => array(1),
       
   121     'access callback' => 'blog_page_user_access',
       
   122     'access arguments' => array(1),
       
   123     'type' => MENU_CALLBACK,
       
   124     'file' => 'blog.pages.inc',
       
   125   );
       
   126   $items['blog/feed'] = array(
       
   127     'title' => 'Blogs',
       
   128     'page callback' => 'blog_feed_last',
       
   129     'access arguments' => array('access content'),
       
   130     'type' => MENU_CALLBACK,
       
   131     'file' => 'blog.pages.inc',
       
   132   );
       
   133 
       
   134   return $items;
       
   135 }
       
   136 
       
   137 /**
       
   138  * Implements hook_menu_local_tasks_alter().
       
   139  */
       
   140 function blog_menu_local_tasks_alter(&$data, $router_item, $root_path) {
       
   141   global $user;
       
   142 
       
   143   // Add action link to 'node/add/blog' on 'blog' page.
       
   144   if ($root_path == 'blog') {
       
   145     $item = menu_get_item('node/add/blog');
       
   146     if ($item['access']) {
       
   147       $item['title'] = t('Create new blog entry');
       
   148       $data['actions']['output'][] = array(
       
   149         '#theme' => 'menu_local_action',
       
   150         '#link' => $item,
       
   151       );
       
   152     }
       
   153   }
       
   154   // Provide a helper action link to the author on the 'blog/%' page.
       
   155   elseif ($root_path == 'blog/%' && isset($router_item['page_arguments'][0]->uid) && $router_item['page_arguments'][0]->uid == $user->uid) {
       
   156     $data['actions']['output']['blog'] = array(
       
   157       '#theme' => 'menu_local_action',
       
   158     );
       
   159     if (user_access('create blog content')) {
       
   160       $data['actions']['output']['blog']['#link']['title'] = t('Post new blog entry.');
       
   161       $data['actions']['output']['blog']['#link']['href'] = 'node/add/blog';
       
   162     }
       
   163     else {
       
   164       $data['actions']['output']['blog']['#link']['title'] = t('You are not allowed to post a new blog entry.');
       
   165     }
       
   166   }
       
   167 }
       
   168 
       
   169 /**
       
   170  * Access callback for user blog pages.
       
   171  */
       
   172 function blog_page_user_access($account) {
       
   173   // The visitor must be able to access the site's content.
       
   174   // For a blog to 'exist' the user must either be able to
       
   175   // create new blog entries, or it must have existing posts.
       
   176   return $account->uid && user_access('access content') && (user_access('create blog content', $account) || _blog_post_exists($account));
       
   177 }
       
   178 
       
   179 /**
       
   180  * Helper function to determine if a user has blog posts already.
       
   181  */
       
   182 function _blog_post_exists($account) {
       
   183   return (bool)db_select('node', 'n')
       
   184     ->fields('n', array('nid'))
       
   185     ->condition('type', 'blog')
       
   186     ->condition('uid', $account->uid)
       
   187     ->condition('status', 1)
       
   188     ->range(0, 1)
       
   189     ->addTag('node_access')
       
   190     ->execute()
       
   191     ->fetchField();
       
   192 }
       
   193 
       
   194 /**
       
   195  * Implements hook_block_info().
       
   196  */
       
   197 function blog_block_info() {
       
   198   $block['recent']['info'] = t('Recent blog posts');
       
   199   $block['recent']['properties']['administrative'] = TRUE;
       
   200   return $block;
       
   201 }
       
   202 
       
   203 /**
       
   204  * Implements hook_block_configure().
       
   205  */
       
   206 function blog_block_configure($delta = '') {
       
   207   if ($delta == 'recent') {
       
   208     $form['blog_block_count'] = array(
       
   209       '#type' => 'select',
       
   210       '#title' => t('Number of recent blog posts to display'),
       
   211       '#default_value' => variable_get('blog_block_count', 10),
       
   212       '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
       
   213     );
       
   214     return $form;
       
   215   }
       
   216 }
       
   217 
       
   218 /**
       
   219  * Implements hook_block_save().
       
   220  */
       
   221 function blog_block_save($delta = '', $edit = array()) {
       
   222   if ($delta == 'recent') {
       
   223     variable_set('blog_block_count', $edit['blog_block_count']);
       
   224   }
       
   225 }
       
   226 
       
   227 /**
       
   228  * Implements hook_block_view().
       
   229  *
       
   230  * Displays the most recent 10 blog titles.
       
   231  */
       
   232 function blog_block_view($delta = '') {
       
   233   global $user;
       
   234 
       
   235   if (user_access('access content')) {
       
   236     $result = db_select('node', 'n')
       
   237       ->fields('n', array('nid', 'title', 'created'))
       
   238       ->condition('type', 'blog')
       
   239       ->condition('status', 1)
       
   240       ->orderBy('created', 'DESC')
       
   241       ->range(0, variable_get('blog_block_count', 10))
       
   242       ->addTag('node_access')
       
   243       ->execute();
       
   244 
       
   245     if ($node_title_list = node_title_list($result)) {
       
   246       $block['subject'] = t('Recent blog posts');
       
   247       $block['content']['blog_list'] = $node_title_list;
       
   248       $block['content']['blog_more'] = array(
       
   249         '#theme' => 'more_link',
       
   250         '#url' => 'blog',
       
   251         '#title' => t('Read the latest blog entries.'),
       
   252       );
       
   253 
       
   254       return $block;
       
   255     }
       
   256   }
       
   257 }
       
   258 
       
   259 /**
       
   260  * Implements hook_rdf_mapping().
       
   261  */
       
   262 function blog_rdf_mapping() {
       
   263   return array(
       
   264     array(
       
   265       'type' => 'node',
       
   266       'bundle' => 'blog',
       
   267       'mapping' => array(
       
   268         'rdftype' => array('sioc:Post', 'sioct:BlogPost'),
       
   269       ),
       
   270     ),
       
   271   );
       
   272 }