Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///// Файл МОДУЛЬ.module
- /**
- * Обработчик формы
- */
- function МОДУЛЬ_form_submit($form, &$form_state){
- $path = drupal_get_path('module', 'МОДУЛЬ');
- $batch = array(
- 'operations' => array(array('МОДУЛЬ_import_users', array())),
- 'finished' => 'fashion_upgrade_user_finished',
- 'title' => 'Импорт пользователей',
- 'init_message' => 'Инициализация импорта.',
- 'progress_message' => 'Импорт.',
- 'error_message' => 'Ошибка импорта пользователей',
- 'file' => $path.'/МОДУЛЬ_users.inc',
- );
- batch_set($batch);
- }
- //// Файл МОДУЛЬ_users.inc
- /**
- * Batch operation
- */
- function МОДУЛЬ_import_users(&$context){
- if (!isset($context['sandbox']['progress'])) {
- $context['sandbox']['progress'] = 0;
- $context['sandbox']['current_id'] = 0;
- db_set_active('other'); // Импорт из другой БД
- $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT id) FROM other_users'));
- db_set_active();
- }
- $limit = 5; // Количество записей, обрабатываемых за один раз
- db_set_active('other'); // Импорт из другой БД
- $result = db_query_range(
- "SELECT * FROM other_users WHERE other_users.id > %d ORDER BY other_users.id ASC",
- $context['sandbox']['current_id'], 0, $limit);
- db_set_active();
- while ($row = db_fetch_object($result)) {
- if($row->id)
- $user = МОДУЛЬ_user_process($row); // Внешняя функция обработки записи, возвращает объект юзера
- $context['results'][] = $user->uid . ' : ' . check_plain($user->name);
- // Update our progress information.
- $context['sandbox']['progress']++;
- $context['sandbox']['current_id'] = $row->id;
- $context['message'] = check_plain($user->name);
- }
- // 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 МОДУЛЬ_user_process($obj){
- ...
- return $user;
- }
- /**
- * Кнец пакетной обработки
- */
- function МОДУЛЬ_users_finished($success, $results, $operations) {
- if ($success) {
- $message = count($results) . ' processed.';
- }
- else {
- // An error occurred.
- // $operations contains the operations that remained unprocessed.
- $error_operation = reset($operations);
- $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
- }
- drupal_set_message($message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement