Index: signup.module
===================================================================
--- signup.module	(revision 13)
+++ signup.module	(working copy)
@@ -73,13 +73,18 @@
         $signup_info = t('SIGNUP INFORMATION') ."\n\r". $signup_data;
       }
 
+      global $base_url;
       $trans = array(
         '%event' => $event->title,
         '%time' => signup_format_date($event),
         '%username' => $signup->name,
         '%useremail' => $mail_address,
         '%info' => $signup_info,
-        );
+        '%node_url' => "$base_url/node/$event->nid" 
+      );
+
+      $temp = module_invoke_all('signup_email_send', $event, $signup_info, $trans);
+      $trans = empty($temp)? $trans : $temp;
       $message = strtr($event->reminder_email, $trans);
       drupal_mail('signup_reminder_mail', $mail_address, $subject, $message, $from);
       watchdog('signup', t('Reminder for %event sent to %useremail.', array('%event' => $event->title, '%useremail' => $mail_address)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $event->nid));
@@ -297,11 +302,12 @@
 }
 
 function _signup_initialize_event_backend() {
+  define('EVENT_PATH', drupal_get_path('module', 'event'));
   define('SIGNUP_PATH', drupal_get_path('module', 'signup'));
   if (defined('EVENT_API') && EVENT_API == '5.2') {
     include_once(SIGNUP_PATH .'/signup_event_5.x-2.inc');
   }
-  else if (module_exists('event')) {
+  else if (module_exists('event') || EVENT_PATH) {
     include_once(SIGNUP_PATH .'/signup_event_5.x-1.inc');
   }
   else if (module_exists('date')) {
@@ -376,8 +382,8 @@
   }
   else {
     $node = NULL;
-  }
-  
+  }
+   
   $signup_type_default = variable_get('signup_node_default_state_'. $form['type']['#value'], 'disabled');
 
   // If the current user has the global 'administer all signups' permission
@@ -467,15 +473,75 @@
  * @param $form_id The ID of the form being submitted.
  * @param $form_values The constructed form values array of the submitted form.
  */
-function signup_form_submit($form_id, $form_values) {
-  if (isset($form_values['signup_username'])) {
+function signup_form_submit($form_id, $form_values)
+{
+  // if anon user form submited and parametr create user set
+
+  if(isset($form_values['signup_anon_mail']) && variable_get('signup_anon_signup_create_user', 0))
+  {
+    $mail = $form_values['signup_anon_mail'];
+    $user_local = signup_create_new_user($mail); // Create user.
+    $form_values['uid'] = $user_local->uid;
+    unset($form_values['signup_anon_mail']); //Unset for signup_sign_up_user() sign as registeated user.
+  }
+
+  if (isset($form_values['signup_username']))
+  {
     $account = user_load(array('name' => $form_values['signup_username']));
     $form_values['uid'] = $account->uid;
   }
+
+  $form_values =  module_invoke_all('signup_submit', $form_values);
   signup_sign_up_user($form_values);
 }
 
 /**
+ * Create and return a new user.
+ *
+ * User name created from email by getting string before '@' and adding number to it
+ * to make a unique user name.
+ *
+ * @ingroup signup_core
+ * @param string $email  Email for new user.
+ *
+ * @return object user
+ */
+function signup_create_new_user($email)
+{
+  // get user name
+  $tmp = explode('@', $email);
+  $uname = $tmp[0];
+  //check user with this name not exist
+  $user_test = user_load(array('name' => $uname));
+  if ($user_test && $user_test->uid)
+  { //If user with this name exists
+    $num = 1;
+    while (!signup_user_name_free($uname . $num)) {
+      $num++; //add number to user name
+    }
+    $uname = $uname . $num;
+  }
+
+  $form_values = array(
+            'name' => $uname,
+            'pass' => user_password(),
+            'mail' => $email,
+            'notify' => 1,
+  );
+
+  $user_local = user_register_submit('', $form_values); //register user.
+
+  //i did this for a reason -- to re-use notifications
+  //          drupal_execute('user_register', $form_values);
+  $user_local = user_load(array('mail' => $email)); // load user
+  if (!$user_local || !$user_local->uid) {
+    drupal_set_message(t('Could not create new user.'), 'error');
+  }
+
+  return $user_local;
+}
+
+/**
  * Validates the email address on the anonymous user signup form.
  *
  * @param $form
@@ -488,6 +554,25 @@
 }
 
 /**
+ *  Check if user with this name not registered.
+ *
+ *  @param string $name User name to check
+ *
+ *  @return bool true if not register, else false
+ */
+function signup_user_name_free($name)
+{
+  $user_local = user_load(array('name' => $name));
+  if ($user_local && $user_local->uid)
+  {
+    return false;
+  }
+  return true;
+}
+
+
+
+/**
  * Validates the username on the admin form to signup another user.
  *
  * @param $form
@@ -522,7 +607,7 @@
 function signup_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   global $form_values;
   $signup_type_default = variable_get('signup_node_default_state_'. $node->type, 'disabled');
-  
+
   switch ($op) {
     case 'insert':
       if (isset($form_values['signup_enabled'])) {
@@ -669,9 +754,9 @@
           $node->content['signup'] = array(
             '#value' => $output,
             '#weight' => 10,
-          );
-        }
-      }
+             );
+         }
+       }
       break;
   }
 }
@@ -804,21 +889,34 @@
 
 function _signup_print_current_signup($node, $signup_info) {
   $form_data = unserialize($signup_info->form_data);
+
+  $form_data = module_invoke_all('signup_before_display', $node, $form_data);
+
   $header = array(array('data' => t('Your signup information'), 'colspan' => 2));
   $rows = array();
   if (is_array($form_data)) {
     $rows += signup_build_signup_data($form_data, 'table');
   }
+
+  //$rows = module_invoke_all('signup_after_display', $rows);
+
   $output = '';
-  if (!empty($rows)) {
+  if (!empty($rows))
+  {
     $output .= theme('table', $header, $rows);
   }
+
+  $temp = module_invoke_all('signup_view_info');
+
+  $output .= implode(" ", $temp);
+
   if (user_access('cancel own signups')) {
     $output .= drupal_get_form('signup_form_cancel', $node);
   }
   return $output;
 }
 
+
 /**
  * @defgroup signup_callback
  *   Functions which are the menu callbacks for this module.
@@ -902,17 +1000,54 @@
       unset($signup_themed_form['signup_form_data']['Name']['#default_value']);
     }
   }
+
   $signup_form += $signup_themed_form;
-  
+
   $form['collapse']['signup_user_form'] = $signup_form;
   $form['collapse']['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Sign up'),
   );
+
   return $form;
 }
 
 /**
+ * Builder function for the signup admin anonymous user create from email form
+ * @ingroup signup_callback
+ *
+ * @param $node
+ *   The fully loaded node object.
+ * @param $signup_type
+ *   Determines what kind of signup to generate a form for. Possible values:
+ *    'auth' -- regular authenticated user signup form
+ *    'anon' -- anonymous user signup form (includes required email field).
+ *    'admin' -- admin form to signup another user (includes user selector).
+ * @param $fieldset
+ *   Boolean that indicates if the signup form should be in a fieldset.
+ */
+function signup_form_anon_admin($node, $fieldset = TRUE)
+{
+  $temp = signup_form($node, 'anon', $fieldset = TRUE); // get anon form
+  $temp['collapse']['#title'] = t('Sign up anonymous user'); // Set new title
+  if (!empty($temp['collapse']['signup_user_form']['signup_form_data']['Name']['#default_value'])) {
+    unset($temp['collapse']['signup_user_form']['signup_form_data']['Name']['#default_value']);
+		} //  Empty username field;
+		return $temp;
+}
+
+function signup_form_anon_admin_validate($form_id, $form_values)
+{
+  $form_values['#value'] = $form_values['signup_anon_mail']; // prepare $form_values for validate.
+  signup_form_validate_anon($form_values, $form_values[$nid]);
+}
+
+function signup_form_anon_admin_submit($form_id, &$form_values)
+{
+  signup_form_submit($form_id, &$form_values);
+}
+
+/**
  * Builder function for the cancel signup form
  * @ingroup signup_callback
  */
@@ -984,7 +1119,7 @@
   $form['header']['#value'] = $header;
 
   $sql .= tablesort_sql($header);
-  
+  
   $result = pager_query($sql, 25, 0, $sql_count);
 
   // Loop through the signup nodes, and generate our form elements
@@ -1229,6 +1364,21 @@
     '#description' => t('If the signup form is included at the bottom of each node, the signup form will be encapsulated in a collapsible fieldset. This setting controls if that fieldset is expanded or collapsed by default.'),
   );
 
+  $form['adv_settings']['signup_anon_signup_create_user'] = array(
+    '#title' => t('Create new user when anonymous user signup.'),
+    '#type' => 'checkbox',    
+    '#default_value' => variable_get('signup_anon_signup_create_user', 0),
+    '#description' => t('If the anonymous user signup create new user based on entered email.'),
+  );
+
+  $form['adv_settings']['signup_offer_login'] = array(
+    '#title' => t('Offer to login for anonymous user.'),
+    '#type' => 'checkbox',    
+    '#default_value' => variable_get('signup_offer_login', 0),
+    '#description' => t('Offer to login for anonymous user if entered email has been registred on site.'),
+  );
+
+
   // Use our own submit handler, so we can do some processing before
   // we hand control to system_settings_form_submit.
   $form['#submit']['signup_settings_page_submit'] = array();
@@ -1381,19 +1531,25 @@
 
     // This is not used for web display, only email, so the output is
     // not being sanitized and linked as we would for web output.
+    global $base_url;
     $trans = array(
       "%event" => $node->title,
       "%time" => $starttime,
       "%username" => $user->name,
       "%useremail" => $user_mail,
-      "%info" => $signup_data
+      "%info" => $signup_data,
+  	  '%node_url' => "$base_url/node/$node->nid" 
     );
+
     $from = variable_get('site_mail', 'noadmin@noadmin.com');
 
     // If a confirmation is to be sent, compose the mail message,
     // translate the string substitutions, and send it.
     if ($node->signup_send_confirmation && $user_mail) {
       $subject = t('Signup confirmation for event: !event', array('!event' => $node->title));
+
+      $temp = module_invoke_all('signup_email_send', $node, $signup_info, $trans);
+      $trans = empty($temp)? $trans : $temp;
       $message = strtr($node->signup_confirmation_email, $trans);
       drupal_mail('signup_confirmation_mail', $user_mail, $subject, $message, $from);
     }
@@ -1454,8 +1610,10 @@
     $form_data = unserialize($signed_up_user->form_data);
 
     // Compose the user data.
+
+    $form_data = module_invoke_all('signup_before_display', $node, $form_data, $signed_up_user);
+
     $signup_form_data = signup_build_signup_data($form_data);
-
     // The username and the unique form identifier are different for
     // anon signups and registered user signups.  For registered users,
     // provide a link to the user profile, and use the uid as the
@@ -1470,6 +1628,10 @@
       $id = $signed_up_user->uid;
     }
 
+
+    //$signup_form_data = implode(' ', module_invoke_all('signup_after_display', &$signup_form_data));
+     
+    //$signup_form_data = $signup_form_data[0];
     // Build the row for this user.
     $rows[] = array(
       $username .'<br />'. gmdate(variable_get('signup_date_string', 'M jS, g:i A'), $signed_up_user->signup_time + $offset),
@@ -1477,11 +1639,14 @@
       drupal_get_form('signup_user_cancel_form_'. $id, $id, $node->nid, $signed_up_user->uid, $signed_up_user->anon_mail)
     );
   }
+
   $output .= theme('table', $header, $rows);
 
   // Add a form to allow the administrator to signup other users.
   $output .= drupal_get_form('signup_form', $node, 'admin');
+  $output .= drupal_get_form('signup_form_anon_admin', $node);
 
+
   return $output;
 }
 
@@ -1667,7 +1832,16 @@
     $message = 'Invalid email address entered for signup.';
   }
   else if (db_num_rows(db_query("SELECT mail FROM {users} WHERE mail = '%s'", $anon_mail))) {
-    $message = 'The email address entered belongs to a registered user.';
+		  global $user;
+		  global $base_url;
+		  if (variable_get('signup_offer_login', 0) && ($user->uid == 0))
+		  {
+		    $login = "<a href='$base_url/user/login/?destination=node/$nid/signup'>login</a>";
+		    $message = "You have attempted to sign up with an email address that belongs to a registered user. <br>
+						  Please $login before trying to sign up.<br> You will be returned to this page after logging in.";	  	
+		  }else{
+		    $message = 'The email address entered belongs to a registered user.';
+		  }
   }
   else if (db_num_rows(db_query("SELECT anon_mail FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $anon_mail, $nid))) {
     $message = 'The email address entered has already been used to sign up for this event.';
@@ -1698,7 +1872,7 @@
  * @ingroup signup_internal
  */
 function _signup_admin_form($node) {
-  $signup_token_description = t('Supported string substitutions: %event, %time, %username, %useremail, %info (user signup information).');
+  $signup_token_description = t('Supported string substitutions: %event, %time, %username, %useremail, %info, %node_url (user signup information).');
 
   // Load the default admin form data for new nodes.
   if (!$node || !$node->signup) {
@@ -1795,7 +1969,13 @@
           $rows[] = array($key .':', check_plain($value));
         }
       }
+
+      $temp = $rows;
+      $rows = array();
+      $temp = module_invoke_all('signup_after_display', &$temp);
+      $rows += $temp;
       return $rows;
+       
     case 'output':
       $output = '';
       // Loop through each first level element.
@@ -1808,6 +1988,7 @@
           $output .= '<div>'. $key .': '. check_plain($value) .'</div>';
         }
       }
+      $output = implode(' ', module_invoke_all('signup_after_display', &$output));
       return $output;
     case 'email':
       $output = '';
@@ -1821,6 +2002,7 @@
           $output .= $value ."\n\r";
         }
       }
