Index: filter_default.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filter_default/filter_default.info,v
retrieving revision 1.1
diff -u -p -r1.1 filter_default.info
--- filter_default.info	12 Sep 2007 20:40:39 -0000	1.1
+++ filter_default.info	13 Sep 2007 12:45:56 -0000
@@ -1,3 +1,5 @@
 ; $Id: filter_default.info,v 1.1 2007/09/12 20:40:39 bjaspan Exp $
 name = Filter Default
 description = Allows each role to have a default input format for nodes and comments.
+dependencies = filter
+package = Content
Index: filter_default.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filter_default/filter_default.module,v
retrieving revision 1.4
diff -u -p -r1.4 filter_default.module
--- filter_default.module	12 Sep 2007 20:58:16 -0000	1.4
+++ filter_default.module	13 Sep 2007 12:43:32 -0000
@@ -18,41 +18,46 @@ function filter_default_menu($may_cache)
 }
 
 function filter_default_admin_defaults_form() {
-  $form = array();
-
-  $roles = array(0 => '') + user_roles();
+  drupal_set_title(t('Default input format per role'));
+  
+  $form    = array();
+  
+  $roles   = array(0 => '') + user_roles();
   $formats = filter_formats();
   foreach ($formats as $format) {
     $format_list[$format->format] = $format->name;
   }
 
   $form['help'] = array(
-    '#value' => t('<p>You may assign a default input format for each role on your site.  Whenever a text input box with multiple allowed input formats is displayed, the initially selected input format will be set based on the lowest-weighted role (as defined below) that the user posseses.  For a user that does not have any role indicated in this table, the default input format will be the one selected on the %list_link.</p><p>Note that this does <em>not</em> prevent the user from selecting any other input format they are authorized to use; it only sets the initially selected format.</p>',
-      array('%list_link' => l('list tab', 'admin/filters'))));
-
-  for ($i = 1; $i < count($roles); $i++) {
-    list($def_role, $def_format) = variable_get('filter_default_'.$i,
-      array());
+    '#value' => t('<p>You may assign a default input format for each role on your site.  Whenever a text input box with multiple allowed input formats is displayed, the initially selected input format will be set based on the lowest-weighted role (as defined below) that the user posseses. For a user that does not have any role indicated in this table, the default input format will be the one selected on the !list_link.</p>
+      <p>Note that this does <em>not</em> prevent the user from selecting any other input format they are authorized to use; it only sets the initially selected format.</p>', array('!list_link' => l('list tab', 'admin/settings/filters')))
+  );
+  
+  for ($i = 1; $i < count($roles); ++$i) {
+    list($def_role, $def_format) = variable_get('filter_default_'. $i, array());
 
     $form['role_'.$i] = array(
       '#type' => 'select',
       '#options' => $roles,
-      '#default_value' => $def_role);
+      '#default_value' => $def_role,
+    );
     $form['format_'.$i] = array(
       '#type' => 'select',
       '#options' => $format_list,
-      '#default_value' => $def_format);
+      '#default_value' => $def_format,
+    );
   }
   $form['submit'] = array(
     '#type' => 'submit', 
-    '#value' => t('Save default roles'));
-
+    '#value' => t('Save default roles'),
+  );
+  
   return $form;
 }
 
 function theme_filter_default_admin_defaults_form($form) {
-  $roles = user_roles();
-
+  $roles  = user_roles();
+  
   $header = array(t('Weight'), t('Role'), t('Default input format'));
 
   $rows = array();
@@ -67,54 +72,52 @@ function theme_filter_default_admin_defa
   $output .= drupal_render($form['help']);
   $output .= theme('table', $header, $rows);
   $output .= drupal_render($form);
+  
   return $output;
 }
 
 function filter_default_admin_defaults_form_validate($form_id, $form) {
   $roles = user_roles();
   $formats = filter_formats();
+  $default_format = variable_get('filter_default_format', 1);
 
-  for ($i = 1; $i < count($roles)+1; $i++) {
-    if ($form['role_'.$i] > 0 &&
-      (strpos($formats[$form['format_'.$i]]->roles,
-        ','.$roles[$form['role_'.$i]].',') === FALSE &&
-        strpos($formats[$form['format_'.$i]]->roles,
-          ','.$form['role_'.$i].',') === FALSE)) {
-      form_set_error('format_'.$i,
-        t('Role %role is not allowed to use format %format.',
-          array('%role' => $roles[$form['role_'.$i]],
-            '%format' => $formats[$form['format_'.$i]]->name)));
+  for ($i = 1, $ii = count($roles); $i <= $ii; ++$i) {
+    if ($form['role_'. $i] > 0) { 
+      $allowed_roles = explode(',', $formats[$form['format_'. $i]]->roles);
+      if (!in_array($form['role_'. $i], $allowed_roles) && $form['format_'. $i] != $default_format) {
+        form_set_error('format_'. $i, t('Role %role is not allowed to use format %format.', array(
+          '%role' => $roles[$form['role_'. $i]],
+          '%format' => $formats[$form['format_'. $i]]->name,
+        )));
+      }
     }
   }
 }
 
 function filter_default_admin_defaults_form_submit($form_id, $form) {
   $roles = user_roles();
-  for ($i = 1; $i < count($roles)+1; $i++) {
+  for ($i = 1, $ii = count($roles); $i <= $ii; ++$i) {
     if ($form['role_'.$i] > 0) {
-      variable_set('filter_default_'.$i,
-        array($form['role_'.$i], $form['format_'.$i]));
-    } else {
+      variable_set('filter_default_'. $i, array($form['role_'. $i], $form['format_'. $i]));
+    }
+    else {
       variable_del('filter_default_'.$i);
     }
   }
 }
   
 function _filter_default_form_alter_filters($format_new, &$form) {
-  if (isset($form['#validate']) && is_array($form['#validate']) &&
-    array_key_exists('filter_form_validate', $form['#validate'])) {
-    
+  if (is_array($form['#validate']) && array_key_exists('filter_form_validate', $form['#validate'])) {
     foreach (element_children($form) as $el) {
       // Until admin/settings/filters is submitted,
       // filter_default_format is not set.  Assume Filtered HTML.
-      $default = variable_get('filter_default_format', 1);
-      if ($form[$el]['#type'] == 'radio' && 
-        $form[$el]['#default_value'] == $default) {
+      $default = variable_get('filter_default_format', FILTER_FORMAT_DEFAULT);
+      if ($form[$el]['#type'] == 'radio' && $form[$el]['#default_value'] == $default) {
         $form[$el]['#default_value'] = $format_new;
       }
     }
   }
-  if (is_array($form)) {
+  if (is_array($form) && is_array(element_children($form))) {
     foreach (element_children($form) as $el) {
       _filter_default_form_alter_filters($format_new, $form[$el]);
     }
@@ -148,3 +151,4 @@ function filter_default_form_alter($form
     }
   }
 }
+
