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	7 Jan 2008 21:55:33 -0000
@@ -1,3 +1,4 @@
 ; $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.
+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	7 Jan 2008 22:47:52 -0000
@@ -12,12 +12,14 @@ function filter_default_menu($may_cache)
       'type' => MENU_LOCAL_TASK,
       'weight' => 0.5,
       'access' => user_access('administer filters'),
-      );
+    );
   }
   return $items;
 }
 
 function filter_default_admin_defaults_form() {
+  drupal_set_title(t('Default input format per role'));
+  
   $form = array();
 
   $roles = array(0 => '') + user_roles();
@@ -27,26 +29,28 @@ function filter_default_admin_defaults_f
   }
 
   $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'))));
+    '#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 <a href="!list_link">list tab</a>.</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' => url('admin/filters'))));
 
   for ($i = 1; $i < count($roles); $i++) {
-    list($def_role, $def_format) = variable_get('filter_default_'.$i,
-      array());
+    list($def_role, $def_format) = variable_get('filter_default_'. $i, array());
 
-    $form['role_'.$i] = array(
+    $form['role_'. $i] = array(
       '#type' => 'select',
       '#options' => $roles,
-      '#default_value' => $def_role);
-    $form['format_'.$i] = array(
+      '#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'));
-
+    '#type' => 'submit',
+    '#value' => t('Save default roles'),
+  );
+  
   return $form;
 }
 
@@ -56,79 +60,83 @@ function theme_filter_default_admin_defa
   $header = array(t('Weight'), t('Role'), t('Default input format'));
 
   $rows = array();
-  for ($i = 1; $i < count($roles)+1; $i++) {
+  for ($i = 1, $ii = count($roles); $i <= $ii; $i++) {
     $row = array();
     $row[] = $i;
-    $row[] = drupal_render($form['role_'.$i]);
-    $row[] = drupal_render($form['format_'.$i]);
+    $row[] = drupal_render($form['role_'. $i]);
+    $row[] = drupal_render($form['format_'. $i]);
     $rows[] = $row;
   }
 
   $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', FILTER_FORMAT_DEFAULT);
 
-  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++) {
+    // Skip empty role selects.
+    if ($form['role_'. $i] > 0) { 
+      // Fetch role ids that are allowed to use the selected format.
+      $allowed_roles = explode(',', $formats[$form['format_'. $i]]->roles);
+      // Throw an error, if selected role is not allowed to use selected format
+      // and the selected format is not the default format.
+      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++) {
-    if ($form['role_'.$i] > 0) {
-      variable_set('filter_default_'.$i,
-        array($form['role_'.$i], $form['format_'.$i]));
-    } else {
-      variable_del('filter_default_'.$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_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 (isset($form['#validate']) && 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);
+      $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]);
     }
   }
 }
-  
+
 /**
  * Determines whether the form is creating something new.
- *
  */
 function _filter_default_is_new($form_id, $form) {
   switch ($form['#id']) {
     case 'comment-form':
       return empty($form['cid']['#value']);
+    case 'block-admin-configure':
+      return empty($form['delta']['#value']);
     case 'node-form':
       return empty($form['nid']['#value']);
   }
@@ -139,8 +147,8 @@ function filter_default_form_alter($form
   if (_filter_default_is_new($form_id, $form)) {
     global $user;
     $roles = user_roles();
-    for ($i = 1; $i < count($roles)+1; $i++) {
-      list($role, $format) = variable_get('filter_default_'.$i, array());
+    for ($i = 1, $ii = count($roles); $i <= $ii; $i++) {
+      list($role, $format) = variable_get('filter_default_'. $i, array());
       if (array_key_exists($role, $user->roles)) {
         _filter_default_form_alter_filters($format, $form);
         break;
@@ -148,3 +156,4 @@ function filter_default_form_alter($form
     }
   }
 }
+
