Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.21
diff -u -r1.21 form.inc
--- includes/form.inc	23 Nov 2005 16:21:12 -0000	1.21
+++ includes/form.inc	28 Nov 2005 20:21:32 -0000
@@ -189,6 +189,97 @@
 }
 
 /**
+ * Create an option list consisting of numbers.
+ *
+ * @param int $first
+ * @param int $last
+ * @param int $step
+ * @param string $format
+ *   Format (printf-style) of option labels.
+ * @param bool $blank
+ *   Whether to prepend an empty option.
+ * @return array
+ *   Options as value => title pairs.
+ */
+function form_options_numeric_list($first, $last, $step = 1, $format = '%d', $blank = false) {
+  $options = $blank ? array('' => '') : array();
+  for ($i = $first; $first > $last? $i >= $last: $i <= $last; $i += $step) {
+    $options[$i] = sprintf($format, $i);
+  }
+  return $options;
+}
+
+/**
+ * Get options through DB query. 
+ *
+ * @param string $query
+ *   DB query with table names enclosed in {}s (like: "{node}"). 
+ *   The query should return one or two columns.
+ * @param bool $blank
+ *   Whether to prepend an empty option.
+ * @return array
+ *   Options as value => title pairs.
+ */
+function form_options_query($query, $blank = false) {
+  $options = $blank ? array('' => '') : array();
+  $result = db_query($query);
+  while ($row = db_fetch_array($result)) {
+    $row = array_values($row);
+    if (count($row)==1) {
+      $row[1] = $row[0];
+    }
+    $options[$row[0]] = $row[1];
+  }
+  return $options;
+}
+
+/**
+ * Get options from MySQL DB enum- or set-type column definition.
+ *
+ * @param string $table
+ * @param string $field
+ * @param bool $blank
+ *   Whether to prepend an empty option.
+ * @param bool $caps
+ *   If true, capitalize the first letter of each option label.
+ * @param bool $spaces
+ *   If true, convert underscores to spaces in each option label.
+ * @return array
+ *   Options as value => title pairs.
+ */
+function form_options_enum_field($table, $field, $blank = false, $caps = false, $spaces = true) {
+  global $db_type;
+  if (substr($db_type, 0, 5) != 'mysql') {
+    return array();
+  }
+  $options = $blank ? array('' => '') : array();
+  $result = db_query("SHOW COLUMNS FROM {%s} LIKE \"%s\"", $table, $field);
+  $result = db_fetch_array($result);
+  if ($result["Type"]) {
+    preg_match("/((set|enum)\((.*?)\))/", $result["Type"], $values);
+    $values = explode(',', $values['3']);
+    $values = str_replace(',', '', $values);
+    foreach($values as $v) {
+      $options[$v] = substr($v, 1, -1);
+    }
+    if ($spaces) {
+      foreach ($options as $key => $c) {
+        $options[$key] = str_replace('_', ' ', $c);
+      }
+    }
+    if ($caps) {
+      foreach ($options as $key=>$c) {
+        $options[$key] = ucfirst($c);
+      }
+    }
+    return $options;
+  }
+  else {
+    return array();
+  }
+}
+
+/**
  * File an error against a form element. If the name of the element is
  * edit[foo][bar] then you may pass either foo or foo][bar as $name
  * foo will set an error for all its children.
