Index: sites/all/modules/default_filter/default_filter.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/default_filter/default_filter.module,v
retrieving revision 1.6
diff -u -r1.6 default_filter.module
--- sites/all/modules/default_filter/default_filter.module	24 Aug 2007 18:54:53 -0000	1.6
+++ sites/all/modules/default_filter/default_filter.module	26 Aug 2007 01:10:12 -0000
@@ -1,192 +1,183 @@
 <?php
-
-/**
- * @file
- */
+// $Id$
 
 /**
  * Implementation of hook_help().
  */
-/**
-* Display help and module information
-* @param section which section of the site we're displaying help
-* @return help text for section
-*/
 function default_filter_help($section='') {
+  $output = '';
 
-	$output = '';
-
-	switch ($section) {
-		case "admin/help/default_filter":
-		case "admin/settings/default_filter":
-			$output = '<p>' . t('This module allows you to set default filter formats per node type per role, preventing users from having to tick a checkbox every time they add a new node. This is useful for sites that set the global default to a less rich format to prevent anonymous users from using (for example) full HTML.') . '</p>';
-			$output .= '<p>' . t('To add a new default, select a role, an existing filter format, and a node type from the controls below. You may also wish to specify a weight in order to give some formats preference over others. For example, a user might belong to two roles that you have specified different defaults for. In this case, you would give a lighter weight to the more privileged role, so that its default is returned for that user, while other users who belong only to the less-privileged role would get the heavier default filter.') . '</p>';
-			$output .= '<p>' . t('This module honors filter format privileges. That is, if a given role has not been given permission to use a format in the filter administration section, this module does not override that permission. At present, this happens behind the scenes, and the user interface section does not enforce the prohibition yet. So be sure you have allowed a role to use a given filter prior to adding defaults here.') . '</p>';
-			break;	
-		case "admin/help#default_filter":
-		case "admin/modules/#description":
-			$output = '<p>' . t('This module allows you to specify default filter formats by role.') . '</p>';
-			break;
-	}
-
-	return $output;
+  switch ($section) {
+    case "admin/help/default_filter":
+    case "admin/settings/default_filter":
+      $output = '<p>' . t('This module allows you to set default filter formats per node type per role, preventing users from having to tick a checkbox every time they add a new node. This is useful for sites that set the global default to a less rich format to prevent anonymous users from using (for example) full HTML.') . '</p>';
+      $output .= '<p>' . t('To add a new default, select a role, an existing filter format, and a node type from the controls below. You may also wish to specify a weight in order to give some formats preference over others. For example, a user might belong to two roles that you have specified different defaults for. In this case, you would give a lighter weight to the more privileged role, so that its default is returned for that user, while other users who belong only to the less-privileged role would get the heavier default filter.') . '</p>';
+      $output .= '<p>' . t('This module honors filter format privileges. That is, if a given role has not been given permission to use a format in the filter administration section, this module does not override that permission. At present, this happens behind the scenes, and the user interface section does not enforce the prohibition yet. So be sure you have allowed a role to use a given filter prior to adding defaults here.') . '</p>';
+      break;  
+    case "admin/help#default_filter":
+    case "admin/modules/#description":
+      $output = '<p>' . t('This module allows you to specify default filter formats by role.') . '</p>';
+      break;
+  }
+  return $output;
 }
 
+/**
+ * Implementation of hook_perm().
+ */
 function default_filter_perm(){
-	return array(
-		'administer default filters'
-	);
+  return array('administer default filters');
 }
 
 /**
-  * Settings submit code.
-  */
+ * Settings submit code.
+ */
 function default_filter_admin_submit(){
-	db_query('INSERT INTO {default_filters} (role_id, format_id, node_type, weight) VALUES(%d, %d, "%s", %d)', $_POST['role_id'], $_POST['format_id'], $_POST['node_type'], $_POST['weight']);
-	drupal_set_message(t('Default filter settings saved.'));
+  db_query('INSERT INTO {default_filters} (role_id, format_id, node_type, weight) VALUES(%d, %d, "%s", %d)', $_POST['role_id'], $_POST['format_id'], $_POST['node_type'], $_POST['weight']);
+  drupal_set_message(t('Default filter settings saved.'));
 }
 
 /**
-  * Settings form.
-  */
+ * Settings form.
+ */
 function default_filter_admin(){
-	global $base_url;
-	$form = array();
-
-	$roles = user_roles();
-	$formats = array();
-	foreach(filter_formats() as $key => $format){
-		$formats[$format->format] = $format->name;
-	}
-	$types = array();
-	foreach(node_get_types() as $type){
-		$types[$type->type] = $type->name;
-	}
-
-	$form['new'] = array(
-		'#type'			=> 'fieldset',
-		'#title'		=> t('New Filter Association')
-	);
-	$form['new']['role_id'] = array(
-		'#id'			=> 'role_id',
-		'#type'			=> 'select',
-		'#title'		=> t('Role'),
-		'#options'		=> $roles,
-		'#description'		=> t('Role to set a default format for.')
-	);
-	$form['new']['format_id'] = array(
-		'#id'			=> 'format_id',
-		'#type'			=> 'select',
-		'#title'		=> t('Format'),
-		'#options'		=> $formats,
-		'#description'		=> t('Default format.')
-	);
-	$form['new']['node_type'] = array(
-		'#id'			=> 'node_type',
-		'#type'			=> 'select',
-		'#title'		=> t('Node Type'),
-		'#options'		=> $types,
-		'#description'		=> t('Node type to set a default filter for.')
-	);
-	$form['new']['weight'] = array(
-		'#id'			=> 'weight',
-		'#type'			=> 'textfield',
-		'#title'		=> t('Weight'),
-		'#default_value'	=> 0,
-		'#description'		=> t('Because people can belong to multiple roles, you may need to set a weight to assure that a given format for a given role for a given node type is set as the default before others are evaluated. The topmost grouping will be used. Negative weights rise to the top.')	
-	);
-
-	$output = system_settings_form($form);
-
-	$headers = array(t('Role'), t('Format'), t('Node Type'), t('Weight'), t('Delete'));
-	$rows = array();
-	$result = db_query('SELECT d.id, r.name role, f.name format, d.node_type, d.weight FROM {default_filters} d, {filter_formats} f, {role} r WHERE d.role_id = r.rid AND d.format_id = f.format ORDER BY d.role_id, d.weight ASC, d.format_id');
-	while($row = db_fetch_object($result)){
-		$data[] = array(
-			$row->role,
-			$row->format,
-			$row->node_type,
-			$row->weight,
-			'<a href="' . $base_url . '/admin/settings/default_filter/delete/' . $row->id . '">' . t('Delete') . '</a>'
-		);
-	}
-
-	if(sizeof($data) > 0){
-		$output['existing'] = array(
-			'#type'			=> 'fieldset',
-			'#title'		=> t('Existing Format Associations')
-		);
-		$output['existing']['html'] = array(
-			'#type'			=> 'item',
-			'#value'		=> theme('table', $headers, $data)
-		);
-	}
+  global $base_url;
+  $form = array();
 
-	return $output;
+  $roles = user_roles();
+  $formats = array();
+  foreach(filter_formats() as $key => $format){
+    $formats[$format->format] = $format->name;
+  }
+  $types = array();
+  foreach(node_get_types() as $type){
+    $types[$type->type] = $type->name;
+  }
+
+  $form['new'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('New Filter Association'),
+  );
+  $form['new']['role_id'] = array(
+    '#id' => 'role_id',
+    '#type' => 'select',
+    '#title' => t('Role'),
+    '#options' => $roles,
+    '#description' => t('Role to set a default format for.'),
+  );
+  $form['new']['format_id'] = array(
+    '#id' => 'format_id',
+    '#type' => 'select',
+    '#title' => t('Format'),
+    '#options' => $formats,
+    '#description' => t('Default format.'),
+  );
+  $form['new']['node_type'] = array(
+    '#id' => 'node_type',
+    '#type' => 'select',
+    '#title' => t('Node Type'),
+    '#options' => $types,
+    '#description' => t('Node type to set a default filter for.'),
+  );
+  $form['new']['weight'] = array(
+    '#id' => 'weight',
+    '#type' => 'textfield',
+    '#title' => t('Weight'),
+    '#default_value' => 0,
+    '#description' => t('Because people can belong to multiple roles, you may need to set a weight to assure that a given format for a given role for a given node type is set as the default before others are evaluated. The topmost grouping will be used. Negative weights rise to the top.'),
+  );
+
+  $output = system_settings_form($form);
+
+  $headers = array(t('Role'), t('Format'), t('Node Type'), t('Weight'), t('Delete'));
+  $rows = array();
+  $result = db_query('SELECT d.id, r.name role, f.name format, d.node_type, d.weight FROM {default_filters} d, {filter_formats} f, {role} r WHERE d.role_id = r.rid AND d.format_id = f.format ORDER BY d.role_id, d.weight ASC, d.format_id');
+  while($row = db_fetch_object($result)){
+    $data[] = array(
+      $row->role,
+      $row->format,
+      $row->node_type,
+      $row->weight,
+      '<a href="' . $base_url . '/admin/settings/default_filter/delete/' . $row->id . '">' . t('Delete') . '</a>',
+    );
+  }
+
+  if(sizeof($data) > 0){
+    $output['existing'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Existing Format Associations'),
+    );
+    $output['existing']['html'] = array(
+      '#type' => 'item',
+      '#value' => theme('table', $headers, $data)
+    );
+  }
+  return $output;
 }
 
+/**
+ * Implementation of hook_menu().
+ */
 function default_filter_menu($may_cache){
-	$items[] = array(
-		'path'			=> 'admin/settings/default_filter',
-		'title'			=> t('Default Filter Settings'),
-		'callback'		=> 'drupal_get_form',
-		'callback arguments'	=> 'default_filter_admin',
-		'access'		=> user_access('administer default filters'),
-		'description'		=> t('Specify default filter formats by role.'),
-		'type'			=> MENU_NORMAL_ITEM
-	);
-	if(arg(4)){
-		$items[] = array(
-			'path'		  	=> 'admin/settings/default_filter/delete/' . arg(4),
-			'title'		 	=> t('Delete Default Filter Association'),
-			'callback'	      	=> 'drupal_get_form',
-			'callback arguments'    => array('default_filter_delete', intval(arg(4))),
-			'access'		=> user_access('administer default filters'),
-			'type'		  	=> MENU_NORMAL_ITEM
-		);
-	}
-
-	return $items;
+  $items[] = array(
+    'path' => 'admin/settings/default_filter',
+    'title' => t('Default Filter Settings'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => 'default_filter_admin',
+    'access' => user_access('administer default filters'),
+    'description' => t('Specify default filter formats by role.'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  if(arg(4)){
+    $items[] = array(
+      'path' => 'admin/settings/default_filter/delete/' . arg(4),
+      'title' => t('Delete Default Filter Association'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('default_filter_delete', intval(arg(4))),
+      'access' => user_access('administer default filters'),
+      'type' => MENU_NORMAL_ITEM,
+    );
+  }
+  return $items;
 }
 
 /**
-  * Delete submit
-  */
+ * Delete submit
+ */
 function default_filter_delete_submit(){
-	if($_POST['id'] && $id = intval($_POST['id'])){
-		db_query('DELETE FROM {default_filters} WHERE id = %d', $id);
-		drupal_set_message(t('Filter association deleted.'));
-	}
-	else{
-		drupal_set_message(t('Error deleting association.'));
-	}
-	drupal_goto('admin/settings/default_filter');
+  if($_POST['id'] && $id = intval($_POST['id'])){
+    db_query('DELETE FROM {default_filters} WHERE id = %d', $id);
+    drupal_set_message(t('Filter association deleted.'));
+  }
+  else{
+    drupal_set_message(t('Error deleting association.'));
+  }
+  drupal_goto('admin/settings/default_filter');
 }
 
 /**
-  * Delete form.
-  */
+ * Delete form.
+ */
 function default_filter_delete($id){
-	global $base_url;
-	//$form = array('#action' => '/tell-a-friend', '#method' => 'post');
-	$form = array();
-	$form['id'] = array(
-		'#type'	 	=> 'hidden',
-		'#value'	=> $id
-	);
-	$form['warning'] = array(
-		'#type'	 	=> 'item',
-		'#value'	=> t('Are you sure you wish to delete this association?')
-	);
-	$form['op'] = array(
-		'#type'	 => 'submit',
-		'#value'	=> 'Delete'
-	);
-	$form['cancel'] = array(
-		'#type'	 	=> 'item',
-		'#value'	=> '<a href="' . $base_url . '/admin/settings/default_filter">' . t('Cancel') . '</a>'
-	);
-	return $form;
+  global $base_url;
+  $form = array();
+  $form['id'] = array(
+    '#type' => 'hidden',
+    '#value' => $id,
+  );
+  $form['warning'] = array(
+    '#type' => 'item',
+    '#value' => t('Are you sure you wish to delete this association?'),
+  );
+  $form['op'] = array(
+    '#type' => 'submit',
+    '#value' => 'Delete',
+  );
+  $form['cancel'] = array(
+    '#type' => 'item',
+    '#value' => '<a href="' . $base_url . '/admin/settings/default_filter">' . t('Cancel') . '</a>',
+  );
+  return $form;
 }
 
 
@@ -198,57 +189,58 @@
   * to make the two line up.
   */
 function default_filter_form_alter($form_id, &$form){
-	if($form['#id'] == 'node-form' && array_key_exists('body_filter', $form)){
-		$type = $form['type']['#value'];
-		$format = default_filter_retrieve($type);
-
-		//The following two if statements are the result of an anomaly I never figured
-		//out but needed to code around. My install used "filter" as the key for the
-		//formats, but others' installs used "format". This block looks for each. I've
-		//also added below an if statement to confirm that whichever one gets set exists.
-		$format_key = 'this is not a valid key';
-		if(array_key_exists('filter', $form['body_filter'])){
-			$format_key = 'filter';
-		}
-		if(array_key_exists('format', $form['body_filter'])){
-			$format_key = 'format';
-		}
-
-		if(array_key_exists($format_key, $form['body_filter'])){
-			//Unset any pre-selected filter. If multiples are checked, Gecko
-			//browsers seem to take the last selected but IE takes the first selected.
-			foreach($form['body_filter'][$format_key] as $key => $val){
-			    if (is_numeric($key)) {
-				if (isset($form['body_filter'][$format_key][$key]['#value'])) {
-				    unset($form['body_filter'][$format_key][$key]['#value']);
-				}
-				if (isset($form['body_filter'][$format_key][$key]['#default_value'])) {
-				    unset($form['body_filter'][$format_key][$key]['#default_value']);
-				}
-			    }
-			}
-
-			if($format > 0 && isset($form['body_filter'][$format_key][$format])){
-				$form['body_filter'][$format_key][$format]['#value'] = $format;
-			}
-		}
-	}
+  if($form['#id'] == 'node-form' && array_key_exists('body_filter', $form)){
+    $form['default_filter_passed_form'] = array(
+      '#type' => 'value',
+      '#value' => 1,
+    );
+    $type = $form['type']['#value'];
+    $format = default_filter_retrieve($type);
+
+    //The following two if statements are the result of an anomaly I never figured
+    //out but needed to code around. My install used "filter" as the key for the
+    //formats, but others' installs used "format". This block looks for each. I've
+    //also added below an if statement to confirm that whichever one gets set exists.
+    $format_key = 'this is not a valid key';
+    if(array_key_exists('filter', $form['body_filter'])){
+      $format_key = 'filter';
+    }
+    if(array_key_exists('format', $form['body_filter'])){
+      $format_key = 'format';
+    }
+
+    if(array_key_exists($format_key, $form['body_filter']) && $format && !isset($form['nid']['#value'])){
+      //Unset any pre-selected filter. If multiples are checked, Gecko
+      //browsers seem to take the last selected but IE takes the first selected.
+      foreach($form['body_filter'][$format_key] as $key => $val){
+        if (is_numeric($key)) {
+          if (isset($form['body_filter'][$format_key][$key]['#value'])) {
+            unset($form['body_filter'][$format_key][$key]['#value']);
+          }
+          if (isset($form['body_filter'][$format_key][$key]['#default_value'])) {
+            unset($form['body_filter'][$format_key][$key]['#default_value']);
+          }
+        }
+      }
+      if(isset($form['body_filter'][$format_key][$format])){
+        $form['body_filter'][$format_key][$format]['#default_value'] = $format;
+      }
+    }
+  }
 }
 
-
-
 /**
   * On node insert, get the lightest-weighted default filter for the user's
   * roles and set the node's format to that value. This handles cases in which
   * the user bypasses the node form (e.g. by using a blog editing client).
   */
 function default_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
-	if($op == 'insert'){
-		$format = default_filter_retrieve($node->type);
-		if($format > 0){
-			$node->format = $format;
-		}
-	}
+  if($op == 'insert'){
+    $format = default_filter_retrieve($node->type);
+    if($format && !isset($node->default_filter_passed_form)){
+      db_query('UPDATE {node_revisions} SET format = %d WHERE nid = %d', $format, $node->nid);
+    }
+  }
 }
 
 /**
@@ -256,33 +248,29 @@
   * current user's roles.
   */
 function default_filter_retrieve($type){
-	global $user;
-	$roles = array_keys($user->roles);
+  global $user;
+  $roles = array_keys($user->roles);
 
-	$eligible = array();
-	$eligible_formats = filter_formats();
-	//For each format available to this user...
-	foreach($eligible_formats as $f){
-		//Get the roles...
-		$eligible_roles = array_unique(split(',', $f->roles));
-
-		//Append to the $eligible array a string that groups the format with 
-		//a role that has access to that format. This is basically a flat list
-		//of filters and the roles that can use them.
-		foreach($eligible_roles as $r){
-			$eligible[] = $f->format . '.' . $r;
-		}
-	}
-
-	$format = 0;	
-	//Only return a format if the user belongs to some role (so this may not work for the admin user).
-	if(sizeof($roles) > 0){
-		//Note the CONCAT function. This assures that we don't let users who 
-		//belong to some role access filters that belong to some other role.
-		//$format = db_result(db_query('SELECT format_id FROM {default_filters} WHERE CONCAT(format_id, ".", role_id) IN ("' . join('","', $eligible) . '") AND role_id IN ("' . join('","', $roles) . '") AND node_type = "%s" ORDER BY weight ASC LIMIT 1', $type));
-		$format = db_result(db_query('SELECT format_id FROM {default_filters} WHERE CONCAT(format_id, ".", role_id) IN ("' . join('","', $eligible) . '") AND node_type = "%s" ORDER BY weight ASC LIMIT 1', $type));
-	}
-	return $format;
+  $eligible = array();
+  $eligible_formats = filter_formats();
+  //For each format available to this user...
+  foreach($eligible_formats as $f){
+    //Get the roles...
+    $eligible_roles = array_unique(split(',', $f->roles));
+
+    //Append to the $eligible array a string that groups the format with 
+    //a role that has access to that format. This is basically a flat list
+    //of filters and the roles that can use them.
+    foreach($eligible_roles as $r){
+      $eligible[] = $f->format . '.' . $r;
+    }
+  }
+
+  //Note the CONCAT function. This assures that we don't let users who 
+  //belong to some role access filters that belong to some other role.
+  //$format = db_result(db_query('SELECT format_id FROM {default_filters} WHERE CONCAT(format_id, ".", role_id) IN ("' . join('","', $eligible) . '") AND role_id IN ("' . join('","', $roles) . '") AND node_type = "%s" ORDER BY weight ASC LIMIT 1', $type));
+  $format = db_result(db_query('SELECT format_id FROM {default_filters} WHERE CONCAT(format_id, ".", role_id) IN ("' . join('","', $eligible) . '") AND node_type = "%s" ORDER BY weight ASC LIMIT 1', $type));
+  return $format;
 }
 
 
