--- content_profile_registration.module.org	2009-01-07 11:02:28.000000000 +0000
+++ content_profile_registration.module	2009-02-04 12:00:50.234208500 +0000
@@ -13,7 +13,35 @@
   // Note: We don't add the registration form to the admin create user form.
   if ($form_id == 'user_register' && module_exists('content') && arg(0) != 'admin') {
     require_once drupal_get_path('module', 'node') .'/node.pages.inc';
-    $profile_types = content_profile_get_types('names', 'registration_use');
+
+    if (module_exists('path')) { // if path module exist, translate url from alias
+	$path = drupal_get_path_alias($_GET['q']);
+	$arg = explode('/',drupal_get_path_alias($path));
+    } else {
+	$arg = arg(); // otherwise use standard one
+    }
+    $last_arg = $arg[count($arg)-1]; // get last argument of URI path
+    if (count($arg)>1 && $last_arg) { // if additional registration arg is set, then filter profile types fields to the correct one
+	$profile_types_ = content_profile_get_types('names', 'registration_use');
+	// check through each profile_types for the registration arg
+	foreach ($profile_types_ as $k => $v){
+	    if ($k == _content_profile_registration_tidy_arg2($last_arg)){
+		$profile_types = array();
+		$profile_types[$k] = $v;
+		break;
+	    }
+	}
+    }
+    if (empty($profile_types)) { // otherwise return default user registration form from variable set in content profile registration settings
+	$default_content_profile = variable_get('default_content_profile', 0);
+	if ($default_content_profile == '0'){
+	    $profile_types = content_profile_get_types('names', 'registration_use');
+	} else { // use data for default content profile type
+	    $profile_types_ = content_profile_get_types('names', 'registration_use');
+	    $profile_types = array($default_content_profile => $profile_types_[$default_content_profile]);
+	}
+    }
+
     $form += array('#field_info' => array());
 
     foreach ($profile_types as $type => $typename) {
@@ -65,6 +93,14 @@
         '#description' => t('Use this content type on the user registration page'),
         '#default_value' => content_profile_get_settings($type, 'registration_use'),
       );
+	// TODO: this should only be writable when 'use on registration form' is active - jquery to the rescue? - or should this be a sub input of registration_use ?
+	// how to display the registration url to the user?
+      $form['registration']['registration_url'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Use URL argument to select this content type on registration.'),
+        '#description' => t('If selected, this content type can be created from the url:') . base_path() . 'user/register/' . str_replace(variable_get('trim_text', ''), '', $form_state['type']),	// need to use url trimming here...
+        '#default_value' => content_profile_get_settings($type, 'registration_url'),
+      );
       $options = _content_profile_registration_get_field_select($type, FALSE);
       if (!empty($options)) {
         $form['registration']['registration_hide'] = array(
@@ -147,20 +183,37 @@
  */
 function content_profile_registration_user_register_submit($form, &$form_state) {
   foreach (content_profile_get_types('names', 'registration_use') as $type => $typename) {
+    if (module_exists('autoassignrole')) { // if autoassignrole module enabled...
+	$path = drupal_get_path_alias($_GET['q']);
+	$page = db_fetch_object(db_query("SELECT rid FROM {autoassignrole_page} WHERE path = '%s'", $path)); // check if current page...
+	$enabled = variable_get('autoassignrole_content_profile_'. $type, 0);  // ...should shown current content type
+    } else {
+	$page->rid = $enabled = 0; // if module not enabled, enable all content types
+    }
+   if ($enabled == $page->rid) { // check if user have permission to add current content type, if yes - then add it
     if ($node = &$form_state['content_profile_registration'][$type]['node']) {
       // Set user's information for the node.
       $node->title = $form_state['user']->name;
       $node->uid = $form_state['user']->uid;
       $node->name = $form_state['user']->name;
+      $node->type = $type;
 
       // Create the node.
       $node = node_submit($node);
+      if (module_exists('auto_nodetitle')) {
+	  auto_nodetitle_set_title($node); // run node_autotitle (if it's new node, it will contain only name of patterns
+      }
       node_save($node);
+      if ($node->nid) {
+         $node = node_load($node->nid); // load again
+         node_save($node); // to save it again (to replace title with proper data with auto_nodetitle module)
+      }
       // Give us a nice log message.
       if ($node->nid) {
         watchdog('content', 'Content Profile: added %user %type upon registration.', array('%user' => $node->name, '%type' => $type), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
       }
     }
+   }
   }
 }
 
@@ -169,7 +222,62 @@
  */
 function content_profile_registration_content_profile_settings() {
   return array(
+    'registration_url' => NULL, 
     'registration_use' => FALSE,
     'registration_hide' => array(),
   );
 }
+
+/**
+ *	Tidy up arg(2) from user/register/foo to be used for deciding what content profile fields to spit out on registration form
+ *	Convert to lowercase, remove non-alphanumeric chars (except for - and _) so it looks pretty as a url!
+ *	remove all non alphanumerical and _ chars - would transliteration need to be taken into consideration?
+*/
+function _content_profile_registration_tidy_arg2($arg2){
+	return strtolower(
+		preg_replace('#([^a-z0-9_]+)#i', '_', $arg2)
+	) . variable_get('append_text', '');
+}
+
+/**
+ * I need to add an admin menu for content_profile_registration to allow the admin user to be able to:
+ * 	choose whether user/register is the default user registration form or whether it should have a custom content-type as the base registration form
+ *	choose whether a word should be filtered from url - ie. I use foobar_profile as the machine name for all my content profile types, and this would mean that user/register/foobar-profile would be needed to trigger the correct fields
+*/
+function content_profile_registration_menu() {
+  $items = array();
+
+	$items['admin/settings/content-profile/registration'] = array(
+		'title' => 'Content Profile Registration',
+		'description' => 'Configure Content Profile Registration',
+		'page callback' => 'drupal_get_form',
+		'access callback' => 'user_access',
+		'page arguments' => array('content_profile_registration_admin_settings', $type),
+		'access arguments' => array('administer nodes'),
+		'type' => MENU_DEFAULT_LOCAL_TASK,
+		'weight' => 0,
+	);
+	return $items;
+}
+
+/**
+ * Menu callback; content profile registration settings.
+ */
+function content_profile_registration_admin_settings(&$form_state, $type) {
+  $form_state['type'] = $type;
+
+  $form['default_content_profile'] = array(
+    '#type' => 'select',
+		'#options' => array_merge(array(0 => t('None')), content_profile_get_types('names', 'registration_use')),
+    '#title' => t('Default Registration Content Profile'),
+    '#description' => t('Select the default content type profile that will be used by default. Choose \'None\' to use the default Drupal registration form.'),
+		'#default_value' => variable_get('default_content_profile', 0),
+  );
+  $form['append_text'] = array(
+    '#type' => 'textfield',
+    '#title' => t("Append a constant string to the end of the registration url argument."),
+    '#description' => t('If you have appended a constant string to the end of your profile content types machine names, enter it here, and it will automatically be added to the end of the argument used on the registration form. i.e. If the machine names for your content profile types end in \'_profile\', enter \'_profile\' here, and the url http://www.example.com/user/register/foobar will use the machine type foobar_profile.'),
+    '#default_value' => variable_get('trim_text',''),
+  );
+    return system_settings_form($form);
+}
