Setting up send to a friend for non-members

Was working on some modifications for the Print module's send to a friend functionality. In this case we have a legacy database that has several years worth of site registrations. If a user isn't registered for the site yet we want to send them an additional paragraph explaining how to get the registration code for the site.

By doing a hook_form_alter() on the send as email form it's possible to extract the submit function for the form and have our own processor handle it. Here's a way to handle splitting the members and non-members up for mailing.

#000000">#0000BB"><?php
#007700">function #0000BB">cali_print_mail_form_submit#007700">(#0000BB">$form_id#007700">, #0000BB">$form_values#007700">) {
 
#0000BB">$to_array #007700">= array();
 
#0000BB">$to_members #007700">= array();
 
#0000BB">$to_nonmembers #007700">= array();
  if (!
#0000BB">array_key_exists#007700">(#DD0000">'cancel'#007700">, #0000BB">$form_values#007700">)) {
  if (!empty(
#0000BB">$form_values#007700">[#DD0000">'fld_from_name'#007700">])) {
   
#0000BB">$from #007700">= #DD0000">'"'#007700">. #0000BB">$form_values#007700">[#DD0000">'fld_from_name'#007700">] .#DD0000">'" <'#007700">. #0000BB">$form_values#007700">[#DD0000">'fld_from_addr'#007700">] .#DD0000">'>'#007700">;
  }
  else {
   
#0000BB">$from #007700">= #0000BB">$form_values#007700">[#DD0000">'fld_from_addr'#007700">];
  }
 
#0000BB">$to #007700">= #0000BB">$form_values#007700">[#DD0000">'txt_to_addrs'#007700">];
 
#FF8000">// Split $to into members and non-members array
 
#0000BB">$to_array #007700">= #0000BB">explode#007700">(#DD0000">','#007700">, #0000BB">$to#007700">);
  foreach (
#0000BB">$to_array #007700">as #0000BB">$key #007700">=> #0000BB">$value#007700">) {
   
#0000BB">$result #007700">= #0000BB">db_query#007700">(#DD0000">'SELECT lr.uid from {legacy_registration} lr where lr.email = "%s"'#007700">, #0000BB">trim#007700">(#0000BB">$value#007700">));
   
#0000BB">$count #007700">= #0000BB">db_num_rows#007700">(#0000BB">$result#007700">);
    if (
#0000BB">$count#007700">) {
     
#0000BB">$to_members#007700">[] = #0000BB">$value#007700">;
    }
    else {
     
#0000BB">$to_nonmembers#007700">[] = #0000BB">$value#007700">;
    }
  }
 
#0000BB">$params #007700">= array();
 
#0000BB">$params#007700">[#DD0000">'subject'#007700">] = #0000BB">$form_values#007700">[#DD0000">'fld_subject'#007700">];
 
#0000BB">$params#007700">[#DD0000">'message'#007700">] = #0000BB">$form_values#007700">[#DD0000">'txt_message'#007700">];
 
#0000BB">$params#007700">[#DD0000">'path'#007700">] = #0000BB">$form_values#007700">[#DD0000">'path'#007700">];
 
#0000BB">$params#007700">[#DD0000">'cid'#007700">] = isset(#0000BB">$form_values#007700">[#DD0000">'cid'#007700">]) ? #0000BB">$form_values#007700">[#DD0000">'cid'#007700">] : #0000BB">NULL#007700">;
 
#0000BB">$params#007700">[#DD0000">'teaser'#007700">] = #0000BB">$form_values#007700">[#DD0000">'chk_teaser'#007700">];
 
#0000BB">$to_members #007700">= #0000BB">string#007700">(#0000BB">implode#007700">(#DD0000">', '#007700">, #0000BB">$to_members#007700">));
 
#0000BB">$to_nonmembers #007700">= #0000BB">string#007700">(#0000BB">implode#007700">(#DD0000">', '#007700">, #0000BB">$to_nonmembers#007700">));
 
 
#FF8000">//send to members frist then we can modify the message
 
#0000BB">print_mail_mail#007700">(#DD0000">'sendpage'#007700">, #0000BB">$message#007700">, #0000BB">$params#007700">);
 
#0000BB">$ret_members #007700">= #0000BB">drupal_mail#007700">(#DD0000">'print_mail_sendpage'#007700">, #0000BB">$to_members#007700">, #0000BB">$message#007700">[#DD0000">'subject'#007700">], #0000BB">$message#007700">[#DD0000">'body'#007700">], #0000BB">$from#007700">, #0000BB">$message#007700">[#DD0000">'headers'#007700">]);
 
#FF8000">//send to non-members
 
#0000BB">$params#007700">[#DD0000">'message'#007700">] = #DD0000">"You have received a message for a for the super website." #007700">.
                       
#DD0000">"You need to go to your site registrar to get a registration key.\n\n" #007700">. #0000BB">$params#007700">[#DD0000">'message'#007700">];
 
#0000BB">print_mail_mail#007700">(#DD0000">'sendpage'#007700">, #0000BB">$message#007700">, #0000BB">$params#007700">);
 
#0000BB">$ret_nonmembers #007700">= #0000BB">drupal_mail#007700">(#DD0000">'print_mail_sendpage'#007700">, #0000BB">$to_nonmembers#007700">, #0000BB">$message#007700">[#DD0000">'subject'#007700">], #0000BB">$message#007700">[#DD0000">'body'#007700">], #0000BB">$from#007700">, #0000BB">$message#007700">[#DD0000">'headers'#007700">]);
 
 
  if (
#0000BB">$ret_members #007700">|| #0000BB">$ret_nonmembers#007700">) {
   
#0000BB">flood_register_event#007700">(#DD0000">'print_mail'#007700">);
   
#0000BB">watchdog#007700">(#DD0000">'print_mail'#007700">, #0000BB">t#007700">(#DD0000">'%name [%from] sent %page to [%to]'#007700">, array(#DD0000">'%name' #007700">=> #0000BB">$form_values#007700">[#DD0000">'fld_from_name'#007700">], #DD0000">'%from' #007700">=> #0000BB">$form_values#007700">[#DD0000">'fld_from_addr'#007700">], #DD0000">'%page' #007700">=> #0000BB">$form_values#007700">[#DD0000">'path'#007700">], #DD0000">'%to' #007700">=> #0000BB">$to#007700">)));
   
#0000BB">$site_name #007700">= #0000BB">variable_get#007700">(#DD0000">'site_name'#007700">, #0000BB">t#007700">(#DD0000">'us'#007700">));
   
#0000BB">$print_mail_text_confirmation #007700">= #0000BB">variable_get#007700">(#DD0000">'print_mail_text_confirmation'#007700">, #0000BB">t#007700">(#DD0000">'Thank you for spreading the word about !site.'#007700">));
   
#0000BB">drupal_set_message#007700">(#0000BB">t#007700">(#0000BB">$print_mail_text_confirmation#007700">, array(#DD0000">'!site' #007700">=> #0000BB">$site_name#007700">)));

   

#0000BB">$nodepath #007700">= #0000BB">drupal_get_normal_path#007700">(#0000BB">$params#007700">[#DD0000">'path'#007700">]);
   
#0000BB">db_query#007700">(#DD0000">"UPDATE {print_mail_page_counter} SET sentcount = sentcount + %d, sent_timestamp = %d WHERE path = '%s'"#007700">, #0000BB">count#007700">(#0000BB">split#007700">(#DD0000">','#007700">, #0000BB">$to#007700">)), #0000BB">time#007700">(), #0000BB">$nodepath#007700">);
  }
  }
  return
#0000BB">preg_replace#007700">(#DD0000">'!^book/export/html/!'#007700">, #DD0000">'node/'#007700">, #0000BB">$form_values#007700">[#DD0000">'path'#007700">]);
}
#0000BB">?>

Category: 

1 Comment