profile2_regpath.module in Profile2 Registration Path 7.2
Same filename and directory in other branches
Attach profile2 form to registration form according to path.
File
profile2_regpath.moduleView source
<?php
/**
* @file
* Attach profile2 form to registration form according to path.
*/
// @todo Create tests
// @todo Make profile2_regpath settings exportable via CTools.
/**
* Implements hook_menu().
*/
function profile2_regpath_menu() {
$items = array();
// @todo Load profiles GROUP BY path ORDER BY weight ASC.
$reg_paths = profile2_regpath_get_reg_paths();
if ($reg_paths) {
// Set menu items for each registration path.
foreach ($reg_paths as $key => $value) {
$path = $value->path;
// We need to load multiple profile types here, because more than one
// profile can be assigned to a given regpath. Profile type with lightest
// weight is given precedence when choosing labels.
if ($profile_types = profile2_regpath_get_profiles($path)) {
// Add profile-specific administrative 'add user' page.
$items['admin/people/p2rp-create/' . $path] = array(
'title' => 'Add user (' . $profile_types[0]->label . ' profile)',
'page callback' => '_profile2_regpath_user_register',
'page arguments' => array(
'profiles' => $profile_types,
),
'access arguments' => array(
'administer users',
),
'type' => MENU_LOCAL_ACTION,
'file' => 'profile2_regpath.inc',
);
// We will use hook_menu_alter() to deal with the 'user' path later.
if ($path != 'user') {
$misc = unserialize($profile_types[0]->misc);
// Just set default values for this in the install file? I don't like checking.
if (isset($misc['path_type']) ? $path_type = $misc['path_type'] : ($path_type = 'separate')) {
}
if ($path_type != 'tabs') {
$registration_path = $path . '/register';
}
else {
$registration_path = 'user/' . $path . '/register';
}
$items[$registration_path] = array(
'title' => isset($misc['tab_text']) && trim($misc['tab_text']) != '' ? $misc['tab_text'] : t('Register as @profile_type', array(
'@profile_type' => $profile_types[0]->label,
)),
'page callback' => '_profile2_regpath_user_register',
'page arguments' => array(
'profiles' => $profile_types,
),
'access callback' => 'user_register_access',
'file' => 'profile2_regpath.inc',
'type' => MENU_LOCAL_TASK,
);
if ($path_type != 'tabs') {
$items[$path] = array(
'title' => 'Log in',
'page callback' => '_profile2_regpath_user_login',
'page arguments' => array(
'profiles' => $profile_types,
),
'access callback' => 'user_is_anonymous',
'file' => 'profile2_regpath.inc',
'menu_name' => 'user-menu',
'type' => MENU_CALLBACK,
);
$items[$path . '/login'] = array(
'title' => 'Log in',
'page callback' => '_profile2_regpath_user_login',
'page arguments' => array(
'profiles' => $profile_types,
),
'access callback' => 'user_is_anonymous',
'file' => 'profile2_regpath.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[$path . '/password'] = array(
'title' => 'Request new password',
'type' => MENU_LOCAL_TASK,
'page callback' => '_profile2_regpath_user_password',
'page arguments' => array(
'profiles' => $profile_types,
),
'access callback' => 'user_is_anonymous',
'file' => 'profile2_regpath.inc',
);
}
}
}
}
}
return $items;
}
/**
* Implements hook_menu_alter().
*/
function profile2_regpath_menu_alter(&$items) {
// Check to see if the default 'user' path is being used with Profile2.
if ($user_paths = profile2_regpath_get_profiles('user')) {
// Build form at user/register using _profile2_regpath_user_register().
$items['user/register']['page callback'] = '_profile2_regpath_user_register';
$items['user/register']['page arguments'] = array(
'profiles' => $user_paths,
);
$items['user/register']['file'] = 'profile2_regpath.inc';
$items['user/register']['file path'] = drupal_get_path('module', 'profile2_regpath');
return $items;
}
}
/**
* Implements hook_permission().
*/
function profile2_regpath_permission() {
$permissions = array(
'administer profile2_regpath' => array(
'title' => t('Administer Profile2 registration paths'),
'description' => t('Enable and configure unique registration paths per Profile2 profile type.'),
),
);
return $permissions;
}
/**
* Implements hook_form_FORM_ID_alter() for the registration form.
*/
function profile2_regpath_form_profile2_type_form_alter(&$form, &$form_state, $form_id) {
module_load_include('inc', 'profile2_regpath', 'profile2_regpath.admin');
_profile2_regpath_form_profile2_type_form_alter($form, $form_state, $form_id);
}
/**
* Implements hook_profile2_type_delete().
*/
function profile2_regpath_profile2_type_delete($type) {
// Delete table entries for deleted profile2 profile type.
db_delete('profile2_regpath')
->condition('profile_id', $type->id)
->execute();
}
/**
* Implements hook_form_alter().
*/
function profile2_regpath_form_alter(&$form, &$form_state, $form_id) {
$menu_item = menu_get_item();
// Check to see if we're building this form for a block.
if (strpos($form_id, 'profile2_regpath_form_block_') === 0) {
module_load_include('inc', 'profile2_regpath');
$profile_types = profile2_regpath_regpath_load_multiple(array(
'profile_type' => $form_state['profile_type'],
'status' => 1,
));
profile2_regpath_attach_profile_fields($form, $form_state, $form_id, $profile_types);
}
elseif ($form_id == 'user_register_form') {
// Logintoboggan might display user_register_form on 403 pages.
if (module_exists('logintoboggan') && variable_get('site_403') == 'toboggan/denied' && strpos(drupal_get_http_header('Status'), '403') === 0) {
$profile_types = profile2_regpath_get_profiles('user');
}
else {
// Get profile2 profile types from current path.
$url = drupal_parse_url($form['#action']);
$path = ltrim($url['path'], '/');
// Check to see if this is an alias. If so, use source path.
if ($source = drupal_lookup_path('source', $path)) {
$path = $source;
}
// Grab first part of URL.
$path_parts = explode('/', $path);
$path_key = reset($path_parts);
// Load profiles for this path key.
$profile_types = profile2_regpath_regpath_load_multiple(array(
'path' => $path_key,
'status' => 1,
));
}
// Attach associated fields.
module_load_include('inc', 'profile2_regpath');
profile2_regpath_attach_profile_fields($form, $form_state, $form_id, $profile_types);
}
}
/**
* Implements hook_user_insert().
*/
function profile2_regpath_user_insert(&$edit, $account, $category) {
// Show custom confirmation message.
if (isset($_SESSION['profile2_regpath']['confirmation_message'])) {
drupal_set_message(filter_xss($_SESSION['profile2_regpath']['confirmation_message']));
unset($_SESSION['profile2_regpath']['confirmation_message']);
}
}
/**
* Provides profile_id by profile machine_name.
*
* @param string $profile_type
* Machine-name of profile2 profile type.
*
* @return string
* The profile id for indicated profile type.
*/
function profile2_regpath_get_profile_id($profile_type) {
$profile_id = db_query("SELECT id FROM {profile_type} WHERE type = :profile_type", array(
':profile_type' => $profile_type,
))
->fetchField();
return $profile_id;
}
/**
* Returns object containing all p2rp data.
*
* @param string $path
* (optional) path value for WHERE condition. Defaults to NULL.
*
* @param string $groupby
* (optional) field to groupby. Defaults to NULL.
*
* @return object
* An object containing all matching profile2 registration path enabled
* profile types.
*/
function profile2_regpath_get_profiles($path = NULL, $groupby = NULL, $pid = NULL) {
// Get data object of all registration paths.
$query = db_select('profile2_regpath', 'pr');
$query
->join('profile_type', 'pt', 'pr.profile_id = pt.id');
$query
->fields('pr', array(
'path',
'roles',
'misc',
'status',
));
$query
->fields('pt', array(
'id',
'label',
'type',
));
if ($path) {
$query
->condition('path', $path);
}
if ($groupby) {
$query
->groupBy($groupby);
}
if ($pid) {
$query
->condition('profile_id', $pid);
}
$query
->condition('pr.status', 1);
$query
->orderBy('pr.weight', 'ASC');
$result = $query
->execute();
$profile_types = $result
->fetchAll();
return $profile_types;
}
/**
* Returns object containing all p2rp registration paths.
*
* @return array
* An array containing all active registration paths & path types.
*/
function profile2_regpath_get_reg_paths() {
$reg_paths = NULL;
// Get data object of all registration paths.
$query = db_select('profile2_regpath', 'pr');
$query
->fields('pr', array(
'path',
'misc',
));
$query
->groupBy('path');
$query
->groupBy('misc');
$query
->condition('pr.status', 1);
$result = $query
->execute();
$reg_paths = $result
->fetchAll();
return $reg_paths;
}
/**
* Provides label by profile machine_name.
*
* @param string $profile_type
* Machine-name of profile2 profile type.
*
* @return string
* The label for indicated profile type.
*/
function profile2_regpath_get_profile_label($profile_type) {
$profile_label = db_query("SELECT label FROM {profile_type} WHERE type = :profile_type", array(
':profile_type' => $profile_type,
))
->fetchField();
return $profile_label;
}
/**
* Implements 'load' callback for regpath exportables.
*/
function profile2_regpath_regpath_load($profile_type) {
// Prevent "Call to undefined function ctools_include()" error while going to profile manage page
module_load_include('module', 'ctools', 'ctools');
ctools_include('export');
$result = ctools_export_load_object('profile2_regpath', 'names', array(
$profile_type,
));
if (isset($result[$profile_type])) {
return $result[$profile_type];
}
}
/**
* Implements 'load multiple' callback for regpath exportables.
*/
function profile2_regpath_regpath_load_multiple($conditions) {
// Prevent "Call to undefined function ctools_include()" error
module_load_include('module', 'ctools', 'ctools');
ctools_include('export');
$results = ctools_export_load_object('profile2_regpath', 'conditions', $conditions);
return array_filter($results);
}
/**
* Implements 'load all' callback for regpath exportables.
*/
function profile2_regpath_regpath_load_all() {
// Prevent "Call to undefined function ctools_include()" error while updating database
module_load_include('module', 'ctools', 'ctools');
ctools_include('export');
$results = ctools_export_load_object('profile2_regpath');
return array_filter($results);
}
/**
* Save a single regpath.
*/
function profile2_regpath_regpath_save(&$regpath) {
$exist = profile2_regpath_regpath_load($regpath['profile_type']);
$update = $exist ? 'profile_type' : array();
// Check if we have profile id when creating new regpath
if (!$update) {
// We are creating new profile type
if (!isset($regpath['profile_id']) || !is_numeric($regpath['profile_id']) || $regpath['profile_id'] <= 0) {
// Unvalid profile id
// New profile type need profile id, we load it from database
$profile_type = profile2_type_load($regpath['profile_type']);
if (empty($profile_type)) {
// No profile types was found
return FALSE;
}
else {
$regpath['profile_id'] = $profile_type->id;
}
}
}
return db_merge('profile2_regpath')
->key(array(
'profile_type' => $regpath['profile_type'],
))
->fields($regpath)
->execute();
}
/**
* Delete a regpath.
*/
function profile2_regpath_regpath_delete($regpath) {
// Prevent "Call to undefined function ctools_include()" error
module_load_include('module', 'ctools', 'ctools');
$profile_type = is_object($regpath) ? $regpath->profile_type : $regpath;
db_query('DELETE FROM {profile2_regpath} WHERE profile_type=:profile_type', array(
':profile_type' => $profile_type,
));
// Clear the Ctools export API cache.
ctools_include('export');
ctools_export_load_object_reset('profile2_regpath');
}
/**
* Implements hook_features_revert().
* Rebuild menu after all profile2_regpath components is reverted.
*
* @param type $module_namestring $module_name
* The name of the feature module whose components should be reverted.
*/
function profile2_regpath_features_revert($module_name) {
return profile2_regpath_features_rebuild($module_name);
}
/**
* Implements hook_features_rebuild().
* Rebuild menu after all profile2_regpath components is re-built.
*
* @param string $module_name
* The name of the feature module whose components should be rebuilt.
*/
function profile2_regpath_features_rebuild($module_name) {
$regpaths = module_invoke($module_name, 'default_profile2_regpath');
if (!empty($regpaths)) {
foreach ($regpaths as $regpath) {
// Disable non-schema properties
unset($regpath->disabled);
unset($regpath->api_version);
$regpath = get_object_vars($regpath);
$profile = profile2_type_load($regpath['profile_type']);
$regpath['profile_id'] = $profile->id;
profile2_regpath_regpath_save($regpath);
}
}
menu_rebuild();
}