+      $output = implode(' ', module_invoke_all('signup_after_display', &$output));
       return $output;
   }
 }
@@ -1843,7 +2025,7 @@
     return $form;
   }
 
-  $tokens = array('%event', '%username', '%useremail');
+  $tokens = array('%event', '%username', '%useremail', '%node_url');
   if (function_exists('signup_extra_tokens')) {
     $tokens = array_merge($tokens, signup_extra_tokens());
   }
@@ -1870,7 +2052,7 @@
     '#type' => 'value',
     '#value' => $node->nid,
   );
-  
+
   global $user;
   if (user_access('administer site configuration')) {
     $form['from'] = array(
@@ -1919,15 +2101,18 @@
     $event = node_load($form_values['nid']);
     foreach ($addresses as $signup) {
       $mail_address = $signup->anon_mail ? $signup->anon_mail : $signup->mail;
+      global $base_url;
       $trans = array(
         '%event' => $event->title,
         '%username' => $signup->uid ? $signup->name : variable_get('anonymous', t('Anonymous')),
         '%useremail' => $mail_address,
-//        '%info' => $signup_info,
+      // '%info' => $signup_info,
+        '%node_url' => "$base_url/node/$event->nid" 
       );
 
       $trans['%time'] = signup_format_date($event);
-
+      $temp = module_invoke_all('signup_email_send', $event, $signup_info, $trans);
+      $trans = empty($temp)? $trans : $temp;
       $message = strtr($form_values['message'], $trans);
       drupal_mail('signup_broadcast_mail', $mail_address, $subject, $message, $from);
       watchdog('signup', t('Broadcast email for %event sent to %email.', array('%event' => $event->title, '%email' => $mail_address)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $event->nid));
@@ -1968,4 +2153,4 @@
 function signup_views_default_views() {
   require_once(drupal_get_path('module', 'signup') .'/signup_views.inc');
   return _signup_views_default_views();
-}
+}
\ No newline at end of file
