<?php

/**
 * @file
 * Moves the comment form for a node into a block.
 */

/**
 * Implements hook_block() for Drupal 6 compatibility
 */
function comment_form_block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
  case 'view':
    //$block = node_comment_block_block_view($delta);
    $block = array();
    if ($delta == 'comment_form') {
      if (arg(0) == 'node' && is_numeric($nid = arg(1)) && !arg(2)) {
        global $user;
        $node = node_load($nid);
        if ($node->comment !== 0 AND user_access('post comments', $user)) {
          $block['subject'] = NULL; // This should be NULL otherwise there will be duplicate h2 elements.
          $block['content'] = '<h2>' . bts('Post new comment', array(), NULL, 'boinc:forum-post-new-comment') . '</h2>';
          $block['content'] .= drupal_get_form('comment_form', array('nid' => $nid)); 
        }
      }
    }
    break;
  case 'list':
    $blocks = comment_form_block_block_info();
    return $blocks;
    break;
  default:
  }
  return $block;
}

/**
 * Implements hook_block_info().
 */
function comment_form_block_block_info() {
  $blocks['comment_form'] = array(
    'info' => bts('Comment form', array(), NULL, 'boinc:forum-comment-form'),
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */ /*
function comment_form_block_block_view($delta = '') {
  $block = array();

  if ($delta == 'node_comments') {
    if (arg(0) == 'node' && is_numeric($nid = arg(1)) && !arg(2)) {
      $node = node_load($nid);

      if ($node->comment !== 0) {
        $block['subject'] = NULL; // This should be NULL otherwise there will be duplicate h2 elements.
        $block['content'] = '';

        $node = node_view($node);

        if ($node['comments']) {
          $block['content'] .= render($node['comments']);
        }
      }
    }
  }

  return $block;
} */

/**
 * Implements hook_preprocess_HOOK() for nodes.
 */
function comment_form_block_preprocess_node(&$variables) {
}

/**
 * Implements hook_form_FORM_ID_alter() for block_admin_configure.
 */
function comment_form_block_form_block_admin_configure_alter(&$form, &$form_state) {
  if (isset($form['delta'])) {
    if ($form['delta']['#value'] == 'comment_form') {
      $form['settings']['title']['#disabled'] = TRUE;
      $form['settings']['title']['#description'] = t('The title for this block cannot be overridden.');
    }
  }
}
