View source
<?php
function patterns_export_php($filename, $tagmodules, $info, $to, $format) {
list($sections, $modules) = patterns_export_core_php($tagmodules);
return patterns_export_finalize_export($to, $sections, $info, $modules, $format, $filename);
}
function patterns_export_core_php($tagmodules) {
$result = array();
$modules = array();
foreach ($tagmodules as $module_name => $module) {
foreach ($module as $tag => $values) {
if (!isset($values[PATTERNS_EXPORT]) || empty($values[PATTERNS_EXPORT])) {
continue;
}
if (isset($values[PATTERNS_FILES])) {
$files = !is_array($values[PATTERNS_FILES]) ? array(
$values[PATTERNS_FILES],
) : $values[PATTERNS_FILES];
foreach ($files as $file) {
require_once $file;
}
}
array_push($modules, $module_name);
if (!is_array($values[PATTERNS_EXPORT])) {
$sections[$tag] = call_user_func($values[PATTERNS_EXPORT]);
}
else {
foreach ($values[PATTERNS_EXPORT] as $f) {
$result[$tag] = isset($result[$tag]) ? $result[$tag] : array();
$sections[$tag] = array_merge($result[$tag], call_user_func($f));
}
}
}
}
$result[] = $sections;
$result[] = $modules;
return $result;
}
function patterns_export_batch($filename, $tagmodules, $info, $to, $format) {
$_SESSION['patterns_export_batch_info']['info'] = $info;
$_SESSION['patterns_export_batch_info']['to'] = $to;
$_SESSION['patterns_export_batch_info']['format'] = $format;
$_SESSION['patterns_export_batch_info']['filename'] = $filename;
$cur_sec = 1;
$tot_sec = count($tagmodules);
$result = array();
$modules = array();
$operations = array();
foreach ($tagmodules as $module_name => $module) {
foreach ($module as $tag => $values) {
if (!isset($values[PATTERNS_EXPORT]) || empty($values[PATTERNS_EXPORT])) {
continue;
}
$action_params = array(
$module_name,
$tag,
$values[PATTERNS_EXPORT],
);
$action_params[] = isset($values[PATTERNS_FILES]) ? $values[PATTERNS_FILES] : array();
$operations[] = array(
'patterns_export_batch_core',
$action_params,
);
}
if (!isset($_SESSION['patterns_export_batch_info'])) {
$_SESSION['patterns_export_batch_info'] = array();
}
if (!isset($_SESSION['patterns_export_batch_info']['modules'])) {
$_SESSION['patterns_export_batch_info']['modules'] = array();
}
array_push($_SESSION['patterns_export_batch_info']['modules'], $module_name);
$cur_sec++;
}
$batch = array(
'title' => t('Patterns export'),
'progress_message' => t('Running action @current out of @total'),
'finished' => 'patterns_export_batch_finish',
'operations' => $operations,
);
batch_set($batch);
return TRUE;
}
function patterns_export_batch_core($module_name, $tag, $export_funcs, $files = NULL, &$context) {
$msg_ctx = t('%component: now processing %tag. ', array(
'%component' => $module_name,
'%tag' => $tag,
));
if ($_SESSION['patterns_export_batch_info']['to'] != PATTERNS_EXPORT_TO_DB) {
$msg_ctx .= t('After the download has started, click !back.', array(
'!back' => l(t('here to go back'), 'admin/patterns/export'),
));
}
$context['message'] = $msg_ctx;
if (!empty($context['results']['abort'])) {
return;
}
module_load_include('module', 'patterns_exports');
patterns_io_load_components();
$sections = array();
if (!empty($files)) {
$files = !is_array($files) ? array(
$files,
) : $files;
foreach ($files as $file) {
require_once $file;
}
}
if (!is_array($export_funcs)) {
$export_funcs = array(
$export_funcs,
);
}
$result = array();
foreach ($export_funcs as $f) {
if (!function_exists($f)) {
$context['results']['abort'] = 1;
$context['results']['error_message'] = t('%module.%tag: export function %f not found.', array(
'%f' => $f,
'%module' => $module_name,
'%tag' => $tag,
));
return FALSE;
}
$result[$tag] = isset($result[$tag]) ? $result[$tag] : array();
$sections[$tag] = array_merge($result[$tag], call_user_func($f));
}
if (!isset($context['results']['modules'])) {
$context['results']['modules'] = array();
}
if (!isset($context['results']['sections'])) {
$context['results']['sections'] = array();
}
array_push($context['results']['modules'], $module_name);
$context['results']['sections'] = array_merge($context['results']['sections'], $sections);
}
function patterns_export_batch_finish($success, $results, $operations) {
$modules = $_SESSION['patterns_export_batch_info']['modules'];
if (empty($results['abort'])) {
foreach ($modules as $m) {
drupal_set_message(t('Component "@component" ran successfully.', array(
'@component' => $m,
)));
}
$to = $_SESSION['patterns_export_batch_info']['to'];
$info = $_SESSION['patterns_export_batch_info']['info'];
$format = $_SESSION['patterns_export_batch_info']['format'];
$filename = $_SESSION['patterns_export_batch_info']['filename'];
unset($_SESSION['patterns_export_batch_info']);
return patterns_export_finalize_export($to, $results['sections'], $info, $results['modules'], $format, $filename);
}
else {
drupal_set_message(t('An error occurred during the export.'), 'error');
drupal_set_message($results['error_message'], 'error');
unset($_SESSION['patterns_export_batch_info']);
return FALSE;
}
}