<?php
// $Id: comment_subject.module,v 1.7.2.2 2009/11/19 07:41:13 sinasalek Exp $

/**
 * Implementation of hook_form_alter().
 */
function comment_subject_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'comment_form' && $form['subject']['#default_value']=='') {
    switch (arg(0)) {
      case 'node':
        $node = node_load(array('nid' => arg(1)));
        $subject = $node->title;
        break;
      case 'comment':
        if (arg(1)=='reply') {
          if (is_numeric(arg(3))) {
            $comment = _comment_load(arg(3));
            $subject = $comment->subject;
          } else {
            $node = node_load(arg(2));
            $subject = $node->title;
          }
        } else if (arg(1)=='edit' && is_numeric(arg(2))) {
          $comment = _comment_load(arg(2));
          $subject = $comment->subject;
        }
        break;
    }
    if (!preg_match('/^' . preg_quote(t('Re:')) . '/i', $subject)) {
      $subject = t('Re:') . ' ' . $subject;
    }
    // comment subjects can not be longer than 64 characters
    $subject = truncate_utf8($subject, 64, TRUE, TRUE);

    // nevermind whether #type should or shouldn't be hidden
    // just update the #default_value
    // which is compatible with comment_update_6002 and preserves backward compatibility
    if (!isset($form['subject'])) $form['subject']['#type'] = 'hidden';
    $form['subject']['#default_value'] = $subject;
  }
}
