Commit 881b675d authored by RoSk0's avatar RoSk0

Ported bulk user sync functionality.

parent 5861c3a9
......@@ -230,69 +230,3 @@ function crm_core_user_sync_delete_relation_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/crm-core/user-sync/contact-to-user-management';
drupal_set_message(t('Relation has been deleted.'));
}
/**
* Submit callback. Handles user synchronization.
*/
function crm_core_user_sync_admin_form_user_sync_submit($form, &$form_state) {
$operations[] = array('_crm_core_user_sync_batch_processing', array());
$batch = array(
'operations' => $operations,
'title' => t('Processing user synchronization'),
'finished' => '_crm_core_user_sync_batch_processing_finished',
'file' => drupal_get_path('module', 'crm_core_user_sync') . '/crm_core_user_sync.admin.inc'
);
batch_set($batch);
}
/**
* Helper function for batch processing of users synchronization.
*
* @param $uid
* User id for which contact should be created.
*/
function _crm_core_user_sync_batch_processing(&$context) {
$query = db_select('users', 'users');
$query->leftJoin('field_data_endpoints', 'endpoints', "endpoints.endpoints_entity_id = users.uid AND endpoints.endpoints_entity_type = 'user'");
$query->addField('users', 'uid');
$query->addField('endpoints', 'entity_id', 'rid');
$query->condition('users.status', "'0'", '<>');
$query->isNull('endpoints.entity_id');
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$count_query = $query->countQuery();
$context['sandbox']['max'] = $count_query->execute()->fetchField();
$context['sandbox']['progress'] = 0;
}
// Limit users quantity to process per request.
$limit = 20;
$uids = $query->range(0, $limit)->execute()->fetchCol();
$users = user_load_multiple($uids);
foreach ($users as $user) {
crm_core_user_sync_sync($user);
$context['sandbox']['progress']++;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
$context['results']['synced'] = $context['sandbox']['max'];
}
}
/**
* Helper to set message to user when batch processing finished.
*/
function _crm_core_user_sync_batch_processing_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('@count users have been associated with contacts.', array('@count' => $results['synced'])));
}
else {
drupal_set_message(t('Something went wrong. Please check !dblog.', array(
'!dblog' =>
l(t('Recent log messages'), 'admin/reports/dblog')
)), 'error');
}
}
......@@ -6,6 +6,7 @@ use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\crm_core_user_sync\UserSyncBatch;
/**
* Configure crm_core_user_sync settings.
......@@ -134,7 +135,7 @@ class SettingsForm extends ConfigFormBase {
$form['crm_core_user_sync_wrapper']['user_sync'] = [
'#type' => 'submit',
'#value' => $this->t('Synchronize Users'),
'#submit' => ['crm_core_user_sync_admin_form_user_sync_submit'],
'#submit' => ['::bulkUserSync'],
];
$form['crm_core_user_sync_wrapper']['description'] = [
......@@ -145,6 +146,19 @@ class SettingsForm extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
/**
* Sets batch for bulk user synchronization.
*/
public function bulkUserSync(array $form, FormStateInterface $form_state) {
$operations[] = array([UserSyncBatch::class, 'progress'], array());
$batch = array(
'operations' => $operations,
'title' => t('Processing user synchronization'),
'finished' => [UserSyncBatch::class, 'finished'],
);
batch_set($batch);
}
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\crm_core_user_sync;
/**
* Methods for running the ConfigImporter in a batch.
*
* @package Drupal\crm_core_user_sync
*/
class UserSyncBatch {
/**
* Batch operation callback.
*/
public static function progress(&$context) {
$userStorage = \Drupal::entityTypeManager()->getStorage('user');
if (empty($context['sandbox'])) {
$max = $userStorage
->getQuery()
->condition('uid', 1, '>')
->count()
->execute();
$context['sandbox']['max'] = $max;
$context['sandbox']['progress'] = 0;
$context['results']['synced'] = 0;
$context['sandbox']['last_uid'] = 1;
}
$limit = 20;
$uids = $userStorage
->getQuery()
->sort('uid')
->condition('uid', $context['sandbox']['last_uid'], '>')
->range(0, $limit)
->execute();
$accounts = $userStorage->loadMultiple($uids);
foreach ($accounts as $account) {
if ($result = \Drupal::service('crm_core_user_sync.relation')->relate($account)) {
$context['results']['synced']++;
}
$context['sandbox']['last_uid'] = $account->id();
$context['sandbox']['progress']++;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Batch finished callback.
*/
public static function finished($success, $results, $operations) {
\Drupal::messenger()->addMessage(t('@count users have been associated with contacts.', ['@count' => $results['synced']]));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment