Index: modules/content_profile_registration.module =================================================================== --- modules/content_profile_registration.module (revision 279) +++ modules/content_profile_registration.module (working copy) @@ -7,15 +7,71 @@ */ /** + * Implementation of hook_user + */ +function content_profile_registration_user($op, &$edit, &$account, $category = NULL) { + // trick user module to show the basic input forms (name, mail, pass) + // into a fieldset + if($op == 'register') { + return array( + 'content_profile_registration_dummy_form' => array( + '#type' => 'fieldset', + '#title' => 'Dummy fieldset' + ) + ); + } +} + +/** * Implementation of hook_form_alter(). */ function content_profile_registration_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'user_register' && module_exists('content')) { + //unset the dummy fieldset + unset($form['content_profile_registration_dummy_form']); + require_once drupal_get_path('module', 'node') .'/node.pages.inc'; - $profile_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use')); + // If path module exist, translate url from alias. + if (module_exists('path')) { + $path = drupal_get_path_alias($_GET['q']); + $arg = explode('/',drupal_get_path_alias($path)); + } + else { + // Otherwise use standard one. + $arg = arg(); + } + // Get last argument of URI path. + $last_arg = $arg[count($arg)-1]; + // If additional registration arg is set, then filter profile types fields to the correct one. + if (count($arg)>1 && $last_arg) { + $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; + } + } + } + // Otherwise return default user registration form from variable + // set in content profile registration settings. + if (empty($profile_types)) { + $default_content_profile = variable_get('default_content_profile', 0); + if ($default_content_profile == '0'){ + $profile_types = array(); + } + 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) { + if (!user_access(strtolower("create $type content"))) { // if user don't have permission to add current content type, don't show the content type form + continue; + } // Initialize new node: $node = array('uid' => 0, 'name' => '', 'type' => $type); // Get the node form @@ -64,6 +120,15 @@ '#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'), + ); $form['registration']['admin_user_create_use'] = array( '#type' => 'checkbox', '#title' => t('Use on administrative user creation form'), @@ -152,18 +217,41 @@ */ function content_profile_registration_user_register_submit($form, &$form_state) { foreach (content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use')) as $type => $typename) { - 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; - - // Create the node. - $node = node_submit($node); - node_save($node); - // 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")); + if (module_exists('autoassignrole')) { + $path = drupal_get_path_alias($_GET['q']); + // Check if AAR knows about the current path. + $page = db_fetch_object(db_query("SELECT rid FROM {autoassignrole_page} WHERE path = '%s'", $path)); + $enabled = variable_get('autoassignrole_content_profile_'. $type, 0); // ...should shown current content type + } + else { + // if module not enabled, enable all content types. + $page->rid = $enabled = 0; + } + // Check if user have permission to add current content type, if yes - then add it. + if ($enabled == $page->rid) { + 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')) { + // Run node_autotitle (if it's new node, it will contain only name of patterns). + auto_nodetitle_set_title($node); + } + node_save($node); + if ($node->nid) { + // Load and save again (to replace title with proper data with auto_nodetitle module). + $node = node_load($node->nid); + node_save($node); + } + // 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")); + } } } } @@ -174,8 +262,69 @@ */ function content_profile_registration_content_profile_settings() { return array( + 'registration_url' => NULL, 'registration_use' => FALSE, 'admin_user_create_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', ''); +} + +/** + * Add an 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); +} \ No newline at end of file