diff --git modules/content_profile_registration.module modules/content_profile_registration.module index 537f6d7..4dfecc8 100644 --- modules/content_profile_registration.module +++ modules/content_profile_registration.module @@ -17,21 +17,64 @@ function content_profile_registration_form_alter(&$form, &$form_state, $form_id) // can easily customize the registration form. $default_types = content_profile_get_types('names', (arg(0) == 'admin' ? 'admin_user_create_use' : 'registration_use')); $form += array('#content_profile_registration_use_types' => $default_types); - + $form['#content_profile_merge'] = array('#after_build', '#pre_render'); + $form['#content_profile_hide'] = array(); foreach ($form['#content_profile_registration_use_types'] as $type => $typename) { + $form['#content_profile_hide'][$type] = content_profile_get_settings($type, 'registration_hide'); content_profile_registration_add_profile_form($type, $form, $form_state); } } + else if ($form_id == 'user_profile_form' && $form['_category']['#value'] == 'account') { + require_once drupal_get_path('module', 'node') .'/node.pages.inc'; + $form['#content_profile_hide'] = array(); + // Allow other modules to customize the used profile types, so modules + // can easily customize the registration form. + $default_types = content_profile_get_types('names', 'user_profile_form'); + + $form += array('#content_profile_registration_use_types' => $default_types); + $account = $form['_account']['#value']; + $form['#content_profile_merge'] = array('#after_build', '#pre_render', '#validate', '#submit'); + foreach ($form['#content_profile_registration_use_types'] as $type => $typename) { + $node = content_profile_load($type, $form['#uid']); + // If node has not been created, account owner must be able to create and current user + // must be able to create. + $create_access = empty($node) && node_access('create', $type, $account) && node_access('create', $type); + // If node has been created, user must be able to edit to update + $edit_access = !empty($node) && node_access('update', $node); + if ($create_access || $edit_access) { + $form['#content_profile_hide'][$type] = content_profile_get_settings($type, 'user_profile_hide'); + content_profile_registration_add_profile_form($type, $form, $form_state, $node); + } + else { + unset($form['#content_profile_registration_use_types'][$type]); + } + } + } elseif ($form_id == 'content_profile_admin_settings') { $type = $form_state['type']; // Let other modules add registration child elements before us! $form += array('registration' => array()); + $field_select = _content_profile_registration_get_field_select($type); $form['registration'] += array( '#type' => 'fieldset', '#title' => t('User Registration'), '#description' => t('Customize how this content profile shows up on the user registration page.'), '#collapsible' => TRUE, ); + $form['registration']['user_profile_form'] = array( + '#type' => 'checkbox', + '#title' => t('Use on Account edit'), + '#description' => t('Use this content type on the user edit page'), + '#default_value' => content_profile_get_settings($type, 'user_profile_form'), + ); + + $form['registration']['user_profile_hide'] = array( + '#type' => 'checkboxes', + '#title' => t('Hide form fields'), + '#description' => t('Hide fields from the user profile form. Required fields cannot be hidden and are not shown here.'), + '#options' => $field_select, + '#default_value' => content_profile_get_settings($type, 'user_profile_hide'), + ); $form['registration']['registration_use'] = array( '#type' => 'checkbox', '#title' => t('Use on Registration'), @@ -57,6 +100,7 @@ function content_profile_registration_form_alter(&$form, &$form_state, $form_id) function content_profile_registration_admin_form_submit($form, &$form_state) { $form_state['values']['registration_hide'] = array_keys(array_filter($form_state['values']['registration_hide'])); + $form_state['values']['user_profile_hide'] = array_keys(array_filter($form_state['values']['user_profile_hide'])); } /** @@ -96,6 +140,19 @@ function _content_profile_registration_get_field_select($type) { return $return; } +function content_profile_apply_pre_render(&$form) { + foreach (element_children($form) as $key) { + content_profile_apply_pre_render($form[$key]); + } + if (isset($form['#pre_render'])) { + foreach ($form['#pre_render'] as $function) { + if (function_exists($function)) { + $form = $function($form); + } + } + } +} + /** * Adds in the profile form of the given content type to the registration form * @@ -103,38 +160,57 @@ function _content_profile_registration_get_field_select($type) { * @see content_profile_registration_user_register_validate() * @see content_profile_registration_user_register_submit() */ -function content_profile_registration_add_profile_form($type, &$form, &$form_state) { +function content_profile_registration_add_profile_form($type, &$form, &$form_state, $node = NULL) { // Initialize new node and add in its form. - $node = array('uid' => 0, 'name' => '', 'type' => $type); + if (empty($node)) { + $node = (object) array('uid' => 0, 'name' => '', 'type' => $type); + } // Get the original node form. $node_form = drupal_retrieve_form($type .'_node_form', $form_state, $node); drupal_prepare_form($type .'_node_form', $node_form, $form_state); - $node_form += array('#field_info' => array()); $form_add = array(); + //content_profile_apply_pre_render($node_form); // If non-CCK form elements are hidden, only copy over the CCK stuff - if (in_array('other', content_profile_get_settings($type, 'registration_hide'))) { - foreach ($node_form['#field_info'] as $field_name => $info) { - if (isset($node_form[$field_name])) { - $form_add[$field_name] = $node_form[$field_name]; - } - } + if (in_array('other', $form['#content_profile_hide'][$type])) { + $fields_move = array_merge( + array_keys($node_form['#field_info']), + array( + 'title', + '#node', + 'type', + 'nid', + 'vid', + ) + ); // Copy over any fieldgroups $keys = array_keys($node_form); foreach ($keys as $key) { - if (stristr($key, 'group_')) { + if (in_array($key, $fields_move) || stristr($key, 'group_')) { $form_add[$key] = $node_form[$key]; } } - // Add the title - $form_add['title'] = $node_form['title']; - - // Set this to the values of one node, as it might be need by some #ahah callbacks - $form_add['#node'] = $node_form['#node']; - $form_add['type'] = $node_form['type']; } else { + // Merge in items that need to be merged + $exclude = array( + 'node_form_validate', + 'node_form_submit', + ); + foreach($form['#content_profile_merge'] as $key) { + if (empty($form[$key])) { + $form[$key] = $node_form[$key]; + } + else { + foreach($node_form[$key] as $function) { + if (!in_array($function, $form[$key])) { + $form[$key][] = $function; + } + } + } + $node_form[$key] = array_diff($node_form[$key], $exclude); + } foreach (array('uid', 'name', 'author', 'buttons', 'language', '#theme', 'options') as $key) { unset($node_form[$key]); } @@ -142,7 +218,7 @@ function content_profile_registration_add_profile_form($type, &$form, &$form_sta } // Hide fields as configured - foreach (content_profile_get_settings($type, 'registration_hide') as $field_name) { + foreach ($form['#content_profile_hide'][$type] as $field_name) { if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type, $field_name))) { unset($form_add[$group_name][$field_name]); if (count(element_children($form_add[$group_name])) == 0) { @@ -158,10 +234,17 @@ function content_profile_registration_add_profile_form($type, &$form, &$form_sta $form += array('#field_info' => array()); $form['#field_info'] += $node_form['#field_info']; $form += $form_add; - + // Add in further callbacks needed, if not yet done. - if (!isset($form['#content_profile_weights'])) { + if ($form['form_id']['#value'] == 'user_profile_form') { + $form['#submit'][] = 'content_profile_registration_user_edit_submit'; + $form['#submit'] = array_diff($form['#submit'], array('content_profile_registration_user_register_submit')); + } + elseif (!isset($form['#content_profile_weights'])) { $form['#submit'][] = 'content_profile_registration_user_register_submit'; + } + + if (!isset($form['#content_profile_weights'])) { $form['#validate'][] = 'content_profile_registration_user_register_validate'; $form['#pre_render'][] = 'content_profile_registration_alter_weights'; } @@ -180,6 +263,7 @@ function content_profile_registration_add_profile_form($type, &$form, &$form_sta } } + /** * Pre render callback that makes sure our elements are grouped together. * The ordering in between the single elements is kept. @@ -211,7 +295,7 @@ function content_profile_registration_user_register_validate($form, &$form_state // Make sure there is no user name so we can node_validate unset($node->name); - if (!in_array('other', content_profile_get_settings($type, 'registration_hide'))) { + if (!in_array('other', $form['#content_profile_hide'][$type])) { node_validate($node, $form); } elseif (module_exists('content')) { @@ -266,12 +350,36 @@ function content_profile_registration_user_register_submit($form, &$form_state) } /** + * Submits the user edit form + */ +function content_profile_registration_user_edit_submit($form, &$form_state) { + foreach ($form['#content_profile_registration_use_types'] as $type => $typename) { + if ($node = &$form_state['content_profile_registration'][$type]['node']) { + $current_node = content_profile_load($type, $node->_account->uid); + // Set user's information for the node. + $node->uid = $node->_account->uid; + $node->name = $node->_account->name; + // Update + // Merge in data of current node + $node = (array)$node; + foreach($node as $key => $val) { + $current_node->$key = $val; + } + $current_node = node_submit($current_node); + node_save($current_node); + } + } +} + +/** * Implementation of hook_content_profile_settings(). */ function content_profile_registration_content_profile_settings() { return array( 'registration_use' => FALSE, 'admin_user_create_use' => FALSE, + 'user_profile_form' => FALSE, 'registration_hide' => array(), + 'user_profile_hide' => array(), ); }