Index: relativity.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/relativity/relativity.module,v
retrieving revision 1.41.2.12
diff -r1.41.2.12 relativity.module
50c50
< function relativity_list_possible_children($type='', $parent_nid='') {
---
> function relativity_list_possible_children($type='', $parent_nid='', $is_autocomplete=false, $part) {
54a55,59
>   if($is_autocomplete == true){
>     $part = "%%$part%%";
>   } else {
>     $is_autocomplete = false;
>   }
87a93
>     $sql = array();
89c95,103
<       $result = db_query('SELECT DISTINCT n.nid, t.tid, v.vid FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid INNER JOIN {term_data} v ON t.tid = v.tid WHERE n.type = \'%s\' AND n.status = 1 ORDER BY v.vid, t.tid', $type);
---
>       $sql[0] = 'SELECT DISTINCT n.nid, t.tid, v.vid FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid INNER JOIN {term_data} v ON t.tid = v.tid WHERE n.type = \'%s\' AND n.status = 1 '; 
>       $sql[1] = 'ORDER BY v.vid, t.tid';
>     } else {
>       $sql[0] = 'SELECT n.nid FROM {node} n WHERE n.type = \'%s\' AND n.status = 1 ';
>       $sql[1] = 'ORDER BY n.title';
>     }
>     
>     if ($is_autocomplete) {
>       $result = db_query($sql[0] .' AND n.title LIKE \'%s\' '. $sql[1] .' LIMIT 10', $type, $part);
91c105
<       $result = db_query('SELECT n.nid FROM {node} n WHERE n.type = \'%s\' AND n.status = 1 ORDER BY n.title', $type);
---
>       $result = db_query($sql[0].$sql[1], $type);
92a107
>     
110c125,129
<           $links[$key][] = array('title' => $child_node->title, 'child_node' => $child_node->nid, 'parent_node' => $parent_nid);
---
>           if ($is_autocomplete === false) {
>             $links[$key][] = array('title' => $child_node->title, 'child_node' => $child_node->nid, 'parent_node' => $parent_nid);
>           } else {
>             $links[$child_node->title.' [nid:' . $child_node->nid . ']'] = check_plain($child_node->title);
>           }
112c131,135
<           $links[] = theme('relativity_link', $child_node->title, "addparent/$child_node->nid/parent/$parent_nid", $parent_nid);
---
>           if ($is_autocomplete === false) {
>             $links[] = theme('relativity_link', $child_node->title, "addparent/$child_node->nid/parent/$parent_nid", $parent_nid);
>           } else {
>             $links[$child_node->title.' [nid:' . $child_node->nid . ']'] = check_plain($child_node->title);
>           }
116a140,144
>   if($is_autocomplete !== false){
>     print drupal_to_js($links);
>     exit();
>   }
>   
301a330
>         'callback arguments' => array('','',arg(5),arg(6)),
485,489d513
<   // Node Sorting Options
<   $group['sorting_options'] = array(
<     '#type' => 'fieldset',
<     '#title' => t('Node Sorting Options'),
<   );
497a522,538
>   
>   $group['global_options']['relativity_select_method'] = array(
>     '#type' => 'radios',
>     '#title' => t('How should nodes be selected?'),
>     '#return_value' => 1,
>     '#default_value' => variable_get('relativity_select_method', t('Both')),
>   	'#options' => array(t('List') => t('List'), t('Autocomplete') => t('Autocomplete'), t('Both') => t('Both')),
>     '#description' => '<ul><li>'. t('List') . t(' : A link to a paged list of nodes') .'</li>'
>                       .'<li>'. t('Autocomplete') . t(' : A autocomplete form') .'</li>'
>                       .'<li>'. t('Both') . t(' : The two options together') .'</li>',
>   );
>   
>   // Node Sorting Options
>   $group['sorting_options'] = array(
>     '#type' => 'fieldset',
>     '#title' => t('Node Sorting Options'),
>   );
1232a1274,1284
>  * Implementation of hook_forms()
>  */
> function relativity_forms(){
>   foreach (array_keys(node_get_types()) as $type) {
>     $forms['relativity_autocomplete_child_'.$type]['callback'] = 'relativity_autocomplete_child';
>   }
>   return $forms; 
> }
> 
> 
> /**
1614,1615c1666,1678
<         $items[] = l(t('Attach existing !type', array('!type'=>$type_name)), "relativity/listnodes/$type/parent/$parent_nid", array('class' => 'relativity_attach_' . $type));
<       }
---
>         switch (variable_get('relativity_select_method', t('Both'))) {
>         	case t('List'):
>         	  $items[] = l(t('Attach existing !type (list)', array('!type'=>$type_name)), "relativity/listnodes/$type/parent/$parent_nid", array('class' => 'relativity_attach_' . $type));;
>         	break;        	
>         	case t('Autocomplete'):
>         	  $items[] = drupal_get_form('relativity_autocomplete_child_'.$type, array('type_name'=>$type_name, 'type'=>$type, 'parent_nid'=>$parent_nid));
>         	break;        	
>         	default:
>         	  $items[] = l(t('Attach existing !type (list)', array('!type'=>$type_name)), "relativity/listnodes/$type/parent/$parent_nid", array('class' => 'relativity_attach_' . $type));;
>         	  $items[] = drupal_get_form('relativity_autocomplete_child_'.$type, array('type_name'=>$type_name, 'type'=>$type, 'parent_nid'=>$parent_nid));
>         	break; 
>         }
>       } 
1624a1688,1778
> /**
>  * Defines a form for autocomplete_child.
>  * @param $details array of info for the form
>  * @return array for formAPI
>  */
> function relativity_autocomplete_child($details) {
>   $subform = $details['type'];
>   
>   //Base is needed as the formID is relativity_autocomplete_child_$type 
>   $form['#base'] = 'relativity_autocomplete_child';
>   
>   //Subform is needed to give JS a unique ID when there are more than one autocomplete forms on a page
>   $form[$subform] = array(
>     '#type' => 'form',
>     '#tree' => 'true'
>   );
>   
>   $form[$subform]['relativity_child_name'] = array(
>     '#title' => t('Attach existing !type (auto-complete)', array('!type'=>$details['type_name'])),
>     '#type' => 'textfield',
>     '#autocomplete_path' => 'relativity/listnodes/'.$details['type'].'/parent/'.$details['parent_nid'].'/true',
>     '#description' => t('Type the title of the !type', array('!type'=>$details['type_name']))
>   );
>   $form[$subform]['relativity_child_type'] = array(
>     '#type' => 'hidden',
>     '#value'=> $details['type'],
>   );
>   $form[$subform]['relativity_child_parent_nid'] = array(
>     '#type' => 'hidden',
>     '#value'=> $details['parent_nid'],
>   );
>   $form[$subform]['relativity_submit'] = array(
>     '#type' => 'submit',
>     '#value' => t('Attach').' '.$details['type_name']
>   );
>   
>   return $form;
> }
> 
> /** 
>  * Handle autocomplete_child form submission.
>  */
> function relativity_autocomplete_child_submit($form_id, $form_values) {
>   
>   //find the sub form with the correct data.
>   foreach ($form_values as $data) {
>     if (isset($data['relativity_child_name'])) {
>       $form_data = $data;
>       break;
>     }
>   }
> 
>   //extract nid if it exsists
>   preg_match('/\[nid:(\d*)]/', $form_data['relativity_child_name'], $matches);
>   if ( count($matches) > 1 ) {
>     $nid = $matches[1]; 
>   }
>   else { //no NID set, so try to grab it
>     $node_title = $form_data['relativity_child_name'];
>     $result = db_query("SELECT nid FROM {node} WHERE LOWER(title) LIKE LOWER('%s%%')", $node_title, 0, 10);
>     
>     if (db_num_rows($result) == 1) {
>       $nid = db_result($result);
>     } 
>     else if (db_num_rows($result) > 1) { //too many results
>       $error = t('More than one %type has the title "%title", please select the required node from !link', 
>         array( 
>         	'%type' => $form_data['relativity_child_type_name'],
>         	'%title' => $node_title, 
>         	'!link' => l('this list','relativity/listnodes/'. $form_data['relativity_child_type'] .'/parent/'. $form_data['relativity_child_parent_nid']))
>         );
>     }
>   }
>   
>   if (!$nid){
>     $error = t('No %type found with the title "%title"', 
>       array(
>       	'%type' => $form_data['relativity_child_type_name'],
>       	'%title' => $node_title 
>       )
>     );
>   }
>   
>   if ($error){
>     drupal_set_message($error, 'error');
>   } else {
>     drupal_goto('relativity/addparent/'.$nid.'/parent/'.$form_data['relativity_child_parent_nid']); 
>   }
> }
> 
> 
