Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: SCHOLAR-2-0-BE…
Fetching contributors…

Cannot retrieve contributors at this time

596 lines (510 sloc) 21.842 kB
<?php
include_once('vsite_users.features.inc');
/**
* Implementation of hook_user()
*/
function vsite_users_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'view':
$account -> content['web_sites'] = array(
'#title' => 'Site Membership Summary',
'#type' => 'user_profile_item',
'#value' => vsite_users_show_groups_list($account),
);
break;
}
}
/**
* Implementation of hook_profile_alter()
*/
function vsite_users_profile_alter(&$account) {
//removing existing summary information (history, blog, etc.)
unset($account->content['summary']);
}
/**
* Implements hook_menu
*/
function vsite_users_menu() {
$items = array();
$items['cp/users'] = array(
'title' => 'Users',
'page callback' => 'vsite_users_members',
'file' => 'vsite_users.pages.inc',
'weight' => 50,
);
$items['cp/users/members'] = array(
'title' => 'Manage Members',
'page callback' => 'vsite_users_members',
'file' => 'vsite_users.pages.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
// make them all part of 'cp' menu
foreach ($items as $path => $item) {
$items[$path]['menu_name'] = 'cp';
$items[$path]['access callback'] = 'cp_access_cp';
}
$items['cp/users/add'] = array(
'title' => 'Add User',
'page callback' => 'vsite_users_add_user',
'access callback' => 'cp_access_cp',
'file' => 'vsite_users.pages.inc',
'type' => MENU_CALLBACK,
'weight' => 10
);
$items['cp/users/user_lookup'] = array(
'title' => '',
'page callback' => 'vsite_users_lookup_user',
'access callback' => 'cp_access_cp',
'file' => 'vsite_users.pages.inc',
'type' => MENU_CALLBACK,
'weight' => 10
);
return $items;
}
/**
* Implements hook_menu_alter()
*/
function vsite_users_menu_alter(&$a_menu) {
global $user;
//Check to see if there are avalible sites to join, if not do not show the link
$a_menu['user/%user/edit/groups']['access callback'] = 'vsite_users_groups_avalible_to_join';
}
/**
* Tests if there are groups avalible for a user to join
* @param user $account
*/
function vsite_users_groups_avalible_to_join($account){
if(!user_edit_access($account)) return false;
//Returns the register form, og_register has a list of the avalible sites
$form = og_user('register', array(), $account, $category = NULL);
return isset($form['og_register']);
}
/**
* Show the sites that a user belongs to in segmented list
*
* @param $account user
* @return
*/
function vsite_users_show_groups_list($account) {
$my_sites = array();
$member_sites = array();
foreach ( $account -> og_groups as $sid => $site) {
//load the vsite
$vsite = vsite_get_vsite($sid);
if($vsite){
switch ($site['is_admin']) {
case "1":
//check if user owns the site, if not then user might be a super user
if ($account-> uid == $site['uid']) {
$my_sites[] = array(
'name' => $site['title'],
'url' => $vsite->get_absolute_url(),
);
}
//user has full admin rights to web site but is not the owner
else{
$member_sites[] = array(
'name' => $site['title'],
'url' => $vsite->get_absolute_url(),
);
}
break;
case "0":
$member_sites[] = array(
'name' => $site['title'],
'url' => $vsite->get_absolute_url(),
);
break;
}
}
}
return theme('vsite_users_list_sites', $my_sites, $member_sites);
}
/**
* Implementation of hook_mail_alter().
*/
function vsite_users_mail_alter(&$message) {
switch ($message['id']) {
//Edit the UCreate Email Message
case 'ucreate_ucreate-create':
$message['subject'] = t('An account has been created for you at !site_name', array('!site_name' => variable_get('site_name', 'OpenScholar')));
$vsite = vsite_get_vsite();
//Get the just created user
$n_new_user = db_result(db_query('SELECT u.uid FROM {users} u ORDER BY u.created DESC LIMIT 1'));
$o_user = user_load($n_new_user);
// $password = user_password();
// $edit = array('pass' => $password);
$o_profile = content_profile_load('vsite_users_profile', $n_new_user);
// user_save($o_user, $edit);
//checking for custom welcome message string submitted in form
$personal_msg_text = (!empty($_POST['welcome_message_body'])) ?
"\n================================================\n" .
$_POST['welcome_message_body'] .
"\n================================================\n\n" : '' ;
$a_msg_variables = array(
'!personal_message' => $personal_msg_text,
'!username' => $o_user->name,
'!site' => variable_get('site_name', 'OpenScholar'),
'!login_url' => str_replace($vsite->group->purl . '/', '', user_pass_reset_url($o_user)), //remove purl from url
'!uri' => $base_url,
'!uri_brief' => preg_replace('!^https?://!', '', $base_url),
'!mailto' => $o_user->mail,
'!date' => format_date(time(), 'medium', '', NULL, $message['language']->language),
'!login_uri' => url('user', array('absolute' => TRUE, 'language' => $message['language'])),
'!edit_uri' => url('user/'. $o_user->uid .'/edit', array('absolute' => TRUE, 'language' => $message['language'])),
'!password' => $password,
'!firstname' => $_POST['vsite_users_first_name'], //$o_profile->vsite_users_first_name[0]['value'],
);
//This is unused: Alternatively using the following username and temporary password:\n\nUsername: !username\nTemporary password: !password\n\nAfter logging in, you will be redirected to !edit_uri so you can change your password.\n\n
$message['body'] = t("!personal_messageDear !firstname,\n\nA site owner at the !site website has created an account for you. You may now log in by either clicking through on the following link or pasting it into your browser:\n\n !login_url\n\n(Note: This is a one-time login; it can be used only once.)\n\nSincerely,\nThe !site Team", $a_msg_variables);
break;
}
}
/**
* Define default role for
*
* @param unknown_type $name
* @return unknown
*/
function vsite_users_ucreate_role($name = 'scholar user') {
$rid =array_search($name, user_roles());
foreach (user_roles() as $key => $val) {
if ($key > 2 && $key == $rid) {
$default_role[$rid] = $rid;
}
}
return $default_role;
}
/**
* Return the owner of the vsite
* @return user
*/
function vsite_user_edit() {
global $user;
return user_edit($user);
}
/**
* Return the owner of the vsite
* @return user
*/
function vsite_user_edit_access() {
$vsite = vsite_get_vsite();
if (!$vsite) return FALSE;
return user_edit_access(user_load($vsite->group->uid));
}
/**
* Function that loads the content profile form for a site
* @return form
*/
function vsite_user_content_profile_edit_page($s_type) {
global $user;
return content_profile_page_edit($s_type, $user);
}
/**
* Change the form theme to ours without the extra node settings displayed
*/
function vsite_users_theme() {
$themes = array(
'vsite_users_profile_node_form' => array(
'arguments' => array('form' => NULL),
'path' => drupal_get_path('module', 'vsite_users') . '/theme',
'file' => 'vsite_users.theme.inc',
),
'vsite_users_list_sites' => array(
'arguments' => array('my_sites' => array(), 'member_sites' => array()),
'path' => drupal_get_path('module', 'vsite_users') . '/theme',
'file' => 'vsite_users.theme.inc',
),
);
return ($themes);
}
/**
* For the content profile form:
* - Hide the preview and delete buttons
* - Save as the site owner
*/
function vsite_users_form_vsite_users_profile_node_form_alter(&$form, &$form_state) {
//Only apply these alterations on the admin page where we want this form custom for the site owner
if (!(arg(0) == 'cp' && arg(1) == 'users' && arg(2) == 'myaccount' && arg(3) == 'personal')) {
content_profile_form_alter($form, $form_state, 'vsite_users_profile_node_form');
if (!$form['name']) $form['name'] = array('#type' => 'value', '#value' => $form['author']['name']['#default_value']); //Fix admin only bug
return;
}//If we are not in CP use the regular alter func.
if (isset($form['#redirect'])) unset($form['#redirect']);
if (isset($form['buttons']['submit'])) {
$form['buttons']['submit']['#submit'][] = 'vsite_users_form_vsite_users_profile_node_submit';
}
$form['buttons']['preview']['#access'] = FALSE;
$form['buttons']['delete']['#access'] = FALSE;
}
/**
* In CP keep the form from redirecting to the node or redirect to home
*/
function vsite_users_form_vsite_users_profile_node_submit($form, &$form_state) {
//customize the confirmation message for add/edit form
drupal_get_messages($type = NULL, $clear_queue = TRUE);
drupal_set_message(t('Your personal information has been saved.'));
$o_profile = FALSE;
if (($scholar = vsite_get_vsite()) && $form_state['values']['uid'] == $scholar->group->uid) {
$o_profile = content_profile_load('vsite_users_profile', $scholar->group->uid);
}
if (is_array($form_state['values']) //The form has been submitted
&& !$o_profile
&& $form_state['values']['uid'] == $scholar->group->uid ) { //There was not already a profile
$form_state['redirect'] = '<front>';
}
elseif (isset($form_state['redirect'])) {
unset($form_state['redirect']); //No Redirect for you
}
}
/**
* Implementation of hook_nodeapi().
*/
function vsite_users_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($op == 'prepare' && is_content_profile($node) && !isset($node->nid) && $node->uid && !user_access('administer nodes') && arg(0) == 'cp' && arg(1) == 'users' && arg(2) == 'add') {
$node->uid = 0;
}
}
/**
* Edit the ucreate form to hide the site dialogue and set defaults
*/
function vsite_users_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'ucreate_user_form':
$vsite = vsite_get_vsite();
$form['og_register']['og_register']['#value'] = $vsite->id;
$form['og_register']['og_register']['#type'] = 'value';
$form['og_register']['#type'] = 'value';
//using a default role for new users created with ucreate (set in hook_strongarm)
$form['roles']['#type'] = 'value';
$form['roles']['#value'] = variable_get('ucreate_default_roles', array());
$form['name']['#weight'] = 1;
$form['name']['#description'] = "The shortname used to login usually first initial + last name";
$form['mail']['#weight'] = 2;
$form['mail_confirm']['#weight'] = 3;
$form['welcome_message_body']['#weight'] = 4;
//Add Registration info to form
$form['vsite_users_first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
'#weight' => -3,
'#description' => t(''),
);
$form['vsite_users_last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#required' => TRUE,
'#weight' => -2,
'#description' => ''
);
if (is_array($form['#submit']) && array_search('ucreate_user_form_submit', $form['#submit']) !== FALSE) unset($form['#submit'][array_search('ucreate_user_form_submit', $form['#submit'])]); //Hijack this submit
if (is_array($form['#validate']) && array_search('ucreate_user_form_validate', $form['#validate']) !== FALSE) unset($form['#validate'][array_search('ucreate_user_form_validate', $form['#validate'])]); //Hijack this submit validate
//We will do our own mail validation
if (isset($form['mail']['#element_validate'])) unset($form['mail']['#element_validate']);
if (isset($form['name']['#element_validate'])) unset($form['name']['#element_validate']);
//Add Validate handler
if (!is_array($form['#validate'])) $form['#validate'] = array();
array_unshift($form['#validate'], 'vsite_users_user_form_validate');
//Add submit handler for registration info
if (!is_array($form['#submit'])) $form['#validate'] = array();
array_unshift($form['#submit'], 'vsite_users_user_form_submit');
ctools_include('dependent');
foreach (element_children($form) as $s_key) {
if (!in_array($form[$s_key]['#type'], array('textarea', 'textfield'))) continue;
$form[$s_key]['#process'] = array('ctools_dependent_process');
$form[$s_key]['#dependency'] = array('radio:create_type' => array('new'));
}
$form['create_type'] = array(
'#title' => "",
'#type' => 'radios',
'#options' => array('existing' => "", 'new' => ""),
'#default_value' => (array_key_exists('input', $form_state) && array_key_exists('create_type', $form_state['input']))?$form_state['input']['create_type']:'existing',
'#tree' => TRUE,
'#weight' => -6,
'#attributes' => array('style' => "display:none;")
);
$form['lookup_user'] = array(
'#type' => 'textfield',
'#title' => "Find an Existing User",
'#maxlength' => 80,
'#autocomplete_path' => 'cp/users/user_lookup',
'#element_validate' => array('vsite_node_autocomplete_validate'),
'#weight' => -5,
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:create_type' => array('existing')),
'#description' => "Type in a persons name, login, or e-mail address to search for their account.",
);
$form['add_new_user'] = array(
'#type' => 'item',
'#value' => '<a href="#" onClick="$(\'#edit-create-type-new\').attr(\'checked\', true); $(\'#edit-create-type-new\').change(); $(this).parent(\'div.form-item\').hide(); $(\'#edit-lookup-user\').val(\'\'); return false;" class="context-button" title="add a new user">Add new user</a>',
'#description' => 'If you can\'t find their account, click "Add new user" to create a new account',
'#weight' => -4,
);
if (array_key_exists('input', $form_state) && array_key_exists('create_type', $form_state['input']) && $form_state['input']['create_type'] == 'existing') {
$form['vsite_users_first_name']['#required'] =
$form['vsite_users_last_name']['#required'] =
$form['mail']['#required'] =
$form['mail_confirm']['#required'] =
$form['name']['#required'] = FALSE;
}
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#weight' => 101,
'#attributes' => array(
'onclick' => 'Drupal.CTools.Modal.dismiss(); return false;'
),
);
break;
case 'user_profile_form':
//Redirect the save password form to the profile page
if (!array_key_exists('redirect_from_reset', $form)) $form['redirect_from_reset'] = array('#type' => 'hidden', '#default_value' => FALSE);
if ($_SERVER['REDIRECT_STATUS'] == '200' && strstr($_SERVER['HTTP_REFERER'], 'user/reset') !== FALSE) {
$form['redirect_from_reset']['#value'] = TRUE;
}//Are we coming from the special login link?
$form['#submit'][] = 'vsite_users_redirect_reset_submit';
unset($form['account']['#title']);
//Remove unused fields
if (array_key_exists('contact', $form)) vsite_users_hide_form_value($form['contact']);
if (array_key_exists('biblio_fieldset', $form)) vsite_users_hide_form_value($form['biblio_fieldset']);
if (array_key_exists('timezone', $form)) vsite_users_hide_form_value($form['timezone']);
break;
case 'vsite_users_profile_node_form':
//Remove unused fields
if (array_key_exists('path', $form)) vsite_users_hide_form_value($form['path']);
if (array_key_exists('attachments', $form)) vsite_users_hide_form_value($form['attachments']);
//Hide the picture till it is used in some way, people are confusing this with site logo
if (array_key_exists('field_user_profile_photo', $form)) vsite_users_hide_form_value($form['field_user_profile_photo']);
break;
}
}
function vsite_users_hide_form_value(&$element) {
foreach (element_children($element) as $s_key) {
$element[$s_key]['#type'] = 'value';
vsite_users_hide_form_value($element[$s_key]);;
}
$element['#type'] = 'value';
}
/**
* Validation handler for ucreate_user_form()
*/
function vsite_users_user_form_validate($form, &$form_state) {
$account = FALSE;
$form_state['values']['lookup_user'] = trim($form_state['values']['lookup_user']);
if (strlen($form_state['values']['lookup_user']) && !(($account = user_load(array('name' => $form_state['values']['lookup_user']))) || ($account = user_load(array('mail' => $form_state['values']['lookup_user']))))) {
form_set_error('lookup_user', t('A user with the username "' . $form_state['values']['lookup_user'] . '" can not be found.'));
}
elseif ($account) {
$form_state['values']['name'] = $account->name;
}
elseif ($form_state['values']['mail'] != $form_state['values']['mail_confirm']) {
form_set_error('email_confirm', t('E-mail addresses don\'t match'));
}
elseif ($form_state['input']['create_type'] == 'existing' && !strlen($form_state['values']['lookup_user'])) {
form_set_error('lookup_user', t('No user has been entered, search for a user or click "Add new user"'));
}
elseif ($form_state['input']['create_type'] != 'existing') {
ucreate_validate_mail($form['mail'], $form_state);
ucreate_validate_name($form['name'], $form_state);
}
}
/**
* Submit handler for ucreate_user_form().
*/
function vsite_users_user_form_submit($form, &$form_state) {
$vsite = vsite_get_vsite();
if (strlen($form_state['values']['lookup_user'])) {
$account = user_load(array('name' => $form_state['values']['lookup_user']));
if(!$account) $account = user_load(array('mail' => $form_state['values']['lookup_user']));
$s_message = t('[!name] is now a member of [!group]', array('!name' => $account->name, '!group' => $vsite->group->title));
}
if ($account || ($account = user_load(array('name' => $form_state['values']['name']))) || ($account = user_load(array('mail' => $form_state['values']['mail'])))) {
og_save_subscription($vsite->group->nid, $account->uid, array('is_active' => 1));
drupal_set_message($s_message?$s_message:t('A user with the name [!name] and e-mail address[!email] already existed, they are now a member of [!group]', array('!name' => $account->name, '!email' => $account->mail, '!group' => $vsite->group->title)));
return TRUE;
}
// If user roles were handled through a UI element, process accordingly.
// This sucks. See user_save() for why this is necessary.
if ($form['roles']['#type'] != 'value') {
$rids = array();
foreach ($form_state['values']['roles'] as $rid => $enabled) {
if (!empty($enabled)) {
$rids[$rid] = $rid;
}
}
$form_state['values']['roles'] = $rids;
}
//Create the user and store it in the form for other submit functions
$form_state['user'] = ucreate_user_create($form_state['values']);
//Create the profile node for the new user
$node = new stdClass();
$node -> vsite_users_first_name[0]['value'] = $form_state['values']['vsite_users_first_name'];
$node -> vsite_users_last_name[0]['value'] = $form_state['values']['vsite_users_last_name'];
if (empty($node->title) && (!module_exists('auto_nodetitle') || auto_nodetitle_get_setting($type) != AUTO_NODETITLE_OPTIONAL)) {
$node->title = $form_state['user']->name;
}
$node->uid = $form_state['user']->uid;
$node->name = $form_state['user']->name;
$node -> type = count(content_profile_get_types() == 1) ? key(content_profile_get_types()) : 'vsite_users_profile';
$node -> status = 1; //not sure this is necessary?
$node = node_submit($node);
node_save($node);
}
/**
* Submit handler used to check the passed reset value, if it is present we will redirect
*/
function vsite_users_redirect_reset_submit($form, &$form_state) {
if (is_array($form_state['values'])
&& array_key_exists('redirect_from_reset', $form_state['values'])
&& $form_state['values']['redirect_from_reset']) {
$vsites = vsite_get_vsite_by_owner($form['#uid']);
if(count($vsites) == 1 && is_object($vsites[0])){
drupal_goto( $vsites[0]->get_absolute_url() );
}
}
}
/**
* hook_scholar_conformation
*
* Returns an array of urls to overide and create cp_confirm_popups for
* @return array
*/
function vsite_users_confirm_popup_paths() {
return array(
"og/unsubscribe/%/%",
"og/subscribe/%",
"og/create_admin/%/%",
"og/delete_admin/%/%",
);
}
/**
* Implement Hook vsite_generic_settings
* @return array
*/
function vsite_users_vsite_generic_settings() {
$a_settings = array();
//Should Vsite Contact forms allow anon. contact?
$a_settings['vsite_users_contact_form_anonymous'] = array(
'group' => array(
'#id' => 'privacy',
'#title' => t('Privacy'),
'#weight' => 0,
),
'form' => array(
'#type' => 'checkbox',
'#title' => t('Anonymous Contact'),
'#default_value' => variable_get('vsite_users_contact_form_anonymous', 1),
'#description' => "When this is checked a user does not need to be logged in to contact you.",
)
);
return $a_settings;
}
function vsite_users_form_og_remove_admin_confirm_alter(&$form, &$form_state) {
$form['#submit'][] = 'vsite_users_og_remove_admin_confirm_submit';
//$form_state['redirect_old'] = $_GET['destination'];
}
function vsite_users_og_remove_admin_confirm_submit($form, &$form_state) {
$form_state['redirect'] = $_GET['destination'];
}
Jump to Line
Something went wrong with that request. Please try again.