Index: text.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/text.module,v
retrieving revision 1.41.2.12
diff -u -p -r1.41.2.12 text.module
--- text.module	12 Mar 2007 00:50:34 -0000	1.41.2.12
+++ text.module	7 Dec 2007 23:07:18 -0000
@@ -22,13 +22,24 @@ 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)'));
+      $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'), 2 => t('Filtered text (fixed input format)'));
       $form['text_processing'] = array(
         '#type' => 'radios',
         '#title' => t('Text processing'),
         '#default_value' => isset($field['text_processing']) ? $field['text_processing'] : 0,
         '#options' => $options,
       );
+      $formats = filter_formats();
+      foreach ($formats as $id=>$format) {
+        $formats[$id]=$format->name;
+      }
+      $form['default_format'] = array(
+        '#type' => 'select',
+        '#title' => t('Default format'),
+        '#description' => t('The default format will only have an effect when the field is not processed as plain text. When the format can be selected by the user, this default does not override normal access permissions.'),
+        '#options' => $formats,
+        '#default_value' => isset($field['default_format']) ? $field['default_format'] : variable_get('filter_default_format', 1),
+      );
       $form['max_length'] = array(
         '#type' => 'textfield',
         '#title' => t('Maximum length'),
@@ -36,6 +47,12 @@ function text_field_settings($op, $field
         '#required' => FALSE,
         '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
       );
+      $form['regex'] = array(
+        '#type' => 'textfield',
+	'#title' => t('Regular expression'),
+	'#description' => t('Validate this field against a pattern. Pattern is in Perl-compatible syntax and requires forward slashes. Please remember to adequately describe the required format in the help text, as the validation error will contain no details on why the input is invalid.'),
+	'#default_value' => isset($field['regex']) ? $field['regex'] : '',
+      );
       $form['allowed_values'] = array(
         '#type' => 'textarea',
         '#title' => t('Allowed values list'),
@@ -60,7 +77,7 @@ function text_field_settings($op, $field
       return $form;
 
     case 'save':
-      return array('text_processing', 'max_length', 'allowed_values', 'allowed_values_php');
+      return array('text_processing', 'default_format', 'max_length', 'regex', 'allowed_values', 'allowed_values_php');
 
     case 'database columns':
       $columns = array(
@@ -73,7 +90,7 @@ function text_field_settings($op, $field
       else {
         $columns['value']['length'] = $field['max_length'];
       }
-      if ($field['text_processing'] == 0) {
+      if ($field['text_processing'] != 1) {
         unset($columns['format']);
       }
       return $columns;
@@ -89,7 +106,7 @@ function text_field_settings($op, $field
              'value-type' => 'array',
             ),
           );
-        }
+      }
       else {
         return array(
           'like' => array(
@@ -128,6 +145,13 @@ function text_field($op, &$node, $field,
           }
         }
       }
+      if ($field['regex']) {
+        foreach ($items as $delta => $item) {
+          $matching = @preg_match($field['regex'], $item);
+	  $error_field = $field['field_name'] .']['. $delta .'][value';
+	  if (!$matching) form_set_error($error_field, t('%label does not match the required input pattern.', array('%label' => $field['widget']['label'])));
+	}
+      }
       break;
   }
 }
@@ -181,7 +205,12 @@ function text_field_formatter($field, $i
   }
 
   if ($field['text_processing']) {
-    return check_markup($text, $item['format'], is_null($node) || isset($node->in_preview));
+    if ($item['format'] == 0) {
+      $item['format'] = $field['default_format'];
+    }
+    $access_check = $field['text_processing'] == 1; // Only check access if user can select format
+    $return = check_markup($text, $item['format'], $access_check && (is_null($node) || isset($node->in_preview)));
+    return $return;
   }
   else {
     return check_plain($text);
@@ -263,8 +292,24 @@ function text_widget($op, &$node, $field
                 '#weight' => $field['widget']['weight'],
               );
             }
-            if ($field['text_processing']) {
+            if ($field['text_processing'] == 1) {
+              $data['format'] = $data['format'] ? $data['format'] : $field['default_format'];
               $form[$field['field_name']][$delta]['format'] = filter_form($data['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format'));
+            } else if ($field['text_processing'] == 2) {
+	      $formats = filter_formats();
+	      $format = $formats[$field['default_format']];
+              $form[$field['field_name']][$delta]['format'] = array(
+                '#type' => 'fieldset',
+		'#collapsible' => true,
+		'#collapsed' => true,
+		'#title' => t('Input format'),
+
+		'tips' => array(
+                  '#type' => 'item',
+		  '#title' => $format->name,
+		  '#value' => theme('filter_tips', _filter_tips($field['default_format'], FALSE)),
+		),
+	      );
             }
             $delta++;
           }
@@ -290,8 +335,24 @@ function text_widget($op, &$node, $field
               '#weight' => $field['widget']['weight'],
             );
           }
-          if ($field['text_processing']) {
+          if ($field['text_processing'] == 1) {
+            $items[$delta]['format'] = $items[$delta]['format'] ? $items[$delta]['format'] : $field['default_format'];
             $form[$field['field_name']][$delta]['format'] = filter_form($items[$delta]['format'], $form[$field['field_name']][$delta]['value']['#weight'] + 1, array($field['field_name'], $delta, 'format'));
+          } else if ($field['text_processing'] == 2) {
+            $formats = filter_formats();
+	    $format = $formats[$field['default_format']];
+            $form[$field['field_name']][$delta]['format'] = array(
+              '#type' => 'fieldset',
+              '#collapsible' => true,
+              '#collapsed' => true,
+              '#title' => t('Input format'),
+
+              'tips' => array(
+                '#type' => 'item',
+                '#title' => $format->name,
+                '#value' => theme('filter_tips', _filter_tips($field['default_format'], FALSE)),
+              ),
+            );
           }
         }
       }
@@ -318,9 +379,25 @@ function text_widget($op, &$node, $field
             '#weight' => $field['widget']['weight'],
           );
         }
-        if ($field['text_processing']) {
+        if ($field['text_processing'] == 1) {
+          $items[0]['format'] = $items[0]['format'] ? $items[0]['format'] : $field['default_format'];
           $form[$field['field_name']][0]['format'] = filter_form($items[0]['format'], $form[$field['field_name']][0]['value']['#weight'] + 1, array($field['field_name'], 0, 'format'));
-        }
+        } else if ($field['text_processing'] == 2) {
+          $formats = filter_formats();
+          $format = $formats[$field['default_format']];
+          $form[$field['field_name']][$delta]['format'] = array(
+            '#type' => 'fieldset',
+            '#collapsible' => true,
+            '#collapsed' => true,
+            '#title' => t('Input format'),
+
+            'tips' => array(
+              '#type' => 'item',
+              '#title' => $format->name,
+              '#value' => theme('filter_tips', _filter_tips($field['default_format'], FALSE)),
+            ),
+          );	  
+	}
       }
       return $form;
 
