<?php
// $Id: excerpt.module,v 1.6 2005/12/30 17:31:10 unconed Exp $

function excerpt_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows users to enter a separate excerpt for nodes.');
    case 'admin/node/configure/defaults':
      return t('<p>If you want users to be able to enter separate excerpts for nodes, check the <em>excerpt</em> box in the appropriate column.</p>');
  }
}

function excerpt_nodeapi(&$node, $op, $arg) {
  switch ($op) {
    case 'validate':
      if (trim($node->teaser) == '') {
        $node->teaser = node_teaser($node->body);
      }
      break;
    case 'view':
      $node->readmore = $node->teaser != $node->body;
      break;
  }
}

function excerpt_form_alter($form_id, &$form) {
  if (isset($form['type'])) {
    $node = $form['#node'];
    
    if ($form['type']['#value'] .'_node_settings' == $form_id) {
      $form['excerpt'] = array('#type' => 'fieldset', '#title' => t('Excerpt'));
      $form['excerpt']['excerpt_options_'. $form['type']['#value']] = array(
        '#type' => 'radios',
        '#title' => t('Teasers'),
        '#default_value' => variable_get('excerpt_options_'. $form['type']['#value'], 1),
        '#options' => array(t('Auto-generated'), t('Manual excerpt')),
        '#description' => t('Choose whether teasers are generated automatically or can be entered manually by the author.')
      );
    }
    
    if ($form['type']['#value'] .'_node_form' == $form_id && variable_get('excerpt_options_'. $node->type, 1)) {
      $form['teaser'] = array(
        '#type' => 'textarea',
        '#title' => t('Teaser'),
        '#default_value' => $node->teaser,
        '#cols' => 60,
        '#rows' => 5,
        '#weight' => -17.5,
        '#description' => t('Enter an excerpt for this item. It will be shown on listing pages along with a <em>read more</em> link which leads to the full view. Leave empty to auto-generate one from the body.')
      );
    }
  
  }
}