View source
<?php
function oa_export_batch_export($blueprint, $export_type = 'file') {
oa_export_verify_blueprint($blueprint);
switch ($export_type) {
case 'file':
$_SESSION['oa_export'] = array();
if ($_SESSION['oa_export']['directory'] = oa_export_create_temp_export_directory($blueprint->name, file_directory_temp())) {
$file_dir = oa_export_create_directories($_SESSION['oa_export']['directory'] . '/' . OA_EXPORT_FILES);
if ($file_dir) {
$_SESSION['oa_export']['files_directory'] = $file_dir;
$tmp_dir = file_directory_temp();
if (!is_writable($tmp_dir)) {
drupal_set_message(t('In order for this export to work "%dir" needs to be writable.', array(
'%dir' => $tmp_dir,
)), 'error');
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
$batch = array(
'title' => t('Blueprint Download'),
'init_message' => t('Preparing to download "!name" blueprint to a file.', array(
'!name' => $blueprint->name,
)),
'finished' => 'oa_export_batch_file_download_finished',
);
oa_export_batch_export_operations($batch, $blueprint);
batch_set($batch);
batch_process('oa_export/download');
}
else {
drupal_set_message(t('Could not create @file_dir', array(
'@file_dir' => $file_dir,
)), 'error');
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
}
else {
drupal_set_message(t('Could not create a directory in your system temporary directory.'), 'error');
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
break;
case 'module':
$batch = array(
'title' => t('Blueprint Module Export'),
'init_message' => t('Preparing to export the "!name" blueprint to a module.', array(
'!name' => $blueprint->name,
)),
'finished' => 'oa_export_batch_module_export_finished',
);
oa_export_batch_export_operations($batch, $blueprint);
batch_set($batch);
break;
default:
break;
}
}
function oa_export_batch_export_operations(&$batch, $blueprint) {
$wrapper = entity_metadata_wrapper('taxonomy_term', $blueprint);
$space = $wrapper->{BLUEPRINT_SPACE}
->value();
$batch['operations'][] = array(
'_oa_export_batch_export_blueprint',
array(
$blueprint,
$space,
),
);
$groups = oa_core_get_groups_by_parent($space->nid, NULL, NULL, FALSE, NULL, TRUE);
foreach ($groups as $id) {
$entity = entity_load_single('node', $id);
$batch['operations'][] = array(
'_oa_export_batch_export_dependency',
array(
$entity,
),
);
}
}
function _oa_export_batch_export_blueprint($blueprint, $space, &$context) {
if (empty($context['sandbox']['max'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = 1;
$context['results']['export'] = array();
$context['results']['messages'] = array();
$context['results']['total'] = (!empty($context['results']['total']) ? $context['results']['total'] : 0) + $context['sandbox']['max'];
}
$space_reference = $blueprint->field_oa_clone_space;
oa_export_entity_export('taxonomy_term', $blueprint, $context['results']);
$context['results']['export']['taxonomy_term:' . $blueprint->tid]->field_oa_clone_space = $space_reference;
$space->oa_parent_space = array();
oa_export_entity_export('node', $space, $context['results']);
$context['sandbox']['progress']++;
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
function _oa_export_batch_export_dependency($entity, &$context) {
if (empty($context['sandbox']['max'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = 1;
$context['results']['total'] = (!empty($context['results']['total']) ? $context['results']['total'] : 0) + $context['sandbox']['max'];
}
oa_export_entity_export('node', $entity, $context['results']);
$context['sandbox']['progress']++;
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
function oa_export_batch_file_download_finished($success, $results, $operations) {
if ($success) {
try {
$base = basename($_SESSION['oa_export']['directory']);
$tmp_dir = file_directory_temp();
$_SESSION['oa_export']['file'] = $tmp_dir . '/' . $base . '.tar.gz';
if (!oa_export_create_json_export(OA_EXPORT_JSON_FILE, $results['export'], $_SESSION['oa_export']['directory'])) {
drupal_set_message(t('There was an error creating the export file.'), 'error');
oa_export_cleanup(isset($clean_dir) ? $clean_dir : NULL, OA_EXPORT_REDIRECT);
}
else {
$new_tar = new Archive_Tar($_SESSION['oa_export']['file']);
$new_tar
->createModify(array(
$_SESSION['oa_export']['directory'],
), '', $tmp_dir);
$public = variable_get('file_public_path', conf_path() . '/files');
if ($copy = file_unmanaged_copy($_SESSION['oa_export']['file'], $public, FILE_EXISTS_REPLACE)) {
$_SESSION['oa_export']['download_path'] = url($copy);
}
else {
drupal_set_message(t('There was a problem copying the export to !public', array(
'!public' => $public,
), 'error'));
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
drupal_set_message(t('Finished exporting your Blueprint. Check below for notices.'), 'status');
foreach ($results['messages'] as $message) {
drupal_set_message($message, 'warning');
}
if (isset($_SESSION['oa_export']['module'])) {
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
}
} catch (Exception $e) {
drupal_set_message(t('Error: %message', array(
'%message' => $e
->getMessage(),
)));
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
}
else {
drupal_set_message(t('The batch export was unsuccessful'), 'error');
oa_export_cleanup($_SESSION['oa_export']['directory'], OA_EXPORT_REDIRECT);
}
}
function oa_export_batch_module_export_finished($success, $results, $operations) {
$module_path = drupal_get_path('module', $_SESSION['oa_export']['module']);
if ($_SESSION['oa_export']['type'] == 'new') {
$export_path = $module_path;
}
else {
if ($_SESSION['oa_export']['type'] == 'existing') {
$export_path = $module_path . '/' . OA_EXPORT_DIR;
}
}
if ($success) {
$file = oa_export_create_json_export(OA_EXPORT_JSON_FILE, $results['export'], $module_path . '/' . OA_EXPORT_DIR);
if ($file) {
drupal_set_message(t('The @module module was created at @location.', array(
'@module' => $_SESSION['oa_export']['module'],
'@location' => $module_path,
)), 'status');
$message = 'If you exported the blueprint as a new module then just enable the module on the system you want to import the blueprint to. ';
$message .= 'If you exported the blueprint to an existing module, update the existing module on the system you are running the import and run a database update.';
drupal_set_message(t($message), 'warning');
if (file_exists($module_path . '/' . OA_EXPORT_TEMP_DIR)) {
file_unmanaged_delete_recursive($module_path . '/' . OA_EXPORT_TEMP_DIR);
}
oa_export_cleanup();
}
else {
drupal_set_message(t('Unable to create the file for the entity export!'), 'error');
oa_export_cleanup($export_path, OA_EXPORT_REDIRECT);
}
}
else {
drupal_set_message(t('The batch finished but something went wrong. Try again or contact your system administrator.'), 'error');
oa_export_cleanup($export_path, OA_EXPORT_REDIRECT);
}
}
function oa_export_batch_file_download_finished_redirect() {
if (empty($_SESSION['oa_export']['directory']) || empty($_SESSION['oa_export']['file'])) {
return t('The file cannot be found.');
}
$redirect = $_SESSION['oa_export']['download_path'];
drupal_add_js('setTimeout(function() { window.location.href = "' . $redirect . '"; }, 2000);', 'inline');
oa_export_remove_directory($_SESSION['oa_export']['directory']);
$output = theme('blueprint_download_page', array());
unlink($_SESSION['oa_export']['file']);
oa_export_cleanup();
return $output;
}