<?php
// $Id: text.module,v 1.32.2.4 2006/07/11 01:02:50 tavon Exp $

/**
 * @file
 * Defines simple text field types.
 */

/**
 * Implementation of hook_help().
 */
function text_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Defines simple text field types. <em>Note: Requires content.module.</em>');
  }
}

/**
 * Implementation of hook_field_info().
 */
function text_field_info() {
  return array(
    'text' => array('label' => 'Text'),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function text_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'));
      $form['text_processing'] = array(
        '#type' => 'radios',
        '#title' => t('Text processing'),
        '#default_value' => isset($field['text_processing']) ? $field['text_processing'] : 0,
        '#options' => $options,
      );
      $form['max_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum length'),
        '#default_value' => isset($field['max_length']) ? $field['max_length'] : '',
        '#required' => FALSE,
        '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
      );
      $form['allowed_values'] = array(
        '#type' => 'textarea',
        '#title' => t('Allowed values'),
        '#default_value' => isset($field['allowed_values']) ? $field['allowed_values'] : '',
        '#required' => FALSE,
        '#rows' => 10,
        '#description' => t('The possible values this field can contain. Any other values will result in an error. Enter one value per line.'),
      );
      return $form;

    case 'save':
      return array('text_processing', 'max_length', 'allowed_values');

    case 'database columns':
      $columns = array(
        'value' => array('type' => 'varchar', 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),
        'format' => array('type' => 'int', 'length' => 10, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      );
      if ($field['max_length'] == 0 || $field['max_length'] > 255) {
        $columns['value']['type'] = 'longtext';
      }
      else {
        $columns['value']['length'] = $field['max_length'];
      }
      if ($field['text_processing'] == 0) {
        unset($columns['format']);
      }
      return $columns;

    case 'filter operators':
      return array(
        '=' => t('is equal to'),
        '!=' => t('is not equal to'),
        'LIKE' => t('matches the pattern'),
      );
  }
}

/**
 * Implementation of hook_field().
 */
function text_field($op, &$node, $field, &$node_field, $teaser, $page) {
  switch ($op) {
    case 'view':
      foreach ($node_field as $delta => $item) {
        $node_field[$delta]['view'] = text_field_view_item($field, $item, $node);
      }
      return theme('field', $node, $field, $node_field, $teaser, $page);

    case 'validate':
      $allowed_values = explode("\n", $field['allowed_values']);
      $allowed_values = array_map('trim', $allowed_values);
      $allowed_values = array_filter($allowed_values, 'strlen');

      if (is_array($node_field)) {
        foreach ($node_field as $delta => $item) {
          if ($item['value'] != '') {
            if (count($allowed_values) && !in_array($item['value'], $allowed_values)) {
              form_set_error($field['field_name'], t('Illegal value for %name.', array('%name' => t($field['widget']['label']))));
            }
          }
        }
      }
      break;
  }
}

/**
 * Implementation of hook_field_view_item().
 *
 * The extra $node argument is necessary so that filter access can be checked on
 * node preview.
 */
function text_field_view_item($field, $node_field_item, $node = NULL) {
  if (!isset($node_field_item['value'])) {
    return '';
  }

  // check permissions in check_markup if previewing.
  $preview = ($node && isset($node->in_preview));

  if ($field['text_processing']) {
    return check_markup($node_field_item['value'], $node_field_item['format'], $preview);
  }
  else {
    return check_plain($node_field_item['value']);
  }
}

/**
 * Implementation of hook_widget_info().
 */
function text_widget_info() {
  return array(
    'text' => array(
      'label' => 'Text Field',
      'field types' => array('text'),
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function text_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $form['rows'] = array(
        '#type' => 'textfield',
        '#title' => t('Rows'),
        '#default_value' => $widget['rows'] ? $widget['rows'] : 1,
        '#required' => TRUE,
      );
      return $form;

    case 'validate':
      if (!is_numeric($widget['rows']) || intval($widget['rows']) != $widget['rows'] || $widget['rows'] <= 0) {
        form_set_error('rows', t('"Rows" must be a positive integer.'));
      }
      break;

    case 'save':
      return array('rows');
  }
}


/**
 * Implementation of hook_widget().
 */
function text_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'form':
      $form = array();

      $form[$field['field_name']] = array('#tree' => TRUE);

      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#title'] = t($field['widget']['label']);
        $delta = 0;
        foreach ($node_field as $data) {
          if ($data['value']) {
            if ($field['widget']['rows'] == 1) {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textfield',
                '#title' => '',
                '#default_value' => $data['value'],
                '#required' => ($delta == 0) ? $field['required'] : FALSE,
                '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
                '#weight' => $field['widget']['weight'],
              );
            }
            else {
              $form[$field['field_name']][$delta]['value'] = array(
                '#type' => 'textarea',
                '#title' => '',
                '#default_value' => $data['value'],
                '#required' => ($delta == 0) ? $field['required'] : FALSE,
                '#rows' => $field['widget']['rows'],
                '#weight' => $field['widget']['weight'],
              );
            }
            if ($field['text_processing']) {
              $form[$field['field_name']][$delta]['format'] = filter_form($data['format'], NULL, array($field['field_name'], $delta, 'format'));
            }
            $delta++;
          }
        }
        foreach (range($delta, $delta + 2) as $delta) {
          if ($field['widget']['rows'] == 1) {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textfield',
              '#title' => '',
              '#default_value' => '',
              '#required' => ($delta == 0) ? $field['required'] : FALSE,
              '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
              '#weight' => $field['widget']['weight'],
            );
          }
          else {
            $form[$field['field_name']][$delta]['value'] = array(
              '#type' => 'textarea',
              '#title' => '',
              '#default_value' => '',
              '#required' => ($delta == 0) ? $field['required'] : FALSE,
              '#rows' => $field['widget']['rows'],
              '#weight' => $field['widget']['weight'],
            );
          }
          if ($field['text_processing']) {
            $form[$field['field_name']][$delta]['format'] = filter_form($node_field[$delta]['format'], NULL, array($field['field_name'], $delta, 'format'));
          }
        }
      }
      else {
        if ($field['widget']['rows'] == 1) {
          $form[$field['field_name']][0]['value'] = array(
            '#type' => 'textfield',
            '#title' => t($field['widget']['label']),
            '#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '',
            '#required' => $field['required'],
            '#description' => $field['widget']['description'],
            '#maxlength' => $field['max_length'] ? $field['max_length'] : '',
            '#weight' => $field['widget']['weight'],
          );
        }
        else {
          $form[$field['field_name']][0]['value'] = array(
            '#type' => 'textarea',
            '#title' => t($field['widget']['label']),
            '#default_value' => $node_field[0]['value'],
            '#required' => $field['required'],
            '#rows' => $field['widget']['rows'],
            '#description' => $field['widget']['description'],
            '#weight' => $field['widget']['weight'],
          );
        }
        if ($field['text_processing']) {
          $form[$field['field_name']][0]['format'] = filter_form($node_field[0]['format'], NULL, array($field['field_name'], 0, 'format'));
        }
      }
      return $form;

    case 'process form values':
      // Don't save empty fields.
      if ($field['multiple']) {
        foreach ($node_field as $delta => $item) {
          if ($item['value'] == '') {
            unset($node_field[$delta]);
          }
        }
      }
      break;
  }
}
