123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /**
- * @file
- * Delete users who are not included in an import file.
- */
- /**
- * Implements hook_cron().
- */
- function user_import_delete_cron() {
- user_import_delete_cancel_accounts();
- }
- function user_import_delete_preprocess_user_import_list(&$variables) {
- $accounts_not_created = db_query('SELECT u.uid FROM {user_import_delete} u WHERE delete_ready = 1')->rowCount();
- if (!empty($accounts_not_created)) {
- drupal_set_message(t('@delete_count accounts scheduled to be cancelled.', array('@delete_count' => $accounts_not_created)), 'status');
- }
- }
- /**
- * Implements hook_form_alter().
- *
- */
- function user_import_delete_form_alter(&$form, &$form_state, $form_id) {
- if ($form_id == 'user_import_edit') {
- array_unshift($form['import']['#submit'], 'user_import_delete_import_submit');
- }
- }
- /**
- * Implementation of hook_user_import_form_fieldsets().
- * Add options to User Import settings form.
- */
- function user_import_delete_user_import_form_fieldset($import, $collapsed) {
- $form = array();
- user_import_delete_user_import_edit_account_cancel($form, $import, $collapsed);
- return $form;
- }
- /**
- * Create a list of all existing users.
- */
- function user_import_delete_import_submit($form, &$form_state) {
- global $user;
- if (empty($form_state['values']['user_import_delete'])) {
- return;
- }
- $import_id = $form_state['values']['import_id'];
- $method = $form_state['values']['user_import_delete'];
- // Can't find a way to copy all users into table and set import_id and cancellation_method in one query,
- // so have to do it in three steps.
- // Delete any records without an import_id.
- $deleted_count = db_delete('user_import_delete')
- ->condition('import_id', 0)
- ->execute();
- // Add all users, except user 1.
- $query = db_select('users', 'u');
- $query->addField('u','uid', 'uid');
- $query->condition('uid', 1, '>');
- $query->condition('uid', $user->uid, '!=');
- db_insert('user_import_delete')
- ->from($query)
- ->execute();
- // Add import_id to all users.
- $updated_count = db_update('user_import_delete')
- ->fields(array(
- 'import_id' => $import_id,
- 'cancellation_method' => $method,
- ))
- ->condition('import_id', 0)
- ->execute();
- }
- /**
- * Implementation of hook_user_import_after_save().
- *
- * Delete imported users from log of original users,
- * so we have a list of which users where not in the import.
- *
- **/
- function user_import_delete_user_import_pre_save($settings, $account, $fields, $errors, $update_setting_per_module) {
- $method = $settings['user_import_delete'];
- // If no cancellation method is set, don't add accounts to list to be processed.
- if (empty($method)) {
- return;
- }
- // Check if accounts are to be updated as well as created.
- $update_setting = _user_import_update_user_check($settings['update_user']);
- // @todo document this logic.
- if (isset($account['uid'])) {
- db_delete('user_import_delete')
- ->condition('import_id', $settings['import_id'])
- ->condition('uid', $account['uid'])
- ->execute();
- }
- elseif (!$update_setting) { // If account is not being updated check there's a match between existing account and import.
- $email = $fields['user']['email'][0];
- $uid = db_query('SELECT u.uid FROM {users} u WHERE mail = :mail', array(':mail' => $email))->fetchField();
- if (!empty($uid)) {
- db_delete('user_import_delete')
- ->condition('import_id', $settings['import_id'])
- ->condition('uid', $uid)
- ->execute();
- }
- }
- }
- /**
- * Implementation of hook_user_import_imported().
- * Process import once it's completed.
- *
- **/
- function user_import_delete_user_import_imported($import_id, $settings) {
- $method = $settings['user_import_delete'];
- if (!empty($method)) {
- user_import_delete_set_delete_flag($import_id);
- user_import_delete_cancel_accounts();
- }
- }
- /**
- * Flag users as ready for deletion.
- *
- **/
- function user_import_delete_set_delete_flag($import_id) {
- // Prevent cancelling Anonymous user.
- db_delete('user_import_delete')
- ->condition('uid', 0)
- ->execute();
- // Prevent cancelling user 1 without confirmation.
- db_delete('user_import_delete')
- ->condition('uid', 1)
- ->execute();
- // Set users as ready to be deleted.
- $updated_count = db_update('user_import_delete')
- ->fields(array(
- 'delete_ready' => 1,
- ))
- ->condition('import_id', $import_id)
- ->execute();
- }
- /**
- * Delete users flagged as ready for deletion.
- *
- **/
- function user_import_delete_cancel_accounts() {
- $accounts_not_created = db_query('SELECT u.uid, u.cancellation_method FROM {user_import_delete} u WHERE delete_ready = 1 ORDER BY import_id');
- foreach ($accounts_not_created as $account_info) {
- $account = user_load($account_info->uid);
- if (!empty($account) && !empty($account_info->cancellation_method)) {
- // Set not to notify users that their account has been cancelled.
- $edit['user_cancel_notify'] = '';
- if ($account_info->cancellation_method != 'user_cancel_delete') {
- // Allow modules to add further sets to this operation.
- module_invoke_all('user_cancel', $edit, $account, $account_info->cancellation_method);
- }
- _user_cancel($edit, $account, $account_info->cancellation_method);
- db_delete('user_import_delete')
- ->condition('uid', $account->uid)
- ->execute();
- }
- }
- return;
- }
- /**
- * Options for the User Import settings form.
- */
- function user_import_delete_user_import_edit_account_cancel(&$form, $import, $collapsed) {
- $options = array(
- 'user_cancel_block' => t('Disable the account and keep its content'),
- 'user_cancel_block_unpublish' => t('Disable the account and unpublish its content'),
- 'user_cancel_reassign' => t('Delete the account and make its content belong to the !anonymous-name user', array('!anonymous-name' => variable_get('anonymous', t('Anonymous')))),
- 'user_cancel_delete' => t('Delete the account and its content'),
- );
- $form['optional']['user_import_delete'] = array(
- '#type' => 'select',
- '#title' => t('Existing Accounts Not In Import'),
- '#description' => t('How to deal with existing accounts that are not in this import.'),
- '#default_value' => isset($import['options']['user_import_delete']) ? $import['options']['user_import_delete'] : '',
- '#empty_option' => t("Leave active"),
- '#options' => $options,
- );
- }
|