function bulk_export_export

FAPI gateway to the bulk exporter.

Parameters

$cli: Whether this function is called from command line.

$options: A collection of options, only passed in by drush_ctools_export().

1 call to bulk_export_export()
ctools_drush_export in drush/ctools.drush.inc
Drush callback: export.
1 string reference to 'bulk_export_export'
bulk_export_menu in bulk_export/bulk_export.module
Implements hook_menu().

File

bulk_export/bulk_export.module, line 46

Code

function bulk_export_export($cli = FALSE, $options = array()) {
    ctools_include('export');
    $form = array();
    $schemas = ctools_export_get_schemas(TRUE);
    $exportables = $export_tables = array();
    foreach ($schemas as $table => $schema) {
        if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) {
            $exportables[$table] = $schema['export']['list callback']();
        }
        else {
            $exportables[$table] = ctools_export_default_list($table, $schema);
        }
        natcasesort($exportables[$table]);
        $export_tables[$table] = $schema['module'];
    }
    if ($exportables) {
        $form_state = array(
            're_render' => FALSE,
            'no_redirect' => TRUE,
            'exportables' => $exportables,
            'export_tables' => $export_tables,
            'name' => '',
            'code' => '',
            'module' => '',
        );
        // If called from drush_ctools_export, get the module name and
        // select all exportables and call the submit function directly.
        if ($cli) {
            $module_name = $options['name'];
            $form_state['values']['name'] = $module_name;
            if (isset($options['selections'])) {
                $exportables = $options['selections'];
            }
            $form_state['values']['tables'] = array();
            foreach ($exportables as $table => $names) {
                if (!empty($names)) {
                    $form_state['values']['tables'][] = $table;
                    $form_state['values'][$table] = array();
                    foreach ($names as $name => $title) {
                        $form_state['values'][$table][$name] = $name;
                    }
                }
            }
            $output = bulk_export_export_form_submit($form, $form_state);
        }
        else {
            $output = drupal_build_form('bulk_export_export_form', $form_state);
            $module_name = $form_state['module'];
        }
        if (!empty($form_state['submitted']) || $cli) {
            drupal_set_title(t('Bulk export results'));
            $output = '';
            $module_code = '';
            $api_code = array();
            $dependencies = $file_data = array();
            foreach ($form_state['code'] as $module => $api_info) {
                if ($module == 'general') {
                    $module_code .= $api_info;
                }
                else {
                    foreach ($api_info as $api => $info) {
                        $api_hook = ctools_plugin_api_get_hook($module, $api);
                        if (empty($api_code[$api_hook])) {
                            $api_code[$api_hook] = '';
                        }
                        $api_code[$api_hook] .= "  if (\$module == '{$module}' && \$api == '{$api}') {\n";
                        $api_code[$api_hook] .= "    return array('version' => {$info['version']});\n";
                        $api_code[$api_hook] .= "  }\n";
                        $dependencies[$module] = TRUE;
                        $file = $module_name . '.' . $api . '.inc';
                        $code = "<?php\n\n";
                        $code .= "/**\n";
                        $code .= " * @file\n";
                        $code .= " * Bulk export of {$api} objects generated by Bulk export module.\n";
                        $code .= " */\n\n";
                        $code .= $info['code'];
                        if ($cli) {
                            $file_data[$file] = $code;
                        }
                        else {
                            $export_form = drupal_get_form('ctools_export_form', $code, t('Place this in @file', array(
                                '@file' => $file,
                            )));
                            $output .= drupal_render($export_form);
                        }
                    }
                }
            }
            // Add hook_ctools_plugin_api at the top of the module code, if there is any.
            if ($api_code) {
                foreach ($api_code as $api_hook => $text) {
                    $api = "\n/**\n";
                    $api .= " * Implements hook_{$api_hook}().\n";
                    $api .= " */\n";
                    $api .= "function {$module_name}_{$api_hook}(\$module, \$api) {\n";
                    $api .= $text;
                    $api .= "}\n";
                    $module_code = $api . $module_code;
                }
            }
            if ($module_code) {
                $module = "<?php\n\n";
                $module .= "/**\n";
                $module .= " * @file\n";
                $module .= " * Bulk export of objects generated by Bulk export module.\n";
                $module .= " */\n";
                $module .= $module_code;
                if ($cli) {
                    $file_data[$module_name . '.module'] = $module;
                }
                else {
                    $export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array(
                        '@file' => $form_state['module'] . '.module',
                    )));
                    $output = drupal_render($export_form) . $output;
                }
            }
            $info = strtr("name = @module export module\n", array(
                '@module' => $form_state['module'],
            ));
            $info .= strtr("description = Export objects from CTools\n", array(
                '@module' => $form_state['values']['name'],
            ));
            foreach ($dependencies as $module => $junk) {
                $info .= "dependencies[] = {$module}\n";
            }
            $info .= "package = Chaos tool suite\n";
            $info .= "core = 7.x\n";
            if ($cli) {
                $file_data[$module_name . '.info'] = $info;
            }
            else {
                $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array(
                    '@file' => $form_state['module'] . '.info',
                )));
                $output = drupal_render($export_form) . $output;
            }
        }
        if ($cli) {
            return $file_data;
        }
        else {
            return $output;
        }
    }
    else {
        return t('There are no objects to be exported at this time.');
    }
}