Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group type by field #14

Merged
merged 8 commits into from
Aug 8, 2018
12 changes: 9 additions & 3 deletions includes/activation_form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ function os2intra_user_import_activate_form_validate($form, &$form_state) {
}

// Check if it matches the users birthday field
$birthday_field = field_get_items('user', $user, 'field_os2intra_birthdate');
if ($input_birthday !== $birthday_field[0]['value']) {
form_set_error('form', 'Please check your input.');
$birthday_field_name = variable_get('os2intra_user_import_birthday_field');
if (!empty(field_info_instance('user', $birthday_field_name, 'user'))) {
$birthday_field = field_get_items('user', $user, $birthday_field_name);
if ($input_birthday !== date("dmy", strtotime($birthday_field[0]['value']))) {
form_set_error('form', 'Please check your input.');
}
}
else {
form_set_error('form', 'Activation form has wrong configuration settings. Please contact site administrator');
}
}

Expand Down
18 changes: 9 additions & 9 deletions includes/csv_mapping.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
* Helper function to parse file contents to array with relevant info
*/
function _os2intra_user_import_process_file($file_path, $lines = FALSE) {
$identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION);
$rows = array();
if (empty($identification_method)) {
drupal_set_message('User activation mode is not setup. Please check user import configuration /admin/config/os2intra/user_import/settings', 'error');
return $rows;
}

// CSV field mapping
// rewrite for settings page?
$mapping = variable_get('os2intra_mapping');
Expand Down Expand Up @@ -59,22 +66,15 @@ function _os2intra_user_import_process_file($file_path, $lines = FALSE) {
}
// Skip if row has no ad_id and no employee_id
if ($mapped_row['ad_id'] == '' && $mapped_row['employee_id'] == '' ) {
os2intra_user_import_save_log('', 'No ad_id and no employee_id found in row: ' . implode(';', $row));
continue;
}

// Generate ad_id if missing
// Try to set ad_id from ad_name column.
if (!isset($mapped_row['ad_id'])) {
if (isset($mapped_row['ad_name']) && !empty($mapped_row['ad_name'])) {
$mapped_row['ad_id'] = $mapped_row['ad_name'];
}
else {
if (variable_get('os2intra_generate_username')) {
$mapped_row['ad_id'] = os2intra_user_import_generate_username($mapped_row);
}
else {
continue;
}
}
}

$rows[] = $mapped_row;
Expand Down
17 changes: 13 additions & 4 deletions includes/departments.inc
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,12 @@ function os2intra_user_import_create_department($tid, $department, $department_n
}

// Group type "Organisation"
// Todo: make more generic
if (field_info_instance('node', 'field_os2intra_grouptype', variable_get('os2intra_groups_node_type'))) {
$node->field_os2intra_grouptype[LANGUAGE_NONE][0]['tid'] = 6;
$os2intra_groups_organisation_tid = variable_get('os2intra_groups_organisation_tid');
if (empty($os2intra_groups_organisation_tid)) {
os2intra_user_import_save_log('', 'Group ' . $name . ' has incorrect settings. Organization group type definition is empty.');
}
$node->field_os2intra_grouptype[LANGUAGE_NONE][0]['tid'] = $os2intra_groups_organisation_tid;
}

// Closed group, require invitation
Expand Down Expand Up @@ -220,11 +223,17 @@ function os2intra_user_import_add_user_to_parent_departments($account, $departme
}
}

// Find the node ID for each department in parent terms
// Find the node ID for each department in parent terms.
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', variable_get('os2intra_groups_node_type'));
$query->fieldCondition($group_organisation_field, $group_organisation_reference, $parent_tids, 'IN');
// Additional condition when there are few types of groups.
if (field_info_instance('node', 'field_os2intra_grouptype', variable_get('os2intra_groups_node_type'))) {
$query->fieldCondition('field_os2intra_grouptype', 'tid', variable_get('os2intra_groups_organisation_tid'));
}

// Added membership only for real department.
if (field_info_instance('node', 'field_os2intra_department_id', variable_get('os2intra_groups_node_type'))) {
$query->fieldCondition('field_os2intra_department_id', 'value', 'NULL', '!=');
}
Expand Down Expand Up @@ -263,4 +272,4 @@ if ($parent_term) {
}
else
return FALSE;
}
}
31 changes: 25 additions & 6 deletions includes/settings_form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ $csv_cols_fallback_1 = array(
'#type' => 'fieldset',
'#title' => t('User activation'),
'#collapsible' => TRUE,
'#weight' => -100,
);

$form['os2intra_user_import_activate']['activation_fields'] = array(
Expand All @@ -164,7 +165,9 @@ $csv_cols_fallback_1 = array(
$form['os2intra_user_import_activate']['activation_fields']['os2intra_user_import_activate_identification'] = array(
'#title' => t('User identification'),
'#type' => 'select',
'#required' => TRUE,
'#options' => array(
'' => t('None'),
'ad_id' => t('AD id'),
'employee_id' => ('Employee number'),
),
Expand Down Expand Up @@ -265,6 +268,27 @@ $csv_cols_fallback_1 = array(
'#options' => $group_type_fields_select,
'#default_value' => variable_get('os2intra_user_import_group_department_title_field')
);

if (isset($group_type_fields['field_os2intra_grouptype'])) {
// For case when group has group type field we have to define
// Organizational term.
$vid = taxonomy_vocabulary_machine_name_load('os2intra_tax_group_type')->vid;
$options = array('' => 'None');
foreach (taxonomy_get_tree($vid) as $item) {
$key = $item->tid;
$value = $item->name;
$options[$key] = $value;
}
$form['os2intra_structure_settings']['os2intra_groups_organisation_tid'] = array(
'#type' => 'select',
'#title' => t('Organization term'),
'#description' => t('Gruppe Type taxonomy term define groups as Organization'),
'#required' => TRUE,
'#options' => $options,
'#default_value' => variable_get('os2intra_groups_organisation_tid'),
);
}

} else {
$form['os2intra_structure_settings']['os2intra_groups_organisation_connection_field'] = array(
'#markup' => t('Please select "Groups node type" and save the form before you can select "Groups -> organisation connection field"')
Expand Down Expand Up @@ -294,6 +318,7 @@ $csv_cols_fallback_1 = array(
'#options' => array_merge(array('none' => t('None')), $user_type_fields_select),
'#default_value' => variable_get('os2intra_user_organisation_field')
);

// Variable which contains the name of user profile field which used for save reference to own user's organization(department).
$form['os2intra_structure_settings']['os2intra_user_department_field'] = array(
'#type' => 'select',
Expand Down Expand Up @@ -358,12 +383,6 @@ $csv_cols_fallback_1 = array(
'#description' => t('If checked only membership from current import will be added'),
'#default_value' => variable_get('os2intra_revoke_og_roles')
);
// Variable which contains settings to determinate how username should be generated.
$form['os2intra_structure_settings']['os2intra_generate_username'] = array(
'#type' => 'checkbox',
'#title' => t('Generate unique user name from "user full name + department name" when AD id is not present'),
'#default_value' => variable_get('os2intra_generate_username')
);
// Variable which contains birthday format.
$form['os2intra_structure_settings']['os2intra_user_import_birthdate_db_format'] = array(
'#type' => 'textfield',
Expand Down
124 changes: 102 additions & 22 deletions includes/users.inc
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,59 @@ function os2intra_user_import_disable_users() {
}

/**
* Check if the user is already created in the system
* Check if the user is already created in the system.
*/
function os2intra_user_import_check_users(&$users, $duplicate_emails, $duplicate_ids) {
function os2intra_user_import_check_users(&$users) {
$update_users = array();

// Check duplicated emails. Drupal users should have unique email.
$dup_emails = array();
$user_email = array_column($users, 'email');
if (!empty($user_email) && count($users) !== count(array_unique($user_email))) {
$dup_emails = array_unique(array_diff_assoc($user_email, array_unique($user_email)));
os2intra_user_import_save_log('', 'Duplicate email found in import file: ' . implode(', ', $dup_emails));
}

// Check duplicated AD id. AD id values should be unique.
$ad_id = array_column($users, 'ad_id');
$dup_id = array();
if (!empty($ad_id) && count($users) !== count(array_unique($ad_id))) {
$dup_id = array_unique(array_diff_assoc($ad_id, array_unique($ad_id)));
os2intra_user_import_save_log('', 'Duplicate User AD id found in import file: ' . implode(', ', $dup_id));
}

$identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION);
// For employee_id activation mode employee_id values should be unique.
if ($identification_method == 'employee_id') {
$employee_id = array_column($users, 'employee_id');
$dup_em_id = array();
if (!empty($employee_id) && count($users) !== count(array_unique($employee_id))) {
$dup_em_id = array_unique(array_diff_assoc($employee_id, array_unique($employee_id)));
os2intra_user_import_save_log('', 'Duplicate employee id found in import file: ' . implode(', ', $dup_em_id));
}
}

foreach ($users as $key => $user) {
if (array_search($user['email'], $duplicate_emails) !== false) {
if (array_search($user['email'], $dup_emails) !== FALSE) {
os2intra_user_import_save_log('', 'Duplicate email found: Skip user ' . $user['email']);
unset($users[$key]);
continue;
}

if (array_search($user['ad_id'], $duplicate_ids) !== false) {
if (array_search($user['ad_id'], $dup_id) !== FALSE) {
unset($users[$key]);
os2intra_user_import_save_log('', 'Duplicate user AD found: Skip user ' . $user['ad_id']);
continue;
}

if ($identification_method == 'employee_id') {
if (array_search($user['employee_id'], $dup_em_id) !== FALSE) {
unset($users[$key]);
os2intra_user_import_save_log('', 'Duplicate user employee id found: Skip user ' . $user['employee_id']);
continue;
}
}

$result = NULL;

$identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION);
Expand Down Expand Up @@ -166,7 +202,7 @@ function os2intra_user_import_save_user($user, $uid = '') {
// Attach new group.
// We rebuild the association completely so we start with an empty
// array.
$user_groups = array();
$user_groups = array(LANGUAGE_NONE => array());
if (variable_get('os2intra_user_default_groups')) {
$def_groups = explode(',', variable_get('os2intra_user_default_groups'));
foreach ($def_groups as $def_group) {
Expand Down Expand Up @@ -217,7 +253,7 @@ function os2intra_user_import_save_user($user, $uid = '') {
// This is only done in order to migrate user groups, from users before the
// import groups field was added.
if ($no_import_groups) {
$user_groups = array();
$user_groups = array(LANGUAGE_NONE => array());
}

// Add back the groups we want to keep.
Expand Down Expand Up @@ -298,7 +334,7 @@ function os2intra_user_import_save_user($user, $uid = '') {
$current_timestamp = time();
}

// Populate fields
// Populate static fields.
$fields = array(

// Set employee_id
Expand All @@ -319,15 +355,6 @@ function os2intra_user_import_save_user($user, $uid = '') {
// Mobile
'field_os2intra_mobile' => array(LANGUAGE_NONE => array(0 => array('value' => (array_key_exists('mobile', $user)) ? $user['mobile'] : ''))),

// Set birthdate
variable_get('os2intra_user_import_birthday_field') => array(LANGUAGE_NONE => array(0 => array('value' => (array_key_exists('birthdate', $user)) ? $user['birthdate'] : NULL))),

//Set users nodes
variable_get('os2intra_user_group_field') => array(LANGUAGE_NONE => $og_user_node),

// Set department membership
variable_get('os2intra_user_organisation_field') => variable_get('os2intra_save_old_departments', false) ? $user_groups : array(LANGUAGE_NONE => array(0 => array('target_id' => $department_nid))),

// Activate user
'status' => ($account->uid != 0) ? $account->status : 1,

Expand All @@ -337,6 +364,54 @@ function os2intra_user_import_save_user($user, $uid = '') {

'field_os2intra_import_timestamp' => array(LANGUAGE_NONE => array(0 => array('value' => $current_timestamp))),
);

// Populate dynamic fields.
// Set birthdate.
$birthday_field = variable_get('os2intra_user_import_birthday_field');
if (!empty($birthday_field)) {
$fields[$birthday_field] = array(
LANGUAGE_NONE => array(
0 => array(
'value' => (array_key_exists('birthdate', $user)) ? $user['birthdate'] : NULL,
),
),
);
}

// Set users groups.
$group_field = variable_get('os2intra_user_group_field');
if (!empty($group_field)) {
$fields[$group_field] = array(LANGUAGE_NONE => $og_user_node);
}

// Set department membership.
$organisation_field = variable_get('os2intra_user_organisation_field');
if (!empty($organisation_field)) {
$organisational_groups = variable_get('os2intra_save_old_departments', FALSE) ? $user_groups : array(LANGUAGE_NONE => array(0 => array('target_id' => $department_nid)));
if ($organisation_field != $group_field) {
// Different field used for department membership.
$fields[$organisation_field] = $organisational_groups;
}
else {
// Merging field values when the same field used
// to store organizations and user groups references.
$groups = !empty($fields[$group_field][LANGUAGE_NONE]) ? $fields[$group_field][LANGUAGE_NONE] : array();
foreach ($organisational_groups[LANGUAGE_NONE] as $org_group) {
$exist = FALSE;
foreach ($groups as $group) {
$exist = $group['target_id'] == $org_group['target_id'];
if ($exist) {
break;
}
}
if (!$exist) {
$groups[] = $org_group;
}
}

$fields[$group_field][LANGUAGE_NONE] = $groups;
}
}

if (isset($user['email'])) {
$fields['mail'] = $user['email'];
Expand All @@ -362,26 +437,31 @@ function os2intra_user_import_save_user($user, $uid = '') {
// Handle whether we're updating or creating a new user
// if we're updating we don't generate username and sets password
if (!is_numeric($uid)) {
$password = user_password(8);

// At this step even if the ad_id was empty initially it must have been generated
// By default user name comes from ad_id.
$fields['name'] = $user['ad_id'];

// For empty ad_id generate username.
if (empty($user['ad_id'])) {
$fields['name'] = os2intra_user_import_generate_username($user);
}

$password = user_password(8);
$fields['pass'] = $password;

// Before create check if user has correct name.
if (empty($fields['name'])) {
os2intra_user_import_save_log($user['employee_id'], 'Cannot add user with empty name/AD_id. Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']);
os2intra_user_import_save_log($user['employee_id'], 'Cannot add user with empty name. Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']);
return;
}

// Before create check if user with this name already exist.
if (!empty(user_load_by_name($fields['name']))) {
os2intra_user_import_save_log($user['employee_id'], 'User with name/AD_id already exists Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']);
os2intra_user_import_save_log($user['employee_id'], 'User with name already exists Employee id: ' . $user['employee_id'] . ' Ad id: ' . $user['ad_id']);
return;
}
}


// Set Opus Roles
if (field_info_instance('user', 'os2intra_users_opus_roles', 'user')) {
$fields['os2intra_users_opus_roles'][LANGUAGE_NONE] = array();
Expand Down Expand Up @@ -431,7 +511,7 @@ function os2intra_user_import_save_user($user, $uid = '') {
);
}

if (!user_get_authmaps($authmap)) {
if (!empty($authmap) && !user_get_authmaps($authmap)) {
user_set_authmaps($account, $authmap);
}
// Add users to parent departments
Expand Down
Loading