Skip to content

Instantly share code, notes, and snippets.

@pcambra
Created December 28, 2015 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcambra/4063f9bd856bb4ce5554 to your computer and use it in GitHub Desktop.
Save pcambra/4063f9bd856bb4ce5554 to your computer and use it in GitHub Desktop.
Get contacts present on Hrinfo but not on HID export files
<?php
/**
* @file
* hr_contacts module drush integration.
*/
/**
* Implements hook_drush_command().
*
* @return array
* An associative array describing your command(s).
*
* @see drush_parse_command()
*/
function hr_contacts_drush_command() {
$items = array();
$items['hr-contacts-get-non-hid'] = array(
'description' => "Get the contacts in HRinfo but not in HID",
'drupal dependencies' => array(),
'aliases' => array(),
);
return $items;
}
/**
* Callback to get the contacts that aren't in HID.
*/
function drush_hr_contacts_get_non_hid() {
$query = new EntityFieldQuery();
$results = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'hr_contact')
->execute();
if (empty($results['node'])) {
return;
}
$contacts = array_keys($results['node']);
$profiles = db_query("SELECT uid, cid FROM {hid_profiles}")->fetchAllAssoc('uid');
$global_hid_file_path = file_directory_temp() . '/global_hid_contacts.csv';
$local_hid_file_path = file_directory_temp() . '/local_hid_contacts.csv';
$batch = array(
'operations' => array(
array(
'drush_hr_contacts_get_non_hid_operation',
array(
$contacts,
$profiles,
$global_hid_file_path,
$local_hid_file_path,
),
),
),
'finished' => 'drush_hr_contacts_get_non_hid_finished',
'title' => t('Calculating'),
'init_message' => t('Preparing contacts calculation...'),
'progress_message' => t('Calculating contacts...'),
'error_message' => t('Contacts process resulted in an error.'),
);
batch_set($batch);
drush_backend_batch_process();
}
function drush_hr_contacts_get_non_hid_finished($success, $results, $operations) {
drush_log('Process finished');
}
/**
* Iterates the contacts and checks if they are in the HID files.
*/
function drush_hr_contacts_get_non_hid_operation($contacts, $profiles, $global_hid_file_path, $local_hid_file_path, &$context) {
$limit = 150;
$context['finished'] = 0;
if (!isset($context['sandbox']['file'])) {
$headers = array(
'Contact id (Hrinfo)',
'Contact url',
'First Name',
'Last Name',
'Email',
);
// Create the file and print the labels in the header row.
$file_path = file_directory_temp() . '/contacts-report.csv';
$handle = fopen($file_path, 'w');
fputcsv($handle, $headers);
fclose($handle);
$context['sandbox']['contacts'] = $contacts;
$context['sandbox']['total_contacts'] = count($contacts);
$context['sandbox']['file'] = $file_path;
$context['results']['count'] = 0;
}
$handle = fopen($context['sandbox']['file'], 'a');
$global_hid_handle = fopen($global_hid_file_path, 'r');
$local_hid_handle = fopen($local_hid_file_path, 'r');
if ($contacts_pending = count($context['sandbox']['contacts'])) {
$actual_limit = min($contacts_pending, $limit);
$contact_info = array_slice(
$context['sandbox']['contacts'],
$context['results']['count'],
$actual_limit,
TRUE
);
// Load all the contacts for the iteration.
$contact_nodes = node_load_multiple($contact_info);
foreach ($contact_nodes as $contact_node) {
$wrapper = entity_metadata_wrapper('node', $contact_node);
$email = $wrapper->field_email->value();
// We might not have the email of the contact.
if (!empty($email)) {
// Step 1. If the contact is present in the profiles table, skip it.
if (drush_hr_contacts_email_is_in_profile($email, $profiles)) {
continue;
}
// Step 2. If the contact is present in the global list, skip it.
if (drush_hr_contacts_email_is_in_file($email, $global_hid_handle)) {
continue;
}
// Step 3. If the contact is present in the local list, skip it.
if (drush_hr_contacts_email_is_in_file($email, $local_hid_handle)) {
continue;
}
}
// Otherwise, add it to the csv.
$row = array();
$row[] = $wrapper->getIdentifier();
$row[] = url('node/' . $wrapper->getIdentifier());
$row[] = $wrapper->field_first_name->value();
$row[] = $wrapper->field_last_name->value();
$row[] = $email;
fputcsv($handle, $row);
rewind($global_hid_handle);
rewind($local_hid_handle);
}
$context['results']['count'] += $actual_limit;
$context['finished'] = $context['results']['count'] / $context['sandbox']['total_contacts'];
}
else {
$context['finished'] = 1;
}
fclose($handle);
fclose($global_hid_handle);
fclose($local_hid_handle);
$context['message'] = t(
'Processed @count of @total contacts.',
array(
'@count' => $context['results']['count'],
'@total' => $context['sandbox']['total_contacts'],
)
);
}
/**
* Checks the email in the profiles.
*/
function drush_hr_contacts_email_is_in_profile($email, $profiles) {
foreach ($profiles as $profile) {
if (strstr($profile->cid, $email) !== FALSE) {
return TRUE;
}
}
return FALSE;
}
/**
* Reads the file and looks for the email.
*/
function drush_hr_contacts_email_is_in_file($email, $handle) {
while ($row = fgetcsv($handle)) {
if (in_array($email, $row)) {
// $meta_data = stream_get_meta_data($handle);
// $filename = $meta_data['uri'];
// drush_log("Found $email in $filename");
return TRUE;
}
}
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment