Skip to content

Commit

Permalink
Merge pull request #7 from bellcom/f/odsherred_user_import_fix
Browse files Browse the repository at this point in the history
F/odsherred user import fix
  • Loading branch information
stankut authored Apr 13, 2018
2 parents f9a9a53 + cae50ed commit edc330e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 49 deletions.
6 changes: 3 additions & 3 deletions includes/activation_form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* User activation form
*/
function os2intra_user_import_activate_form($form_state) {
switch (variable_get('os2intra_user_import_activate_identification')) {
switch (variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION)) {
case 'ad_id':
$form['ad_id'] = array(
'#type' => 'textfield',
Expand All @@ -19,14 +19,14 @@ function os2intra_user_import_activate_form($form_state) {
break;
}

if (variable_get('os2intra_user_import_activate_email')) {
if (variable_get('os2intra_user_import_activate_email', OS2INTRA_USER_IMPORT_ACTIVATE_EMAIL)) {
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail adress'),
);
}

if (variable_get('os2intra_user_import_activate_birthday')) {
if (variable_get('os2intra_user_import_activate_birthday', OS2INTRA_USER_IMPORT_ACTIVATE_BIRTHDAY)) {
$form['birthdate'] = array(
'#type' => 'textfield',
'#title' => t('Birthdate'),
Expand Down
58 changes: 31 additions & 27 deletions includes/departments.inc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function os2intra_user_import_create_department($tid, $department, $department_n
$node->{$group_organisation_field}[LANGUAGE_NONE][][$group_organisation_reference] = $tid;

// Save department id on node
if (field_info_instance('taxonomy_term', 'field_os2intra_department_id', variable_get('os2intra_groups_node_type'))) {
if (field_info_instance('node', 'field_os2intra_department_id', variable_get('os2intra_groups_node_type'))) {
$node->field_os2intra_department_id[LANGUAGE_NONE][0]['value'] = $department;
}

Expand All @@ -194,8 +194,9 @@ function os2intra_user_import_create_department($tid, $department, $department_n
/**
* Add user to parent departments
*/
function os2intra_user_import_add_user_to_parent_departments($account) {
$og_department_node = node_load(field_get_items('user', $account, variable_get('os2intra_user_organisation_field'))[0]['target_id']);
function os2intra_user_import_add_user_to_parent_departments($account, $department_nid) {
//$og_department_node = node_load(field_get_items('user', $account, variable_get('os2intra_user_organisation_field'))[0]['target_id']);
$og_department_node = node_load($department_nid);
if (empty($og_department_node)) {
return;
}
Expand All @@ -206,40 +207,43 @@ function os2intra_user_import_add_user_to_parent_departments($account) {
// Add user to their primary department and give them the 'medarbejder' role
$values = array(
'entity' => $account,
'field_name' => variable_get('os2intra_user_organisation_field'),
'field_name' => variable_get('os2intra_user_primary_og_role_reference_field'),
);
og_group('node', $og_department_node->nid, $values);
og_role_grant('node', $og_department_node->nid, $account->uid, variable_get('os2intra_user_import_og_role_id_primary', 16));
// Add user to all parent departments and give 'organisatorisk' role on them.
$parent_terms = taxonomy_get_parents_all($organisation_tid);

foreach ($parent_terms as $parent_term) {
if ($parent_term->tid != $organisation_tid) {
$parent_tids[] = $parent_term->tid;
if (isset($parent_terms)) {
foreach ($parent_terms as $parent_term) {
if ($parent_term->tid != $organisation_tid) {
$parent_tids[] = $parent_term->tid;
}
}
}

// 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');
$results = $query->execute();

if (isset($results['node'])) {
foreach ($results['node'] as $result) {
$og_group_node_ids[] = $result->nid;
// 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');
if (field_info_instance('node', 'field_os2intra_department_id', variable_get('os2intra_groups_node_type'))) {
$query->fieldCondition('field_os2intra_department_id', 'value', 'NULL', '!=');
}
$results = $query->execute();

$values = array(
'entity' => $account,
'field_name' => variable_get('os2intra_user_department_field'),
);
if (isset($results['node'])) {
foreach ($results['node'] as $result) {
$og_group_node_ids[] = $result->nid;
}

foreach ($og_group_node_ids as $nid) {
// Add the user to the group and set role.
og_group('node', $nid, $values);
og_role_grant('node', $nid, $account->uid, variable_get('os2intra_user_import_og_role_id_parent', 14));
$values = array(
'entity' => $account,
'field_name' => variable_get('os2intra_user_parent_og_role_reference_field'),
);
foreach ($og_group_node_ids as $nid) {
// Add the user to the group and set role.
og_group('node', $nid, $values);
og_role_grant('node', $nid, $account->uid, variable_get('os2intra_user_import_og_role_id_parent', 14));
}
}
}
}
Expand Down
63 changes: 53 additions & 10 deletions includes/settings_form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $csv_cols_fallback_1 = array(
'#description' => t('ID of the group role to grant the user, when added to parent groups.'),
);

$form['os2intra_user_import_og_role_id_user'] = array(
$form['os2intra_user_import_og_role_id_primary'] = array(
'#title' => t('Primary OG Role Grant ID'),
'#type' => 'textfield',
'#default_value' => variable_get('os2intra_user_import_og_role_id_primary', 16),
Expand All @@ -149,6 +149,7 @@ $csv_cols_fallback_1 = array(
'#default_value' => variable_get('os2intra_user_import_user_disable_action', 'user_disable'),
);

// Settings for data structure compatibility in different installations.
$form['os2intra_structure_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Structure settings'),
Expand All @@ -162,17 +163,20 @@ $csv_cols_fallback_1 = array(
$available_vocabularies[$vocabulary->vid] = $vocabulary->name;
}
asort($available_vocabularies);
// Variable which contains organization vocabulary ID.
$form['os2intra_structure_settings']['os2intra_groups_vocabulary_vid'] = array(
'#type' => 'select',
'#title' => t('Groups vocabulary name'),
'#description' => t('Taxonomy vocabulary which contains organisation terms'),
'#required' => TRUE,
'#options' => $available_vocabularies,
'#default_value' => variable_get('os2intra_groups_vocabulary_vid')
);

// Variable which contains user's job titles vocabulary ID.
$form['os2intra_structure_settings']['os2intra_job_title_vocabulary_vid'] = array(
'#type' => 'select',
'#title' => t('Job title vocabulary name'),
'#description' => t('Taxonomy vocabulary which contains job titles'),
'#required' => TRUE,
'#options' => $available_vocabularies,
'#default_value' => variable_get('os2intra_job_title_vocabulary_vid')
Expand All @@ -184,9 +188,11 @@ $csv_cols_fallback_1 = array(
$available_node_types[$node_type->type] = $node_type->name;
}
asort($available_node_types);
// Variable which contains content type what used for groups.
$form['os2intra_structure_settings']['os2intra_groups_node_type'] = array(
'#type' => 'select',
'#title' => t('Groups node type'),
'#description' => t('Content type which used for organizations'),
'#required' => TRUE,
'#options' => $available_node_types,
'#default_value' => variable_get('os2intra_groups_node_type')
Expand All @@ -198,23 +204,29 @@ $csv_cols_fallback_1 = array(
foreach ($group_type_fields as $field_name => $value) {
$group_type_fields_select[$field_name] = $field_name;
}
// Variable which contains content type what used for groups.
$form['os2intra_structure_settings']['os2intra_groups_organisation_connection_field'] = array(
'#type' => 'select',
'#title' => t('Groups -> organisation connection field'),
'#description' => t('Field name in organization group node which is used for connecting with organization taxonomy'),
'#required' => TRUE,
'#options' => $group_type_fields_select,
'#default_value' => variable_get('os2intra_groups_organisation_connection_field')
);
// Variable which contains type of reference which used for connection between group node and organization taxonomy.
$form['os2intra_structure_settings']['os2intra_groups_organisation_connection_reference'] = array(
'#type' => 'select',
'#title' => t('Groups -> organisation connection reference type'),
'#description' => t('Reference type to organization taxonomy in organization group node (Term Reference or Entity Reference)'),
'#required' => TRUE,
'#options' => array('target_id' => 'target_id', 'tid' => 'tid'),
'#default_value' => variable_get('os2intra_groups_organisation_connection_reference')
);
// Variable which contains the name of field in group node which used as group title.
$form['os2intra_structure_settings']['os2intra_user_import_group_department_title_field'] = array(
'#type' => 'select',
'#title' => t('Groups department title field'),
'#description' => t('The name of field in organization node which is used as organization title'),
'#required' => TRUE,
'#options' => $group_type_fields_select,
'#default_value' => variable_get('os2intra_user_import_group_department_title_field')
Expand All @@ -230,80 +242,111 @@ $csv_cols_fallback_1 = array(
foreach ($user_type_fields as $field_name => $value) {
$user_type_fields_select[$field_name] = $field_name;
}
// Variable which contains the name of user profile field which used for save references to user's groups.
$form['os2intra_structure_settings']['os2intra_user_group_field'] = array(
'#type' => 'select',
'#title' => t('User group reference field'),
'#description' => t('The name of field in the user profile which contains references to user\'s groups nodes'),
'#required' => TRUE,
'#options' => $user_type_fields_select,
'#default_value' => variable_get('os2intra_user_group_field')
);

$form['os2intra_structure_settings']['os2intra_user_organisation_field'] = array(
// Variable which contains the name of user profile field which used for save references to all user's organizations(departments).
$form['os2intra_structure_settings']['os2intra_user_organisation_field'] = array(
'#type' => 'select',
'#title' => t('User organisation reference field'),
'#description' => t('The name of field in the user profile which contains references to user\'s organisations nodes'),
'#required' => TRUE,
'#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',
'#title' => t('User department reference field'),
'#description' => t('The name of field in user profile which contains reference to user\'s own department'),
'#required' => TRUE,
'#options' => $user_type_fields_select,
'#default_value' => variable_get('os2intra_user_department_field')
);
// Variable which contains the name of user profile field the membership with "Primary OG Role" should be registered in.
$form['os2intra_structure_settings']['os2intra_user_primary_og_role_reference_field'] = array(
'#type' => 'select',
'#title' => t('Primary OG Role reference_field'),
'#description' => t('The name of the field, the membership with "Primary OG Role" should be registered in'),
'#required' => TRUE,
'#options' => $user_type_fields_select,
'#default_value' => variable_get('os2intra_user_primary_og_role_reference_field')
);
// Variable which contains the name of user profile field the membership with "Parent OG Role" should be registered in.
$form['os2intra_structure_settings']['os2intra_user_parent_og_role_reference_field'] = array(
'#type' => 'select',
'#title' => t('Parent OG Role reference field'),
'#description' => t('The name of the field, the membership with "Parent OG Role" should be registered in'),
'#required' => TRUE,
'#options' => $user_type_fields_select,
'#default_value' => variable_get('os2intra_user_parent_og_role_reference_field')
);
// Variable which contains the name of user profile field which contains user\'s birthday.
$form['os2intra_structure_settings']['os2intra_user_import_birthday_field'] = array(
'#type' => 'select',
'#title' => t('User birthday field'),
'#description' => t('Field in user profile which contains user\'s birthday'),
'#required' => TRUE,
'#options' => $user_type_fields_select,
'#default_value' => variable_get('os2intra_user_import_birthday_field')
);

// Variable which contains settings to determinate if user should be added as members to parent departmnets.
$form['os2intra_structure_settings']['os2intra_add_users_to_parent_departments'] = array(
'#type' => 'checkbox',
'#description' => t('If checked, users will be added as member into parent departments with "Parent OG Role"'),
'#title' => t('Add users to parent departments'),
'#default_value' => variable_get('os2intra_add_users_to_parent_departments')
);

// Variable which contains settings to determinate if user should be added as manager to parent departmnets.
$form['os2intra_structure_settings']['os2intra_add_managers_to_departments'] = array(
'#type' => 'checkbox',
'#description' => t('If checked, user will be added as manager to parents departments'),
'#title' => t('Add managers to departments'),
'#default_value' => variable_get('os2intra_add_managers_to_departments')
);
// Variable which contains settings to determinate if old references to user's departments should saved during import.
$form['os2intra_structure_settings']['os2intra_save_old_departments'] = array(
'#type' => 'checkbox',
'#title' => t('Save old departments references during import'),
'#description' => t('If unchecked only current department reference will be saved for user'),
'#default_value' => variable_get('os2intra_save_old_departments')
);
// Variable which contains settings to determinate if old user memberships should saved during import.
$form['os2intra_structure_settings']['os2intra_revoke_og_roles'] = array(
'#type' => 'checkbox',
'#title' => t('Save old departments memberships'),
'#description' => t('If unchecked only membership in current department will be added'),
'#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',
'#title' => t('Birthdate expected format (to write into database)'),
'#default_value' => variable_get('os2intra_user_import_birthdate_db_format'),
'#description' => t('Date format. See possible values in the <a href="http://php.net/manual/en/function.date.php" target="_blank">PHP manual</a>.')
);

// Variable which contains type of reference which used for connection between user profile and job title taxonomy.
$form['os2intra_structure_settings']['os2intra_user_import_job_title_connection_reference'] = array(
'#type' => 'select',
'#title' => t('Users -> job title connection reference type'),
'#description' => t('Reference type to job taxonomy in user profie(Term Reference or Entity Reference)'),
'#required' => TRUE,
'#options' => array('target_id' => 'target_id', 'tid' => 'tid'),
'#default_value' => variable_get('os2intra_user_import_job_title_connection_reference')
);

// Mapping settings
$form['os2intra_mapping'] = array(
'#type' => 'fieldset',
'#title' => t('Mapping'),
Expand Down
19 changes: 15 additions & 4 deletions includes/users.inc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function os2intra_user_import_check_users(&$users, $duplicate_emails, $duplicate

$result = NULL;

$identification_method = variable_get('os2intra_user_import_activate_identification');
$identification_method = variable_get('os2intra_user_import_activate_identification', OS2INTRA_USER_IMPORT_ACTIVATE_IDENTIFICATION);
if ($identification_method == 'ad_id') {
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'user');
Expand Down Expand Up @@ -367,6 +367,18 @@ function os2intra_user_import_save_user($user, $uid = '') {
// At this step even if the ad_id was empty initially it must have been generated
$fields['name'] = $user['ad_id'];
$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']);
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']);
return;
}
}


Expand Down Expand Up @@ -396,7 +408,7 @@ function os2intra_user_import_save_user($user, $uid = '') {
// Set department membership and role
$values = array(
'entity' => $account,
'field_name' => variable_get('os2intra_user_organisation_field')
'field_name' => variable_get('os2intra_user_primary_og_role_reference_field')
);
og_group('node', $department_nid, $values);
og_role_grant('node', $department_nid, $account->uid, variable_get('os2intra_user_import_og_role_id_primary', 16));
Expand All @@ -422,10 +434,9 @@ function os2intra_user_import_save_user($user, $uid = '') {
if (!user_get_authmaps($authmap)) {
user_set_authmaps($account, $authmap);
}

// Add users to parent departments
if (variable_get('os2intra_add_users_to_parent_departments') && $department_nid) {
os2intra_user_import_add_user_to_parent_departments($account);
os2intra_user_import_add_user_to_parent_departments($account, $department_nid);
}

// Write log entry
Expand Down
Loading

0 comments on commit edc330e

Please sign in to comment.