Skip to content

Instantly share code, notes, and snippets.

@miteshmap
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save miteshmap/cce62b2858627426aa2a to your computer and use it in GitHub Desktop.

Select an option

Save miteshmap/cce62b2858627426aa2a to your computer and use it in GitHub Desktop.
Implemented use of hook_user_operations with batch.
/**
* Implements hook_user_operations().
*/
function custom_users_user_operations() {
$operations['custom_users_salesforce'] = array(
'label' => t('Create Salesforce Account'),
'callback' => 'custom_users_operations_create_salesforce_callback',
);
return $operations;
}
/**
* Callback function for admin mass generating user salesforce account.
*
* @param $uids
* An array of user IDs.
*/
function custom_users_operations_create_salesforce_callback(array $uids) {
$batch = array(
'title' => t('Creating Salesforce Account'),
'init_message' => t('Preparing to create salesforce account'),
'operations' =>array(
array('custom_users_operations_create_salesforce_batch', array($uids)),
),
'progress_message' => '',
'finished' => 'custom_users_operations_create_salesforce_batch_finished',
);
batch_set($batch);
batch_process('admin/people');
}
function custom_users_operations_create_salesforce_batch($uids, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($uids);
$context['sandbox']['uids'] = $uids;
}
// Process nodes by groups of 5.
$count = min(5, count($context['sandbox']['uids']));
for ($i = 1; $i <= $count; $i++) {
// For each nid, load the node, reset the values, and save it.
$uid = array_shift($context['sandbox']['uids']);
$account = user_load($uid);
$result = joc_users_salesforce_account_create($account);
// Save their JOC & metering data
if (!empty($result) && $result) {
$edit = array();
$edit['data']['JOC'] = $result;
$edit['roles'] = $account->roles;
if (empty($edit['roles'][JOC_ROLE_REGISTERED]))
$edit['roles'][JOC_ROLE_REGISTERED] = 'JOC Registered';
$account->status = 1;
// Save user data in drupal
user_save($account, $edit);
}
else {
watchdog('joc_user_operation', 'Salesforce account for @email is exist or not created',
array('@email' => $account->mail), WATCHDOG_NOTICE);
}
// Store result for post-processing in the finished callback.
$context['results'][] = $uid;
// Update our progress information.
$context['sandbox']['progress']++;
$context['message'] = t('Processing @current out of @total', array(
'@current' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
));
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function custom_users_operations_create_salesforce_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('The Salesforce account has been created for users.'));
}
else {
drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
$message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
$message .= theme('item_list', array('items' => $results));
drupal_set_message($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment