--- relatedcontent.module	2007-04-01 19:07:39.000000000 -0300
+++ relatedcontent_new.module	2007-06-30 10:15:42.000000000 -0300
@@ -57,7 +57,13 @@ function theme_relatedcontent_form_alter
   if (isset($form['title']) && is_array($form['title'])) {
     foreach (element_children($form['title']) as $nid) {
       $row = array();
-      $row[] = drupal_render($form['nodes'][$nid]);
+	  if (isset($form['nodes'][$nid]))
+	  {
+      	$row[] = drupal_render($form['nodes'][$nid]);
+	  } else if (isset($form['sel_nodes'][$nid]))
+	  {
+      	$row[] = drupal_render($form['sel_nodes'][$nid]);
+	  }
       $row[] = drupal_render($form['title'][$nid]);
       $row[] = drupal_render($form['name'][$nid]);
       $row[] = drupal_render($form['created'][$nid]);
@@ -85,10 +91,308 @@ function theme_relatedcontent_form_alter
  * Implementation of hook_menu().
  */
 function relatedcontent_menu($may_cache) {
+  $items = array();
   if (!$may_cache) {
     $base = drupal_get_path('module', 'relatedcontent'); 
     drupal_add_css($base .'/relatedcontent.css');
+	if (arg(0) == 'node' && is_numeric(arg(1))) {
+	  $node = node_load(arg(1));
+	  if ($node->nid) {
+		$items[] = array(
+			  'path' => 'node/' . arg(1) . '/relatedcontent',
+			  'title' => t('Related Content'),
+			  'callback' => '_related_content_edit_form',
+		  	  'callback arguments' => array($node),
+			  'access' => true,
+			  'type' => MENU_LOCAL_TASK,
+			  'weight' => 2
+		  );
+	  }
+	}	
+  }
+  return $items;
+}
+
+function _related_content_edit_form($node) {
+  $output = '<div id="related-content-div">';
+  $output .= drupal_get_form('_relatedcontent_edit_form_upload', $node);
+  $output .= theme('pager');
+  $output .= '</div>';
+
+  return $output;
+}
+
+function _relatedcontent_edit_form_upload_submit($form_id, $form_values) {
+
+	// validate action
+	$invalid_action = true;
+	$valid_actions = array( 'add_content', 'remove_content', 'filter_title', 'clear_filter' );
+	foreach($valid_actions as $action) {
+		if (isset($form_values[$action])) {
+			$invalid_action = false;
+			break;
+		}
+	}
+
+	if ($invalid_action) {
+		return;
+	}
+
+	// validate current node ID
+	$current_nid 	= $form_values['current_nid'];
+	if ($current_nid == null || ! is_numeric( $current_nid) ||
+		 $current_nid <= 0 ) {
+		return;
+	}
+
+	// execute action
+	switch( $action ) {
+
+		// add selected nodes to related content
+		case 'add_content':
+			// get list of selected nodes
+			$node_list 		= array();
+			foreach($form_values['nodes'] as $nid) {
+				if ($nid) {
+					$node_list[] = $nid;
+				}
+			}
+
+			// update database
+			if (count($node_list) > 0) {
+				_relatedcontent_db_insert( $current_nid, $node_list );
+			}
+			break;
+
+		// remove selected nodes from related content
+		case 'remove_content':
+			// get list of selected nodes
+			$node_list 		= array();
+			foreach($form_values['sel_nodes'] as $nid) {
+				if ($nid) {
+					$node_list[] = $nid;
+				}
+			}
+
+			// update database
+			if (count($node_list) > 0) {
+				_relatedcontent_db_delete( $current_nid, $node_list );
+			}
+			break;
+
+		// set node filter
+		case 'filter_title':
+			$_SESSION['related_content_filter_search']		= $form_values['search'];
+			$_SESSION['related_content_filter_node_type']	= $form_values['node_type'];
+			break;
+
+		// clear node filter
+		case 'clear_filter':
+			$_SESSION['related_content_filter_search'] 		= "";
+			$_SESSION['related_content_filter_node_type']	= -1;;
+			break;
+	}
+
+}
+
+function _relatedcontent_edit_form_upload($node) {
+
+  // save current node id for future reference
+  $current_nid = $node->nid;
+
+  // Get the name of the view to feed the assembler.
+  $view_name = _relatedcontent_get_view($node->type);
+
+  // If the view name is empty, RelatedContent is disabled.
+  if (empty($view_name)) {
+		return;
+	}
+  
+  // Get the selected nodes
+  $selected_nodes = _relatedcontent_db_load($node->nid);
+
+  // Add selected nodes to form
+  $form['selected_content']  = array(
+    '#type' 					=> 'fieldset',
+    '#weight' 				=> 0,
+    '#collapsible' 		=> false,
+    '#collapsed' 			=> empty($selected_nodes),
+    '#title' 					=> t('Related content'),
+    '#description' 		=> t('This is the list of related content to this <em>' . $node->type . '</em>.'),
+  );
+  
+  foreach($selected_nodes as $nid) {
+    $node = node_load($nid);
+    $sel_nodes[$node->nid] = '';
+    $form['selected_content']['title'][$node->nid] = array('#value' => l( substr($node->title,0,30), 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
+    $form['selected_content']['name'][$node->nid] =  array('#value' => node_get_types('name', $node));
+    $form['selected_content']['created'][$node->nid] = array('#value' => format_date($node->created, 'small'));
+    $form['selected_content']['username'][$node->nid] = array('#value' => theme('username', $node));
+  }
+
+  $form['selected_content']['#theme'] = 'relatedcontent_form_alter_node';
+
+  // Form for selecting nodes to be added.
+  $form['selected_content']['sel_nodes'] = array(
+    '#type' 					=> 'checkboxes',
+    '#options' 				=> $sel_nodes,
+    '#default_value' 	=> array()
+  );
+
+  // Add a button to remove selected nodes
+  $form['selected_content']['remove']	= array(
+  	'#type'						=> 'submit',
+		'#name'						=> 'remove_content',
+		'#id'							=> 'remove_content',
+		'#value'					=> t('Remove checked content')
+  );
+
+ 
+ 	// fiter box
+  $form['filter_box'] = array(
+    '#type' 					=> 'fieldset',
+    '#weight' 				=> 1,
+    '#collapsible' 		=> false,
+    '#collapsed' 			=> false,
+    '#title' 					=> t('Content Filter'),
+  );
+
+  $form['filter_box']['search'] = array(
+    '#type' 					=> 'textfield',
+    '#title' 					=> t('Search in title'),
+		'#size'						=> 50,
+		'#maxlength'			=> 30,
+		'#default_value'	=> $_SESSION['related_content_filter_search'],
+    '#description' 		=> t('Type part of the title here to find nodes with that title only.')
+  );
+
+  $node_types = node_get_types();
+  $node_types_list = array( '-1' => 'Any node type');
+  foreach( $node_types as $node)
+  {
+  	$node_types_list[$node->type] = $node->name;
+  }
+  $form['filter_box']['node_type'] = array(
+    '#type' 					=> 'select',
+    '#title' 					=> t('Filter by Node Type'),
+		'#default_value'	=> isset($_SESSION['related_content_filter_node_type']) ? $_SESSION['related_content_filter_node_type'] : -1,
+		'#options'				=> $node_types_list,
+    '#description' 		=> t( 'Filter nodes by content type to narrow your search.')
+  );
+
+
+  // clear and submit butons
+  $form['filter_box']['search_go']	= array(
+  	'#type'						=> 'submit',
+		'#name'						=> 'filter_title',
+		'#id'							=> 'filter_title',
+		'#value'					=> t('Filter')
+  );
+
+  $form['filter_box']['clear_filter']	= array(
+  	'#type'						=> 'submit',
+		'#name'						=> 'clear_filter',
+		'#id'							=> 'clear_filter',
+		'#value'					=> t('Clear filter')
+  );
+
+  // Filtered nodes
+  $form['relatedcontent'] = array(
+    '#type' 				=> 'fieldset',
+    '#weight' 			=> 2,
+    '#collapsible' 	=> false,
+    '#collapsed' 		=> empty($selected_nodes),
+    '#title' 				=> t('Filtered content'),
+    '#description' 	=> t(
+      'Select nodes to be added to this <em>'. $node->type .'</em>. For more information, see the !help.',
+      array('!help' => l(t('help page'), 'admin/help/relatedcontent'))
+    )
+  );
+
+  // Load the view that will provide the node list.
+  $view_obj = views_get_view($view_name);
+
+  // Handle the case that the view has been removed.
+  if (!isset($view_obj)) {
+
+    // Set an error message
+    $message = t(
+		  'RelatedContent uses the view "!view" as its source of nodes. But it is no longer avaiable. Please, <a href="!settings-url">select another view</a>.',
+		  array(
+		    '!view' => $view_name,
+				'!views-url' => url('admin/build/views/add'),
+				'!settings-url' => url("admin/content/types/$type"),
+				)
+    );
+    watchdog('view not found', $message, WATCHDOG_ERROR);
+    drupal_set_message($message, 'error');
+
+    // Present an explanation
+    $form['relatedcontent']['#description'] = $message;
+
+    // Nothing more to do.
+    return;
   }
+
+  // Get the number of nodes to be shown on each page.
+  $limit = _relatedcontent_get_length($node->type);
+
+  // filter nodes by title and node type
+  if (isset($_SESSION['related_content_filter_search']) && strlen(trim($_SESSION['related_content_filter_search'])) > 0 ) {
+    $search_string = trim($_SESSION['related_content_filter_search']);
+	views_view_add_filter($view_obj, 'node', 'title', 'contains', $search_string, '');
+  }
+
+  if (isset($_SESSION['related_content_filter_node_type']) && strlen(trim($_SESSION['related_content_filter_node_type'])) > 0 &&
+      $_SESSION['related_content_filter_node_type'] != '-1') {
+    $search_string = trim($_SESSION['related_content_filter_node_type']);
+	views_view_add_filter($view_obj, 'node', 'type', '=', $search_string, '');
+  }
+
+  // Let views module make the query.
+  views_sanitize_view($view_obj);
+
+  $view = views_build_view('result', $view_obj, array(), true, 15); 
+  $result = $view['result'];
+
+  // Form for selecting nodes to be added.
+  while ($node = db_fetch_object($result)) {
+  	if (isset($selected_nodes[$node->nid]))
+		continue;
+    $node = node_load($node->nid);
+    $nodes[$node->nid] = '';
+    $form['relatedcontent']['title'][$node->nid] = array('#value' => l( substr($node->title,0,30), 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
+    $form['relatedcontent']['name'][$node->nid] =  array('#value' => node_get_types('name', $node));
+    $form['relatedcontent']['created'][$node->nid] = array('#value' => format_date($node->created, 'small'));
+    $form['relatedcontent']['username'][$node->nid] = array('#value' => theme('username', $node));
+  }
+
+  $form['relatedcontent']['nodes'] = array(
+    '#type' => 'checkboxes',
+    '#options' => $nodes,
+    '#default_value' => $selected_nodes
+  );
+
+  // Theme the table.
+  $form['relatedcontent']['#theme'] = 'relatedcontent_form_alter_node';
+
+  // save current node_id
+  $form['relatedcontent']['current_nid']	= array(
+  	'#type'						=> 'hidden',
+		'#name'						=> 'current_nid',
+		'#id'							=> 'current_nid',
+		'#value'					=> $current_nid
+  );
+
+  // Agregar boton para agregar seleccionados
+  $form['relatedcontent']['add']	= array(
+  	'#type'						=> 'submit',
+		'#name'						=> 'add_content',
+		'#id'							=> 'add_content',
+		'#value'					=> t('Add checked to related content')
+  );
+
+  return $form;
 }
 
 /**
@@ -98,9 +402,6 @@ function relatedcontent_form_alter($form
   if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
     _relatedcontent_form_alter_node_type($form);
   }
-  elseif (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
-    _relatedcontent_form_alter_node($form);
-  }
 }
 
 /**
@@ -197,98 +498,6 @@ function _relatedcontent_available_views
 
 
 /******************************************************************************
- * ALTER NODE FORM
- ******************************************************************************/
-
-/**
- * Add RelatedContent to node forms of enabled content types. 
- */
-function _relatedcontent_form_alter_node(&$form) {
-
-  // Get the node type.
-  $type = $form['type']['#value'];
-
-  // Get the name of the view to feed the assembler.
-  $view_name = _relatedcontent_get_view($type);
-
-  // If the view name is empty, RelatedContent is disabled.
-  if (empty($view_name)) return;
-  
-  // Get the selected nodes
-  $selected_nodes = $form['#node']->nodes;
-  
-  // RelatedContent's fieldset
-  $form['relatedcontent'] = array(
-    '#type' => 'fieldset',
-    '#collapsible' => true,
-    '#collapsed' => empty($selected_nodes),
-    '#title' => t('Related content'),
-    '#description' => t(
-      'Select nodes to be added to this <em>'. $type .'</em>. For more information, see the !help.',
-      array('!help' => l(t('help page'), 'admin/help/relatedcontent'))
-    ),
-  );
-  
-  // Load the view that will provide the node list.
-  $view = views_get_view($view_name);
-
-  // Handle the case that the view has been removed.
-  if (!isset($view)) {
-
-    // Set an error message
-    $message = t(
-		  'RelatedContent uses the view "!view" as its source of nodes. But it is no longer avaiable. Please, <a href="!settings-url">select another view</a>.',
-		  array(
-		    '!view' => $view_name,
-				'!views-url' => url('admin/build/views/add'),
-				'!settings-url' => url("admin/content/types/$type"),
-				)
-    );
-    watchdog('view not found', $message, WATCHDOG_ERROR);
-    drupal_set_message($message, 'error');
-
-    // Present an explanation
-    $form['relatedcontent']['#description'] = $message;
-
-    // Nothing more to do.
-    return;
-
-  }
-
-  // Get the number of nodes to be shown on each page.
-  $limit = _relatedcontent_get_length($type);
-
-  // Let views module make the query.
-  $view = views_build_view('result', $view, array(), $limit > 0, $limit);
-  $result = $view['result'];
-
-  // Form for selecting nodes to be added.
-  while ($node = db_fetch_object($result)) {
-    $node = node_load($node->nid);
-    $nodes[$node->nid] = '';
-    $form['relatedcontent']['title'][$node->nid] = array('#value' => l($node->title, 'node/'. $node->nid) .' '. theme('mark', node_mark($node->nid, $node->changed)));
-    $form['relatedcontent']['name'][$node->nid] =  array('#value' => node_get_types('name', $node));
-    $form['relatedcontent']['created'][$node->nid] = array('#value' => format_date($node->created, 'small'));
-    $form['relatedcontent']['username'][$node->nid] = array('#value' => theme('username', $node));
-  }
-  $form['relatedcontent']['nodes'] = array(
-    '#type' => 'checkboxes',
-    '#options' => $nodes,
-    '#default_value' => $selected_nodes
-  );
-
-    // Theme the table.
-    $form['relatedcontent']['#theme'] = 'relatedcontent_form_alter_node';
-
-}
-
-function _relatedcontent_list_view(&$form) {
-  
-}
-
-
-
-/******************************************************************************
  * IMPLEMENTING THE NODE API
  ******************************************************************************/
 
@@ -367,8 +576,14 @@ function _relatedcontent_db_load($nid) {
 /**
  * Delete nid of included nodes in database.
  */
-function _relatedcontent_db_delete($nid) {
-  db_query("DELETE FROM {relatedcontent} WHERE nid = %d", $nid);
+function _relatedcontent_db_delete($nid, $related_nid_list = null) {
+  if ($related_nid_list)
+  {
+  	foreach( $related_nid_list as $related_nid )
+  	  db_query("DELETE FROM {relatedcontent} WHERE nid = %d and include_nid = %d", $nid, $related_nid);
+  } else {
+  	db_query("DELETE FROM {relatedcontent} WHERE nid = %d", $nid);
+  }
 }
 
 
@@ -527,12 +742,12 @@ function _relatedcontent_help() {
     <ol>
       <li>
         <p>
-          Locate and open the collapsible section called <em>Related content</em>.
+          Click the new tab called <em>Related content</em> to access the related contents tabs.
         </p>
       </li>
       <li>
         <p>
-          Check the checkboxes to the left of the nodes whose teasers are going to be shown.
+          There are two lists of nodes and a filter. The top list shows nodes which are related to the current node. You can remove nodes by clicking on their corresponding checkboxes and then on the <em>Remove checked content</em> button. To add content use the bottom list to select the nodes and then click <em>Add checked to related content</em>. Finally you can use the filter to further limit the list of nodes shown on the bottom half of the screen.
         </p>
       </li>
     </ol>
@@ -647,3 +862,5 @@ EOT;
 	return t($help, array('!version' => $version, '!year' => $year));
 	
 }
+
+// vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:
