View source
<?php
module_load_include('inc', 'patterns_export', 'api');
module_load_include('inc', 'patterns_export', 'core');
module_load_include('inc', 'patterns_export', 'finalize');
function patterns_export_menu_alter(&$items) {
$items['admin/patterns/export'] = array(
'title' => 'Export',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'patterns_export',
),
'access arguments' => array(
'administer patterns',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function patterns_export($form, &$form_state) {
$form = array();
if (!patterns_parser_ready()) {
$messages = t('No available patterns parser was found.</br>');
$messages .= t(' Go to the !modules page to enable more Patterns parsers.', array(
'!modules' => l(t('modules'), 'admin/modules'),
));
drupal_set_message($messages, 'warning');
return $form;
}
patterns_io_load_components();
return patterns_export_page1($form, $form_state);
}
function patterns_export_page1($form, &$form_state) {
$tm_index = patterns_tagmodules_get_index();
$tm_index = patterns_tagmodules_filter($tm_index, NULL, PATTERNS_EXPORT);
if (count($tm_index) == 0) {
drupal_set_message(t('The components currently installed do not allow automatic export of the configuration of the web site'), 'error');
$form['disabled'] = array(
'#markup' => l(t('Click here to back'), 'admin/patterns'),
);
return $form;
}
$text = 'In this area you can export the current configuration of your web site to a new pattern file that can be inserted in the database, or downloaded.';
$title = 'Export';
patterns_forms_add_page_header($form, $title, $text);
$form['all'] = array(
'#type' => 'fieldset',
'#title' => 'Components',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['all']['all'] = array(
'#type' => 'fieldset',
'#title' => 'I want to select the components individually',
'#type' => 'checkbox',
);
$form['all']['ext'] = array(
'#type' => 'fieldset',
'#title' => 'Components with automatic export enabled',
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name="all"]' => array(
'checked' => TRUE,
),
),
),
);
foreach ($tm_index as $module => $tags) {
$form['all']['ext'][$module] = array(
'#type' => 'fieldset',
'#title' => $module,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['all']['ext'][$module]['options'] = array(
'#type' => 'checkboxes',
'#title' => t('Export options'),
'#options' => array_combine(array_keys($tags), array_keys($tags)),
);
foreach ($tags as $tag => $forms) {
$input_name = _patterns_export_build_input_name($module, $tag);
$form['all']['ext'][$module][$tag]['options'] = array(
'#type' => 'checkboxes',
'#title' => $tag,
'#options' => array_combine(array_keys($forms), array_keys($forms)),
'#states' => array(
'visible' => array(
':input[name="' . $input_name . '"]' => array(
'checked' => TRUE,
),
),
'checked' => array(
':input[name="' . $input_name . '"]' => array(
'checked' => TRUE,
),
),
),
);
}
}
patterns_forms_get_info_section($form, array(), array(
'collapsed' => TRUE,
));
patterns_forms_get_formats_selector($form, patterns_forms_get_default_format(), t('Export data in this format'), FALSE);
patterns_forms_get_name_selector($form);
patterns_forms_get_export_to_selector($form, array(
'title' => 'Export options',
));
patterns_forms_get_exec_mode_selector($form['export']);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Export'),
);
$form['#validate'][] = 'patterns_export_validate';
return $form;
}
function patterns_export_validate($form, &$form_state) {
if (!isset($form_state['values']['format'])) {
form_set_error('format', t('No valid format selected.'));
}
if (!empty($form_state['values']['filename'])) {
form_set_error('format', t('Please specify a valid file name.'));
}
if (!isset($form_state['values']['export']['to'])) {
form_set_error('export[to]', t('Please specify a valid export action.'));
}
if (!isset($form_state['values']['export']['mode'])) {
form_set_error('export[mode]', t('Please specify a valid export mode.'));
}
if (!isset($form_state['values']['info'])) {
form_set_error('to', t('No info section found.'));
}
return TRUE;
}
function patterns_export_submit($form, &$form_state) {
$tm_index = patterns_moduletags_get_index();
$format = $form_state['values']['format'];
$filename = $form_state['values']['pattern_name'];
if (!isset($form_state['values']['all']) || $form_state['values']['all'] == 0) {
$exports = $tm_index;
}
else {
$exports = array();
foreach ($form_state['values']['ext'] as $module => $data) {
foreach ($data['options'] as $tag => $enabled) {
if (empty($enabled)) {
continue;
}
$exports[$module][$tag][PATTERNS_EXPORT] = array();
foreach ($data[$tag]['options'] as $export_key => $func) {
if (!empty($func)) {
$exports[$module][$tag][PATTERNS_EXPORT][] = $tm_index[$module][$tag][PATTERNS_EXPORT][$func];
}
}
if (empty($exports[$module][$tag][PATTERNS_EXPORT])) {
if (isset($tm_index[$module][$tag][PATTERNS_EXPORT][PATTERNS_EXPORT_ALL])) {
$exports[$module][$tag][PATTERNS_EXPORT][] = $tm_index[$module][$tag][PATTERNS_EXPORT][PATTERNS_EXPORT_ALL];
}
}
if (isset($tm_index[$module][$tag][PATTERNS_FILES])) {
$exports[$module][$tag][PATTERNS_FILES] = $tm_index[$module][$tag][PATTERNS_FILES];
}
}
}
}
if (empty($exports)) {
form_set_error('ext', t('No valid component selected.'));
return FALSE;
}
$form_state['redirect'] = 'admin/patterns/export';
patterns_export_start_engine($filename, $exports, $form_state['values']['info'], $form_state['values']['export']['to'], $format, $form_state['values']['export']['mode']);
}
function patterns_export_start_engine($filename, $tagmodules = array(), $info = array(), $to = PATTERNS_EXPORT_TO_DB, $format = PATTERNS_FORMAT_UNKNOWN, $mode = PATTERNS_EXEC_PHP) {
$format = patterns_io_get_format_from_file($filename, $format);
if (!$format) {
drupal_set_message(t('Unkwnown export format. Aborting.'), 'error');
return FALSE;
}
if (!in_array($mode, array(
'php',
'batch',
))) {
drupal_set_message(t('Unknown execution mode: %mode', array(
'%mode' => $mode,
)), 'error');
return FALSE;
}
$func = 'patterns_export_' . $mode;
return $func($filename, $tagmodules, $info, $to, $format);
}
function patterns_export_list_export_functions($reset = TRUE) {
$moduletags = patterns_moduletags_get_index(NULL, $reset, $reset);
return patterns_moduletags_filter($moduletags, NULL, PATTERNS_EXPORT);
}
function patterns_export_is_valid_mode($mode = NULL) {
if (empty($mode)) {
return FALSE;
}
return in_array($mode, array(
PATTERNS_EXPORT_TO_DB,
PATTERNS_EXPORT_TO_FILE,
PATTERNS_EXPORT_TO_ZIP,
));
}
function _patterns_export_build_input_name($module, $tag) {
return 'ext[' . $module . '][options][' . $tag . ']';
}